|
5 | 5 | from django.db.utils import IntegrityError |
6 | 6 | from packaging.requirements import Requirement |
7 | 7 | from rest_framework import serializers |
| 8 | +from pydantic import ValidationError |
| 9 | +from pypi_attestations import Distribution, Provenance, VerificationError |
8 | 10 |
|
9 | 11 | from pulpcore.plugin import models as core_models |
10 | 12 | from pulpcore.plugin import serializers as core_serializers |
@@ -464,6 +466,65 @@ class Meta: |
464 | 466 | model = python_models.PythonPackageContent |
465 | 467 |
|
466 | 468 |
|
| 469 | +class PackageProvenanceSerializer(core_serializers.NoArtifactContentUploadSerializer): |
| 470 | + """ |
| 471 | + A Serializer for PackageProvenance. |
| 472 | + """ |
| 473 | + |
| 474 | + package = core_serializers.DetailRelatedField( |
| 475 | + help_text=_("The package that the provenance is for."), |
| 476 | + view_name_pattern=r"content(-.*/.*)-detail", |
| 477 | + queryset=python_models.PythonPackageContent.objects.all(), |
| 478 | + ) |
| 479 | + provenance = serializers.JSONField(read_only=True, default=dict) |
| 480 | + sha256 = serializers.CharField(read_only=True) |
| 481 | + verify = serializers.BooleanField( |
| 482 | + default=True, |
| 483 | + write_only=True, |
| 484 | + help_text=_("Verify each attestation in the provenance."), |
| 485 | + ) |
| 486 | + |
| 487 | + def deferred_validate(self, data): |
| 488 | + """ |
| 489 | + Validate that the provenance is valid and pointing to the correct package. |
| 490 | + """ |
| 491 | + data = super().deferred_validate(data) |
| 492 | + try: |
| 493 | + provenance = Provenance.model_validate_json(data["file"].read()) |
| 494 | + data["provenance"] = provenance.model_dump(mode="json") |
| 495 | + except ValidationError as e: |
| 496 | + raise serializers.ValidationError( |
| 497 | + _("The uploaded provenance is not valid: {}".format(e)) |
| 498 | + ) |
| 499 | + if data.pop("verify"): |
| 500 | + dist = Distribution(name=data["package"].filename, digest=data["package"].sha256) |
| 501 | + try: |
| 502 | + for attestation_bundle in provenance.attestation_bundles: |
| 503 | + publisher = attestation_bundle.publisher |
| 504 | + policy = publisher._as_policy() |
| 505 | + for attestation in attestation_bundle.attestations: |
| 506 | + attestation.verify(policy, dist) |
| 507 | + except VerificationError as e: |
| 508 | + raise serializers.ValidationError(_("Provenance verification failed: {}".format(e))) |
| 509 | + return data |
| 510 | + |
| 511 | + def retrieve(self, validated_data): |
| 512 | + sha256 = python_models.PackageProvenance.calculate_sha256(validated_data["provenance"]) |
| 513 | + content = python_models.PackageProvenance.objects.filter( |
| 514 | + sha256=sha256, _pulp_domain=get_domain() |
| 515 | + ).first() |
| 516 | + return content |
| 517 | + |
| 518 | + class Meta: |
| 519 | + fields = core_serializers.NoArtifactContentUploadSerializer.Meta.fields + ( |
| 520 | + "package", |
| 521 | + "provenance", |
| 522 | + "sha256", |
| 523 | + "verify", |
| 524 | + ) |
| 525 | + model = python_models.PackageProvenance |
| 526 | + |
| 527 | + |
467 | 528 | class MultipleChoiceArrayField(serializers.MultipleChoiceField): |
468 | 529 | """ |
469 | 530 | A wrapper to make sure this DRF serializer works properly with ArrayFields. |
|
0 commit comments