Skip to content

Commit

Permalink
improve how trivy db and java-db is handled (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorvezani authored Nov 7, 2024
1 parent 95ba385 commit 57e1d3f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 8 deletions.
3 changes: 3 additions & 0 deletions plugins/ci/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 5.7.10
- fix trivy db / java-db downloading

## 5.7.9
- bumped trivy to v0.57.0

Expand Down
15 changes: 12 additions & 3 deletions plugins/ci/pkg/ci/trivy.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,21 @@ func scanImagesWithTrivy(images []trivymodels.Image, configurationObject models.
args := []string{
"image", "--download-db-only",
"--db-repository", "ghcr.io/aquasecurity/trivy-db:2,public.ecr.aws/aquasecurity/trivy-db:2,docker.io/aquasec/trivy-db:2",
"--java-db-repository", "ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1,docker.io/aquasec/trivy-java-db:1",
}
output, err := commands.ExecWithMessage(exec.Command("trivy", args...), "downloading trivy database")
if err != nil {
return nil, "", fmt.Errorf("unable to download trivy database, %v: %s", err, output)
}

args = []string{
"image", "--download-java-db-only",
"--java-db-repository", "ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1,docker.io/aquasec/trivy-java-db:1",
}
output, err = commands.ExecWithMessage(exec.Command("trivy", args...), "downloading trivy java database")
if err != nil {
return nil, "", fmt.Errorf("unable to download trivy java database, %v: %s", err, output)
}

reportByRef := map[string]*trivymodels.TrivyResults{}
errorsByRef := map[string]*multierror.Error{}
for _, currentImage := range images {
Expand Down Expand Up @@ -454,9 +463,9 @@ func (ci *CIScan) SkipTrivyManifests() bool {
// ScanImageFile will scan a single file with Trivy and return the results.
func ScanImageFile(imagePath, imageID, tempDir, extraFlags string) (*trivymodels.TrivyResults, error) {
reportFile := tempDir + "/trivy-report-" + imageID + ".json"
cmd := exec.Command("trivy", "-d", "image", "--skip-update", "-f", "json", "-o", reportFile, "--input", imagePath)
cmd := exec.Command("trivy", "-d", "image", "--skip-db-update", "--skip-java-db-update", "-f", "json", "-o", reportFile, "--input", imagePath)
if extraFlags != "" {
cmd = exec.Command("trivy", "-d", "image", "--skip-update", extraFlags, "-f", "json", "-o", reportFile, "--input", imagePath)
cmd = exec.Command("trivy", "-d", "image", "--skip-db-update", "--skip-java-db-update", extraFlags, "-f", "json", "-o", reportFile, "--input", imagePath)
}
_, err := util.RunCommand(cmd, "scanning "+imageID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion plugins/ci/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.7.9
5.7.10
3 changes: 3 additions & 0 deletions plugins/trivy/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.31.2
* fix trivy db / java-db cache

## 0.31.1
* bumped trivy to 0.57.0

Expand Down
5 changes: 4 additions & 1 deletion plugins/trivy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ COPY --from=downloader /usr/local/bin/trivy /usr/local/bin/trivy

RUN TRIVY_CACHE_DIR=/var/tmp trivy image \
--download-db-only \
--db-repository "ghcr.io/aquasecurity/trivy-db:2","public.ecr.aws/aquasecurity/trivy-db:2","docker.io/aquasec/trivy-db:2" \
--db-repository "ghcr.io/aquasecurity/trivy-db:2","public.ecr.aws/aquasecurity/trivy-db:2","docker.io/aquasec/trivy-db:2"

RUN TRIVY_CACHE_DIR=/var/tmp trivy image \
--download-java-db-only \
--java-db-repository "ghcr.io/aquasecurity/trivy-java-db:1","public.ecr.aws/aquasecurity/trivy-java-db:1","docker.io/aquasec/trivy-java-db:1"

ENV CLOUDSDK_CONFIG=/tmp/gcloud
Expand Down
10 changes: 9 additions & 1 deletion plugins/trivy/cmd/trivy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ func main() {
args := []string{
"image", "--download-db-only",
"--db-repository", "ghcr.io/aquasecurity/trivy-db:2,public.ecr.aws/aquasecurity/trivy-db:2,docker.io/aquasec/trivy-db:2",
"--java-db-repository", "ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1,docker.io/aquasec/trivy-java-db:1",
}
_, err := util.RunCommand(exec.Command("trivy", args...), "downloading trivy database")
if err != nil {
logrus.Fatal(err)
}

args = []string{
"image", "--download-java-db-only",
"--java-db-repository", "ghcr.io/aquasecurity/trivy-java-db:1,public.ecr.aws/aquasecurity/trivy-java-db:1,docker.io/aquasec/trivy-java-db:1",
}
_, err = util.RunCommand(exec.Command("trivy", args...), "downloading trivy java database")
if err != nil {
logrus.Fatal(err)
}
}

host := os.Getenv("FAIRWINDS_INSIGHTS_HOST")
Expand Down
2 changes: 1 addition & 1 deletion plugins/trivy/pkg/image/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func getOsArch(imageCfg models.TrivyImageConfig) string {
func ScanImage(extraFlags, pullRef string, registryOAuth2AccessTokenMap map[string]string) (*models.TrivyResults, error) {
imageID := nonWordRegexp.ReplaceAllString(pullRef, "_")
reportFile := TempDir + "/trivy-report-" + imageID + ".json"
args := []string{"-d", "image", "--skip-update", "--security-checks", "vuln", "-f", "json", "-o", reportFile}
args := []string{"-d", "image", "--skip-db-update", "--skip-java-db-update", "--security-checks", "vuln", "-f", "json", "-o", reportFile}
if extraFlags != "" {
args = append(args, extraFlags)
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/trivy/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.31.1
0.31.2

0 comments on commit 57e1d3f

Please sign in to comment.