Skip to content

Commit 2aca2e4

Browse files
feat(contrib/trivy) fill image info into scan results (future-architect#1475)
* feat(contrib/trivy) fill image info into scan results * fix match size * fix match size
1 parent 14518d9 commit 2aca2e4

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

contrib/trivy/parser/v2/parser.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package v2
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"regexp"
67
"time"
78

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

38-
var dockerTagPattern = regexp.MustCompile(`:.+$`)
39+
var dockerTagPattern = regexp.MustCompile(`^(.*):(.*)$`)
3940

4041
func setScanResultMeta(scanResult *models.ScanResult, report *types.Report) error {
4142
if len(report.Results) == 0 {
4243
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/")
4344
}
4445

4546
scanResult.ServerName = report.ArtifactName
46-
if report.ArtifactType == "container_image" && !dockerTagPattern.MatchString(scanResult.ServerName) {
47-
scanResult.ServerName += ":latest" // Complement if the tag is omitted
47+
if report.ArtifactType == "container_image" {
48+
matches := dockerTagPattern.FindStringSubmatch(report.ArtifactName)
49+
var imageName, imageTag string
50+
if 2 < len(matches) {
51+
// including the image tag
52+
imageName = matches[1]
53+
imageTag = matches[2]
54+
} else {
55+
// no image tag
56+
imageName = report.ArtifactName
57+
imageTag = "latest" // Complement if the tag is omitted
58+
}
59+
scanResult.ServerName = fmt.Sprintf("%s:%s", imageName, imageTag)
60+
if scanResult.Optional == nil {
61+
scanResult.Optional = map[string]interface{}{}
62+
}
63+
scanResult.Optional["TRIVY_IMAGE_NAME"] = imageName
64+
scanResult.Optional["TRIVY_IMAGE_TAG"] = imageTag
4865
}
4966

5067
if report.Metadata.OS != nil {

contrib/trivy/parser/v2/parser_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,10 @@ var redisSR = &models.ScanResult{
263263
BinaryNames: []string{"bsdutils", "pkgA"},
264264
},
265265
},
266-
Optional: nil,
266+
Optional: map[string]interface{}{
267+
"TRIVY_IMAGE_NAME": "redis",
268+
"TRIVY_IMAGE_TAG": "latest",
269+
},
267270
}
268271

269272
var strutsTrivy = []byte(`
@@ -718,7 +721,10 @@ var osAndLibSR = &models.ScanResult{
718721
BinaryNames: []string{"libgnutls30"},
719722
},
720723
},
721-
Optional: nil,
724+
Optional: map[string]interface{}{
725+
"TRIVY_IMAGE_NAME": "quay.io/fluentd_elasticsearch/fluentd",
726+
"TRIVY_IMAGE_TAG": "v2.9.0",
727+
},
722728
}
723729

724730
func TestParseError(t *testing.T) {

0 commit comments

Comments
 (0)