Skip to content

Commit

Permalink
testing28
Browse files Browse the repository at this point in the history
  • Loading branch information
an1l4 committed Aug 22, 2023
1 parent 737a6a5 commit ab82d3e
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions agent/kubviz/trivy_sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os/exec"
"sync"
Expand Down Expand Up @@ -43,7 +44,7 @@ func executeCommandSbom(command string) ([]byte, error) {
if err != nil {
log.Println("Execute Command Error", err.Error())
}
log.Println("*******output", outc.String(), errc.String())
// log.Println("*******output", outc.String(), errc.String())

return outc.Bytes(), err
}
Expand Down Expand Up @@ -151,22 +152,36 @@ func executeCommandSbom(command string) ([]byte, error) {
func RunTrivySbomScan(config *rest.Config, js nats.JetStreamContext, wg *sync.WaitGroup, errCh chan error) {
log.Println("trivy run started****************")
defer wg.Done()

command1 := "trivy -h"
out1, err := executeCommandSbom(command1)
images, err := ListImages(config)

if err != nil {
log.Printf("Error executing Trivy -h command %v", err)
log.Printf("failed to list images: %v", err)
}

command := "trivy image --format cyclonedx docker.io/crossplane/crossplane@sha256:50641735fad95c8a9eb27008b44f6cad14861efcb615d70ba10b8100b2b45bf7 --cache-dir /tmp/.cache"
out, err := executeCommandSbom(command)

log.Println("trivy docker-crossplane command executed******")

if err != nil {
log.Printf("Error executing Trivy sbom-docker-crossplane command %v", err)
for _, image := range images {
fmt.Printf("pullable Image %#v\n", image.PullableImage)

command := fmt.Sprintf("trivy image --format cyclonedx %s %s", image.PullableImage, "--cache-dir /tmp/.cache")
out, err := executeCommandSbom(command)

if err != nil {
log.Printf("Error executing Trivy for image %s: %v", image.PullableImage, err)
continue // Move on to the next image in case of an error
}

// Check if the output is empty or invalid JSON
if len(out) == 0 {
log.Printf("Trivy output is empty for image %s", image.PullableImage)
continue // Move on to the next image
}

var report model.Sbom
err = json.Unmarshal(out, &report)
if err != nil {
log.Printf("Error unmarshaling JSON data for image %s: %v", image.PullableImage, err)
continue // Move on to the next image in case of an error
}

// Publish the report using the given function
publishTrivySbomReport(report, js, errCh)
}
log.Println("datas is getting1", string(out1))
log.Println("datas is getting", string(out))
}

0 comments on commit ab82d3e

Please sign in to comment.