Skip to content

Commit

Permalink
fix: read CycloneDX BOM components from metadata (#3092)
Browse files Browse the repository at this point in the history
Signed-off-by: dervoeti <[email protected]>
  • Loading branch information
dervoeti authored Aug 12, 2024
1 parent df1e5b5 commit 3161e18
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
25 changes: 25 additions & 0 deletions cmd/syft/internal/test/integration/sbom_metadata_component_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package integration

import (
"reflect"
"testing"

"github.com/anchore/syft/syft/pkg"
"github.com/anchore/syft/syft/source"
)

func TestSbomMetadataComponent(t *testing.T) {
sbom, _ := catalogFixtureImage(t, "image-sbom-metadata-component", source.SquashedScope, "+sbom-cataloger")

expectedPkgs := []string{"first-subcomponent", "main-component"}
foundPkgs := []string{}

for sbomPkg := range sbom.Artifacts.Packages.Enumerate(pkg.JavaPkg) {
foundPkgs = append(foundPkgs, sbomPkg.Name)
}

// check if both the package in `.metadata.component` and the one in `.components` were found
if !reflect.DeepEqual(expectedPkgs, foundPkgs) {
t.Errorf("expected packages %v, got %v", expectedPkgs, foundPkgs)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM scratch
COPY test.cdx.json /
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"bomFormat" : "CycloneDX",
"specVersion" : "1.5",
"serialNumber" : "urn:uuid:dc807d4b-0415-35ab-ba61-49b5d39bc2d9",
"version" : 1,
"metadata" : {
"component" : {
"name" : "main-component",
"version" : "1.2.3",
"purl" : "pkg:maven/org.example/[email protected]",
"type" : "library",
"bom-ref" : "pkg:maven/org.example/[email protected]"
}
},
"components" : [
{
"name" : "first-subcomponent",
"version" : "2.3.4",
"purl" : "pkg:maven/org.example/[email protected]",
"type" : "library",
"bom-ref" : "pkg:maven/org.example/[email protected]"
}
],
"dependencies" : [
{
"ref" : "pkg:maven/org.example/[email protected]",
"dependsOn" : [
"pkg:maven/org.example/[email protected]"
]
}
]
}
19 changes: 15 additions & 4 deletions syft/format/internal/cyclonedxutil/helpers/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,23 @@ func ToSyftModel(bom *cyclonedx.BOM) (*sbom.SBOM, error) {
}

func collectBomPackages(bom *cyclonedx.BOM, s *sbom.SBOM, idMap map[string]interface{}) error {
if bom.Components == nil {
return fmt.Errorf("no components are defined in the CycloneDX BOM")
componentsPresent := false
if bom.Components != nil {
for i := range *bom.Components {
collectPackages(&(*bom.Components)[i], s, idMap)
}
componentsPresent = true
}
for i := range *bom.Components {
collectPackages(&(*bom.Components)[i], s, idMap)

if bom.Metadata != nil && bom.Metadata.Component != nil {
collectPackages(bom.Metadata.Component, s, idMap)
componentsPresent = true
}

if !componentsPresent {
return fmt.Errorf("no components are defined in the CycloneDX BOM")
}

return nil
}

Expand Down

0 comments on commit 3161e18

Please sign in to comment.