Skip to content

Commit 09637bd

Browse files
committed
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)
1 parent 6e6e0d9 commit 09637bd

3 files changed

Lines changed: 46 additions & 6 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed uploads to the PyPI endpoint failing when Pulp stores artifacts in Google Cloud Storage.

‎pulp_python/app/pypi/serializers.py‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ def validate(self, data):
9696
if action != "file_upload":
9797
raise serializers.ValidationError(_("We do not support the :action {}").format(action))
9898
file = data.get("content")
99+
# Remote storage backends rename the file when the artifact is saved.
100+
filename = file.name
99101
for ext, packagetype in DIST_EXTENSIONS.items():
100-
if file.name.endswith(ext):
102+
if filename.endswith(ext):
101103
if (filetype := data.get("filetype")) and filetype != packagetype:
102104
raise serializers.ValidationError(
103105
{
104106
"filetype": _(
105107
"filetype {} does not match found filetype {} for file {}"
106-
).format(filetype, packagetype, file.name)
108+
).format(filetype, packagetype, filename)
107109
}
108110
)
109111
break
@@ -113,7 +115,7 @@ def validate(self, data):
113115
"content": _(
114116
"Extension on {} is not a valid python extension "
115117
"(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)"
116-
).format(file.name)
118+
).format(filename)
117119
}
118120
)
119121

@@ -130,7 +132,7 @@ def validate(self, data):
130132
att_bundle = AttestationBundle(publisher=publisher, attestations=attestations)
131133
provenance = Provenance(attestation_bundles=[att_bundle])
132134
try:
133-
verify_provenance(file.name, sha256, provenance, offline=True)
135+
verify_provenance(filename, sha256, provenance, offline=True)
134136
except AttestationError as e:
135137
raise serializers.ValidationError(
136138
{"attestations": _("Attestations failed verification: {}").format(e)}
@@ -143,8 +145,8 @@ def validate(self, data):
143145
except IntegrityError:
144146
artifact = Artifact.objects.get(sha256=artifact.sha256, pulp_domain=get_domain())
145147
artifact.touch()
146-
log.info(f"Artifact for {file.name} already existed in database")
147-
data["content"] = (artifact, file.name)
148+
log.info(f"Artifact for {filename} already existed in database")
149+
data["content"] = (artifact, filename)
148150
return data
149151

150152

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import hashlib
2+
from unittest import mock
3+
4+
from django.core.files.base import ContentFile
5+
from django.test import TestCase
6+
7+
from pulpcore.plugin.files import PulpTemporaryUploadedFile
8+
from pulpcore.plugin.util import get_domain
9+
10+
from pulp_python.app.pypi.serializers import PackageUploadSerializer
11+
12+
13+
class TestPackageUploadSerializer(TestCase):
14+
def test_filename_survives_a_storage_that_renames_the_upload(self):
15+
filename = "shelf_reader-0.1-py2-none-any.whl"
16+
payload = b"not a real wheel"
17+
storage = get_domain().get_storage()
18+
original_save = storage._save
19+
20+
def save_and_rename(name, content):
21+
# The Google Cloud Storage backend renames the saved file to its storage path.
22+
saved = original_save(name, content)
23+
content.name = saved
24+
return saved
25+
26+
serializer = PackageUploadSerializer(
27+
data={
28+
"content": PulpTemporaryUploadedFile.from_file(ContentFile(payload, name=filename)),
29+
"sha256_digest": hashlib.sha256(payload).hexdigest(),
30+
}
31+
)
32+
with mock.patch.object(storage, "_save", side_effect=save_and_rename):
33+
self.assertTrue(serializer.is_valid(), serializer.errors)
34+
35+
artifact, uploaded_filename = serializer.validated_data["content"]
36+
self.assertEqual(uploaded_filename, filename)
37+
self.assertNotEqual(artifact.file.name, filename)

0 commit comments

Comments
 (0)