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

sbom: use relationship data to reconstruct package files #17

Merged
merged 1 commit into from
Feb 9, 2023
Merged
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
52 changes: 45 additions & 7 deletions sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
spdx_json "github.com/spdx/tools-golang/json"
spdx_common "github.com/spdx/tools-golang/spdx/common"
spdx "github.com/spdx/tools-golang/spdx/v2_3"
)

Expand Down Expand Up @@ -121,16 +122,22 @@ func (l *Loader) scanSBOM(ctx context.Context, fetcher remotes.Fetcher, r *resul
return nil
}

func addSPDX(img *Image, doc *spdx.Document) {
func addSPDX(img *Image, doc spdxDocument) {
sbom := img.SBOM
if sbom == nil {
sbom = &SBOM{}
}

for _, p := range doc.Packages {
var files []string
for _, f := range p.Files {
files = append(files, f.FileName)
for _, relationship := range doc.RelationshipsByPackageID[p.PackageSPDXIdentifier] {
if relationship.Relationship == "CONTAINS" {
f, ok := doc.FilesByID[relationship.RefB.ElementRefID]
if !ok {
continue
}
files = append(files, f.FileName)
}
}

pkg := Package{
Expand Down Expand Up @@ -191,13 +198,44 @@ func normalizeSBOM(sbom *SBOM) {
}
}

func decodeSPDX(dt []byte) (s *spdx.Document, err error) {
func decodeSPDX(dt []byte) (s spdxDocument, err error) {
doc, err := spdx_json.Load2_3(bytes.NewReader(dt))
if err != nil {
return nil, errors.Wrap(err, "unable to decode spdx")
return spdxDocument{}, errors.Wrap(err, "unable to decode spdx")
}
if doc == nil {
return nil, errors.New("decoding produced empty spdx document")
return spdxDocument{}, errors.New("decoding produced empty spdx document")
}
return newSPDXWrapper(doc), nil
}

type spdxDocument struct {
*spdx.Document

PackagesByID map[spdx_common.ElementID]*spdx.Package
FilesByID map[spdx_common.ElementID]*spdx.File

RelationshipsByPackageID map[spdx_common.ElementID][]*spdx.Relationship
}

func newSPDXWrapper(doc *spdx.Document) spdxDocument {
packagesByID := map[spdx_common.ElementID]*spdx.Package{}
for _, p := range doc.Packages {
packagesByID[p.PackageSPDXIdentifier] = p
}
filesByID := map[spdx_common.ElementID]*spdx.File{}
for _, f := range doc.Files {
filesByID[f.FileSPDXIdentifier] = f
}
relationshipsByPackageID := map[spdx_common.ElementID][]*spdx.Relationship{}
for _, r := range doc.Relationships {
relationshipsByPackageID[r.RefA.ElementRefID] = append(relationshipsByPackageID[r.RefA.ElementRefID], r)
}

return spdxDocument{
Document: doc,
PackagesByID: packagesByID,
FilesByID: filesByID,
RelationshipsByPackageID: relationshipsByPackageID,
}
return doc, nil
}