Skip to content

Commit

Permalink
feat(contrib/trivy) fill image info into scan results (#1475)
Browse files Browse the repository at this point in the history
* feat(contrib/trivy) fill image info into scan results

* fix match size

* fix match size
  • Loading branch information
sadayuki-matsuno authored Jun 8, 2022
1 parent 14518d9 commit 2aca2e4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
23 changes: 20 additions & 3 deletions contrib/trivy/parser/v2/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v2

import (
"encoding/json"
"fmt"
"regexp"
"time"

Expand Down Expand Up @@ -35,16 +36,32 @@ func (p ParserV2) Parse(vulnJSON []byte) (result *models.ScanResult, err error)
return scanResult, nil
}

var dockerTagPattern = regexp.MustCompile(`:.+$`)
var dockerTagPattern = regexp.MustCompile(`^(.*):(.*)$`)

func setScanResultMeta(scanResult *models.ScanResult, report *types.Report) error {
if len(report.Results) == 0 {
return xerrors.Errorf("scanned images or libraries are not supported by Trivy. see https://aquasecurity.github.io/trivy/dev/vulnerability/detection/os/, https://aquasecurity.github.io/trivy/dev/vulnerability/detection/language/")
}

scanResult.ServerName = report.ArtifactName
if report.ArtifactType == "container_image" && !dockerTagPattern.MatchString(scanResult.ServerName) {
scanResult.ServerName += ":latest" // Complement if the tag is omitted
if report.ArtifactType == "container_image" {
matches := dockerTagPattern.FindStringSubmatch(report.ArtifactName)
var imageName, imageTag string
if 2 < len(matches) {
// including the image tag
imageName = matches[1]
imageTag = matches[2]
} else {
// no image tag
imageName = report.ArtifactName
imageTag = "latest" // Complement if the tag is omitted
}
scanResult.ServerName = fmt.Sprintf("%s:%s", imageName, imageTag)
if scanResult.Optional == nil {
scanResult.Optional = map[string]interface{}{}
}
scanResult.Optional["TRIVY_IMAGE_NAME"] = imageName
scanResult.Optional["TRIVY_IMAGE_TAG"] = imageTag
}

if report.Metadata.OS != nil {
Expand Down
10 changes: 8 additions & 2 deletions contrib/trivy/parser/v2/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ var redisSR = &models.ScanResult{
BinaryNames: []string{"bsdutils", "pkgA"},
},
},
Optional: nil,
Optional: map[string]interface{}{
"TRIVY_IMAGE_NAME": "redis",
"TRIVY_IMAGE_TAG": "latest",
},
}

var strutsTrivy = []byte(`
Expand Down Expand Up @@ -718,7 +721,10 @@ var osAndLibSR = &models.ScanResult{
BinaryNames: []string{"libgnutls30"},
},
},
Optional: nil,
Optional: map[string]interface{}{
"TRIVY_IMAGE_NAME": "quay.io/fluentd_elasticsearch/fluentd",
"TRIVY_IMAGE_TAG": "v2.9.0",
},
}

func TestParseError(t *testing.T) {
Expand Down

0 comments on commit 2aca2e4

Please sign in to comment.