From e8b8c3117550dc76f32d330507446887d8ab5ed6 Mon Sep 17 00:00:00 2001 From: LilDojd Date: Wed, 11 Nov 2026 00:24:59 +0400 Subject: [PATCH] Keep the upload filename when the storage backend renames the file The Google Cloud Storage backend sets the name of the saved file object to its storage path. The upload serializer read file.name after it saved the artifact, so the upload task got the storage path instead of the package filename. The task then failed to find a known extension, and the upload was lost. The serializer now reads the filename before it saves the artifact. Assisted By: Claude Code (claude-opus-5-5) Signed-off-by: LilDojd Change-Id: I07b6af9ae80ac1393e9253df19e5a8ee6a6a6964 --- CHANGES/+gcs-upload-filename.bugfix | 1 + pulp_python/app/pypi/serializers.py | 14 +++++----- .../tests/unit/test_upload_serializer.py | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 CHANGES/+gcs-upload-filename.bugfix create mode 100644 pulp_python/tests/unit/test_upload_serializer.py diff --git a/CHANGES/+gcs-upload-filename.bugfix b/CHANGES/+gcs-upload-filename.bugfix new file mode 100644 index 000000000..2eb22dc8b --- /dev/null +++ b/CHANGES/+gcs-upload-filename.bugfix @@ -0,0 +1 @@ +Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage. diff --git a/pulp_python/app/pypi/serializers.py b/pulp_python/app/pypi/serializers.py index f3d6db94e..8c915b4c3 100644 --- a/pulp_python/app/pypi/serializers.py +++ b/pulp_python/app/pypi/serializers.py @@ -96,14 +96,16 @@ def validate(self, data): if action != "file_upload": raise serializers.ValidationError(_("We do not support the :action {}").format(action)) file = data.get("content") + # The Google Cloud Storage backend renames the file when the artifact is saved. + filename = file.name for ext, packagetype in DIST_EXTENSIONS.items(): - if file.name.endswith(ext): + if filename.endswith(ext): if (filetype := data.get("filetype")) and filetype != packagetype: raise serializers.ValidationError( { "filetype": _( "filetype {} does not match found filetype {} for file {}" - ).format(filetype, packagetype, file.name) + ).format(filetype, packagetype, filename) } ) break @@ -113,7 +115,7 @@ def validate(self, data): "content": _( "Extension on {} is not a valid python extension " "(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)" - ).format(file.name) + ).format(filename) } ) @@ -130,7 +132,7 @@ def validate(self, data): att_bundle = AttestationBundle(publisher=publisher, attestations=attestations) provenance = Provenance(attestation_bundles=[att_bundle]) try: - verify_provenance(file.name, sha256, provenance, offline=True) + verify_provenance(filename, sha256, provenance, offline=True) except AttestationError as e: raise serializers.ValidationError( {"attestations": _("Attestations failed verification: {}").format(e)} @@ -143,8 +145,8 @@ def validate(self, data): except IntegrityError: artifact = Artifact.objects.get(sha256=artifact.sha256, pulp_domain=get_domain()) artifact.touch() - log.info(f"Artifact for {file.name} already existed in database") - data["content"] = (artifact, file.name) + log.info(f"Artifact for {filename} already existed in database") + data["content"] = (artifact, filename) return data diff --git a/pulp_python/tests/unit/test_upload_serializer.py b/pulp_python/tests/unit/test_upload_serializer.py new file mode 100644 index 000000000..570ad1985 --- /dev/null +++ b/pulp_python/tests/unit/test_upload_serializer.py @@ -0,0 +1,26 @@ +import hashlib +from unittest import mock + +from django.core.files.uploadedfile import SimpleUploadedFile + +from pulpcore.plugin.models import Artifact + +from pulp_python.app.pypi.serializers import PackageUploadSerializer + + +def test_upload_keeps_package_filename_when_saving_the_artifact_renames_the_file(): + filename = "shelf_reader-0.1-py2-none-any.whl" + payload = b"not a real wheel" + upload = SimpleUploadedFile(filename, payload) + artifact = mock.Mock() + # The Google Cloud Storage backend renames the file when the artifact is saved. + artifact.save.side_effect = lambda: setattr(upload, "name", "artifact/ab/cdef") + + serializer = PackageUploadSerializer( + data={"content": upload, "sha256_digest": hashlib.sha256(payload).hexdigest()} + ) + with mock.patch.object(Artifact, "init_and_validate", return_value=artifact): + assert serializer.is_valid(), serializer.errors + + artifact.save.assert_called_once() + assert serializer.validated_data["content"] == (artifact, filename)