Skip to content

Commit 4a2f492

Browse files
feat(sbom): add image labels into SPDX and CycloneDX reports (#7257)
Co-authored-by: Teppei Fukuda <knqyf263@gmail.com>
1 parent f198cf8 commit 4a2f492

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

pkg/sbom/core/bom.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ const (
2525
PropertyClass = "Class"
2626

2727
// Image properties
28-
PropertySize = "Size"
29-
PropertyImageID = "ImageID"
30-
PropertyRepoDigest = "RepoDigest"
31-
PropertyDiffID = "DiffID"
32-
PropertyRepoTag = "RepoTag"
28+
PropertySize = "Size"
29+
PropertyImageID = "ImageID"
30+
PropertyRepoDigest = "RepoDigest"
31+
PropertyDiffID = "DiffID"
32+
PropertyRepoTag = "RepoTag"
33+
PropertyLabelsPrefix = "Labels"
3334

3435
// Package properties
3536
PropertyPkgID = "PkgID"

pkg/sbom/cyclonedx/marshal_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ func TestMarshaler_MarshalReport(t *testing.T) {
105105
RepoDigests: []string{"rails@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"},
106106
ImageConfig: v1.ConfigFile{
107107
Architecture: "arm64",
108+
Config: v1.Config{
109+
Labels: map[string]string{
110+
"vendor": "aquasecurity",
111+
},
112+
},
108113
},
109114
},
110115
Results: types.Results{
@@ -301,6 +306,10 @@ func TestMarshaler_MarshalReport(t *testing.T) {
301306
Name: "aquasecurity:trivy:ImageID",
302307
Value: "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
303308
},
309+
{
310+
Name: "aquasecurity:trivy:Labels:vendor",
311+
Value: "aquasecurity",
312+
},
304313
{
305314
Name: "aquasecurity:trivy:RepoDigest",
306315
Value: "rails@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177",

pkg/sbom/io/encode.go

+9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ func (e *Encoder) rootComponent(r types.Report) (*core.Component, error) {
7373
Value: r.Metadata.ImageID,
7474
})
7575

76+
// Save image labels as properties with `Labels:` prefix.
77+
// e.g. `LABEL vendor="aquasecurity"` => `Labels:vendor` -> `aquasecurity`
78+
for label, value := range r.Metadata.ImageConfig.Config.Labels {
79+
props = append(props, core.Property{
80+
Name: core.PropertyLabelsPrefix + ":" + label,
81+
Value: value,
82+
})
83+
}
84+
7685
p, err := purl.New(purl.TypeOCI, r.Metadata, ftypes.Package{})
7786
if err != nil {
7887
return nil, xerrors.Errorf("failed to new package url for oci: %w", err)

pkg/sbom/io/encode_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io_test
33
import (
44
"testing"
55

6+
v1 "github.com/google/go-containerregistry/pkg/v1"
67
"github.com/package-url/packageurl-go"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
@@ -43,6 +44,13 @@ func TestEncoder_Encode(t *testing.T) {
4344
RepoDigests: []string{
4445
"debian@sha256:4482958b4461ff7d9fabc24b3a9ab1e9a2c85ece07b2db1840c7cbc01d053e90",
4546
},
47+
ImageConfig: v1.ConfigFile{
48+
Config: v1.Config{
49+
Labels: map[string]string{
50+
"vendor": "aquasecurity",
51+
},
52+
},
53+
},
4654
},
4755
Results: []types.Result{
4856
{
@@ -185,6 +193,10 @@ func TestEncoder_Encode(t *testing.T) {
185193
BOMRef: "pkg:oci/debian@sha256%3A4482958b4461ff7d9fabc24b3a9ab1e9a2c85ece07b2db1840c7cbc01d053e90?repository_url=index.docker.io%2Flibrary%2Fdebian",
186194
},
187195
Properties: []core.Property{
196+
{
197+
Name: "Labels:vendor",
198+
Value: "aquasecurity",
199+
},
188200
{
189201
Name: core.PropertyRepoDigest,
190202
Value: "debian@sha256:4482958b4461ff7d9fabc24b3a9ab1e9a2c85ece07b2db1840c7cbc01d053e90",

pkg/sbom/spdx/marshal_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func TestMarshaler_Marshal(t *testing.T) {
4949
RepoDigests: []string{"rails@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"},
5050
ImageConfig: v1.ConfigFile{
5151
Architecture: "arm64",
52+
Config: v1.Config{
53+
Labels: map[string]string{
54+
"vendor": "aquasecurity",
55+
},
56+
},
5257
},
5358
},
5459
Results: types.Results{
@@ -199,6 +204,7 @@ func TestMarshaler_Marshal(t *testing.T) {
199204
PackageAttributionTexts: []string{
200205
"DiffID: sha256:d871dadfb37b53ef1ca45be04fc527562b91989991a8f545345ae3be0b93f92a",
201206
"ImageID: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
207+
"Labels:vendor: aquasecurity",
202208
"RepoDigest: rails@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177",
203209
"RepoTag: rails:latest",
204210
"SchemaVersion: 2",

0 commit comments

Comments
 (0)