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

refactor: use UUID for Packages from pom.xml files. #7879

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions integration/testdata/pom-cyclonedx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat": "CycloneDX",
"specVersion": "1.6",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000005",
"serialNumber": "urn:uuid:3ff14136-e09f-4df9-80ea-000000000007",
"version": 1,
"metadata": {
"timestamp": "2021-08-25T12:20:30+00:00",
Expand All @@ -17,7 +17,7 @@
]
},
"component": {
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000001",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000003",
"type": "application",
"name": "testdata/fixtures/repo/pom",
"properties": [
Expand All @@ -30,7 +30,7 @@
},
"components": [
{
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000002",
"bom-ref": "3ff14136-e09f-4df9-80ea-000000000004",
"type": "application",
"name": "pom.xml",
"properties": [
Expand All @@ -54,7 +54,7 @@
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "com.example:log4shell:1.0-SNAPSHOT"
"value": "3ff14136-e09f-4df9-80ea-000000000001"
},
{
"name": "aquasecurity:trivy:PkgType",
Expand All @@ -72,7 +72,7 @@
"properties": [
{
"name": "aquasecurity:trivy:PkgID",
"value": "com.fasterxml.jackson.core:jackson-databind:2.9.1"
"value": "3ff14136-e09f-4df9-80ea-000000000002"
},
{
"name": "aquasecurity:trivy:PkgType",
Expand All @@ -83,13 +83,13 @@
],
"dependencies": [
{
"ref": "3ff14136-e09f-4df9-80ea-000000000001",
"ref": "3ff14136-e09f-4df9-80ea-000000000003",
"dependsOn": [
"3ff14136-e09f-4df9-80ea-000000000002"
"3ff14136-e09f-4df9-80ea-000000000004"
]
},
{
"ref": "3ff14136-e09f-4df9-80ea-000000000002",
"ref": "3ff14136-e09f-4df9-80ea-000000000004",
"dependsOn": [
"pkg:maven/com.example/[email protected]"
]
Expand Down
8 changes: 4 additions & 4 deletions integration/testdata/pom.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2020-9548",
"PkgID": "com.fasterxml.jackson.core:jackson-databind:2.9.1",
"PkgID": "3ff14136-e09f-4df9-80ea-000000000002",
"PkgName": "com.fasterxml.jackson.core:jackson-databind",
"PkgIdentifier": {
"PURL": "pkg:maven/com.fasterxml.jackson.core/[email protected]",
"UID": "a704d87fd1c0b0e1"
"UID": "d0a262c16975e41d"
},
"InstalledVersion": "2.9.1",
"FixedVersion": "2.9.10.4",
Expand Down Expand Up @@ -89,11 +89,11 @@
},
{
"VulnerabilityID": "CVE-2021-20190",
"PkgID": "com.fasterxml.jackson.core:jackson-databind:2.9.1",
"PkgID": "3ff14136-e09f-4df9-80ea-000000000002",
"PkgName": "com.fasterxml.jackson.core:jackson-databind",
"PkgIdentifier": {
"PURL": "pkg:maven/com.fasterxml.jackson.core/[email protected]",
"UID": "a704d87fd1c0b0e1"
"UID": "d0a262c16975e41d"
},
"InstalledVersion": "2.9.1",
"FixedVersion": "2.9.10.7",
Expand Down
2 changes: 2 additions & 0 deletions pkg/dependency/parser/java/pom/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"sync"

"github.com/aquasecurity/trivy/pkg/uuid"
"github.com/samber/lo"

ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
Expand All @@ -25,6 +26,7 @@ var (
)

type artifact struct {
ID uuid.UUID // UUID is required to build correctly dep tree when multiple modules contain dependencies with same GAV
GroupID string
ArtifactID string
Version version
Expand Down
23 changes: 13 additions & 10 deletions pkg/dependency/parser/java/pom/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"sort"
"strings"

"github.com/aquasecurity/trivy/pkg/uuid"
"github.com/hashicorp/go-multierror"
"github.com/samber/lo"
"golang.org/x/net/html/charset"
Expand Down Expand Up @@ -132,7 +133,7 @@
deps ftypes.Dependencies
rootDepManagement []pomDependency
uniqArtifacts = make(map[string]artifact)
uniqDeps = make(map[string][]string)
uniqDeps = make(map[uuid.UUID][]string)
)

// Iterate direct and transitive dependencies
Expand Down Expand Up @@ -209,26 +210,28 @@
// Offline mode may be missing some fields.
if !art.IsEmpty() {
// Override the version
uniqArtifacts[art.Name()] = artifact{
newArt := artifact{
ID: uuid.New(),
Version: art.Version,
Licenses: result.artifact.Licenses,
Relationship: art.Relationship,
Locations: art.Locations,
}
uniqArtifacts[art.Name()] = newArt

// save only dependency names
// version will be determined later
dependsOn := lo.Map(result.dependencies, func(a artifact, _ int) string {
return a.Name()
})
uniqDeps[packageID(art.Name(), art.Version.String())] = dependsOn
uniqDeps[newArt.ID] = dependsOn
}
}

// Convert to []ftypes.Package and []ftypes.Dependency
for name, art := range uniqArtifacts {
pkg := ftypes.Package{
ID: packageID(name, art.Version.String()),
ID: art.ID.String(),
Name: name,
Version: art.Version.String(),
Licenses: art.Licenses,
Expand All @@ -238,9 +241,9 @@
pkgs = append(pkgs, pkg)

// Convert dependency names into dependency IDs
dependsOn := lo.FilterMap(uniqDeps[pkg.ID], func(dependOnName string, _ int) (string, bool) {
ver := depVersion(dependOnName, uniqArtifacts)
return packageID(dependOnName, ver), ver != ""
dependsOn := lo.FilterMap(uniqDeps[art.ID], func(dependOnName string, _ int) (string, bool) {
id := depID(dependOnName, uniqArtifacts)
return id, id != ""
})

sort.Strings(dependsOn)
Expand All @@ -258,10 +261,10 @@
return pkgs, deps, nil
}

// depVersion finds dependency in uniqArtifacts and return its version
func depVersion(depName string, uniqArtifacts map[string]artifact) string {
// depID finds dependency in uniqArtifacts and return its ID
func depID(depName string, uniqArtifacts map[string]artifact) string {
if art, ok := uniqArtifacts[depName]; ok {
return art.Version.String()
return art.ID.String()
}
return ""
}
Expand Down Expand Up @@ -809,7 +812,7 @@
return parsed, nil
}

func packageID(name, version string) string {

Check failure on line 815 in pkg/dependency/parser/java/pom/parse.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

func `packageID` is unused (unused)
return dependency.ID(ftypes.Pom, name, version)
}

Expand Down
Loading
Loading