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): component UUID to calc SPDXID's #7837

Draft
wants to merge 4 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
20 changes: 10 additions & 10 deletions integration/testdata/conda-spdx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"packages": [
{
"name": "openssl",
"SPDXID": "SPDXRef-Package-22a178da112ac20a",
"SPDXID": "SPDXRef-Package-d8a5e692df746bd2",
"versionInfo": "1.1.1q",
"supplier": "NOASSERTION",
"downloadLocation": "NONE",
Expand Down Expand Up @@ -43,7 +43,7 @@
},
{
"name": "pip",
"SPDXID": "SPDXRef-Package-c22b9ee9a601ba6",
"SPDXID": "SPDXRef-Package-d8a5e692df746bd3",
"versionInfo": "22.2.2",
"supplier": "NOASSERTION",
"downloadLocation": "NONE",
Expand Down Expand Up @@ -72,7 +72,7 @@
},
{
"name": "testdata/fixtures/repo/conda",
"SPDXID": "SPDXRef-Filesystem-2e2426fd0f2580ef",
"SPDXID": "SPDXRef-Filesystem-d8a5e692df746bd1",
"downloadLocation": "NONE",
"filesAnalyzed": false,
"primaryPackagePurpose": "SOURCE",
Expand Down Expand Up @@ -113,26 +113,26 @@
"relationships": [
{
"spdxElementId": "SPDXRef-DOCUMENT",
"relatedSpdxElement": "SPDXRef-Filesystem-2e2426fd0f2580ef",
"relatedSpdxElement": "SPDXRef-Filesystem-d8a5e692df746bd1",
"relationshipType": "DESCRIBES"
},
{
"spdxElementId": "SPDXRef-Filesystem-2e2426fd0f2580ef",
"relatedSpdxElement": "SPDXRef-Package-22a178da112ac20a",
"spdxElementId": "SPDXRef-Filesystem-d8a5e692df746bd1",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd2",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Filesystem-2e2426fd0f2580ef",
"relatedSpdxElement": "SPDXRef-Package-c22b9ee9a601ba6",
"spdxElementId": "SPDXRef-Filesystem-d8a5e692df746bd1",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd3",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Package-22a178da112ac20a",
"spdxElementId": "SPDXRef-Package-d8a5e692df746bd2",
"relatedSpdxElement": "SPDXRef-File-600e5e0110a84891",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Package-c22b9ee9a601ba6",
"spdxElementId": "SPDXRef-Package-d8a5e692df746bd3",
"relatedSpdxElement": "SPDXRef-File-7eb62e2a3edddc0a",
"relationshipType": "CONTAINS"
}
Expand Down
40 changes: 20 additions & 20 deletions integration/testdata/julia-spdx.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"packages": [
{
"name": "Manifest.toml",
"SPDXID": "SPDXRef-Application-18fc3597717a3e56",
"SPDXID": "SPDXRef-Application-d8a5e692df746bd2",
"downloadLocation": "NONE",
"filesAnalyzed": false,
"primaryPackagePurpose": "APPLICATION",
Expand All @@ -35,7 +35,7 @@
},
{
"name": "A",
"SPDXID": "SPDXRef-Package-7784b00da0cb0cb0",
"SPDXID": "SPDXRef-Package-d8a5e692df746bd3",
"versionInfo": "1.9.0",
"supplier": "NOASSERTION",
"downloadLocation": "NONE",
Expand Down Expand Up @@ -68,7 +68,7 @@
},
{
"name": "B",
"SPDXID": "SPDXRef-Package-960543ac5c5f7e10",
"SPDXID": "SPDXRef-Package-d8a5e692df746bd4",
"versionInfo": "1.9.0",
"supplier": "NOASSERTION",
"downloadLocation": "NONE",
Expand All @@ -80,7 +80,7 @@
{
"referenceCategory": "PACKAGE-MANAGER",
"referenceType": "purl",
"referenceLocator": "pkg:julia/[email protected]?uuid=f41f7b98-334e-11e9-1257-49272045fb24"
"referenceLocator": "pkg:julia/[email protected]?uuid=edca9bc6-334e-11e9-3554-9595dbb4349c"
}
],
"primaryPackagePurpose": "LIBRARY",
Expand All @@ -89,7 +89,7 @@
"annotator": "Tool: trivy-dev",
"annotationDate": "2021-08-25T12:20:30Z",
"annotationType": "OTHER",
"comment": "PkgID: f41f7b98-334e-11e9-1257-49272045fb24"
"comment": "PkgID: edca9bc6-334e-11e9-3554-9595dbb4349c"
},
{
"annotator": "Tool: trivy-dev",
Expand All @@ -101,7 +101,7 @@
},
{
"name": "B",
"SPDXID": "SPDXRef-Package-a4705eb108e4f15c",
"SPDXID": "SPDXRef-Package-d8a5e692df746bd5",
"versionInfo": "1.9.0",
"supplier": "NOASSERTION",
"downloadLocation": "NONE",
Expand All @@ -113,7 +113,7 @@
{
"referenceCategory": "PACKAGE-MANAGER",
"referenceType": "purl",
"referenceLocator": "pkg:julia/[email protected]?uuid=edca9bc6-334e-11e9-3554-9595dbb4349c"
"referenceLocator": "pkg:julia/[email protected]?uuid=f41f7b98-334e-11e9-1257-49272045fb24"
}
],
"primaryPackagePurpose": "LIBRARY",
Expand All @@ -122,7 +122,7 @@
"annotator": "Tool: trivy-dev",
"annotationDate": "2021-08-25T12:20:30Z",
"annotationType": "OTHER",
"comment": "PkgID: edca9bc6-334e-11e9-3554-9595dbb4349c"
"comment": "PkgID: f41f7b98-334e-11e9-1257-49272045fb24"
},
{
"annotator": "Tool: trivy-dev",
Expand All @@ -134,7 +134,7 @@
},
{
"name": "testdata/fixtures/repo/julia",
"SPDXID": "SPDXRef-Filesystem-1be792dd0077c431",
"SPDXID": "SPDXRef-Filesystem-d8a5e692df746bd1",
"downloadLocation": "NONE",
"filesAnalyzed": false,
"primaryPackagePurpose": "SOURCE",
Expand All @@ -150,33 +150,33 @@
],
"relationships": [
{
"spdxElementId": "SPDXRef-Application-18fc3597717a3e56",
"relatedSpdxElement": "SPDXRef-Package-7784b00da0cb0cb0",
"spdxElementId": "SPDXRef-Application-d8a5e692df746bd2",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd3",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Application-18fc3597717a3e56",
"relatedSpdxElement": "SPDXRef-Package-960543ac5c5f7e10",
"spdxElementId": "SPDXRef-Application-d8a5e692df746bd2",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd4",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Application-18fc3597717a3e56",
"relatedSpdxElement": "SPDXRef-Package-a4705eb108e4f15c",
"spdxElementId": "SPDXRef-Application-d8a5e692df746bd2",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd5",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-DOCUMENT",
"relatedSpdxElement": "SPDXRef-Filesystem-1be792dd0077c431",
"relatedSpdxElement": "SPDXRef-Filesystem-d8a5e692df746bd1",
"relationshipType": "DESCRIBES"
},
{
"spdxElementId": "SPDXRef-Filesystem-1be792dd0077c431",
"relatedSpdxElement": "SPDXRef-Application-18fc3597717a3e56",
"spdxElementId": "SPDXRef-Filesystem-d8a5e692df746bd1",
"relatedSpdxElement": "SPDXRef-Application-d8a5e692df746bd2",
"relationshipType": "CONTAINS"
},
{
"spdxElementId": "SPDXRef-Package-7784b00da0cb0cb0",
"relatedSpdxElement": "SPDXRef-Package-960543ac5c5f7e10",
"spdxElementId": "SPDXRef-Package-d8a5e692df746bd3",
"relatedSpdxElement": "SPDXRef-Package-d8a5e692df746bd5",
"relationshipType": "DEPENDS_ON"
}
]
Expand Down
38 changes: 10 additions & 28 deletions pkg/sbom/spdx/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package spdx
import (
"context"
"fmt"
"hash/fnv"
"slices"
"sort"
"strings"
"time"

"github.com/mitchellh/hashstructure/v2"
"github.com/package-url/packageurl-go"
"github.com/samber/lo"
"github.com/spdx/tools-golang/spdx"
Expand Down Expand Up @@ -80,31 +80,15 @@ var duplicateProperties = []string{

type Marshaler struct {
format spdx.Document
hasher Hash
appVersion string // Trivy version. It needed for `creator` field
}

type Hash func(v any, format hashstructure.Format, opts *hashstructure.HashOptions) (uint64, error)

type marshalOption func(*Marshaler)

func WithHasher(hasher Hash) marshalOption {
return func(opts *Marshaler) {
opts.hasher = hasher
}
}

func NewMarshaler(version string, opts ...marshalOption) *Marshaler {
func NewMarshaler(version string) *Marshaler {
m := &Marshaler{
format: spdx.Document{},
hasher: hashstructure.Hash,
appVersion: version,
}

for _, opt := range opts {
opt(m)
}

return m
}

Expand Down Expand Up @@ -249,7 +233,7 @@ func (m *Marshaler) rootSPDXPackage(root *core.Component, timeNow, pkgDownloadLo
externalReferences = append(externalReferences, m.purlExternalReference(root.PkgIdentifier.PURL.String()))
}

pkgID, err := calcPkgID(m.hasher, fmt.Sprintf("%s-%s", root.Name, root.Type))
pkgID, err := calcPkgID(root.ID().String())
if err != nil {
return nil, xerrors.Errorf("failed to get %s package ID: %w", pkgID, err)
}
Expand Down Expand Up @@ -301,7 +285,7 @@ func (m *Marshaler) advisoryExternalReference(primaryURL string) *spdx.PackageEx
}

func (m *Marshaler) spdxPackage(c *core.Component, timeNow, pkgDownloadLocation string) (spdx.Package, error) {
pkgID, err := calcPkgID(m.hasher, c)
pkgID, err := calcPkgID(c.ID().String())
if err != nil {
return spdx.Package{}, xerrors.Errorf("failed to get os metadata package ID: %w", err)
}
Expand Down Expand Up @@ -435,7 +419,7 @@ func (m *Marshaler) spdxFiles(c *core.Component) ([]*spdx.File, error) {
}

func (m *Marshaler) spdxFile(filePath string, digests []digest.Digest) (*spdx.File, error) {
pkgID, err := calcPkgID(m.hasher, filePath)
pkgID, err := calcPkgID(filePath)
if err != nil {
return nil, xerrors.Errorf("failed to get %s package ID: %w", filePath, err)
}
Expand Down Expand Up @@ -518,16 +502,14 @@ func getDocumentNamespace(root *core.Component) string {
)
}

func calcPkgID(h Hash, v any) (string, error) {
f, err := h(v, hashstructure.FormatV2, &hashstructure.HashOptions{
ZeroNil: true,
SlicesAsSets: true,
})
func calcPkgID(s string) (string, error) {
h := fnv.New64()
_, err := h.Write([]byte(s))
if err != nil {
return "", xerrors.Errorf("could not build package ID for %+v: %w", v, err)
return "", xerrors.Errorf("could not build package ID for %q: %w", s, err)
}

return fmt.Sprintf("%x", f), nil
return fmt.Sprintf("%x", h.Sum64()), nil
}

func camelCase(inputUnderScoreStr string) (camelCase string) {
Expand Down
Loading