-
Notifications
You must be signed in to change notification settings - Fork 43
/
registry_api.py
910 lines (775 loc) · 32.7 KB
/
registry_api.py
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
"""
Check `Plugin Writer's Guide`_ for more details.
. _Plugin Writer's Guide:
http://docs.pulpproject.org/plugins/plugin-writer/index.html
"""
import json
import logging
import hashlib
import re
from collections import namedtuple
import time
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
from tempfile import NamedTemporaryFile
from django.core.files.storage import default_storage as storage
from django.core.files.base import ContentFile, File
from django.db import IntegrityError, transaction
from django.shortcuts import get_object_or_404
from django.conf import settings
from guardian.shortcuts import get_objects_for_user
from pulpcore.plugin.models import Artifact, ContentArtifact, Task, UploadChunk
from pulpcore.plugin.files import PulpTemporaryUploadedFile
from pulpcore.plugin.tasking import add_and_remove, dispatch
from rest_framework.exceptions import (
AuthenticationFailed,
NotAuthenticated,
PermissionDenied,
NotFound,
ParseError,
Throttled,
ValidationError,
)
from rest_framework.generics import ListAPIView
from rest_framework.pagination import BasePagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.renderers import BaseRenderer, JSONRenderer
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
from rest_framework.settings import api_settings
from rest_framework.viewsets import ViewSet
from rest_framework.views import APIView
from pulp_container.app import models, serializers
from pulp_container.app.access_policy import RegistryAccessPolicy
from pulp_container.app.authorization import AuthorizationService
from pulp_container.app.redirects import (
FileStorageRedirects,
S3StorageRedirects,
AzureStorageRedirects,
)
from pulp_container.app.token_verification import (
RegistryAuthentication,
TokenAuthentication,
RegistryPermission,
TokenPermission,
)
from pulp_container.constants import EMPTY_BLOB
FakeView = namedtuple("FakeView", ["action", "get_object"])
log = logging.getLogger(__name__)
class RepositoryNotFound(NotFound):
"""Exception to render a 404 with the code 'NAME_UNKNOWN'"""
def __init__(self, name):
"""Initialize the exception with the repository name."""
super().__init__(
detail={
"errors": [
{
"code": "NAME_UNKNOWN",
"message": "Repository not found.",
"detail": {"name": name},
}
]
}
)
class RepositoryInvalid(ParseError):
"""Exception to render a 400 with the code 'NAME_INVALID'"""
def __init__(self, name, message=None):
"""Initialize the exception with the repository name."""
message = message or "Invalid repository name."
super().__init__(
detail={
"errors": [{"code": "NAME_INVALID", "message": message, "detail": {"name": name}}]
}
)
class BlobNotFound(NotFound):
"""Exception to render a 404 with the code 'BLOB_UNKNOWN'"""
def __init__(self, digest):
"""Initialize the exception with the blob digest."""
super().__init__(
detail={
"errors": [
{
"code": "BLOB_UNKNOWN",
"message": "Blob not found.",
"detail": {"digest": digest},
}
]
}
)
class ManifestNotFound(NotFound):
"""Exception to render a 404 with the code 'MANIFEST_UNKNOWN'"""
def __init__(self, reference):
"""Initialize the exception with the manifest reference."""
super().__init__(
detail={
"errors": [
{
"code": "MANIFEST_UNKNOWN",
"message": "Manifest not found.",
"detail": {"reference": reference},
}
]
}
)
class ContentRenderer(BaseRenderer):
"""
Rendered class for rendering Manifest and Blob responses.
"""
media_type = "*/*"
format = "txt"
def render(self, data, accepted_media_type=None, renderer_context=None):
"""Encodes the response data."""
return data
class UploadResponse(Response):
"""
An HTTP response class for requests for Uploads.
This response object provides information about Uploads during 'push' operations.
"""
def __init__(self, upload, path, request):
"""
Args:
upload (pulp_container.app.models.Upload): An Upload model used to generate the
response.
path (str): The base_path of the ContainerDistribution (Container repository name)
request (rest_framework.request.Request): Request object not used by this
implementation of Response.
"""
headers = {
"Docker-Upload-UUID": upload.pk,
"Location": f"/v2/{path}/blobs/uploads/{upload.pk}",
"Range": "0-{offset}".format(offset=int(upload.size - 1)),
"Content-Length": 0,
}
super().__init__(headers=headers, status=202)
class ManifestResponse(Response):
"""
An HTTP response class for returning Manifets.
"""
def __init__(self, manifest, path, request, status=200):
"""
Args:
manifest (pulp_container.app.models.Manifest): A Manifest model used to generate the
response.
path (str): The base_path of the ContainerDistribution (Container repository name)
request (rest_framework.request.Request): Request object not used by this
implementation of Response.
status (int): Status code to send with the response.
"""
headers = {
"Docker-Content-Digest": manifest.digest,
"Location": "/v2/{path}/manifests/{digest}".format(path=path, digest=manifest.digest),
"Content-Length": 0,
}
super().__init__(headers=headers, status=status, content_type=manifest.media_type)
class BlobResponse(Response):
"""
An HTTP response class for returning Blobs.
"""
def __init__(self, blob, path, status, request):
"""
Args:
blob (pulp_container.app.models.Blob): A Blob model used to generate the response.
path (str): The base_path of the ContainerDistribution (Container repository name)
request (rest_framework.request.Request): Request object not used by this
implementation of Response.
status (int): Status code to send with the response.
"""
artifact = blob._artifacts.get()
size = artifact.size
headers = {
"Docker-Content-Digest": blob.digest,
"Location": "/v2/{path}/blobs/{digest}".format(path=path, digest=blob.digest),
"Etag": blob.digest,
"Range": "0-{offset}".format(offset=int(size)),
"Content-Length": 0,
"Content-Type": "application/octet-stream",
"Connection": "close",
}
super().__init__(headers=headers, status=status)
class ContainerRegistryApiMixin:
"""
Mixin to add docker registry specifics to APIView classes.
This must be inherited from first to gain precedence.
It adds a registry version header to all responses.
It sets token authentication and token permission.
"""
schema = None
TOKEN_ERROR_CODES = ("invalid_token", "insufficient_scope")
@property
def authentication_classes(self):
"""
List of authentication classes to check for this view.
"""
if settings.get("TOKEN_AUTH_DISABLED", False):
return [RegistryAuthentication]
return [TokenAuthentication]
@property
def permission_classes(self):
"""
List of permission classes to check for this view.
"""
if settings.get("TOKEN_AUTH_DISABLED", False):
return [RegistryPermission]
return [TokenPermission]
@property
def default_response_headers(self):
"""
Provide common headers to all responses.
"""
headers = super().default_response_headers
headers.update({"Docker-Distribution-Api-Version": "registry/2.0"})
return headers
def get_exception_handler_context(self):
"""
Adjust the reder context for exceptions.
"""
context = super().get_exception_handler_context()
if context["request"]:
context["request"].accepted_renderer = JSONRenderer()
context["request"].accepted_media_type = JSONRenderer.media_type
return context
def handle_exception(self, exc):
"""Convert the exception detail to the container api format."""
detail = getattr(exc, "detail", "")
# If detail is a dict, we assume the exception meets the required stucture already
if not isinstance(detail, dict):
if isinstance(exc, (NotAuthenticated, AuthenticationFailed)):
code = "UNAUTHORIZED"
elif isinstance(exc, PermissionDenied):
code = "DENIED"
else:
code = "UNSUPPORTED"
exc.detail = {"errors": [{"code": code, "message": detail, "detail": {}}]}
response = super().handle_exception(exc)
# the auth header is available when the response object is initialized
error_code = getattr(detail, "code", "")
if error_code in self.TOKEN_ERROR_CODES:
response["Www-Authenticate"] += f',error="{error_code}"'
return response
def get_drv_pull(self, path):
"""
Get distribution, repository and repository_version for pull access.
"""
try:
distribution = models.ContainerDistribution.objects.get(base_path=path)
except models.ContainerDistribution.DoesNotExist:
raise RepositoryNotFound(name=path)
if distribution.repository:
repository_version = distribution.repository.latest_version()
elif distribution.repository_version:
repository_version = distribution.repository_version
else:
raise RepositoryNotFound(name=path)
return distribution, distribution.repository, repository_version
def get_dr_push(self, request, path, create=False):
"""
Get distribution and repository for push access.
Optionally create them if not found.
"""
try:
distribution = models.ContainerDistribution.objects.get(base_path=path)
except models.ContainerDistribution.DoesNotExist:
if create:
try:
with transaction.atomic():
repo_serializer = serializers.ContainerPushRepositorySerializer(
data={"name": path}, context={"request": request}
)
repo_serializer.is_valid(raise_exception=True)
repository = repo_serializer.create(repo_serializer.validated_data)
repo_href = serializers.ContainerPushRepositorySerializer(
repository, context={"request": request}
).data["pulp_href"]
dist_serializer = serializers.ContainerDistributionSerializer(
data={"base_path": path, "name": path, "repository": repo_href}
)
dist_serializer.is_valid(raise_exception=True)
distribution = dist_serializer.create(dist_serializer.validated_data)
except ValidationError:
raise RepositoryInvalid(name=path)
except IntegrityError:
# Seems like another process created our stuff already. Retry fetching it.
distribution = models.ContainerDistribution.objects.get(base_path=path)
repository = distribution.repository
if repository:
repository = repository.cast()
if not repository.PUSH_ENABLED:
raise RepositoryInvalid(name=path, message="Repository is read-only.")
else:
raise RepositoryNotFound(name=path)
else:
repository = distribution.repository
if repository:
repository = repository.cast()
if not repository.PUSH_ENABLED:
raise RepositoryInvalid(name=path, message="Repository is read-only.")
elif create:
try:
with transaction.atomic():
repo_serializer = serializers.ContainerPushRepositorySerializer(
data={"name": path}, context={"request": request}
)
repo_serializer.is_valid(raise_exception=True)
repository = repo_serializer.create(repo_serializer.validated_data)
distribution.repository = repository
distribution.save()
except IntegrityError:
# Seems like another process created our stuff already. Retry fetching it.
distribution = models.ContainerDistribution.objects.get(pk=distribution.pk)
repository = distribution.repository
if repository:
repository = repository.cast()
if not repository.PUSH_ENABLED:
raise RepositoryInvalid(name=path, message="Repository is read-only.")
else:
raise RepositoryNotFound(name=path)
else:
raise RepositoryNotFound(name=path)
return distribution, repository
class BearerTokenView(APIView):
"""
Hand out anonymous or authenticated bearer tokens.
"""
# Allow everyone to access but still value authenticated users.
permission_classes = []
def get(self, request):
"""Handles GET requests for the /token/ endpoint."""
try:
service = request.query_params["service"]
except KeyError:
raise ParseError(detail="No service name provided.")
scope = request.query_params.get("scope", "")
authorization_service = AuthorizationService(self.request.user, service, scope)
data = authorization_service.generate_token()
return Response(data=data)
class VersionView(ContainerRegistryApiMixin, APIView):
"""
Handles requests to the /v2/ endpoint.
"""
@property
def permission_classes(self):
"""
List of permission classes to check for this view.
"""
if settings.get("TOKEN_AUTH_DISABLED", False):
return [IsAuthenticated]
return [TokenPermission]
def get(self, request):
"""Handles GET requests for the /v2/ endpoint."""
return Response(data={})
class ContainerCatalogSerializer(ModelSerializer):
"""
Serializer for Distributions in the _catalog endpoint of the registry.
"""
class Meta:
model = models.ContainerDistribution
fields = ["base_path"]
class ContainerCatalogPagination(BasePagination):
"""
Pagination class to paginate repositories by names according to the registry api specification.
"""
def paginate_queryset(self, queryset, request, view=None):
"""
Analyse the pagination parameters and prepare the queryset.
"""
try:
self.n = int(request.query_params.get("n"))
except Exception:
self.n = api_settings.PAGE_SIZE
else:
if self.n > 10 * api_settings.PAGE_SIZE:
self.n = 10 * api_settings.PAGE_SIZE
if self.n < 0:
self.n = 0
last = request.query_params.get("last")
self.url = request.build_absolute_uri()
if last:
queryset = queryset.filter(base_path__gt=last)
return queryset.order_by("base_path")[: self.n]
def get_paginated_response(self, data):
"""
Prepare the paginated container _catalog response.
"""
headers = {}
repositories_names = [repo["base_path"] for repo in data]
if self.n and len(repositories_names) == self.n:
# There's a high chance we haven't gotten all entries here.
parsed_url = list(urlparse(self.url))
query_params = parse_qs(parsed_url[4])
query_params["n"] = str(self.n)
query_params["last"] = repositories_names[-1]
parsed_url[4] = urlencode(query_params)
url = urlunparse(parsed_url)
headers["Link"] = f'<{url}>; rel="next"'
return Response(headers=headers, data={"repositories": repositories_names})
class CatalogView(ContainerRegistryApiMixin, ListAPIView):
"""
Handles requests to the /v2/_catalog endpoint
"""
queryset = models.ContainerDistribution.objects.all().only("base_path")
serializer_class = ContainerCatalogSerializer
pagination_class = ContainerCatalogPagination
access_policy_class = RegistryAccessPolicy()
def get_queryset(self, *args, **kwargs):
"""Filter the queryset based on public repositories and assigned permissions."""
queryset = super().get_queryset()
distribution_permission = "container.pull_containerdistribution"
namespace_permission = "container.namespace_pull_containerdistribution"
public_repositories = queryset.filter(private=False)
repositories_by_distribution = get_objects_for_user(
self.request.user, distribution_permission, queryset
)
namespace_refs = queryset.values_list("namespace", flat=True)
namespaces = models.ContainerNamespace.objects.filter(pk__in=namespace_refs)
repositories_by_namespace = get_objects_for_user(
self.request.user, namespace_permission, namespaces
)
repositories_by_namespace = queryset.filter(namespace__in=repositories_by_namespace)
accessible_repositories = repositories_by_distribution & repositories_by_namespace
return (public_repositories | accessible_repositories).distinct()
class ContainerTagListSerializer(ModelSerializer):
"""
Serializer for Tags in the tags list endpoint of the registry.
"""
class Meta:
model = models.Tag
fields = ["name"]
class ContainerTagListPagination(BasePagination):
"""
Pagination class to paginate tags by names according to the registry api specification.
"""
def paginate_queryset(self, queryset, request, view=None):
"""
Analyse the pagination parameters and prepare the queryset.
"""
try:
self.n = int(request.query_params.get("n"))
except Exception:
self.n = api_settings.PAGE_SIZE
else:
if self.n > 10 * api_settings.PAGE_SIZE:
self.n = 10 * api_settings.PAGE_SIZE
if self.n < 0:
self.n = 0
last = request.query_params.get("last")
self.url = request.build_absolute_uri()
self.path = request.resolver_match.kwargs["path"]
if last:
queryset = queryset.filter(name__gt=last)
return queryset.order_by("name")[: self.n]
def get_paginated_response(self, data):
"""
Prepare the paginated container _catalog response.
"""
headers = {}
tag_names = [tag["name"] for tag in data]
if self.n and len(tag_names) == self.n:
# There's a high chance we haven't gotten all entries here.
parsed_url = list(urlparse(self.url))
query_params = parse_qs(parsed_url[4])
query_params["n"] = str(self.n)
query_params["last"] = tag_names[-1]
parsed_url[4] = urlencode(query_params)
url = urlunparse(parsed_url)
headers["Link"] = f'<{url}>; rel="next"'
return Response(headers=headers, data={"name": self.path, "tags": tag_names})
class TagsListView(ContainerRegistryApiMixin, ListAPIView):
"""
Handles requests to the /v2/<repo>/tags/list endpoint
"""
serializer_class = ContainerTagListSerializer
pagination_class = ContainerTagListPagination
def get_queryset(self):
"""
Handles GET requests to the /v2/<repo>/tags/list endpoint
"""
path = self.request.resolver_match.kwargs["path"]
_, _, repository_version = self.get_drv_pull(path)
return models.Tag.objects.filter(pk__in=repository_version.content).only("name")
class BlobUploads(ContainerRegistryApiMixin, ViewSet):
"""
The ViewSet for handling uploading of blobs.
"""
model = models.Upload
queryset = models.Upload.objects.all()
content_range_pattern = re.compile(r"^(?P<start>\d+)-(?P<end>\d+)$")
def create(self, request, path):
"""
Create a new upload.
"""
_, repository = self.get_dr_push(request, path, create=True)
upload = models.Upload(repository=repository, size=0)
upload.save()
response = UploadResponse(upload=upload, path=path, request=request)
return response
def partial_update(self, request, path, pk=None):
"""
Process a chunk that will be appended to an existing upload.
"""
_, repository = self.get_dr_push(request, path)
chunk = request.META["wsgi.input"]
if "Content-Range" in request.headers or "digest" not in request.query_params:
whole = False
else:
whole = True
if whole:
start = 0
else:
content_range = request.META.get("HTTP_CONTENT_RANGE", "")
match = self.content_range_pattern.match(content_range)
start = 0 if not match else int(match.group("start"))
upload = get_object_or_404(models.Upload, repository=repository, pk=pk)
chunk = ContentFile(chunk.read())
with transaction.atomic():
if upload.size != start:
raise Exception
upload.append(chunk, upload.size)
upload.size += chunk.size
upload.save()
return UploadResponse(upload=upload, path=path, request=request)
def put(self, request, path, pk=None):
"""
Create a blob from uploaded chunks.
"""
_, repository = self.get_dr_push(request, path)
digest = request.query_params["digest"]
# Try to see if the client came back after we told it to backoff with the ``Throttled``
# exception. In that case we answer based on the task state, or make it backoff again.
# This mechanism seems to work with podman but not with docker. However we let the task run
# anyway, since all clients will look with a HEAD request before attemting to upload a blob
# again.
try:
upload = models.Upload.objects.get(pk=pk, repository=repository)
except models.Upload.DoesNotExist as e_upload:
# Upload has been deleted => task has started or even finished
try:
task = Task.objects.filter(
name__endswith="add_and_remove",
reserved_resources_record__resource=f"upload:{pk}",
).last()
except Task.DoesNotExist:
# No upload and no task for it => the upload probably never existed
# return 404
raise e_upload
if task.state == "completed":
task.delete()
blob = models.Blob.objects.get(digest=digest)
return BlobResponse(blob, path, 201, request)
elif task.state in ["waiting", "running"]:
raise Throttled()
else:
error = task.error
task.delete()
raise Exception(str(error))
chunks = UploadChunk.objects.filter(upload=upload).order_by("offset")
with NamedTemporaryFile("ab") as temp_file:
for chunk in chunks:
temp_file.write(chunk.file.read())
temp_file.flush()
uploaded_file = PulpTemporaryUploadedFile.from_file(File(open(temp_file.name, "rb")))
if uploaded_file.hashers["sha256"].hexdigest() == digest[len("sha256:") :]:
try:
artifact = Artifact.init_and_validate(uploaded_file)
artifact.save()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
artifact.touch()
try:
blob = models.Blob(digest=digest, media_type=models.MEDIA_TYPE.REGULAR_BLOB)
blob.save()
except IntegrityError:
blob = models.Blob.objects.get(digest=digest)
blob.touch()
try:
blob_artifact = ContentArtifact(
artifact=artifact, content=blob, relative_path=digest
)
blob_artifact.save()
except IntegrityError:
pass
upload.delete()
dispatched_task = dispatch(
add_and_remove,
[f"upload:{pk}", repository],
kwargs={
"repository_pk": str(repository.pk),
"add_content_units": [str(blob.pk)],
"remove_content_units": [],
},
)
# Wait a small amount of time
for dummy in range(3):
time.sleep(1)
task = Task.objects.get(pk=dispatched_task.pk)
if task.state == "completed":
task.delete()
return BlobResponse(blob, path, 201, request)
elif task.state in ["waiting", "running"]:
continue
else:
error = task.error
task.delete()
raise Exception(str(error))
raise Throttled()
else:
raise Exception("The digest did not match")
class RedirectsMixin:
"""
A mixin used for configuring how the redirects will work based on a storage type.
"""
def __init__(self, *args, **kwargs):
"""
Determine a storage type and initialize the redirect class according to that.
"""
super().__init__(*args, **kwargs)
if settings.DEFAULT_FILE_STORAGE == "pulpcore.app.models.storage.FileSystem":
self.redirects_class = FileStorageRedirects
elif settings.DEFAULT_FILE_STORAGE == "storages.backends.s3boto3.S3Boto3Storage":
self.redirects_class = S3StorageRedirects
elif settings.DEFAULT_FILE_STORAGE == "storages.backends.azure_storage.AzureStorage":
self.redirects_class = AzureStorageRedirects
else:
raise NotImplementedError()
class Blobs(RedirectsMixin, ContainerRegistryApiMixin, ViewSet):
"""
ViewSet for interacting with Blobs
"""
renderer_classes = [ContentRenderer]
def head(self, request, path, pk=None):
"""
Responds to HEAD requests about blobs
"""
return self.get(request, path, pk=pk)
def get(self, request, path, pk=None):
"""Handles GET requests for Blobs."""
distribution, _, repository_version = self.get_drv_pull(path)
redirects = self.redirects_class(distribution, path, request)
try:
blob = models.Blob.objects.get(digest=pk, pk__in=repository_version.content)
except models.Blob.DoesNotExist:
if pk == EMPTY_BLOB:
return redirects.redirect_to_content_app("blobs", pk)
raise BlobNotFound(digest=pk)
return redirects.issue_blob_redirect(blob)
class Manifests(RedirectsMixin, ContainerRegistryApiMixin, ViewSet):
"""
ViewSet for interacting with Manifests
"""
renderer_classes = [ContentRenderer]
# The lookup regex does not allow /, ^, &, *, %, !, ~, @, #, +, =, ?
lookup_value_regex = "[^/^&*%!~@#+=?]+"
def head(self, request, path, pk=None):
"""
Responds to HEAD requests about manifests by reference
"""
return self.get(request, path, pk=pk)
def get(self, request, path, pk=None):
"""
Responds to GET requests about manifests by reference
"""
distribution, _, repository_version = self.get_drv_pull(path)
redirects = self.redirects_class(distribution, path, request)
try:
if pk[:7] != "sha256:":
tag = models.Tag.objects.get(name=pk, pk__in=repository_version.content)
return redirects.issue_tag_redirect(tag)
else:
manifest = models.Manifest.objects.get(digest=pk, pk__in=repository_version.content)
except (models.Tag.DoesNotExist, models.Manifest.DoesNotExist):
raise ManifestNotFound(reference=pk)
return redirects.issue_manifest_redirect(manifest)
def put(self, request, path, pk=None):
"""
Responds with the actual manifest
"""
_, repository = self.get_dr_push(request, path)
# iterate over all the layers and create
chunk = request.META["wsgi.input"]
artifact = self.receive_artifact(chunk)
with storage.open(artifact.file.name) as artifact_file:
raw_data = artifact_file.read()
content_data = json.loads(raw_data)
config_layer = content_data.get("config")
config_blob = models.Blob.objects.get(digest=config_layer.get("digest"))
manifest = models.Manifest(
digest="sha256:{id}".format(id=artifact.sha256),
schema_version=2,
media_type=request.content_type,
config_blob=config_blob,
)
try:
manifest.save()
except IntegrityError:
manifest = models.Manifest.objects.get(digest=manifest.digest)
manifest.touch()
ca = ContentArtifact(artifact=artifact, content=manifest, relative_path=manifest.digest)
try:
ca.save()
except IntegrityError:
pass
layers = content_data.get("layers")
blobs = []
for layer in layers:
blobs.append(layer.get("digest"))
blobs_qs = models.Blob.objects.filter(digest__in=blobs)
thru = []
for blob in blobs_qs:
thru.append(models.BlobManifest(manifest=manifest, manifest_blob=blob))
models.BlobManifest.objects.bulk_create(objs=thru, ignore_conflicts=True, batch_size=1000)
tag = models.Tag(name=pk, tagged_manifest=manifest)
try:
tag.save()
except IntegrityError:
tag = models.Tag.objects.get(name=tag.name, tagged_manifest=manifest)
tag.touch()
tags_to_remove = models.Tag.objects.filter(
pk__in=repository.latest_version().content.all(), name=tag
).exclude(tagged_manifest=manifest)
dispatched_task = dispatch(
add_and_remove,
[repository],
kwargs={
"repository_pk": str(repository.pk),
"add_content_units": [str(tag.pk), str(manifest.pk)],
"remove_content_units": [str(pk) for pk in tags_to_remove.values_list("pk")],
},
)
# Wait a small amount of time
for dummy in range(3):
time.sleep(1)
task = Task.objects.get(pk=dispatched_task.pk)
if task.state == "completed":
task.delete()
return ManifestResponse(manifest, path, request, status=201)
elif task.state in ["waiting", "running"]:
continue
else:
error = task.error
task.delete()
raise Exception(str(error))
raise Throttled()
def receive_artifact(self, chunk):
"""Handles assembling of Manifest as it's being uploaded."""
with NamedTemporaryFile("ab") as temp_file:
size = 0
hashers = {}
for algorithm in Artifact.DIGEST_FIELDS:
hashers[algorithm] = getattr(hashlib, algorithm)()
while True:
subchunk = chunk.read(2000000)
if not subchunk:
break
temp_file.write(subchunk)
size += len(subchunk)
for algorithm in Artifact.DIGEST_FIELDS:
hashers[algorithm].update(subchunk)
temp_file.flush()
digests = {}
for algorithm in Artifact.DIGEST_FIELDS:
digests[algorithm] = hashers[algorithm].hexdigest()
artifact = Artifact(file=temp_file.name, size=size, **digests)
try:
artifact.save()
except IntegrityError:
artifact = Artifact.objects.get(sha256=artifact.sha256)
artifact.touch()
return artifact