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

TC-1842 CycloneDX CPE generates HasMetadata #150

Merged
merged 1 commit into from
Oct 30, 2024
Merged
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
10 changes: 10 additions & 0 deletions demo/graphql/queries-trustification.gql
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,13 @@ query TC_1847_FindDependentProduct_rpm {
...allHasSBOMTree
}
}

query TC_1842_HasMetadata {
HasMetadata (hasMetadataSpec:{key: "cpe", value:"cpe:2.3:a:alpine-baselayout-data:alpine-baselayout-data:3.2.0-r22:*:*:*:*:*:*:*"}) {
subject {
... on Package {
...allPkgTree
}
}
}
}
7 changes: 7 additions & 0 deletions internal/testing/e2e-trustification/e2e
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ diff -u "${SCRIPT_DIR}/expectTC_1847_FindDependentProduct_source.json" "${GUAC_D
cat "$queries" | gql-cli http://localhost:8080/query -o TC_1847_FindDependentProduct_rpm | jq 'del(.. | .id?) | del(.. | .downloadLocation?) | del(.. | .origin?) | .findDependentProduct[].subject.namespaces[]?.names[]?.versions[]?.qualifiers? |= sort | .findDependentProduct' > "${GUAC_DIR}/gotTC_1847_FindDependentProduct_rpm.json"
diff -u "${SCRIPT_DIR}/expectTC_1847_FindDependentProduct_rpm.json" "${GUAC_DIR}/gotTC_1847_FindDependentProduct_rpm.json"

echo @@@@ Ingesting alpine-cyclonedx.json into server for TC-1842
time go run ./cmd/guacone collect files ${GUAC_DIR}/internal/testing/testdata/exampledata/alpine-cyclonedx.json;

echo @@@@ Running TC-1842 queries and validating output
cat "$queries" | gql-cli http://localhost:8080/query -o TC_1842_HasMetadata | jq 'del(.. | .id?) | .HasMetadata ' > "${GUAC_DIR}/gotTC_1842_HasMetadata.json"
diff -u "${SCRIPT_DIR}/expectTC_1842_HasMetadata.json" "${GUAC_DIR}/gotTC_1842_HasMetadata.json"

# Note: graphql_playground is left running, CI will clean it up
37 changes: 37 additions & 0 deletions internal/testing/e2e-trustification/expectTC_1842_HasMetadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"subject": {
"type": "alpine",
"namespaces": [
{
"namespace": "",
"names": [
{
"name": "alpine-baselayout-data",
"versions": [
{
"version": "3.2.0-r22",
"qualifiers": [
{
"key": "arch",
"value": "x86_64"
},
{
"key": "distro",
"value": "alpine-3.16.2"
},
{
"key": "upstream",
"value": "alpine-baselayout"
}
],
"subpath": ""
}
]
}
]
}
]
}
}
]
61 changes: 61 additions & 0 deletions internal/testing/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,70 @@ var (
},
}

CdxHasMetadata = []assembler.HasMetadataIngest{
{
Pkg: &generated.PkgInputSpec{
Type: "deb",
Namespace: ptrfrom.String("debian"),
Name: "base-files",
Version: ptrfrom.String("11.1+deb11u5"),
Qualifiers: []generated.PackageQualifierInputSpec{{Key: "arch", Value: "amd64"}, {Key: "distro", Value: "debian-11"}},
Subpath: ptrfrom.String(""),
},
PkgMatchFlag: generated.MatchFlags{Pkg: "SPECIFIC_VERSION"},
HasMetadata: &generated.HasMetadataInputSpec{
Key: "cpe",
Value: "cpe:2.3:a:base-files:base-files:11.1\\+deb11u5:*:*:*:*:*:*:*",
Timestamp: cdxTime,
Justification: "CycloneDX BOM CPE",
Origin: "GUAC CycloneDX",
Collector: "GUAC",
},
},
{
Pkg: &generated.PkgInputSpec{
Type: "deb",
Namespace: ptrfrom.String("debian"),
Name: "netbase",
Version: ptrfrom.String("6.3"),
Qualifiers: []generated.PackageQualifierInputSpec{{Key: "arch", Value: "all"}, {Key: "distro", Value: "debian-11"}},
Subpath: ptrfrom.String(""),
},
PkgMatchFlag: generated.MatchFlags{Pkg: "SPECIFIC_VERSION"},
HasMetadata: &generated.HasMetadataInputSpec{
Key: "cpe",
Value: "cpe:2.3:a:netbase:netbase:6.3:*:*:*:*:*:*:*",
Timestamp: cdxTime,
Justification: "CycloneDX BOM CPE",
Origin: "GUAC CycloneDX",
Collector: "GUAC",
},
},
{
Pkg: &generated.PkgInputSpec{
Type: "deb",
Namespace: ptrfrom.String("debian"),
Name: "tzdata",
Version: ptrfrom.String("2021a-1+deb11u6"),
Qualifiers: []generated.PackageQualifierInputSpec{{Key: "arch", Value: "all"}, {Key: "distro", Value: "debian-11"}},
Subpath: ptrfrom.String(""),
},
PkgMatchFlag: generated.MatchFlags{Pkg: "SPECIFIC_VERSION"},
HasMetadata: &generated.HasMetadataInputSpec{
Key: "cpe",
Value: "cpe:2.3:a:tzdata:tzdata:2021a-1\\+deb11u6:*:*:*:*:*:*:*",
Timestamp: cdxTime,
Justification: "CycloneDX BOM CPE",
Origin: "GUAC CycloneDX",
Collector: "GUAC",
},
},
}

