5
5
"encoding/json"
6
6
"fmt"
7
7
"log"
8
+ "os"
8
9
"os/exec"
9
10
10
11
"github.com/aquasecurity/trivy/pkg/sbom/cyclonedx"
@@ -47,18 +48,20 @@ func executeCommandSbom(command string) ([]byte, error) {
47
48
48
49
func RunTrivySbomScan (config * rest.Config , js nats.JetStreamContext ) error {
49
50
clearCacheCmd := "trivy image --clear-cache"
50
-
51
- log .Println ("trivy sbom run started" )
51
+ pvcMountPath := "/mnt/agent/kbz"
52
+ trivySbomCacheDir := fmt .Sprintf ("%s/trivy-sbomcache" , pvcMountPath )
53
+ err := os .MkdirAll (trivySbomCacheDir , 0755 )
54
+ if err != nil {
55
+ log .Printf ("Error creating Trivy cache directory: %v\n " , err )
56
+ return err
57
+ }
52
58
images , err := ListImages (config )
53
-
54
59
if err != nil {
55
60
log .Printf ("failed to list images: %v" , err )
56
61
}
57
62
for _ , image := range images {
58
-
59
- command := fmt .Sprintf ("trivy image --format cyclonedx %s %s" , image .PullableImage , "--cache-dir /tmp/.cache" )
60
- out , err := executeCommandSbom (command )
61
-
63
+ sbomcmd := fmt .Sprintf ("trivy image --format cyclonedx %s --cache-dir %s" , image .PullableImage , trivySbomCacheDir )
64
+ out , err := executeCommandSbom (sbomcmd )
62
65
if err != nil {
63
66
log .Printf ("Error executing Trivy for image sbom %s: %v" , image .PullableImage , err )
64
67
continue // Move on to the next image in case of an error
@@ -69,7 +72,6 @@ func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext) error {
69
72
log .Printf ("Trivy output is empty for image sbom %s" , image .PullableImage )
70
73
continue // Move on to the next image
71
74
}
72
-
73
75
var report cyclonedx.BOM
74
76
err = json .Unmarshal (out , & report )
75
77
if err != nil {
0 commit comments