-
Notifications
You must be signed in to change notification settings - Fork 82
Add attestation upload support #1038
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
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 @@ | ||
| Added attestations field to package upload that will create a PEP 740 Provenance object for that content. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Attestation Hosting (PEP 740) | ||
|
|
||
| Pulp Python has support for uploading attestations as originally specified in [PEP 740](https://peps.python.org/pep-0740/). | ||
| Attestations are stored in Pulp as Provenance Content that can be added/synced/removed from python | ||
| repositories. The provenance objects will be available through the Simple API and served by the | ||
| [Integrity API matching PyPI's implementation](https://docs.pypi.org/api/integrity/). | ||
|
|
||
| ## Uploading Attestations | ||
|
|
||
| Attestations can be uploaded to Pulp with its package as a JSON list under the field `attestations`. | ||
|
|
||
| ```bash | ||
| att=$(jq '[.]' twine-6.2.0.tar.gz.publish.attestation) | ||
| # multiple attestation files can be combined using --slurp and '.', jq --slurp '.' att1 att2 ... | ||
| http POST $PULP_API/pulp/api/v3/content/python/packages/ \ | ||
| repository="$PYTHON_REPO_HREF" \ | ||
| relative_path=twine-6.2.0.tar.gz \ | ||
| artifact=$PACKAGE_ARTIFACT_PRN \ | ||
| attestations:="$att" | ||
| ``` | ||
|
|
||
| The uploaded attestations can be found in the created Provenance object attached to the content in | ||
| the task report. | ||
|
|
||
| ```json | ||
| // Task output abbreviated | ||
| { | ||
| "pulp_href": "/pulp/api/v3/tasks/019af033-c8e8-7a02-a583-0fac5e39e54b/", | ||
| "state": "completed", | ||
| "name": "pulpcore.app.tasks.base.general_create", | ||
| "created_resources": [ | ||
| "/pulp/api/v3/content/python/provenance/019aeb59-34bb-7ae4-ab95-4f8a62199be9/", | ||
| "/pulp/api/v3/content/python/packages/019aeb59-34b1-7c73-a746-aea2cc3fbd85/" | ||
| ], | ||
| "result": { | ||
| "prn": "prn:python.pythonpackagecontent:019aeb59-34b1-7c73-a746-aea2cc3fbd85", | ||
| "name": "twine", | ||
| "sha256": "418ebf08ccda9a8caaebe414433b0ba5e25eb5e4a927667122fbe8f829f985d8",, | ||
| "version": "6.2.0", | ||
| "artifact": "/pulp/api/v3/artifacts/019aeb59-33c3-7877-9787-22c34eb6c15b/", | ||
| "filename": "twine-6.2.0.tar.gz", | ||
| "pulp_href": "/pulp/api/v3/content/python/packages/019aeb59-34b1-7c73-a746-aea2cc3fbd85/", | ||
| // PRN of newly created Provenance object | ||
| "provenance": "prn:python.packageprovenance:019aeb59-34bb-7ae4-ab95-4f8a62199be9", | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| You can also use twine to upload your packages. Twine will find the attestations in files ending with | ||
| `.attestation` and attach them to the same filename during the upload. Pulp will then add the new | ||
| package and provenance object to the backing repository of the distribution. | ||
|
|
||
| ```bash | ||
| pulp python distribution create --name foo --base-path foo --repository foo | ||
| pypi-attestations sign dist/twine-6.2.0.tar.gz dist/twine-6.2.0-py3-none-any.whl | ||
| twine upload --repository-url $PULP_API/pypi/foo/simple/ --attestations dist/* | ||
| ``` | ||
|
|
||
| ## Interacting with Provenance Content | ||
|
|
||
| Provenance content can be directly uploaded to Pulp through its content endpoint. | ||
|
|
||
| ```bash | ||
| http POST $PULP_API/pulp/api/v3/content/python/provenance/ --form \ | ||
| file@twine.provenance \ | ||
| package="$PACKAGE_PRN" \ | ||
| repository="$REPO_PRN" | ||
| ``` | ||
|
|
||
| Provenance objects are artifactless content, their data is stored in a json field and are unique by | ||
| their sha256 digest. In a repository a provenance object is unique by their associated package, i.e | ||
| a package can only have one provenance in the repository at a time. Provenance objects can't be | ||
| modified after upload as content is immutable, but a new one can be uploaded to replace the existing | ||
| one. Since provenance objects are content they can be added, removed, and synced into repositories. | ||
| To sync provenance objects from an upstream repository set the `provenance` field on the remote. | ||
|
|
||
| ```bash | ||
| http PATCH $PULP_API/$FOO_REMOTE_HREF provenance=true | ||
| pulp python repository sync --repository foo --remote foo | ||
| ``` | ||
|
|
||
| ## Downloading Provenance objects | ||
|
|
||
| A package's provenance objects are exposed through its Simple page and downloaded from the Integrity | ||
| API. The attestations can then be verified using tools like `sigstore` or `pypi-attestations`. | ||
|
|
||
| ```bash | ||
| http $PULP_API/pypi/foo/simple/twine/ "Accept:application/vnd.pypi.simple.v1+json" | jq -r ".files[].provenance" | ||
|
|
||
| http $PULP_API/pypi/foo/integrity/twine/6.2.0/twine-6.2.0.tar.gz/ | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| from typing import Annotated, Literal, Union, get_args | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field | ||
| from pydantic.alias_generators import to_snake | ||
| from pypi_attestations import ( | ||
| Attestation, | ||
| Distribution, | ||
| Publisher, | ||
| ) | ||
|
|
||
|
|
||
| class _PermissivePolicy: | ||
| """A permissive verification policy that always succeeds.""" | ||
|
|
||
| def verify(self, cert): | ||
| """Succeed regardless of the publisher's identity.""" | ||
| pass | ||
|
|
||
|
|
||
| class AnyPublisher(BaseModel): | ||
| """A fallback publisher for any kind not matching other publisher types.""" | ||
|
|
||
| model_config = ConfigDict(alias_generator=to_snake, extra="allow") | ||
|
|
||
| kind: str | ||
|
|
||
| def _as_policy(self): | ||
| """Return a permissive policy that always succeed.""" | ||
| return _PermissivePolicy() | ||
|
|
||
|
|
||
| # Get the underlying Union type of the original Publisher | ||
| # Publisher is Annotated[Union[...], Field(discriminator="kind")] | ||
| _OriginalPublisherTypes = get_args(Publisher.__origin__) | ||
| # Add AnyPublisher to the list of original publisher types | ||
| _ExtendedPublisherTypes = (*_OriginalPublisherTypes, AnyPublisher) | ||
| _ExtendedPublisherUnion = Union[_ExtendedPublisherTypes] | ||
| # Create a new type that fallbacks to AnyPublisher | ||
| ExtendedPublisher = Annotated[_ExtendedPublisherUnion, Field(union_mode="left_to_right")] | ||
|
|
||
|
|
||
| class AttestationBundle(BaseModel): | ||
| """ | ||
| AttestationBundle object as defined in PEP740. | ||
|
|
||
| PyPI only accepts attestations from TrustedPublishers (GitHub, GitLab, Google), but we will | ||
| accept from any user. | ||
| """ | ||
|
|
||
| publisher: ExtendedPublisher | ||
| attestations: list[Attestation] | ||
|
|
||
|
|
||
| class Provenance(BaseModel): | ||
| """Provenance object as defined in PEP740.""" | ||
|
|
||
| version: Literal[1] = 1 | ||
| attestation_bundles: list[AttestationBundle] | ||
|
|
||
|
|
||
| def verify_provenance(filename, sha256, provenance, offline=False): | ||
| """Verify the provenance object is valid for the package.""" | ||
| dist = Distribution(name=filename, digest=sha256) | ||
| for bundle in provenance.attestation_bundles: | ||
| publisher = bundle.publisher | ||
| policy = publisher._as_policy() | ||
| for attestation in bundle.attestations: | ||
| sig_bundle = attestation.to_bundle() | ||
| checkpoint = sig_bundle.log_entry._inner.inclusion_proof.checkpoint | ||
| staging = "sigstage.dev" in checkpoint.envelope | ||
| attestation.verify(policy, dist, staging=staging, offline=offline) | ||
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.