CdxIngestionPredicates = assembler.IngestPredicates{
IsDependency: CdxDeps,
HasSBOM: CdxHasSBOM,
HasMetadata: CdxHasMetadata,
}

cdxTopQuarkusPack, _ = asmhelpers.PurlToPkg("pkg:maven/org.acme/[email protected]?type=jar")
Expand Down
44 changes: 42 additions & 2 deletions pkg/ingestor/parser/cyclonedx/parser_cyclonedx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ import (
"time"

cdx "github.com/CycloneDX/cyclonedx-go"
jsoniter "github.com/json-iterator/go"

"github.com/guacsec/guac/pkg/assembler"
model "github.com/guacsec/guac/pkg/assembler/clients/generated"
asmhelpers "github.com/guacsec/guac/pkg/assembler/helpers"
"github.com/guacsec/guac/pkg/handler/processor"
"github.com/guacsec/guac/pkg/ingestor/parser/common"
"github.com/guacsec/guac/pkg/logging"
jsoniter "github.com/json-iterator/go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary
Expand Down Expand Up @@ -342,6 +341,24 @@ func (c *cyclonedxParser) GetPredicates(ctx context.Context) *assembler.IngestPr
if c.cdxBom.Metadata != nil && c.cdxBom.Metadata.Component != nil {
topLevelArts = c.packageArtifacts[c.cdxBom.Metadata.Component.BOMRef]
topLevelPkgs = c.packagePackages[c.cdxBom.Metadata.Component.BOMRef]
for _, pkgSpec := range topLevelPkgs {
var cpe = c.cdxBom.Metadata.Component.CPE
if cpe != "" {
hasMetadata := assembler.HasMetadataIngest{
Pkg: pkgSpec,
PkgMatchFlag: model.MatchFlags{Pkg: model.PkgMatchTypeSpecificVersion},
HasMetadata: &model.HasMetadataInputSpec{
Key: "cpe",
Value: cpe,
Timestamp: c.timestamp,
Justification: "CycloneDX BOM CPE",
Origin: "GUAC CycloneDX",
Collector: "GUAC",
},
}
preds.HasMetadata = append(preds.HasMetadata, hasMetadata)
}
}
}

// adding top level package edge manually for all depends on package
Expand Down Expand Up @@ -375,6 +392,29 @@ func (c *cyclonedxParser) GetPredicates(ctx context.Context) *assembler.IngestPr
}
}

if c.cdxBom.Components != nil {
for _, component := range *c.cdxBom.Components {
var cpe = component.CPE
if cpe != "" {
for _, pkgSpec := range c.packagePackages[component.BOMRef] {
hasMetadata := assembler.HasMetadataIngest{
Pkg: pkgSpec,
PkgMatchFlag: model.MatchFlags{Pkg: model.PkgMatchTypeSpecificVersion},
HasMetadata: &model.HasMetadataInputSpec{
Key: "cpe",
Value: cpe,
Timestamp: c.timestamp,
Justification: "CycloneDX BOM CPE",
Origin: "GUAC CycloneDX",
Collector: "GUAC",
},
}
preds.HasMetadata = append(preds.HasMetadata, hasMetadata)
}
}
}
}

preds.Vex = c.vulnData.vex
preds.VulnMetadata = c.vulnData.vulnMetadata
preds.CertifyVuln = c.vulnData.certifyVuln
Expand Down
Loading