-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce package UIDs for improved vulnerability mapping (#6583)
Signed-off-by: knqyf263 <[email protected]>
- Loading branch information
Showing
71 changed files
with
1,531 additions
and
874 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ RUN curl --retry 5 -OL https://github.com/protocolbuffers/protobuf/releases/down | |
|
||
# Install Go tools | ||
RUN go install github.com/twitchtv/twirp/[email protected] | ||
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1 | ||
RUN go install github.com/magefile/mage@v1.14.0 | ||
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.0 | ||
RUN go install github.com/magefile/mage@v1.15.0 | ||
|
||
ENV TRIVY_PROTOC_CONTAINER=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,11 @@ import ( | |
"path/filepath" | ||
"testing" | ||
|
||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types" | ||
"github.com/aquasecurity/trivy/pkg/types" | ||
) | ||
|
||
|
@@ -25,7 +25,7 @@ func TestSBOM(t *testing.T) { | |
name string | ||
args args | ||
golden string | ||
override types.Report | ||
override OverrideFunc | ||
}{ | ||
{ | ||
name: "centos7 cyclonedx", | ||
|
@@ -35,31 +35,17 @@ func TestSBOM(t *testing.T) { | |
artifactType: "cyclonedx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-cyclonedx.json", | ||
ArtifactType: ftypes.ArtifactType("cyclonedx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)", | ||
Vulnerabilities: []types.DetectedVulnerability{ | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.json" | ||
want.ArtifactType = ftypes.ArtifactCycloneDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.json (centos 7.6.1810)" | ||
|
||
require.Len(t, got.Results[0].Vulnerabilities, 3) | ||
want.Results[0].Vulnerabilities[0].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[1].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[2].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
}, | ||
}, | ||
{ | ||
|
@@ -88,31 +74,17 @@ func TestSBOM(t *testing.T) { | |
artifactType: "cyclonedx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl", | ||
ArtifactType: ftypes.ArtifactType("cyclonedx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)", | ||
Vulnerabilities: []types.DetectedVulnerability{ | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
{ | ||
PkgIdentifier: ftypes.PkgIdentifier{ | ||
BOMRef: "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl" | ||
want.ArtifactType = ftypes.ArtifactCycloneDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-cyclonedx.intoto.jsonl (centos 7.6.1810)" | ||
|
||
require.Len(t, got.Results[0].Vulnerabilities, 3) | ||
want.Results[0].Vulnerabilities[0].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[1].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
want.Results[0].Vulnerabilities[2].PkgIdentifier.BOMRef = "pkg:rpm/centos/[email protected]?arch=x86_64&epoch=1&distro=centos-7.6.1810" | ||
}, | ||
}, | ||
{ | ||
|
@@ -123,14 +95,12 @@ func TestSBOM(t *testing.T) { | |
artifactType: "spdx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-spdx.txt", | ||
ArtifactType: ftypes.ArtifactType("spdx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)", | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.txt" | ||
want.ArtifactType = ftypes.ArtifactSPDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-spdx.txt (centos 7.6.1810)" | ||
}, | ||
}, | ||
{ | ||
|
@@ -141,14 +111,12 @@ func TestSBOM(t *testing.T) { | |
artifactType: "spdx", | ||
}, | ||
golden: "testdata/centos-7.json.golden", | ||
override: types.Report{ | ||
ArtifactName: "testdata/fixtures/sbom/centos-7-spdx.json", | ||
ArtifactType: ftypes.ArtifactType("spdx"), | ||
Results: types.Results{ | ||
{ | ||
Target: "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)", | ||
}, | ||
}, | ||
override: func(t *testing.T, want, got *types.Report) { | ||
want.ArtifactName = "testdata/fixtures/sbom/centos-7-spdx.json" | ||
want.ArtifactType = ftypes.ArtifactSPDX | ||
|
||
require.Len(t, got.Results, 1) | ||
want.Results[0].Target = "testdata/fixtures/sbom/centos-7-spdx.json (centos 7.6.1810)" | ||
}, | ||
}, | ||
{ | ||
|
@@ -195,20 +163,30 @@ func TestSBOM(t *testing.T) { | |
osArgs = append(osArgs, tt.args.input) | ||
|
||
// Run "trivy sbom" | ||
err := execute(osArgs) | ||
assert.NoError(t, err) | ||
|
||
// Compare want and got | ||
switch tt.args.format { | ||
case "json": | ||
compareSBOMReports(t, tt.golden, outputFile, tt.override) | ||
default: | ||
require.Fail(t, "invalid format", "format: %s", tt.args.format) | ||
} | ||
runTest(t, osArgs, tt.golden, outputFile, types.Format(tt.args.format), runOptions{ | ||
override: overrideFuncs(overrideSBOMReport, overrideUID, tt.override), | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func overrideSBOMReport(t *testing.T, want, got *types.Report) { | ||
want.Metadata.ImageID = "" | ||
want.Metadata.ImageConfig = v1.ConfigFile{} | ||
want.Metadata.DiffIDs = nil | ||
for i, result := range want.Results { | ||
for j := range result.Vulnerabilities { | ||
want.Results[i].Vulnerabilities[j].Layer.DiffID = "" | ||
} | ||
} | ||
|
||
// when running on Windows FS | ||
got.ArtifactName = filepath.ToSlash(filepath.Clean(got.ArtifactName)) | ||
for i, result := range got.Results { | ||
got.Results[i].Target = filepath.ToSlash(filepath.Clean(result.Target)) | ||
} | ||
} | ||
|
||
// TODO(teppei): merge into compareReports | ||
func compareSBOMReports(t *testing.T, wantFile, gotFile string, overrideWant types.Report) { | ||
want := readReport(t, wantFile) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,8 @@ | |
"PkgID": "[email protected]_64", | ||
"PkgName": "openssl-libs", | ||
"PkgIdentifier": { | ||
"PURL": "pkg:rpm/alma/[email protected]?arch=x86_64\u0026distro=alma-8.5\u0026epoch=1" | ||
"PURL": "pkg:rpm/alma/[email protected]?arch=x86_64\u0026distro=alma-8.5\u0026epoch=1", | ||
"UID": "3f965238234faa63" | ||
}, | ||
"InstalledVersion": "1:1.1.1k-4.el8", | ||
"FixedVersion": "1:1.1.1k-5.el8_5", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,8 @@ | |
"PkgID": "[email protected]", | ||
"PkgName": "libcrypto1.1", | ||
"PkgIdentifier": { | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2" | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2", | ||
"UID": "c6c116a4441ec6de" | ||
}, | ||
"InstalledVersion": "1.1.1c-r0", | ||
"FixedVersion": "1.1.1d-r0", | ||
|
@@ -131,7 +132,8 @@ | |
"PkgID": "[email protected]", | ||
"PkgName": "libcrypto1.1", | ||
"PkgIdentifier": { | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2" | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2", | ||
"UID": "c6c116a4441ec6de" | ||
}, | ||
"InstalledVersion": "1.1.1c-r0", | ||
"FixedVersion": "1.1.1d-r2", | ||
|
@@ -213,7 +215,8 @@ | |
"PkgID": "[email protected]", | ||
"PkgName": "libssl1.1", | ||
"PkgIdentifier": { | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2" | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2", | ||
"UID": "e132dcfcc51772ef" | ||
}, | ||
"InstalledVersion": "1.1.1c-r0", | ||
"FixedVersion": "1.1.1d-r0", | ||
|
@@ -285,7 +288,8 @@ | |
"PkgID": "[email protected]", | ||
"PkgName": "libssl1.1", | ||
"PkgIdentifier": { | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2" | ||
"PURL": "pkg:apk/alpine/[email protected]?arch=x86_64\u0026distro=3.10.2", | ||
"UID": "e132dcfcc51772ef" | ||
}, | ||
"InstalledVersion": "1.1.1c-r0", | ||
"FixedVersion": "1.1.1d-r2", | ||
|
Oops, something went wrong.