Skip to content

Commit

Permalink
changes in trivy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nithunikzz committed Sep 11, 2023
1 parent 8b02c08 commit 5245e7e
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions agent/kubviz/trivy.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package main

import (
"bytes"
"encoding/json"
"log"
exec "os/exec"
"strings"

"github.com/aquasecurity/trivy/pkg/k8s/report"
Expand All @@ -12,6 +14,20 @@ import (
"github.com/nats-io/nats.go"
)

func executeCommandTrivy(command string) ([]byte, error) {
cmd := exec.Command("/bin/sh", "-c", command)
var outc, errc bytes.Buffer
cmd.Stdout = &outc
cmd.Stderr = &errc

err := cmd.Run()

if err != nil {
log.Println("Execute Trivy Command Error", err.Error())
}

return outc.Bytes(), err
}
func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
var report report.ConsolidatedReport
//out, err := executeCommand("trivy k8s --report summary cluster --timeout 60m -f json -q --cache-dir /tmp/.cache")
Expand All @@ -22,15 +38,16 @@ func RunTrivyK8sClusterScan(js nats.JetStreamContext) error {
log.Printf("Executing command: %s\n", cmdString)

// Execute the command
out, err := executeCommand(cmdString)
out, err := executeCommandTrivy(cmdString)

// Handle errors and process the command output as needed
if err != nil {
log.Printf("Error executing command: %v\n", err)
}
// Log the command output for debugging purposes
log.Printf("Command output: %s\n", out)
parts := strings.SplitN(out, "{", 2)
outStr := string(out)
parts := strings.SplitN(outStr, "{", 2)
if len(parts) <= 1 {
log.Println("No output from k8s cluster scan command", err)
return err
Expand Down

0 comments on commit 5245e7e

Please sign in to comment.