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
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
from flask import make_response, url_for, request, redirect, Response, abort as flask_abort
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from app import storage, app
|
2015-08-12 20:39:32 +00:00
|
|
|
from data import model, database
|
2015-07-06 19:00:07 +00:00
|
|
|
from digest import digest_tools
|
|
|
|
from endpoints.v2 import v2_bp, require_repo_read, require_repo_write, get_input_stream
|
2015-10-02 18:01:12 +00:00
|
|
|
from endpoints.v2.errors import BlobUnknown, BlobUploadInvalid, BlobUploadUnknown, Unsupported
|
2015-07-06 19:00:07 +00:00
|
|
|
from auth.jwt_auth import process_jwt_auth
|
|
|
|
from endpoints.decorators import anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
from util.cache import cache_control
|
2015-09-02 21:31:44 +00:00
|
|
|
from util.registry.filelike import wrap_with_handler, StreamSlice
|
2015-08-12 20:39:32 +00:00
|
|
|
from storage.basestorage import InvalidChunkException
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2015-07-16 19:49:06 +00:00
|
|
|
BASE_BLOB_ROUTE = '/<namespace>/<repo_name>/blobs/<regex("{0}"):digest>'
|
|
|
|
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]+)$')
|
|
|
|
|
|
|
|
|
|
|
|
class _InvalidRangeHeader(Exception):
|
|
|
|
pass
|
2015-07-16 19:49:06 +00:00
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
def _base_blob_fetch(namespace, repo_name, digest):
|
|
|
|
""" Some work that is common to both GET and HEAD requests. Callers MUST check for proper
|
|
|
|
authorization before calling this method.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
found = model.blob.get_repo_blob_by_digest(namespace, repo_name, digest)
|
2015-07-15 21:25:41 +00:00
|
|
|
except model.BlobDoesNotExist:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise BlobUnknown()
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
'Docker-Content-Digest': digest,
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add the Accept-Ranges header if the storage engine supports resumable
|
|
|
|
# downloads.
|
2015-08-27 18:55:33 +00:00
|
|
|
if storage.get_supports_resumable_downloads(found.locations):
|
2015-08-12 20:39:32 +00:00
|
|
|
logger.debug('Storage supports resumable downloads')
|
|
|
|
headers['Accept-Ranges'] = 'bytes'
|
|
|
|
|
|
|
|
return found, headers
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route(BLOB_DIGEST_ROUTE, methods=['HEAD'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
|
|
|
@cache_control(max_age=31436000)
|
|
|
|
def check_blob_exists(namespace, repo_name, digest):
|
2015-12-02 06:52:58 +00:00
|
|
|
found, headers = _base_blob_fetch(namespace, repo_name, digest)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
response = make_response('')
|
|
|
|
response.headers.extend(headers)
|
2015-12-02 06:52:58 +00:00
|
|
|
response.headers['Content-Length'] = found.image_size
|
2015-08-12 20:39:32 +00:00
|
|
|
return response
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
2015-07-16 19:49:06 +00:00
|
|
|
@v2_bp.route(BLOB_DIGEST_ROUTE, methods=['GET'])
|
2015-07-06 19:00:07 +00:00
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
@cache_control(max_age=31536000)
|
2015-07-06 19:00:07 +00:00
|
|
|
def download_blob(namespace, repo_name, digest):
|
2015-08-12 20:39:32 +00:00
|
|
|
found, headers = _base_blob_fetch(namespace, repo_name, digest)
|
|
|
|
|
2015-11-16 18:51:44 +00:00
|
|
|
path = model.storage.get_layer_path(found)
|
|
|
|
logger.debug('Looking up the direct download URL for path: %s', path)
|
2015-08-12 20:39:32 +00:00
|
|
|
direct_download_url = storage.get_direct_download_url(found.locations, path)
|
|
|
|
|
|
|
|
if direct_download_url:
|
|
|
|
logger.debug('Returning direct download URL')
|
|
|
|
resp = redirect(direct_download_url)
|
|
|
|
resp.headers.extend(headers)
|
|
|
|
return resp
|
|
|
|
|
|
|
|
logger.debug('Streaming layer data')
|
|
|
|
|
|
|
|
# Close the database handle here for this process before we send the long download.
|
|
|
|
database.close_db_filter(None)
|
|
|
|
|
2015-12-02 06:52:58 +00:00
|
|
|
headers['Content-Length'] = found.image_size
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
return Response(storage.stream_read(found.locations, path), headers=headers)
|
|
|
|
|
|
|
|
|
2015-08-13 19:43:49 +00:00
|
|
|
def _render_range(num_uploaded_bytes, with_bytes_prefix=True):
|
|
|
|
return '{0}0-{1}'.format('bytes=' if with_bytes_prefix else '', num_uploaded_bytes - 1)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/uploads/', methods=['POST'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def start_blob_upload(namespace, repo_name):
|
2015-08-12 20:39:32 +00:00
|
|
|
location_name = storage.preferred_locations[0]
|
2015-08-26 21:08:42 +00:00
|
|
|
new_upload_uuid, upload_metadata = storage.initiate_chunked_upload(location_name)
|
|
|
|
model.blob.initiate_upload(namespace, repo_name, new_upload_uuid, location_name, upload_metadata)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
digest = request.args.get('digest', None)
|
|
|
|
if digest is None:
|
|
|
|
# The user will send the blob data in another request
|
|
|
|
accepted = make_response('', 202)
|
|
|
|
accepted.headers['Location'] = url_for('v2.upload_chunk', namespace=namespace,
|
|
|
|
repo_name=repo_name, upload_uuid=new_upload_uuid)
|
|
|
|
accepted.headers['Range'] = _render_range(0)
|
|
|
|
accepted.headers['Docker-Upload-UUID'] = new_upload_uuid
|
|
|
|
return accepted
|
|
|
|
else:
|
|
|
|
# The user plans to send us the entire body right now
|
2015-08-13 19:43:49 +00:00
|
|
|
uploaded = _upload_chunk(namespace, repo_name, new_upload_uuid)
|
2015-08-12 20:39:32 +00:00
|
|
|
uploaded.save()
|
|
|
|
|
|
|
|
return _finish_upload(namespace, repo_name, uploaded, digest)
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/uploads/<upload_uuid>', methods=['GET'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def fetch_existing_upload(namespace, repo_name, upload_uuid):
|
|
|
|
try:
|
|
|
|
found = model.blob.get_blob_upload(namespace, repo_name, upload_uuid)
|
|
|
|
except model.InvalidBlobUpload:
|
|
|
|
raise BlobUploadUnknown()
|
|
|
|
|
2015-09-29 19:02:03 +00:00
|
|
|
# Note: Docker byte ranges are exclusive so we have to add one to the byte count.
|
2015-08-12 20:39:32 +00:00
|
|
|
accepted = make_response('', 204)
|
2015-09-29 19:02:03 +00:00
|
|
|
accepted.headers['Range'] = _render_range(found.byte_count + 1)
|
2015-08-12 20:39:32 +00:00
|
|
|
accepted.headers['Docker-Upload-UUID'] = upload_uuid
|
|
|
|
return accepted
|
|
|
|
|
|
|
|
|
|
|
|
def _current_request_path():
|
|
|
|
return '{0}{1}'.format(request.script_root, request.path)
|
|
|
|
|
|
|
|
|
|
|
|
def _range_not_satisfiable(valid_end):
|
|
|
|
invalid_range = make_response('', 416)
|
|
|
|
invalid_range.headers['Location'] = _current_request_path()
|
|
|
|
invalid_range.headers['Range'] = '0-{0}'.format(valid_end)
|
|
|
|
invalid_range.headers['Docker-Upload-UUID'] = request.view_args['upload_uuid']
|
|
|
|
flask_abort(invalid_range)
|
|
|
|
|
|
|
|
|
2015-09-25 15:51:50 +00:00
|
|
|
def _parse_range_header(range_header_text):
|
2015-08-12 20:39:32 +00:00
|
|
|
""" Parses the range header, and returns a tuple of the start offset and the length,
|
|
|
|
or raises an _InvalidRangeHeader exception.
|
|
|
|
"""
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2015-08-13 19:43:49 +00:00
|
|
|
def _upload_chunk(namespace, repo_name, upload_uuid):
|
2015-08-12 20:39:32 +00:00
|
|
|
""" Common code among the various uploading paths for appending data to blobs.
|
|
|
|
Callers MUST call .save() or .delete_instance() on the returned database object.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
found = model.blob.get_blob_upload(namespace, repo_name, upload_uuid)
|
|
|
|
except model.InvalidBlobUpload:
|
|
|
|
raise BlobUploadUnknown()
|
|
|
|
|
|
|
|
start_offset, length = 0, -1
|
|
|
|
range_header = request.headers.get('range', None)
|
|
|
|
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:
|
|
|
|
_range_not_satisfiable(found.byte_count)
|
|
|
|
|
2015-09-02 21:31:44 +00:00
|
|
|
if start_offset > 0 and start_offset > found.byte_count:
|
|
|
|
_range_not_satisfiable(found.byte_count)
|
|
|
|
|
2015-08-25 19:53:13 +00:00
|
|
|
input_fp = get_input_stream(request)
|
2015-09-25 15:51:50 +00:00
|
|
|
|
2015-09-02 21:31:44 +00:00
|
|
|
if start_offset > 0 and start_offset < found.byte_count:
|
|
|
|
# Skip the bytes which were received on a previous push, which are already stored and
|
|
|
|
# included in the sha calculation
|
|
|
|
input_fp = StreamSlice(input_fp, found.byte_count - start_offset)
|
2015-09-25 15:51:50 +00:00
|
|
|
start_offset = found.byte_count
|
2015-09-02 21:31:44 +00:00
|
|
|
|
2015-08-25 19:53:13 +00:00
|
|
|
input_fp = wrap_with_handler(input_fp, found.sha_state.update)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
try:
|
2015-08-26 21:08:42 +00:00
|
|
|
length_written, new_metadata = storage.stream_upload_chunk({found.location.name}, upload_uuid,
|
|
|
|
start_offset, length, input_fp,
|
|
|
|
found.storage_metadata)
|
2015-08-12 20:39:32 +00:00
|
|
|
except InvalidChunkException:
|
|
|
|
_range_not_satisfiable(found.byte_count)
|
|
|
|
|
2015-08-26 21:08:42 +00:00
|
|
|
found.storage_metadata = new_metadata
|
2015-08-13 19:35:37 +00:00
|
|
|
found.byte_count += length_written
|
2015-08-12 20:39:32 +00:00
|
|
|
return found
|
|
|
|
|
|
|
|
|
|
|
|
def _finish_upload(namespace, repo_name, upload_obj, expected_digest):
|
2015-08-14 16:51:04 +00:00
|
|
|
# Verify that the digest's SHA matches that of the uploaded data.
|
2015-08-12 20:39:32 +00:00
|
|
|
computed_digest = digest_tools.sha256_digest_from_hashlib(upload_obj.sha_state)
|
|
|
|
if not digest_tools.digests_equal(computed_digest, expected_digest):
|
|
|
|
raise BlobUploadInvalid()
|
|
|
|
|
|
|
|
final_blob_location = digest_tools.content_path(expected_digest)
|
2015-09-02 21:31:44 +00:00
|
|
|
|
2015-12-03 16:19:39 +00:00
|
|
|
# Move the storage into place, or if this was a re-upload, cancel it
|
|
|
|
with database.CloseForLongOperation(app.config):
|
|
|
|
if storage.exists({upload_obj.location.name}, final_blob_location):
|
|
|
|
# It already existed, clean up our upload which served as proof that we had the file
|
|
|
|
storage.cancel_chunked_upload({upload_obj.location.name}, upload_obj.uuid,
|
|
|
|
upload_obj.storage_metadata)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# We were the first ones to upload this image (at least to this location)
|
|
|
|
# Let's copy it into place
|
|
|
|
storage.complete_chunked_upload({upload_obj.location.name}, upload_obj.uuid,
|
|
|
|
final_blob_location, upload_obj.storage_metadata)
|
2015-09-02 21:31:44 +00:00
|
|
|
|
|
|
|
# Mark the blob as uploaded.
|
2015-08-12 20:39:32 +00:00
|
|
|
model.blob.store_blob_record_and_temp_link(namespace, repo_name, expected_digest,
|
2015-08-14 16:51:04 +00:00
|
|
|
upload_obj.location, upload_obj.byte_count,
|
2015-08-12 20:39:32 +00:00
|
|
|
app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
|
2015-08-14 16:51:04 +00:00
|
|
|
|
|
|
|
# Delete the upload tracking row.
|
2015-08-12 20:39:32 +00:00
|
|
|
upload_obj.delete_instance()
|
|
|
|
|
|
|
|
response = make_response('', 201)
|
|
|
|
response.headers['Docker-Content-Digest'] = expected_digest
|
|
|
|
response.headers['Location'] = url_for('v2.download_blob', namespace=namespace,
|
|
|
|
repo_name=repo_name, digest=expected_digest)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/uploads/<upload_uuid>', methods=['PATCH'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def upload_chunk(namespace, repo_name, upload_uuid):
|
2015-08-13 19:43:49 +00:00
|
|
|
upload = _upload_chunk(namespace, repo_name, upload_uuid)
|
2015-08-12 20:39:32 +00:00
|
|
|
upload.save()
|
|
|
|
|
2015-08-13 19:35:37 +00:00
|
|
|
accepted = make_response('', 204)
|
2015-08-12 20:39:32 +00:00
|
|
|
accepted.headers['Location'] = _current_request_path()
|
2015-08-13 19:35:37 +00:00
|
|
|
accepted.headers['Range'] = _render_range(upload.byte_count, with_bytes_prefix=False)
|
2015-08-12 20:39:32 +00:00
|
|
|
accepted.headers['Docker-Upload-UUID'] = upload_uuid
|
2015-07-06 19:00:07 +00:00
|
|
|
return accepted
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/uploads/<upload_uuid>', methods=['PUT'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
def monolithic_upload_or_last_chunk(namespace, repo_name, upload_uuid):
|
2015-07-06 19:00:07 +00:00
|
|
|
digest = request.args.get('digest', None)
|
2015-08-12 20:39:32 +00:00
|
|
|
if digest is None:
|
|
|
|
raise BlobUploadInvalid()
|
|
|
|
|
2015-08-13 19:43:49 +00:00
|
|
|
found = _upload_chunk(namespace, repo_name, upload_uuid)
|
2015-08-12 20:39:32 +00:00
|
|
|
return _finish_upload(namespace, repo_name, found, digest)
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/uploads/<upload_uuid>', methods=['DELETE'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def cancel_upload(namespace, repo_name, upload_uuid):
|
|
|
|
try:
|
|
|
|
found = model.blob.get_blob_upload(namespace, repo_name, upload_uuid)
|
|
|
|
except model.InvalidBlobUpload:
|
|
|
|
raise BlobUploadUnknown()
|
|
|
|
|
|
|
|
# We delete the record for the upload first, since if the partial upload in
|
|
|
|
# storage fails to delete, it doesn't break anything
|
|
|
|
found.delete_instance()
|
2015-08-26 21:08:42 +00:00
|
|
|
storage.cancel_chunked_upload({found.location.name}, found.uuid, found.storage_metadata)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
return make_response('', 204)
|
2015-09-29 19:02:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route('/<namespace>/<repo_name>/blobs/<digest>', methods=['DELETE'])
|
|
|
|
@process_jwt_auth
|
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_digest(namespace, repo_name, upload_uuid):
|
|
|
|
# We do not support deleting arbitrary digests, as they break repo images.
|
2015-10-02 18:01:12 +00:00
|
|
|
raise Unsupported()
|
2015-09-29 19:02:03 +00:00
|
|
|
|
|
|
|
|