Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/+gcs-upload-filename.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage.
14 changes: 8 additions & 6 deletions pulp_python/app/pypi/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
# Remote storage backends rename the file when the artifact is saved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please specify Google Cloud Storage in the comment?

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
Expand All @@ -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)
}
)

Expand All @@ -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)}
Expand All @@ -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


Expand Down
37 changes: 37 additions & 0 deletions pulp_python/tests/unit/test_upload_serializer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import hashlib
from unittest import mock

from django.core.files.base import ContentFile
from django.test import TestCase

from pulpcore.plugin.files import PulpTemporaryUploadedFile
from pulpcore.plugin.util import get_domain

from pulp_python.app.pypi.serializers import PackageUploadSerializer


class TestPackageUploadSerializer(TestCase):
def test_filename_survives_a_storage_that_renames_the_upload(self):
filename = "shelf_reader-0.1-py2-none-any.whl"
payload = b"not a real wheel"
storage = get_domain().get_storage()
original_save = storage._save

def save_and_rename(name, content):
# The Google Cloud Storage backend renames the saved file to its storage path.
saved = original_save(name, content)
content.name = saved
return saved

serializer = PackageUploadSerializer(
data={
"content": PulpTemporaryUploadedFile.from_file(ContentFile(payload, name=filename)),
"sha256_digest": hashlib.sha256(payload).hexdigest(),
}
)
with mock.patch.object(storage, "_save", side_effect=save_and_rename):
self.assertTrue(serializer.is_valid(), serializer.errors)

artifact, uploaded_filename = serializer.validated_data["content"]
self.assertEqual(uploaded_filename, filename)
self.assertNotEqual(artifact.file.name, filename)
Comment on lines +26 to +37

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last assert passes regardless of the mock, because the artiifact storage path never equals filename. Could you please change it to check the upload object instead? That confirms the mocked _save actually ran and renamed the file:

        upload = PulpTemporaryUploadedFile.from_file(ContentFile(payload, name=filename))
        serializer = PackageUploadSerializer(
            data={
                "content": upload,
                "sha256_digest": hashlib.sha256(payload).hexdigest(),
            }
        )
        with mock.patch.object(storage, "_save", side_effect=save_and_rename):
            self.assertTrue(serializer.is_valid(), serializer.errors)

        _, uploaded_filename = serializer.validated_data["content"]
        self.assertEqual(uploaded_filename, filename)
        self.assertNotEqual(upload.name, filename)

Loading