generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 28
feat: improve VSA generation with digest for each subject #685
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b3eb53a
fix: disable provenance discovery in case a provenance is already ava…
nathanwn 8c49c69
fix: enable provenance expectation validation for user-provided prove…
nathanwn 1b82d5e
chore: add a table to the database schema to store sha256 digests of …
nathanwn f2ed7f7
chore: introduce maven artifact types and utilities
nathanwn 12066e3
refactor: improve extraction of subjects being build artifacts from w…
nathanwn 7fa98d6
chore: match a witness provenance subject against a maven artifact purl
nathanwn 1fbe7ec
chore: modify vsa generation to populate each subject with a sha256 d…
nathanwn ebf8667
chore: fix type qualifier for java-source artifacts and add comment c…
nathanwn 263414b
chore: rename `artifact_name` to `artifact_filename` for clarity
nathanwn 96da6a9
chore: add comment to clarify the deduplication of PURLs in the polic…
nathanwn 077adc0
chore: add empty list check in the `get_common_purl_from_artifact_pur…
nathanwn 6f948e6
chore: use explicit name for iterable value
nathanwn 9e6f98f
chore: update outdated comment for build artifact extraction function
nathanwn ff22105
chore: simplify maven subject-purl matching logic
nathanwn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,124 @@ | ||
# Copyright (c) 2024 - 2024, Oracle and/or its affiliates. All rights reserved. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | ||
|
||
"""This module declares types and utilities for Maven artifacts.""" | ||
|
||
from packageurl import PackageURL | ||
|
||
from macaron.slsa_analyzer.provenance.intoto import InTotoPayload | ||
from macaron.slsa_analyzer.provenance.intoto.v01 import InTotoV01Subject | ||
from macaron.slsa_analyzer.provenance.intoto.v1 import InTotoV1ResourceDescriptor | ||
from macaron.slsa_analyzer.provenance.witness import ( | ||
extract_build_artifacts_from_witness_subjects, | ||
is_witness_provenance_payload, | ||
load_witness_verifier_config, | ||
) | ||
|
||
|
||
class MavenSubjectPURLMatcher: | ||
behnazh-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""A matcher matching a PURL identifying a Maven artifact to a provenance subject.""" | ||
|
||
@staticmethod | ||
def get_subject_in_provenance_matching_purl( | ||
provenance_payload: InTotoPayload, purl: PackageURL | ||
) -> InTotoV01Subject | InTotoV1ResourceDescriptor | None: | ||
tromai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Get the subject in the provenance matching the PURL. | ||
|
||
In this case where the provenance is assumed to be built from a Java project, | ||
the subject must be a Maven artifact. | ||
|
||
Parameters | ||
---------- | ||
provenance_payload : InTotoPayload | ||
The provenance payload. | ||
purl : PackageURL | ||
The PackageURL identifying the matching subject. | ||
|
||
Returns | ||
------- | ||
InTotoV01Subject | InTotoV1ResourceDescriptor | None | ||
The subject in the provenance matching the given PURL. | ||
""" | ||
if not purl.namespace: | ||
return None | ||
if not purl.version: | ||
return None | ||
if purl.type != "maven": | ||
return None | ||
|
||
if not is_witness_provenance_payload( | ||
payload=provenance_payload, | ||
predicate_types=load_witness_verifier_config().predicate_types, | ||
): | ||
return None | ||
artifact_subjects = extract_build_artifacts_from_witness_subjects(provenance_payload) | ||
|
||
for subject in artifact_subjects: | ||
_, _, artifact_filename = subject["name"].rpartition("/") | ||
subject_purl = create_maven_purl_from_artifact_filename( | ||
artifact_filename=artifact_filename, | ||
group_id=purl.namespace, | ||
version=purl.version, | ||
) | ||
if subject_purl == purl: | ||
return subject | ||
|
||
return None | ||
|
||
|
||
def create_maven_purl_from_artifact_filename( | ||
artifact_filename: str, | ||
group_id: str, | ||
version: str, | ||
) -> PackageURL | None: | ||
"""Create a Maven PackageURL given an artifact filename, a group id, and a version. | ||
|
||
For reference, see: | ||
- https://maven.apache.org/ref/3.9.6/maven-core/artifact-handlers.html | ||
- https://github.com/package-url/purl-spec/blob/master/PURL-TYPES.rst#maven | ||
Notes: | ||
- For the time being, we are only supporting the ``"type"`` qualifier, although the | ||
Maven section in the PackageURL docs also mention the ``"classifier"`` qualifier. | ||
This is because not all artifact types has a unique value of ``"classifier"`` | ||
according to the Artifact Handlers table in the Maven Core reference. In addition, | ||
not supporting the ``"classifier"`` qualifier at the moment simplifies the | ||
implementation for PURL decoding and generation until there is a concrete use | ||
case for this additional qualifier. | ||
- We are only supporting only 4 artifact types: jar, pom, javadoc, and java-source. | ||
|
||
Parameters | ||
---------- | ||
artifact_filename : str | ||
The filename of the artifact. | ||
group_id : str | ||
The group id of the artifact. | ||
version : str | ||
The version of the artifact. | ||
|
||
Returns | ||
------- | ||
PackageURL | None | ||
A Maven artifact PackageURL, or `None` if the filename does not follow any | ||
of the supported artifact name patters. | ||
""" | ||
# Each artifact name should follow the pattern "<artifact-id>-<suffix>" | ||
# where "<suffix>" is one of the following. | ||
suffix_to_purl_qualifiers = { | ||
f"-{version}.jar": {"type": "jar"}, | ||
f"-{version}.pom": {"type": "pom"}, | ||
f"-{version}-javadoc.jar": {"type": "javadoc"}, | ||
f"-{version}-sources.jar": {"type": "java-source"}, | ||
} | ||
|
||
for suffix, purl_qualifiers in suffix_to_purl_qualifiers.items(): | ||
if artifact_filename.endswith(suffix): | ||
artifact_id = artifact_filename[: -len(suffix)] | ||
return PackageURL( | ||
type="maven", | ||
namespace=group_id, | ||
name=artifact_id, | ||
version=version, | ||
qualifiers=purl_qualifiers, | ||
) | ||
|
||
return None |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.