-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: deep traversal of cyclonedx components
feat: improve discoverComps readability and test coverage
- Loading branch information
1 parent
b7a921b
commit 0a23db4
Showing
9 changed files
with
294 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package utils | ||
|
||
import ( | ||
cdx "github.com/CycloneDX/cyclonedx-go" | ||
) | ||
|
||
func traverseComponent(comps *[]*cdx.Component, comp *cdx.Component) { | ||
*comps = append(*comps, comp) | ||
if comp.Components == nil { | ||
return | ||
} | ||
for i := range *comp.Components { | ||
traverseComponent(comps, &(*comp.Components)[i]) | ||
} | ||
} | ||
|
||
func DiscoverCDXComponents(bom *cdx.BOM) []*cdx.Component { | ||
comps := make([]*cdx.Component, 0) | ||
if bom.Metadata != nil && bom.Metadata.Component != nil { | ||
traverseComponent(&comps, bom.Metadata.Component) | ||
} | ||
|
||
if bom.Components != nil { | ||
for i := range *bom.Components { | ||
traverseComponent(&comps, &(*bom.Components)[i]) | ||
} | ||
} | ||
return comps | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/snyk/parlay/internal/utils" | ||
|
||
cdx "github.com/CycloneDX/cyclonedx-go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDiscoverCDXComponents(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
bom := &cdx.BOM{ | ||
Metadata: &cdx.Metadata{ | ||
Component: &cdx.Component{ | ||
Name: "MetaComp", | ||
}, | ||
}, | ||
Components: &[]cdx.Component{ | ||
{ | ||
Name: "Parent", | ||
Components: &[]cdx.Component{ | ||
{Name: "Child"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
result := utils.DiscoverCDXComponents(bom) | ||
|
||
assert.Equal(len(result), 3) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,15 @@ func TestEnrichSBOM_CycloneDX(t *testing.T) { | |
}) | ||
|
||
bom := &cdx.BOM{ | ||
Metadata: &cdx.Metadata{ | ||
Component: &cdx.Component{ | ||
BOMRef: "pkg:golang/github.com/ACME/[email protected]", | ||
Type: cdx.ComponentTypeApplication, | ||
Name: "Project", | ||
Version: "v1.0.0", | ||
PackageURL: "pkg:golang/github.com/ACME/[email protected]", | ||
}, | ||
}, | ||
Components: &[]cdx.Component{ | ||
{ | ||
BOMRef: "pkg:golang/github.com/CycloneDX/[email protected]", | ||
|
@@ -69,7 +78,45 @@ func TestEnrichSBOM_CycloneDX(t *testing.T) { | |
|
||
httpmock.GetTotalCallCount() | ||
calls := httpmock.GetCallCountInfo() | ||
assert.Equal(t, len(components), calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`]) | ||
assert.Equal(t, 2, calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`]) | ||
} | ||
|
||
func TestEnrichSBOM_CycloneDX_NestedComps(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
httpmock.RegisterResponder("GET", `=~^https://packages.ecosyste.ms/api/v1/registries`, | ||
func(req *http.Request) (*http.Response, error) { | ||
return httpmock.NewJsonResponse(200, map[string]interface{}{}) | ||
}) | ||
|
||
bom := &cdx.BOM{ | ||
Components: &[]cdx.Component{ | ||
{ | ||
BOMRef: "@emotion/[email protected]", | ||
Type: cdx.ComponentTypeLibrary, | ||
Name: "babel-plugin", | ||
Version: "v11.11.0", | ||
PackageURL: "pkg:npm/%40emotion/[email protected]", | ||
Components: &[]cdx.Component{ | ||
{ | ||
Type: cdx.ComponentTypeLibrary, | ||
Name: "convert-source-map", | ||
Version: "v1.9.0", | ||
BOMRef: "@emotion/[email protected]|[email protected]", | ||
PackageURL: "pkg:npm/[email protected]", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
doc := &sbom.SBOMDocument{BOM: bom} | ||
|
||
EnrichSBOM(doc) | ||
|
||
httpmock.GetTotalCallCount() | ||
calls := httpmock.GetCallCountInfo() | ||
assert.Equal(t, 2, calls[`GET =~^https://packages.ecosyste.ms/api/v1/registries`]) | ||
} | ||
|
||
func TestEnrichSBOMWithoutLicense(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,37 @@ func TestEnrichSBOM_CycloneDXWithVulnerabilities(t *testing.T) { | |
assert.Equal(t, "SNYK-PYTHON-NUMPY-73513", vuln.ID) | ||
} | ||
|
||
func TestEnrichSBOM_CycloneDXWithVulnerabilities_NestedComponents(t *testing.T) { | ||
teardown := setupTestEnv(t) | ||
defer teardown() | ||
|
||
bom := &cdx.BOM{ | ||
Components: &[]cdx.Component{ | ||
{ | ||
BOMRef: "pkg:pypi/[email protected]", | ||
Name: "pandas", | ||
Version: "0.15.0", | ||
PackageURL: "pkg:pypi/[email protected]", | ||
Components: &[]cdx.Component{ | ||
{ | ||
BOMRef: "pkg:pypi/[email protected]", | ||
Name: "numpy", | ||
Version: "1.16.0", | ||
PackageURL: "pkg:pypi/[email protected]", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
doc := &sbom.SBOMDocument{BOM: bom} | ||
logger := zerolog.Nop() | ||
|
||
EnrichSBOM(doc, logger) | ||
|
||
assert.NotNil(t, bom.Vulnerabilities) | ||
assert.Len(t, *bom.Vulnerabilities, 2) | ||
} | ||
|
||
func TestEnrichSBOM_CycloneDXWithoutVulnerabilities(t *testing.T) { | ||
teardown := setupTestEnv(t) | ||
defer teardown() | ||
|
@@ -109,6 +140,11 @@ func setupTestEnv(t *testing.T) func() { | |
`=~^https://api\.snyk\.io/rest/orgs/[a-z0-9-]+/packages/pkg%3Apypi%2Fnumpy%401.16.0/issues`, | ||
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/numpy_issues.json")), | ||
) | ||
httpmock.RegisterResponder( | ||
"GET", | ||
`=~^https://api\.snyk\.io/rest/orgs/[a-z0-9-]+/packages/pkg%3Apypi%2Fpandas%400.15.0/issues`, | ||
httpmock.NewJsonResponderOrPanic(200, httpmock.File("testdata/pandas_issues.json")), | ||
) | ||
httpmock.RegisterResponder( | ||
"GET", | ||
`=~^https://api\.snyk\.io/rest/orgs/[a-z0-9-]+/packages/.*/issues`, | ||
|
Oops, something went wrong.