Skip to content

Commit 8955913

Browse files
tom1299Thomas Reuhl
authored andcommitted
Add image labels to artifact of vulnerability report
1 parent 1caa4d4 commit 8955913

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

pkg/apis/aquasecurity/v1alpha1/vulnerability_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ type Artifact struct {
6060
// MimeType represents a type and format of an Artifact.
6161
// +optional
6262
MimeType string `json:"mimeType,omitempty"`
63+
64+
// Labels is a map of key value pairs from the scanned artifact
65+
// +optional
66+
Labels map[string]string `json:"labels,omitempty"`
6367
}
6468

6569
// OS is the Operating System of the Artifact

pkg/plugins/trivy/plugin.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,16 @@ func (p *plugin) ParseReportData(ctx trivyoperator.PluginContext, imageRef strin
150150
}
151151

152152
var reports ty.Report
153+
153154
err = json.NewDecoder(logsReader).Decode(&reports)
154155
if err != nil {
155156
return vulnReport, secretReport, nil, err
156157
}
157158

158159
imageDigest := p.getImageDigest(reports)
160+
imageLabels := reports.Metadata.ImageConfig.Config.Labels
159161

160-
registry, artifact, err := ParseImageRef(imageRef, imageDigest)
162+
registry, artifact, err := ParseImageRef(imageRef, imageDigest, imageLabels)
161163
if err != nil {
162164
return vulnReport, secretReport, nil, err
163165
}
@@ -215,7 +217,7 @@ func (p *plugin) NewConfigForConfigAudit(ctx trivyoperator.PluginContext) (confi
215217
return getConfig(ctx)
216218
}
217219

218-
func ParseImageRef(imageRef, imageDigest string) (v1alpha1.Registry, v1alpha1.Artifact, error) {
220+
func ParseImageRef(imageRef, imageDigest string, imageLabels map[string]string) (v1alpha1.Registry, v1alpha1.Artifact, error) {
219221
ref, err := containerimage.ParseReference(imageRef)
220222
if err != nil {
221223
return v1alpha1.Registry{}, v1alpha1.Artifact{}, err
@@ -236,6 +238,9 @@ func ParseImageRef(imageRef, imageDigest string) (v1alpha1.Registry, v1alpha1.Ar
236238
if artifact.Digest == "" {
237239
artifact.Digest = imageDigest
238240
}
241+
242+
artifact.Labels = imageLabels
243+
239244
return registry, artifact, nil
240245
}
241246

pkg/plugins/trivy/plugin_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8250,6 +8250,7 @@ func TestParseImageRef(t *testing.T) {
82508250
name string
82518251
inputImageRef string
82528252
inputImageID string
8253+
inputImageLabels map[string]string
82538254
expectedRegistry v1alpha1.Registry
82548255
expectedArtifact v1alpha1.Artifact
82558256
expectedErr error
@@ -8345,6 +8346,29 @@ func TestParseImageRef(t *testing.T) {
83458346
Tag: "1.0.0",
83468347
},
83478348
},
8349+
{
8350+
name: "well known image with labels",
8351+
inputImageRef: "docker.io/library/busybox:latest",
8352+
inputImageID: "sha256:2bc57c6bcb194869d18676e003dfed47b87d257fce49667557fb8eb1f324d5d6",
8353+
inputImageLabels: map[string]string{
8354+
"org.opencontainers.image.source": "https://github.com/docker-library/busybox",
8355+
"org.opencontainers.image.url": "docker.io/library/busybox",
8356+
"org.opencontainers.image.created": "2019-10-12T07:20:50.52Z",
8357+
},
8358+
expectedRegistry: v1alpha1.Registry{
8359+
Server: "index.docker.io",
8360+
},
8361+
expectedArtifact: v1alpha1.Artifact{
8362+
Repository: "library/busybox",
8363+
Digest: "sha256:2bc57c6bcb194869d18676e003dfed47b87d257fce49667557fb8eb1f324d5d6",
8364+
Labels: map[string]string{
8365+
"org.opencontainers.image.source": "https://github.com/docker-library/busybox",
8366+
"org.opencontainers.image.url": "docker.io/library/busybox",
8367+
"org.opencontainers.image.created": "2019-10-12T07:20:50.52Z",
8368+
},
8369+
Tag: "latest",
8370+
},
8371+
},
83488372
{
83498373
name: "repo with digest",
83508374
inputImageRef: "quay.io/prometheus-operator/prometheus-operator@sha256:1420cefd4b20014b3361951c22593de6e9a2476bbbadd1759464eab5bfc0d34f",
@@ -8380,7 +8404,7 @@ func TestParseImageRef(t *testing.T) {
83808404
}
83818405
for _, tc := range testCases {
83828406
t.Run(tc.name, func(t *testing.T) {
8383-
registry, artifact, err := trivy.ParseImageRef(tc.inputImageRef, tc.inputImageID)
8407+
registry, artifact, err := trivy.ParseImageRef(tc.inputImageRef, tc.inputImageID, tc.inputImageLabels)
83848408
if tc.expectedErr != nil {
83858409
require.Errorf(t, err, "expected: %v", tc.expectedErr)
83868410
}

0 commit comments

Comments
 (0)