Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Feat: trivy image pulling, graceful SIGINT handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Anglade committed Jan 14, 2020
1 parent b6cb85c commit 3428671
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
39 changes: 32 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"

log "github.com/sirupsen/logrus"

Expand All @@ -17,6 +19,8 @@ import (
"golang.org/x/net/context"
)

var debug = false

func getChartImages(chart string) (error, []string) {
images := []string{}
out, err := exec.Command("helm", "template", chart).Output()
Expand Down Expand Up @@ -46,21 +50,26 @@ ScannerLoop:
func scanImage(image string, ctx context.Context, cli *client.Client, cacheDir string, json bool) string {
config := container.Config{
Image: "aquasec/trivy",
Cmd: []string{},
Cmd: []string{"--cache-dir", "/.cache"},
Tty: true,
User: "1000",
}
if json {
config.Cmd = append(config.Cmd, "-q", "-f", "json", "--cache-dir", "/.cache")
config.Cmd = append(config.Cmd, "-f", "json")
}
if debug {
config.Cmd = append(config.Cmd, "-d")
} else {
config.Cmd = append(config.Cmd, "-q")
}
config.Cmd = append(config.Cmd, image)
resp, err := cli.ContainerCreate(ctx, &config, &container.HostConfig{
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock",
cacheDir + ":/.cache"},
Binds: []string{cacheDir + ":/.cache"},
}, nil, "")
if err != nil {
log.Fatalf("Could not create trivy container: %v", err)
}
log.Debugf("Starting container with command: %v", config.Cmd)
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
log.Fatalf("Could not start trivy container: %v", err)
}
Expand Down Expand Up @@ -105,12 +114,12 @@ func scanChart(chart string, json bool, ctx context.Context, cli *client.Client,

func main() {
var jsonOutput bool
var debug bool
var noPull bool
var chart string

flag.BoolVar(&jsonOutput, "json", false, "Enable JSON output")
flag.BoolVar(&debug, "debug", false, "Enable debug logging")

flag.BoolVar(&noPull, "nopull", false, "Don't pull latest trivy image")
flag.Parse()

if debug {
Expand All @@ -131,13 +140,29 @@ func main() {
log.Fatalf("Could not get docker client: %v", err)
}

if !noPull {
log.Info("Pulling latest trivy image")
_, err := cli.ImagePull(ctx, "aquasec/trivy:latest", types.ImagePullOptions{})
if err != nil {
panic(err)
}
log.Info("Pulled latest trivy image")
}

cacheDir, err := ioutil.TempDir("", "helm-trivy")
if err != nil {
log.Fatalf("Could not create cache dir: %v", err)
}
defer os.RemoveAll(cacheDir)
log.Debugf("Using %v as cache directory for vuln db", cacheDir)

defer os.RemoveAll(cacheDir)
go func(cacheDir string) {
sigCh := make(chan os.Signal)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
<-sigCh
os.RemoveAll(cacheDir)
os.Exit(0)
}(cacheDir)

scanChart(chart, jsonOutput, ctx, cli, cacheDir)
}
2 changes: 0 additions & 2 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#! /bin/bash -e

echo toto

version="$(cat plugin.yaml | grep "version" | cut -d '"' -f 2)"
latest_version=$(curl -Is "https://github.com/ObjectifLibre/helm-trivy/releases/latest" | grep "Location" | cut -d'/' -f 8 | tr -d "\r")

Expand Down

0 comments on commit 3428671

Please sign in to comment.