Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sbom): export bom-ref when converting a package to a component #7340

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func readCycloneDX(t *testing.T, filePath string) *cdx.BOM {
return (*bom.Components)[i].Name < (*bom.Components)[j].Name
})
for i := range *bom.Components {
(*bom.Components)[i].BOMRef = ""
sort.Slice(*(*bom.Components)[i].Properties, func(ii, jj int) bool {
return (*(*bom.Components)[i].Properties)[ii].Name < (*(*bom.Components)[i].Properties)[jj].Name
})
Expand Down
12 changes: 12 additions & 0 deletions integration/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestSBOM(t *testing.T) {
name string
args args
golden string
fakeUUID string
override OverrideFunc
}{
{
Expand Down Expand Up @@ -57,6 +58,16 @@ func TestSBOM(t *testing.T) {
},
golden: "testdata/fluentd-multiple-lockfiles.json.golden",
},
{
name: "scan SBOM into SBOM",
args: args{
input: "testdata/fixtures/sbom/pywin32-cyclonedx.json",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we re-use the existing json, fluentd-multiple-lockfiles-cyclonedx.json? You can change this file to use UUID in BOM-Ref.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the testcase is updated fluentd-multiple-lockfiles-cyclonedx.json.
I wanted to use fluentd-multiple-lockfiles.cdx.json.golden also, but this cdx result is a large, because it's result for fluentd-multiple-lockfiles.tar.gz. fluentd-multiple-lockfiles-cyclonedx.json is shorter.
so I've created a new golden file.

format: "cyclonedx",
artifactType: "cyclonedx",
},
fakeUUID: "3ff14136-e09f-4df9-80ea-%012d",
golden: "testdata/pywin32-cyclonedx.cdx.json.golden",
},
{
name: "minikube KBOM",
args: args{
Expand Down Expand Up @@ -165,6 +176,7 @@ func TestSBOM(t *testing.T) {
// Run "trivy sbom"
runTest(t, osArgs, tt.golden, outputFile, types.Format(tt.args.format), runOptions{
override: overrideFuncs(overrideSBOMReport, overrideUID, tt.override),
fakeUUID: tt.fakeUUID,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we only use fakeUUID for serialNumber:

"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000006",

This might be confusing (that we don't overwrite other UUID's).
Maybe we just don't check serialNumber?
@knqyf263 @afdesk wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The serial number is generated by UUID. I think it's better to confirm it is generated as expected rather than ignoring it.

})
})
}
Expand Down
116 changes: 116 additions & 0 deletions integration/testdata/fixtures/sbom/pywin32-cyclonedx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:b61985f0-720e-4ece-81cc-71f49db503b5",
"version": 1,
"metadata": {
"timestamp": "2024-08-01T19:19:56+00:00",
"tools": {
"components": [
{
"type": "application",
"group": "aquasecurity",
"name": "trivy",
"version": "dev"
}
]
},
"component": {
"bom-ref": "d5461c73-cabf-4634-a4ef-489698717e3d",
"type": "application",
"name": ".",
"properties": [
{
"name": "aquasecurity:trivy:SchemaVersion",
"value": "2"
}
]
}
},
"components": [
{
"bom-ref": "469bcf7c-b085-40cf-8932-d581cb91f11b",
"type": "application",
"name": "poetry.lock",
"properties": [
{
"name": "aquasecurity:trivy:Class",
"value": "lang-pkgs"
},
{
"name": "aquasecurity:trivy:Type",
"value": "poetry"
}
]
},
{
"bom-ref": "pkg:pypi/pywin32@227",
"type": "library",
"name": "pywin32",
"version": "227",
"purl": "pkg:pypi/pywin32@227",
"licenses": [
{
"expression": "MIT"
}
],
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "pywin32@227"
},
{
"name": "aquasecurity:trivy:PkgType",
"value": "poetry"
}
]
},
{
"bom-ref": "de3ojve0eoj0j0je",
"type": "library",
"name": "pywin32",
"version": "227",
"purl": "pkg:pypi/pywin32@227",
"licenses": [
{
"expression": "MIT"
}
],
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "pywin32@227"
},
{
"name": "aquasecurity:trivy:PkgType",
"value": "poetry"
}
]
}
],
"dependencies": [
{
"ref": "469bcf7c-b085-40cf-8932-d581cb91f11b",
"dependsOn": [
"pkg:pypi/pywin32@227",
"de3ojve0eoj0j0je"
]
},
{
"ref": "d5461c73-cabf-4634-a4ef-489698717e3d",
"dependsOn": [
"469bcf7c-b085-40cf-8932-d581cb91f11b"
]
},
{
"ref": "pkg:pypi/pywin32@227",
"dependsOn": []
},
{
"ref": "de3ojve0eoj0j0je",
"dependsOn": []
}
],
"vulnerabilities": []
}
121 changes: 121 additions & 0 deletions integration/testdata/pywin32-cyclonedx.cdx.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000008",
"version": 1,
"metadata": {
"timestamp": "2021-08-25T12:20:30+00:00",
"tools": {
"components": [
{
"type": "application",
"group": "aquasecurity",
"name": "trivy",
"version": "dev"
}
]
},
"component": {
"bom-ref": "d5461c73-cabf-4634-a4ef-489698717e3d",
"type": "application",
"name": ".",
"purl": "pkg:/",
"properties": [
{
"name": "aquasecurity:trivy:SchemaVersion",
"value": "2"
}
]
}
},
"components": [
{
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000005",
"type": "application",
"name": "poetry.lock",
"properties": [
{
"name": "aquasecurity:trivy:Class",
"value": "lang-pkgs"
},
{
"name": "aquasecurity:trivy:Type",
"value": "poetry"
}
]
},
{
"bom-ref": "de3ojve0eoj0j0je",
"type": "library",
"name": "pywin32",
"version": "227",
"licenses": [
{
"license": {
"name": "MIT"
}
}
],
"purl": "pkg:pypi/pywin32@227",
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "pywin32@227"
},
{
"name": "aquasecurity:trivy:PkgType",
"value": "poetry"
}
]
},
{
"bom-ref": "pkg:pypi/pywin32@227",
"type": "library",
"name": "pywin32",
"version": "227",
"licenses": [
{
"license": {
"name": "MIT"
}
}
],
"purl": "pkg:pypi/pywin32@227",
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "pywin32@227"
},
{
"name": "aquasecurity:trivy:PkgType",
"value": "poetry"
}
]
}
],
"dependencies": [
{
"ref": "3ff14136-e09f-4df9-80ea-000000000005",
"dependsOn": [
"de3ojve0eoj0j0je",
"pkg:pypi/pywin32@227"
]
},
{
"ref": "d5461c73-cabf-4634-a4ef-489698717e3d",
"dependsOn": [
"3ff14136-e09f-4df9-80ea-000000000005"
]
},
{
"ref": "de3ojve0eoj0j0je",
"dependsOn": []
},
{
"ref": "pkg:pypi/pywin32@227",
"dependsOn": []
}
],
"vulnerabilities": []
}
4 changes: 2 additions & 2 deletions pkg/sbom/cyclonedx/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ func TestMarshaler_MarshalReport(t *testing.T) {
Name: "com.fasterxml.jackson.core:jackson-databind",
Version: "2.13.4.1",
Identifier: ftypes.PkgIdentifier{
BOMRef: "pkg:maven/com.fasterxml.jackson.core/[email protected]?file_path=jackson-databind-2.13.4.1.jar",
BOMRef: "pkg:maven/com.fasterxml.jackson.core/[email protected]",
UID: "9A5066570222D04C",
PURL: &packageurl.PackageURL{
Type: packageurl.TypeMaven,
Expand All @@ -1480,7 +1480,7 @@ func TestMarshaler_MarshalReport(t *testing.T) {
PkgName: "com.fasterxml.jackson.core:jackson-databind",
PkgPath: "jackson-databind-2.13.4.1.jar",
PkgIdentifier: ftypes.PkgIdentifier{
BOMRef: "pkg:maven/com.fasterxml.jackson.core/[email protected]?file_path=jackson-databind-2.13.4.1.jar",
BOMRef: "pkg:maven/com.fasterxml.jackson.core/[email protected]",
UID: "9A5066570222D04C",
PURL: &packageurl.PackageURL{
Type: packageurl.TypeMaven,
Expand Down
5 changes: 3 additions & 2 deletions pkg/sbom/io/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,9 @@ func (*Encoder) component(result types.Result, pkg ftypes.Package) *core.Compone
SrcVersion: utils.FormatSrcVersion(pkg),
SrcFile: srcFile,
PkgIdentifier: ftypes.PkgIdentifier{
UID: pkg.Identifier.UID,
PURL: pkg.Identifier.PURL,
UID: pkg.Identifier.UID,
PURL: pkg.Identifier.PURL,
BOMRef: pkg.Identifier.BOMRef,
},
Supplier: pkg.Maintainer,
Licenses: pkg.Licenses,
Expand Down