2015-07-06 19:00:07 +00:00
|
|
|
import logging
|
2015-08-12 20:39:32 +00:00
|
|
|
import re
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
from flask import url_for, request, redirect, Response, abort as flask_abort
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
from app import storage, app, get_app_url, metric_queue
|
2015-12-09 20:07:37 +00:00
|
|
|
from auth.registry_jwt_auth import process_registry_jwt_auth
|
2018-04-15 11:55:14 +00:00
|
|
|
from auth.permissions import ReadRepositoryPermission
|
2016-08-02 00:48:34 +00:00
|
|
|
from data import database
|
2018-10-04 17:59:53 +00:00
|
|
|
from data.registry_model import registry_model
|
|
|
|
from data.registry_model.blobuploader import (create_blob_upload, retrieve_blob_upload_manager,
|
|
|
|
complete_when_uploaded, BlobUploadSettings,
|
|
|
|
BlobUploadException, BlobTooLargeException)
|
2015-07-06 19:00:07 +00:00
|
|
|
from digest import digest_tools
|
2017-07-20 15:31:22 +00:00
|
|
|
from endpoints.decorators import anon_protect, parse_repository_name
|
2015-07-06 19:00:07 +00:00
|
|
|
from endpoints.v2 import v2_bp, require_repo_read, require_repo_write, get_input_stream
|
2017-06-26 22:16:15 +00:00
|
|
|
from endpoints.v2.errors import (
|
2018-04-15 11:55:14 +00:00
|
|
|
BlobUnknown, BlobUploadInvalid, BlobUploadUnknown, Unsupported, NameUnknown, LayerTooLarge,
|
|
|
|
InvalidRequest)
|
2015-08-12 20:39:32 +00:00
|
|
|
from util.cache import cache_control
|
2018-04-15 11:55:14 +00:00
|
|
|
from util.names import parse_namespace_repository
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
BASE_BLOB_ROUTE = '/<repopath:repository>/blobs/<regex("{0}"):digest>'
|
2015-07-16 19:49:06 +00:00
|
|
|
BLOB_DIGEST_ROUTE = BASE_BLOB_ROUTE.format(digest_tools.DIGEST_PATTERN)
|
2015-08-12 20:39:32 +00:00
|
|
|
RANGE_HEADER_REGEX = re.compile(r'^bytes=([0-9]+)-([0-9]+)$')
|
2015-11-30 20:45:45 +00:00
|
|
|
BLOB_CONTENT_TYPE = 'application/octet-stream'
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class _InvalidRangeHeader(Exception):
|
|
|
|
pass
|
2015-07-16 19:49:06 +00:00
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(BLOB_DIGEST_ROUTE, methods=['HEAD'])
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
2016-03-09 23:09:20 +00:00
|
|
|
@process_registry_jwt_auth(scopes=['pull'])
|
2015-08-12 20:39:32 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
|
|
|
@cache_control(max_age=31436000)
|
2016-03-09 21:20:28 +00:00
|
|
|
def check_blob_exists(namespace_name, repo_name, digest):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
# Find the blob.
|
2018-10-04 17:59:53 +00:00
|
|
|
blob = registry_model.get_repo_blob_by_digest(repository_ref, digest, include_placements=True)
|
2016-08-02 00:48:34 +00:00
|
|
|
if blob is None:
|
|
|
|
raise BlobUnknown()
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
# Build the response headers.
|
|
|
|
headers = {
|
|
|
|
'Docker-Content-Digest': digest,
|
2018-10-04 17:59:53 +00:00
|
|
|
'Content-Length': blob.compressed_size,
|
2017-12-14 18:38:24 +00:00
|
|
|
'Content-Type': BLOB_CONTENT_TYPE,
|
|
|
|
}
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# If our storage supports range requests, let the client know.
|
2018-10-04 17:59:53 +00:00
|
|
|
if storage.get_supports_resumable_downloads(blob.placements):
|
2016-08-02 00:48:34 +00:00
|
|
|
headers['Accept-Ranges'] = 'bytes'
|
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# Write the response to the client.
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(headers=headers)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
2015-07-16 19:49:06 +00:00
|
|
|
@v2_bp.route(BLOB_DIGEST_ROUTE, methods=['GET'])
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
2016-03-09 23:09:20 +00:00
|
|
|
@process_registry_jwt_auth(scopes=['pull'])
|
2015-07-06 19:00:07 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
@cache_control(max_age=31536000)
|
2016-03-09 21:20:28 +00:00
|
|
|
def download_blob(namespace_name, repo_name, digest):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
# Find the blob.
|
2018-10-04 17:59:53 +00:00
|
|
|
blob = registry_model.get_repo_blob_by_digest(repository_ref, digest, include_placements=True)
|
2016-08-02 00:48:34 +00:00
|
|
|
if blob is None:
|
|
|
|
raise BlobUnknown()
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
# Build the response headers.
|
|
|
|
headers = {'Docker-Content-Digest': digest}
|
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# If our storage supports range requests, let the client know.
|
2018-10-04 17:59:53 +00:00
|
|
|
if storage.get_supports_resumable_downloads(blob.placements):
|
2016-08-02 00:48:34 +00:00
|
|
|
headers['Accept-Ranges'] = 'bytes'
|
|
|
|
|
|
|
|
# Short-circuit by redirecting if the storage supports it.
|
2018-10-04 17:59:53 +00:00
|
|
|
path = blob.storage_path
|
2016-08-02 00:48:34 +00:00
|
|
|
logger.debug('Looking up the direct download URL for path: %s', path)
|
2018-10-04 17:59:53 +00:00
|
|
|
direct_download_url = storage.get_direct_download_url(blob.placements, path, request.remote_addr)
|
2015-08-12 20:39:32 +00:00
|
|
|
if direct_download_url:
|
|
|
|
logger.debug('Returning direct download URL')
|
|
|
|
resp = redirect(direct_download_url)
|
|
|
|
resp.headers.extend(headers)
|
|
|
|
return resp
|
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
# Close the database connection before we stream the download.
|
|
|
|
logger.debug('Closing database connection before streaming layer data')
|
2016-08-03 19:18:51 +00:00
|
|
|
with database.CloseForLongOperation(app.config):
|
2016-08-09 20:06:12 +00:00
|
|
|
# Stream the response to the client.
|
2016-08-03 19:18:51 +00:00
|
|
|
return Response(
|
2018-10-04 17:59:53 +00:00
|
|
|
storage.stream_read(blob.placements, path),
|
2016-08-03 19:18:51 +00:00
|
|
|
headers=headers.update({
|
2018-10-04 17:59:53 +00:00
|
|
|
'Content-Length': blob.compressed_size,
|
|
|
|
'Content-Type': BLOB_CONTENT_TYPE,
|
|
|
|
}),
|
|
|
|
)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
def _try_to_mount_blob(repository_ref, mount_blob_digest):
|
2018-04-15 11:55:14 +00:00
|
|
|
""" Attempts to mount a blob requested by the user from another repository. """
|
2018-10-04 17:59:53 +00:00
|
|
|
logger.debug('Got mount request for blob `%s` into `%s`', mount_blob_digest, repository_ref)
|
2018-04-15 11:55:14 +00:00
|
|
|
from_repo = request.args.get('from', None)
|
|
|
|
if from_repo is None:
|
|
|
|
raise InvalidRequest
|
|
|
|
|
|
|
|
# Ensure the user has access to the repository.
|
2018-10-04 17:59:53 +00:00
|
|
|
logger.debug('Got mount request for blob `%s` under repository `%s` into `%s`',
|
|
|
|
mount_blob_digest, from_repo, repository_ref)
|
2018-04-15 11:55:14 +00:00
|
|
|
from_namespace, from_repo_name = parse_namespace_repository(from_repo,
|
|
|
|
app.config['LIBRARY_NAMESPACE'],
|
|
|
|
include_tag=False)
|
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
from_repository_ref = registry_model.lookup_repository(from_namespace, from_repo_name)
|
|
|
|
if from_repository_ref is None:
|
|
|
|
logger.debug('Could not find from repo: `%s/%s`', from_namespace, from_repo_name)
|
|
|
|
return None
|
|
|
|
|
|
|
|
# First check permission.
|
2018-04-15 11:55:14 +00:00
|
|
|
read_permission = ReadRepositoryPermission(from_namespace, from_repo_name).can()
|
|
|
|
if not read_permission:
|
|
|
|
# If no direct permission, check if the repostory is public.
|
2018-10-04 17:59:53 +00:00
|
|
|
if not from_repository_ref.is_public:
|
|
|
|
logger.debug('No permission to mount blob `%s` under repository `%s` into `%s`',
|
|
|
|
mount_blob_digest, from_repo, repository_ref)
|
2018-04-15 11:55:14 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
# Lookup if the mount blob's digest exists in the repository.
|
2018-10-04 17:59:53 +00:00
|
|
|
mount_blob = registry_model.get_repo_blob_by_digest(from_repository_ref, mount_blob_digest)
|
2018-04-15 11:55:14 +00:00
|
|
|
if mount_blob is None:
|
|
|
|
logger.debug('Blob `%s` under repository `%s` not found', mount_blob_digest, from_repo)
|
|
|
|
return None
|
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
logger.debug('Mounting blob `%s` under repository `%s` into `%s`', mount_blob_digest,
|
|
|
|
from_repo, repository_ref)
|
2018-04-15 11:55:14 +00:00
|
|
|
|
|
|
|
# Mount the blob into the current repository and return that we've completed the operation.
|
|
|
|
expiration_sec = app.config['PUSH_TEMP_TAG_EXPIRATION_SEC']
|
2018-10-04 17:59:53 +00:00
|
|
|
mounted = registry_model.mount_blob_into_repository(mount_blob, repository_ref, expiration_sec)
|
|
|
|
if not mounted:
|
2018-04-15 11:55:14 +00:00
|
|
|
logger.debug('Could not mount blob `%s` under repository `%s` not found', mount_blob_digest,
|
|
|
|
from_repo)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Return the response for the blob indicating that it was mounted, and including its content
|
|
|
|
# digest.
|
2018-10-04 17:59:53 +00:00
|
|
|
logger.debug('Mounted blob `%s` under repository `%s` into `%s`', mount_blob_digest,
|
|
|
|
from_repo, repository_ref)
|
|
|
|
|
|
|
|
namespace_name = repository_ref.namespace_name
|
|
|
|
repo_name = repository_ref.name
|
|
|
|
|
2018-04-15 11:55:14 +00:00
|
|
|
return Response(
|
|
|
|
status=201,
|
|
|
|
headers={
|
|
|
|
'Docker-Content-Digest': mount_blob_digest,
|
|
|
|
'Location':
|
2018-10-04 17:59:53 +00:00
|
|
|
get_app_url() + url_for('v2.download_blob',
|
|
|
|
repository='%s/%s' % (namespace_name, repo_name),
|
|
|
|
digest=mount_blob_digest),
|
|
|
|
},
|
|
|
|
)
|
2018-04-15 11:55:14 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/uploads/', methods=['POST'])
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
2016-03-09 23:09:20 +00:00
|
|
|
@process_registry_jwt_auth(scopes=['pull', 'push'])
|
2015-07-06 19:00:07 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
def start_blob_upload(namespace_name, repo_name):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
2015-12-07 17:30:28 +00:00
|
|
|
raise NameUnknown()
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2018-04-15 11:55:14 +00:00
|
|
|
# Check for mounting of a blob from another repository.
|
|
|
|
mount_blob_digest = request.args.get('mount', None)
|
|
|
|
if mount_blob_digest is not None:
|
2018-10-04 17:59:53 +00:00
|
|
|
response = _try_to_mount_blob(repository_ref, mount_blob_digest)
|
2018-04-15 11:55:14 +00:00
|
|
|
if response is not None:
|
|
|
|
return response
|
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
# Begin the blob upload process.
|
|
|
|
blob_uploader = create_blob_upload(repository_ref, storage, _upload_settings())
|
|
|
|
if blob_uploader is None:
|
|
|
|
logger.debug('Could not create a blob upload for `%s/%s`', namespace_name, repo_name)
|
|
|
|
raise InvalidRequest()
|
|
|
|
|
|
|
|
# Check if the blob will be uploaded now or in followup calls. If the `digest` is given, then
|
|
|
|
# the upload will occur as a monolithic chunk in this call. Ohterwise, we return a redirect
|
|
|
|
# for the client to upload the chunks as distinct operations.
|
2015-08-12 20:39:32 +00:00
|
|
|
digest = request.args.get('digest', None)
|
|
|
|
if digest is None:
|
2016-08-02 00:48:34 +00:00
|
|
|
# Short-circuit because the user will send the blob data in another request.
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
|
|
|
status=202,
|
|
|
|
headers={
|
2018-10-04 17:59:53 +00:00
|
|
|
'Docker-Upload-UUID': blob_uploader.blob_upload_id,
|
|
|
|
'Range': _render_range(0),
|
2017-06-26 22:16:15 +00:00
|
|
|
'Location':
|
2018-10-04 17:59:53 +00:00
|
|
|
get_app_url() + url_for('v2.upload_chunk',
|
|
|
|
repository='%s/%s' % (namespace_name, repo_name),
|
|
|
|
upload_uuid=blob_uploader.blob_upload_id)
|
|
|
|
},
|
|
|
|
)
|
2016-07-20 21:53:43 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
# Upload the data sent and commit it to a blob.
|
|
|
|
with complete_when_uploaded(blob_uploader):
|
|
|
|
_upload_chunk(blob_uploader, digest)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# Write the response to the client.
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
|
|
|
status=201,
|
|
|
|
headers={
|
2018-10-04 17:59:53 +00:00
|
|
|
'Docker-Content-Digest': digest,
|
2017-06-26 22:16:15 +00:00
|
|
|
'Location':
|
2018-10-04 17:59:53 +00:00
|
|
|
get_app_url() + url_for('v2.download_blob',
|
|
|
|
repository='%s/%s' % (namespace_name, repo_name),
|
|
|
|
digest=digest),
|
|
|
|
},
|
|
|
|
)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['GET'])
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
2016-03-09 23:09:20 +00:00
|
|
|
@process_registry_jwt_auth(scopes=['pull'])
|
2015-08-12 20:39:32 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
def fetch_existing_upload(namespace_name, repo_name, upload_uuid):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
|
|
|
|
|
|
|
uploader = retrieve_blob_upload_manager(repository_ref, upload_uuid, storage, _upload_settings())
|
|
|
|
if uploader is None:
|
2016-08-02 00:48:34 +00:00
|
|
|
raise BlobUploadUnknown()
|
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
|
|
|
status=204,
|
|
|
|
headers={
|
|
|
|
'Docker-Upload-UUID': upload_uuid,
|
2018-10-04 17:59:53 +00:00
|
|
|
'Range': _render_range(uploader.blob_upload.byte_count + 1), # byte ranges are exclusive
|
|
|
|
},
|
|
|
|
)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PATCH'])
|
|
|
|
@parse_repository_name()
|
|
|
|
@process_registry_jwt_auth(scopes=['pull', 'push'])
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def upload_chunk(namespace_name, repo_name, upload_uuid):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
uploader = retrieve_blob_upload_manager(repository_ref, upload_uuid, storage, _upload_settings())
|
|
|
|
if uploader is None:
|
|
|
|
raise BlobUploadUnknown()
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
# Upload the chunk for the blob.
|
|
|
|
_upload_chunk(uploader)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# Write the response to the client.
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
|
|
|
status=204,
|
|
|
|
headers={
|
2016-10-07 19:41:35 +00:00
|
|
|
'Location': _current_request_url(),
|
2018-10-04 17:59:53 +00:00
|
|
|
'Range': _render_range(uploader.blob_upload.byte_count, with_bytes_prefix=False),
|
|
|
|
'Docker-Upload-UUID': upload_uuid,
|
|
|
|
},
|
|
|
|
)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['PUT'])
|
|
|
|
@parse_repository_name()
|
|
|
|
@process_registry_jwt_auth(scopes=['pull', 'push'])
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def monolithic_upload_or_last_chunk(namespace_name, repo_name, upload_uuid):
|
|
|
|
# Ensure the digest is present before proceeding.
|
|
|
|
digest = request.args.get('digest', None)
|
|
|
|
if digest is None:
|
2016-08-02 22:45:30 +00:00
|
|
|
raise BlobUploadInvalid(detail={'reason': 'Missing digest arg on monolithic upload'})
|
2016-08-02 00:48:34 +00:00
|
|
|
|
|
|
|
# Find the upload.
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
uploader = retrieve_blob_upload_manager(repository_ref, upload_uuid, storage, _upload_settings())
|
|
|
|
if uploader is None:
|
|
|
|
raise BlobUploadUnknown()
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
# Upload the chunk for the blob and commit it once complete.
|
|
|
|
with complete_when_uploaded(uploader):
|
|
|
|
_upload_chunk(uploader, digest)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2016-08-09 20:06:12 +00:00
|
|
|
# Write the response to the client.
|
2017-06-26 22:16:15 +00:00
|
|
|
return Response(status=201, headers={
|
2018-10-04 17:59:53 +00:00
|
|
|
'Docker-Content-Digest': digest,
|
2017-06-26 22:16:15 +00:00
|
|
|
'Location':
|
2018-10-04 17:59:53 +00:00
|
|
|
get_app_url() + url_for('v2.download_blob',
|
|
|
|
repository='%s/%s' % (namespace_name, repo_name),
|
|
|
|
digest=digest),
|
|
|
|
},
|
|
|
|
)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/uploads/<upload_uuid>', methods=['DELETE'])
|
|
|
|
@parse_repository_name()
|
|
|
|
@process_registry_jwt_auth(scopes=['pull', 'push'])
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def cancel_upload(namespace_name, repo_name, upload_uuid):
|
2018-10-04 17:59:53 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name)
|
|
|
|
if repository_ref is None:
|
|
|
|
raise NameUnknown()
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
uploader = retrieve_blob_upload_manager(repository_ref, upload_uuid, storage, _upload_settings())
|
|
|
|
if uploader is None:
|
|
|
|
raise BlobUploadUnknown()
|
2016-08-02 00:48:34 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
uploader.cancel_upload()
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(status=204)
|
2016-08-02 00:48:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<repopath:repository>/blobs/<digest>', methods=['DELETE'])
|
|
|
|
@parse_repository_name()
|
|
|
|
@process_registry_jwt_auth(scopes=['pull', 'push'])
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_digest(namespace_name, repo_name, upload_uuid):
|
|
|
|
# We do not support deleting arbitrary digests, as they break repo images.
|
|
|
|
raise Unsupported()
|
|
|
|
|
|
|
|
|
|
|
|
def _render_range(num_uploaded_bytes, with_bytes_prefix=True):
|
2016-08-02 22:45:30 +00:00
|
|
|
"""
|
|
|
|
Returns a string formatted to be used in the Range header.
|
|
|
|
"""
|
2016-08-02 00:48:34 +00:00
|
|
|
return '{0}0-{1}'.format('bytes=' if with_bytes_prefix else '', num_uploaded_bytes - 1)
|
|
|
|
|
|
|
|
|
2016-10-07 19:41:35 +00:00
|
|
|
def _current_request_url():
|
|
|
|
return '{0}{1}{2}'.format(get_app_url(), request.script_root, request.path)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
def _abort_range_not_satisfiable(valid_end, upload_uuid):
|
|
|
|
"""
|
|
|
|
Writes a failure response for scenarios where the registry cannot function
|
|
|
|
with the provided range.
|
|
|
|
|
|
|
|
TODO(jzelinskie): Unify this with the V2RegistryException class.
|
|
|
|
"""
|
2017-06-26 22:16:15 +00:00
|
|
|
flask_abort(
|
|
|
|
Response(status=416, headers={
|
|
|
|
'Location': _current_request_url(),
|
|
|
|
'Range': '0-{0}'.format(valid_end),
|
|
|
|
'Docker-Upload-UUID': upload_uuid}))
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2015-09-25 15:51:50 +00:00
|
|
|
def _parse_range_header(range_header_text):
|
2016-08-02 00:48:34 +00:00
|
|
|
"""
|
|
|
|
Parses the range header.
|
|
|
|
|
|
|
|
Returns a tuple of the start offset and the length.
|
|
|
|
If the parse fails, raises _InvalidRangeHeader.
|
2015-08-12 20:39:32 +00:00
|
|
|
"""
|
|
|
|
found = RANGE_HEADER_REGEX.match(range_header_text)
|
|
|
|
if found is None:
|
|
|
|
raise _InvalidRangeHeader()
|
|
|
|
|
|
|
|
start = int(found.group(1))
|
|
|
|
length = int(found.group(2)) - start
|
|
|
|
|
2015-09-25 15:51:50 +00:00
|
|
|
if length <= 0:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise _InvalidRangeHeader()
|
|
|
|
|
|
|
|
return (start, length)
|
|
|
|
|
|
|
|
|
2016-08-09 20:02:49 +00:00
|
|
|
def _start_offset_and_length(range_header):
|
2016-08-02 00:48:34 +00:00
|
|
|
"""
|
|
|
|
Returns a tuple of the start offset and the length.
|
|
|
|
If the range header doesn't exist, defaults to (0, -1).
|
|
|
|
If parsing fails, returns (None, None).
|
2015-08-12 20:39:32 +00:00
|
|
|
"""
|
|
|
|
start_offset, length = 0, -1
|
|
|
|
if range_header is not None:
|
|
|
|
try:
|
2015-09-25 15:51:50 +00:00
|
|
|
start_offset, length = _parse_range_header(range_header)
|
2015-08-12 20:39:32 +00:00
|
|
|
except _InvalidRangeHeader:
|
2016-08-02 00:48:34 +00:00
|
|
|
return None, None
|
2016-08-02 22:45:30 +00:00
|
|
|
|
2016-08-02 00:48:34 +00:00
|
|
|
return start_offset, length
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2015-09-02 21:31:44 +00:00
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
def _upload_settings():
|
|
|
|
""" Returns the settings for instantiating a blob upload manager. """
|
|
|
|
expiration_sec = app.config['PUSH_TEMP_TAG_EXPIRATION_SEC']
|
|
|
|
settings = BlobUploadSettings(maximum_blob_size=app.config['MAXIMUM_LAYER_SIZE'],
|
|
|
|
bittorrent_piece_size=app.config['BITTORRENT_PIECE_SIZE'],
|
|
|
|
committed_blob_expiration=expiration_sec)
|
|
|
|
return settings
|
2016-08-02 00:48:34 +00:00
|
|
|
|
|
|
|
|
2018-10-04 17:59:53 +00:00
|
|
|
def _upload_chunk(blob_uploader, commit_digest=None):
|
|
|
|
""" Performs uploading of a chunk of data in the current request's stream, via the blob uploader
|
|
|
|
given. If commit_digest is specified, the upload is committed to a blob once the stream's
|
|
|
|
data has been read and stored.
|
2016-08-02 00:48:34 +00:00
|
|
|
"""
|
2018-10-04 17:59:53 +00:00
|
|
|
start_offset, length = _start_offset_and_length(request.headers.get('range'))
|
|
|
|
if None in {start_offset, length}:
|
|
|
|
raise InvalidRequest()
|
|
|
|
|
|
|
|
input_fp = get_input_stream(request)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Upload the data received.
|
|
|
|
blob_uploader.upload_chunk(app.config, input_fp, start_offset, length, metric_queue)
|
|
|
|
|
|
|
|
if commit_digest is not None:
|
|
|
|
# Commit the upload to a blob.
|
|
|
|
return blob_uploader.commit_to_blob(app.config, commit_digest)
|
|
|
|
except BlobTooLargeException as ble:
|
|
|
|
raise LayerTooLarge(uploaded=ble.uploaded, max_allowed=ble.max_allowed)
|
|
|
|
except BlobUploadException:
|
|
|
|
logger.exception('Exception when uploading blob to %s', blob_uploader.blob_upload_id)
|
|
|
|
_abort_range_not_satisfiable(blob_uploader.blob_upload.byte_count,
|
|
|
|
blob_uploader.blob_upload_id)
|