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

Pick up CycloneDX BOM components from metadata as well #3092

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
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/main-component@1.2.3",
"type" : "library",
"bom-ref" : "pkg:maven/org.example/main-component@1.2.3"
}
},
"components" : [
{
"name" : "first-subcomponent",
"version" : "2.3.4",
"purl" : "pkg:maven/org.example/first-subcomponent@2.3.4",
"type" : "library",
"bom-ref" : "pkg:maven/org.example/first-subcomponent@2.3.4"
}
],
"dependencies" : [
{
"ref" : "pkg:maven/org.example/main-component-assembly@1.2.3",
"dependsOn" : [
"pkg:maven/org.example/first-subcomponent@2.3.4"
]
}
]
}
27 changes: 24 additions & 3 deletions syft/format/internal/cyclonedxutil/helpers/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,32 @@
}

func collectBomPackages(bom *cyclonedx.BOM, s *sbom.SBOM, idMap map[string]interface{}) error {
if bom.Components == nil {
components := []cyclonedx.Component{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this change could be simplified to just adding this block:

	if bom.Metadata.Component != nil
		collectPackages(bom.Metadata.Component, s, idMap)
	}

... and in the collectPackages, we should be recursively adding sub-components, and checking for the type being one of the right types, which it looks like it's doing already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have looked at exactly what the code was doing there before sending you down a path to implement an unnecessary thing 🤦

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I also didn't see the logic was already present. Good thing you spotted it, I refactored it, the change is much simpler now 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have missed suggesting a bom.Metadata != nil bit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, fixed

componentsPresent := false
if bom.Components != nil {
components = *bom.Components
componentsPresent = true
}

if bom.Metadata.Component != nil &&
(bom.Metadata.Component.Type == cyclonedx.ComponentTypeApplication ||
bom.Metadata.Component.Type == cyclonedx.ComponentTypeFramework ||
bom.Metadata.Component.Type == cyclonedx.ComponentTypeLibrary ||
bom.Metadata.Component.Type == cyclonedx.ComponentTypePlatform) {

Check failure on line 53 in syft/format/internal/cyclonedxutil/helpers/decoder.go

View workflow job for this annotation

GitHub Actions / Static analysis

unnecessary leading newline (whitespace)

components = append(components, *bom.Metadata.Component)
componentsPresent = true
if bom.Metadata.Component.Components != nil {
components = append(components, *bom.Metadata.Component.Components...)
}
}

if !componentsPresent {
return fmt.Errorf("no components are defined in the CycloneDX BOM")
}
for i := range *bom.Components {
collectPackages(&(*bom.Components)[i], s, idMap)

for i := range components {
collectPackages(&components[i], s, idMap)
}
return nil
}
Expand Down
Loading