-
Notifications
You must be signed in to change notification settings - Fork 1k
warehouse: PEP 740 models #16625
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
warehouse: PEP 740 models #16625
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e4b0dd7
warehouse: PEP 740 models
woodruffw fe9859e
tests: AttestationFactory helper
woodruffw f07114f
tests: attestation coverage
woodruffw 134fe54
switch to a Provenance model
woodruffw c7611e6
provenance model carries a digest
woodruffw 4fa0b36
remove unused factory
woodruffw 3e9a29f
use SHA-256 for provenance digest
woodruffw 3f3a930
Merge branch 'main' into ww/pep740-models
woodruffw 0abde64
mark provenance column as deferred
woodruffw 34ec661
Add a ProvenanceFactory object and a simple test.
DarkaMaul ac5d36c
Revert "Add a ProvenanceFactory object and a simple test."
woodruffw b317b71
Merge branch 'main' into ww/pep740-models
woodruffw 85eb069
Merge branch 'main' into ww/pep740-models
woodruffw aad735b
Merge branch 'main' into ww/pep740-models
di 1433097
Update warehouse/packaging/models.py
di 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,30 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import hashlib | ||
|
||
import factory | ||
|
||
from tests.common.db.base import WarehouseFactory | ||
from tests.common.db.packaging import FileFactory | ||
from warehouse.attestations.models import Provenance | ||
|
||
|
||
class ProvenanceFactory(WarehouseFactory): | ||
class Meta: | ||
model = Provenance | ||
|
||
file = factory.SubFactory(FileFactory) | ||
# TODO(DM) Generate a better provenance mock | ||
provenance = {} | ||
provenance_digest = factory.LazyAttribute( | ||
lambda o: hashlib.sha256(o.file.filename.encode("utf8")).hexdigest() | ||
) | ||
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,11 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
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,22 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from tests.common.db.attestations import ProvenanceFactory | ||
from tests.common.db.packaging import FileFactory | ||
|
||
|
||
class TestProvenance: | ||
def test_init(self, db_session): | ||
provenance = ProvenanceFactory.create() | ||
file = FileFactory.create(provenance=provenance) | ||
|
||
assert file.provenance == provenance | ||
assert isinstance(provenance.provenance_digest, str) |
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,11 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. |
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,52 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import annotations | ||
|
||
import typing | ||
|
||
from uuid import UUID | ||
|
||
from sqlalchemy import ForeignKey, orm | ||
from sqlalchemy.dialects.postgresql import CITEXT, JSONB | ||
from sqlalchemy.orm import Mapped, mapped_column | ||
|
||
from warehouse import db | ||
|
||
if typing.TYPE_CHECKING: | ||
from warehouse.packaging.models import File | ||
|
||
|
||
class Provenance(db.Model): | ||
""" | ||
A table for PEP 740 provenance objects. | ||
|
||
Provenance objects contain one or more attestation objects. | ||
These attestation objects are grouped into "bundles," each of which | ||
contains one or more attestations along with the Trusted Publisher | ||
identity that produced them. | ||
""" | ||
|
||
__tablename__ = "provenance" | ||
|
||
file_id: Mapped[UUID] = mapped_column( | ||
ForeignKey("release_files.id", onupdate="CASCADE", ondelete="CASCADE"), | ||
) | ||
file: Mapped[File] = orm.relationship(back_populates="provenance") | ||
|
||
# This JSONB has the structure of a PEP 740 provenance object. | ||
provenance: Mapped[dict] = mapped_column(JSONB, nullable=False, deferred=True) | ||
|
||
# The SHA-2/256 digest of the provenance object stored in this row. | ||
# Postgres uses a compact binary representation under the hood and is | ||
# unlikely to provide a permanently stable serialization, so this is the | ||
# hash of the RFC 8785 serialization. | ||
provenance_digest: Mapped[str] = mapped_column(CITEXT) |
48 changes: 48 additions & 0 deletions
48
warehouse/migrations/versions/1b9ae6ec6ec0_add_provenance_table.py
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,48 @@ | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
add provenance table | ||
|
||
Revision ID: 1b9ae6ec6ec0 | ||
Revises: dcf1e3986782 | ||
Create Date: 2024-09-03 23:39:30.853147 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
|
||
from alembic import op | ||
from sqlalchemy.dialects import postgresql | ||
|
||
revision = "1b9ae6ec6ec0" | ||
down_revision = "dcf1e3986782" | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"provenance", | ||
sa.Column("file_id", sa.UUID(), nullable=False), | ||
sa.Column( | ||
"provenance", postgresql.JSONB(astext_type=sa.Text()), nullable=False | ||
), | ||
sa.Column("provenance_digest", postgresql.CITEXT(), nullable=False), | ||
sa.Column( | ||
"id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False | ||
), | ||
sa.ForeignKeyConstraint( | ||
["file_id"], ["release_files.id"], onupdate="CASCADE", ondelete="CASCADE" | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("provenance") |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modeling these fields accurately requires access to the services that we're defining in #16624, so I'm inclined to punt on adding the factory in this PR 🙂
I'm going to save a patch for this and apply it on top of #16624, however.