Skip to content

feat(cyclonedx): Include sbom main component info for Trivy #1991

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

Merged
merged 4 commits into from
Apr 16, 2025

Conversation

javirln
Copy link
Member

@javirln javirln commented Apr 15, 2025

This pull request includes changes to the pkg/attestation/crafter/materials/cyclonedxjson.go file to enhance the extraction of the main component's version from properties and to refactor constant definitions.

Enhancements to version extraction:

  • Added a Properties field to the component structure to capture additional metadata.
  • Implemented logic to extract the version from the aquaTrivyRepoDigestPropertyKey property if the version field is empty.

Ref: #1988

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln requested review from migmartri and jiparis April 15, 2025 14:38
@javirln javirln self-assigned this Apr 15, 2025
for _, prop := range component.Properties {
if prop.Name == aquaTrivyRepoDigestPropertyKey {
if parts := strings.Split(prop.Value, "sha256:"); len(parts) > 1 {
component.Version = fmt.Sprintf("sha256:%s", parts[1])
Copy link
Member

Choose a reason for hiding this comment

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

Is this solving only the version or the info about the main component as well?

image

Copy link
Member Author

Choose a reason for hiding this comment

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

This solves the issue of CYCLONEDX files from Trivy where the version was not being included, the sbom was generated by Trivy:

$ trivy image ghcr.io/chainloop-dev/chainloop/cli:latest --output sbom.json --format cyclonedx
$ chainloop wf run describe --id 866b750c-80e2-422e-ad09-1f4db049d9b8 -o statement | jq '.predicate.materials.[].annotations'
{
  "chainloop.material.cas.inline": true,
  "chainloop.material.name": "material-1744782814076087000",
  "chainloop.material.sbom.main_component.name": "ghcr.io/chainloop-dev/chainloop/cli",
  "chainloop.material.sbom.main_component.type": "container",
  "chainloop.material.sbom.main_component.version": "sha256:bbfd27fcdb15c8082951dc59be2310a2a2e6b95e11002f8411e5918887faa607",
  "chainloop.material.type": "SBOM_CYCLONEDX_JSON"
}

Keep in mind the information about the main component is not required in the CycloneDX specification is not required so it might be cases where the information is not populated and tools can behave differently.

This is an example of Trivy scanning the local system:

$ trivy filesystem ./app/cli --output sbom.json --format cyclonedx
$ chainloop wf run describe --id af935022-4a2c-4043-a2de-abd3431ed504 -o statement | jq '.predicate.materials.[].annotations'
WRN API contacted in insecure mode
{
  "chainloop.material.cas.inline": true,
  "chainloop.material.name": "material-1744784071174128000",
  "chainloop.material.sbom.main_component.name": "app/cli",
  "chainloop.material.sbom.main_component.type": "application",
  "chainloop.material.sbom.main_component.version": "",
  "chainloop.material.type": "SBOM_CYCLONEDX_JSON"
}

And this is syft:

$ syft scan ./app/cli --output cyclonedx-json > sbom.json
$ chainloop wf run describe --id 2c320286-f088-46f8-9095-3576be6b3c0b -o statement | jq '.predicate.materials.[].annotations'
WRN API contacted in insecure mode
{
  "chainloop.material.cas.inline": true,
  "chainloop.material.name": "material-1744784201547639000",
  "chainloop.material.sbom.main_component.name": "./app/cli",
  "chainloop.material.sbom.main_component.type": "file",
  "chainloop.material.sbom.main_component.version": "",
  "chainloop.material.type": "SBOM_CYCLONEDX_JSON"
}

Copy link
Member Author

Choose a reason for hiding this comment

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

On the tests that I've run (container, and filesystem), the main component information was populated. Do you have a way to reproduce the one that was not being added?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok I found an example, having a look, it seems there is another thing involved.

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln marked this pull request as draft April 16, 2025 07:28
Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln marked this pull request as ready for review April 16, 2025 07:44
@javirln
Copy link
Member Author

javirln commented Apr 16, 2025

Fixed reference parsing when using a CycloneDX SBOM generated by Trivy, which included the full repository reference with tag or digest, causing a parsing error.

It now works as expected:

$ trivy image --format cyclonedx --output result.cdx ghcr.io/chainloop-dev/chainloop/cli@sha256:bbfd27fcdb15c8082951dc59be2310a2a2e6b95e11002f8411e5918887faa607
$ chainloop --insecure wf run describe --id 581c418d-e773-44a8-a206-054f8ad1b544 -o statement | jq '.predicate.materials.[].annotations'
WRN API contacted in insecure mode
{
  "chainloop.material.cas.inline": true,
  "chainloop.material.name": "material-1744789223469671000",
  "chainloop.material.sbom.main_component.name": "ghcr.io/chainloop-dev/chainloop/cli",
  "chainloop.material.sbom.main_component.type": "container",
  "chainloop.material.sbom.main_component.version": "sha256:bbfd27fcdb15c8082951dc59be2310a2a2e6b95e11002f8411e5918887faa607",
  "chainloop.material.type": "SBOM_CYCLONEDX_JSON"
}

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
Copy link
Member

@migmartri migmartri left a comment

Choose a reason for hiding this comment

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

Is this correct?

"chainloop.material.sbom.main_component.name": "chainloop-dev/chainloop/cli",

that's missing the whole repository URI no?

@javirln
Copy link
Member Author

javirln commented Apr 16, 2025

Is this correct?

"chainloop.material.sbom.main_component.name": "chainloop-dev/chainloop/cli",

that's missing the whole repository URI no?

No, it's updated in the following commit: 5d0c865, I've updated the comment as well.

@javirln javirln merged commit 2ea217c into chainloop-dev:main Apr 16, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants