-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathcontent.py
More file actions
350 lines (287 loc) · 11.7 KB
/
content.py
File metadata and controls
350 lines (287 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from gettext import gettext as _
from django.db import transaction, IntegrityError
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from pulpcore.app import models
from pulpcore.app.serializers import base, fields, pulp_labels_validator, DetailRelatedField
from pulpcore.app.util import get_domain
class NoArtifactContentSerializer(base.ModelSerializer):
pulp_href = base.DetailIdentityField(view_name_pattern=r"contents(-.*/.*)-detail")
pulp_labels = fields.PulpLabelsField(
help_text=_(
"A dictionary of arbitrary key/value pairs used to describe a specific "
"Content instance."
),
required=False,
validators=[pulp_labels_validator],
)
repository = DetailRelatedField(
help_text=_("A URI of a repository the new content unit should be associated with."),
required=False,
write_only=True,
view_name_pattern=r"repositories(-.*/.*)-detail",
queryset=models.Repository.objects.all(),
)
def get_artifacts(self, validated_data):
"""
Extract artifacts from validated_data.
This function is supposed to extract the information about content artifacts from
validated_data and return a dictionary with artifacts and relative paths as keys.
"""
return {}
def retrieve(self, validated_data):
"""
Retrieve existing content unit if it exists, else return None.
This method is plugin-specific and implementing it for a specific content type
allows for uploading already existing content units of that type.
"""
return None
def validate(self, data):
"""Validate that we have an Artifact or can create one."""
data = super().validate(data)
if repository := data.get("repository"):
if (
self.Meta.model
not in repository.get_model_for_pulp_type(repository.pulp_type).CONTENT_TYPES
):
raise serializers.ValidationError("Content is not supported by this repository.")
return data
def create(self, validated_data):
"""
Create the content and associate it with its Artifacts, or retrieve the existing content.
Args:
validated_data (dict): Data to save to the database
"""
repository = validated_data.pop("repository", None)
artifacts = self.get_artifacts(validated_data)
content = self.retrieve(validated_data)
created = False
if content is not None:
content.touch()
else:
try:
with transaction.atomic():
content = self.Meta.model.objects.create(**validated_data)
for relative_path, artifact in artifacts.items():
models.ContentArtifact.objects.create(
artifact=artifact, content=content, relative_path=relative_path
)
except IntegrityError:
content = self.retrieve(validated_data)
if content is None:
raise
else:
created = True
# Ensure the content now has the uploaded artifact(s)
if not created:
ca_set = content.contentartifact_set
if ca_set.count() == 0 or ca_set.filter(artifact=None).exists():
cas = [
models.ContentArtifact(artifact=a, content=content, relative_path=rp)
for rp, a in artifacts.items()
]
models.ContentArtifact.objects.bulk_create(
cas,
update_conflicts=True,
update_fields=["artifact"],
unique_fields=["content", "relative_path"],
)
if repository:
repository.cast()
content_to_add = self.Meta.model.objects.filter(pk=content.pk)
# create new repo version with uploaded package
with repository.new_version() as new_version:
new_version.add_content(content_to_add)
return content
class Meta:
model = models.Content
fields = base.ModelSerializer.Meta.fields + (
"repository",
"pulp_labels",
)
class SingleArtifactContentSerializer(NoArtifactContentSerializer):
artifact = fields.SingleContentArtifactField(
help_text=_("Artifact file representing the physical content"),
)
relative_path = serializers.CharField(
help_text=_("Path where the artifact is located relative to distributions base_path"),
validators=[fields.relative_path_validator],
write_only=True,
)
def __init__(self, *args, **kwargs):
"""
Initializer for SingleArtifactContentSerializer
"""
super().__init__(*args, **kwargs)
# If the content model has its own database field 'relative_path',
# we should not mark the field write_only
if hasattr(self.Meta.model, "relative_path") and "relative_path" in self.fields:
self.fields["relative_path"].write_only = False
def get_artifacts(self, validated_data):
artifact = validated_data.pop("artifact")
if "relative_path" not in self.fields or self.fields["relative_path"].write_only:
relative_path = validated_data.pop("relative_path")
else:
relative_path = validated_data.get("relative_path")
return {relative_path: artifact}
class Meta:
model = models.Content
fields = NoArtifactContentSerializer.Meta.fields + ("artifact", "relative_path")
class MultipleArtifactContentSerializer(NoArtifactContentSerializer):
artifacts = fields.ContentArtifactsField(
help_text=_(
"A dict mapping relative paths inside the Content to the corresponding"
"Artifact URLs. E.g.: {'relative/path': "
"'/artifacts/1/'"
),
)
def get_artifacts(self, validated_data):
return validated_data.pop("artifacts")
class Meta:
model = models.Content
fields = NoArtifactContentSerializer.Meta.fields + ("artifacts",)
class ContentChecksumSerializer(serializers.Serializer):
"""
Provide a serializer with artifact checksum fields for single artifact content.
If you use this serializer, it's recommended that you prefetch artifacts:
Content.objects.prefetch_related("_artifacts").all()
"""
md5 = fields.ContentArtifactChecksumField(
help_text=_("The MD5 checksum if available."),
checksum="md5",
)
sha1 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-1 checksum if available."),
checksum="sha1",
)
sha224 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-224 checksum if available."),
checksum="sha224",
)
sha256 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-256 checksum if available."),
checksum="sha256",
)
sha384 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-384 checksum if available."),
checksum="sha384",
)
sha512 = fields.ContentArtifactChecksumField(
help_text=_("The SHA-512 checksum if available."),
checksum="sha512",
)
class Meta:
model = models.Content
fields = base.ModelSerializer.Meta.fields + (
"md5",
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
)
class ArtifactSerializer(base.ModelSerializer):
pulp_href = base.IdentityField(view_name="artifacts-detail")
file = serializers.FileField(help_text=_("The stored file."), allow_empty_file=True)
size = serializers.IntegerField(help_text=_("The size of the file in bytes."), required=False)
md5 = serializers.CharField(
help_text=_("The MD5 checksum of the file if available."), required=False, allow_null=True
)
sha1 = serializers.CharField(
help_text=_("The SHA-1 checksum of the file if available."),
required=False,
allow_null=True,
)
sha224 = serializers.CharField(
help_text=_("The SHA-224 checksum of the file if available."),
required=False,
allow_null=True,
)
sha256 = serializers.CharField(
help_text=_("The SHA-256 checksum of the file if available."),
required=False,
allow_null=True,
)
sha384 = serializers.CharField(
help_text=_("The SHA-384 checksum of the file if available."),
required=False,
allow_null=True,
)
sha512 = serializers.CharField(
help_text=_("The SHA-512 checksum of the file if available."),
required=False,
allow_null=True,
)
def validate(self, data):
"""
Validate file by size and by all checksums provided.
Args:
data (django.http.QueryDict) QueryDict mapping Artifact model fields to their
values
Raises:
[rest_framework.exceptions.ValidationError][]: When the expected file size or any
of the checksums don't match their actual values.
"""
super().validate(data)
if "size" in data:
if data["file"].size != int(data["size"]):
raise serializers.ValidationError(_("The size did not match actual size of file."))
else:
data["size"] = data["file"].size
bad_algs = []
for algorithm in models.Artifact.FORBIDDEN_DIGESTS:
if algorithm in data:
bad_algs.append(algorithm)
if bad_algs:
raise serializers.ValidationError(
_("Checksum algorithms {} forbidden for this Pulp instance.").format(bad_algs)
)
for algorithm in reversed(models.Artifact.DIGEST_FIELDS):
digest = data["file"].hashers[algorithm].hexdigest()
if algorithm in data and digest != data[algorithm]:
raise serializers.ValidationError(_("The %s checksum did not match.") % algorithm)
else:
data[algorithm] = digest
if algorithm in models.Artifact.RELIABLE_DIGEST_FIELDS:
validator = UniqueValidator(
models.Artifact.objects.filter(pulp_domain=get_domain()),
message=_("Artifact with {0} checksum of '{1}' already exists.").format(
algorithm, digest
),
)
validator.instance = None
validator(digest, self.fields[algorithm])
return data
class Meta:
model = models.Artifact
fields = base.ModelSerializer.Meta.fields + (
"file",
"size",
"md5",
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
)
class SigningServiceSerializer(base.ModelSerializer):
"""
A serializer for the model declaring a signing service.
"""
pulp_href = base.IdentityField(view_name="signing-services-detail")
name = serializers.CharField(help_text=_("A unique name used to recognize a script."))
public_key = serializers.CharField(
help_text=_("The value of a public key used for the repository verification.")
)
pubkey_fingerprint = serializers.CharField(help_text=_("The fingerprint of the public key."))
script = serializers.CharField(
help_text=_("An absolute path to a script which is going to be used for the signing.")
)
class Meta:
model = models.SigningService
fields = base.ModelSerializer.Meta.fields + (
"name",
"public_key",
"pubkey_fingerprint",
"script",
)