2013-09-25 21:50:03 +00:00
|
|
|
import logging
|
2013-09-30 20:14:48 +00:00
|
|
|
import json
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
from datetime import datetime
|
|
|
|
from time import time
|
|
|
|
|
2016-07-06 18:10:02 +00:00
|
|
|
from flask import make_response, request, session, Response, redirect, abort as flask_abort
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
from app import storage as store, app
|
2015-12-09 21:10:39 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2017-03-16 20:50:09 +00:00
|
|
|
from auth.decorators import extract_namespace_repo_from_session, process_auth
|
2017-06-29 17:24:00 +00:00
|
|
|
from auth.permissions import (ReadRepositoryPermission, ModifyRepositoryPermission)
|
2018-09-24 22:57:27 +00:00
|
|
|
from data import database
|
2018-09-14 21:14:40 +00:00
|
|
|
from data.registry_model import registry_model
|
|
|
|
from data.registry_model.blobuploader import upload_blob, BlobUploadSettings, BlobUploadException
|
2018-09-24 22:57:27 +00:00
|
|
|
from data.registry_model.manifestbuilder import lookup_manifest_builder
|
2016-07-06 18:10:02 +00:00
|
|
|
from digest import checksums
|
2019-03-05 21:50:56 +00:00
|
|
|
from endpoints.v1 import v1_bp, check_v1_push_enabled
|
2018-05-22 22:36:04 +00:00
|
|
|
from endpoints.v1.index import ensure_namespace_enabled
|
2018-12-05 20:19:37 +00:00
|
|
|
from endpoints.decorators import anon_protect, check_region_blacklisted
|
2016-07-06 18:10:02 +00:00
|
|
|
from util.http import abort, exact_abort
|
2016-05-31 20:43:49 +00:00
|
|
|
from util.registry.replication import queue_storage_replication
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
def require_completion(f):
|
2018-09-24 22:57:27 +00:00
|
|
|
""" This make sure that the image push correctly finished. """
|
2013-09-25 21:50:03 +00:00
|
|
|
@wraps(f)
|
2013-09-26 00:00:22 +00:00
|
|
|
def wrapper(namespace, repository, *args, **kwargs):
|
2014-02-16 22:38:47 +00:00
|
|
|
image_id = kwargs['image_id']
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repository_ref is not None:
|
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id)
|
|
|
|
if legacy_image is not None and legacy_image.uploading:
|
|
|
|
abort(400, 'Image %(image_id)s is being uploaded, retry later', issue='upload-in-progress',
|
|
|
|
image_id=image_id)
|
2017-06-29 17:24:00 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
return f(namespace, repository, *args, **kwargs)
|
2013-09-25 21:50:03 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def set_cache_headers(f):
|
|
|
|
"""Returns HTTP headers suitable for caching."""
|
2017-06-29 17:24:00 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
@wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
# Set TTL to 1 year by default
|
|
|
|
ttl = 31536000
|
|
|
|
expires = datetime.fromtimestamp(int(time()) + ttl)
|
|
|
|
expires = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')
|
|
|
|
headers = {
|
|
|
|
'Cache-Control': 'public, max-age={0}'.format(ttl),
|
|
|
|
'Expires': expires,
|
2017-06-29 17:24:00 +00:00
|
|
|
'Last-Modified': 'Thu, 01 Jan 1970 00:00:00 GMT',}
|
2013-09-25 21:50:03 +00:00
|
|
|
if 'If-Modified-Since' in request.headers:
|
|
|
|
response = make_response('Not modified', 304)
|
|
|
|
response.headers.extend(headers)
|
|
|
|
return response
|
|
|
|
kwargs['headers'] = headers
|
|
|
|
# Prevent the Cookie to be sent when the object is cacheable
|
|
|
|
session.modified = False
|
|
|
|
return f(*args, **kwargs)
|
2017-06-29 17:24:00 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/layer', methods=['HEAD'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2013-09-25 21:50:03 +00:00
|
|
|
@require_completion
|
|
|
|
@set_cache_headers
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2014-07-03 01:18:46 +00:00
|
|
|
def head_image_layer(namespace, repository, image_id, headers):
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2018-09-24 22:57:27 +00:00
|
|
|
if permission.can() or (repository_ref is not None and repository_ref.is_public):
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(404)
|
2017-03-22 18:30:33 +00:00
|
|
|
|
2016-07-12 17:48:44 +00:00
|
|
|
logger.debug('Looking up placement locations')
|
2018-09-24 22:57:27 +00:00
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id, include_blob=True)
|
|
|
|
if legacy_image is None:
|
2016-07-11 22:51:28 +00:00
|
|
|
logger.debug('Could not find any blob placement locations')
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2014-09-09 19:54:03 +00:00
|
|
|
# Add the Accept-Ranges header if the storage engine supports resumable
|
2014-07-07 20:21:27 +00:00
|
|
|
# downloads.
|
2016-07-11 22:51:28 +00:00
|
|
|
extra_headers = {}
|
2018-09-24 22:57:27 +00:00
|
|
|
if store.get_supports_resumable_downloads(legacy_image.blob.placements):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Storage supports resumable downloads')
|
2014-07-07 22:19:52 +00:00
|
|
|
extra_headers['Accept-Ranges'] = 'bytes'
|
2014-07-03 01:18:46 +00:00
|
|
|
|
2014-07-07 20:21:27 +00:00
|
|
|
resp = make_response('')
|
2014-07-07 22:19:52 +00:00
|
|
|
resp.headers.extend(headers)
|
2014-07-07 20:21:27 +00:00
|
|
|
resp.headers.extend(extra_headers)
|
|
|
|
return resp
|
2014-07-07 22:19:52 +00:00
|
|
|
|
2014-07-03 01:18:46 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/layer', methods=['GET'])
|
2014-07-03 01:18:46 +00:00
|
|
|
@process_auth
|
|
|
|
@extract_namespace_repo_from_session
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2014-07-03 01:18:46 +00:00
|
|
|
@require_completion
|
|
|
|
@set_cache_headers
|
2018-12-05 20:19:37 +00:00
|
|
|
@check_region_blacklisted()
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2014-07-03 01:18:46 +00:00
|
|
|
def get_image_layer(namespace, repository, image_id, headers):
|
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
2014-07-03 01:18:46 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2018-09-24 22:57:27 +00:00
|
|
|
if permission.can() or (repository_ref is not None and repository_ref.is_public):
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id, include_blob=True)
|
|
|
|
if legacy_image is None:
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
2018-09-24 22:57:27 +00:00
|
|
|
|
|
|
|
path = legacy_image.blob.storage_path
|
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
try:
|
2015-11-16 18:51:44 +00:00
|
|
|
logger.debug('Looking up the direct download URL for path: %s', path)
|
2018-09-24 22:57:27 +00:00
|
|
|
direct_download_url = store.get_direct_download_url(legacy_image.blob.placements, path,
|
|
|
|
request.remote_addr)
|
2014-06-11 19:37:45 +00:00
|
|
|
if direct_download_url:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Returning direct download URL')
|
2014-07-02 04:39:59 +00:00
|
|
|
resp = redirect(direct_download_url)
|
|
|
|
return resp
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2014-11-06 23:00:52 +00:00
|
|
|
# Close the database handle here for this process before we send the long download.
|
|
|
|
database.close_db_filter(None)
|
2016-07-11 22:51:28 +00:00
|
|
|
logger.debug('Streaming layer data')
|
2018-09-24 22:57:27 +00:00
|
|
|
return Response(store.stream_read(legacy_image.blob.placements, path), headers=headers)
|
2014-06-11 19:37:45 +00:00
|
|
|
except (IOError, AttributeError):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.exception('Image layer data not found')
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
2014-07-07 22:19:52 +00:00
|
|
|
|
2013-09-28 04:05:32 +00:00
|
|
|
abort(403)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/layer', methods=['PUT'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2019-03-05 21:50:56 +00:00
|
|
|
@check_v1_push_enabled()
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2015-06-02 19:56:44 +00:00
|
|
|
@anon_protect
|
2013-09-26 00:00:22 +00:00
|
|
|
def put_image_layer(namespace, repository, image_id):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if not permission.can():
|
|
|
|
abort(403)
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(403)
|
2017-03-22 18:30:33 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
logger.debug('Checking for image in manifest builder')
|
2018-11-12 21:27:49 +00:00
|
|
|
builder = lookup_manifest_builder(repository_ref, session.get('manifest_builder'), store)
|
2018-09-24 22:57:27 +00:00
|
|
|
if builder is None:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
layer = builder.lookup_layer(image_id)
|
|
|
|
if layer is None:
|
2016-09-01 23:00:11 +00:00
|
|
|
abort(404)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Storing layer data')
|
2013-09-25 21:50:03 +00:00
|
|
|
input_stream = request.stream
|
|
|
|
if request.headers.get('transfer-encoding') == 'chunked':
|
|
|
|
# Careful, might work only with WSGI servers supporting chunked
|
|
|
|
# encoding (Gunicorn)
|
|
|
|
input_stream = request.environ['wsgi.input']
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2018-09-14 21:14:40 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
extra_handlers = []
|
2014-09-29 21:00:47 +00:00
|
|
|
|
2014-10-20 17:11:33 +00:00
|
|
|
# Add a handler that copies the data into a temp file. This is used to calculate the tarsum,
|
|
|
|
# which is only needed for older versions of Docker.
|
2018-09-24 22:57:27 +00:00
|
|
|
requires_tarsum = bool(builder.get_layer_checksums(layer))
|
2014-10-20 17:11:33 +00:00
|
|
|
if requires_tarsum:
|
|
|
|
tmp, tmp_hndlr = store.temp_store_handler()
|
2018-09-14 21:14:40 +00:00
|
|
|
extra_handlers.append(tmp_hndlr)
|
2014-09-29 21:00:47 +00:00
|
|
|
|
2018-09-14 21:14:40 +00:00
|
|
|
# Add a handler which computes the simple Docker V1 checksum.
|
2018-09-24 22:57:27 +00:00
|
|
|
h, sum_hndlr = checksums.simple_checksum_handler(layer.v1_metadata_string)
|
2018-09-14 21:14:40 +00:00
|
|
|
extra_handlers.append(sum_hndlr)
|
2014-09-29 21:00:47 +00:00
|
|
|
|
2018-09-14 21:14:40 +00:00
|
|
|
uploaded_blob = None
|
|
|
|
try:
|
|
|
|
with upload_blob(repository_ref, store, settings,
|
|
|
|
extra_blob_stream_handlers=extra_handlers) as manager:
|
|
|
|
manager.upload_chunk(app.config, input_stream)
|
|
|
|
uploaded_blob = manager.commit_to_blob(app.config)
|
|
|
|
except BlobUploadException:
|
|
|
|
logger.exception('Exception when writing image data')
|
|
|
|
abort(520, 'Image %(image_id)s could not be written. Please try again.', image_id=image_id)
|
2015-11-04 21:18:53 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
# Compute the final checksum
|
2014-09-29 21:00:47 +00:00
|
|
|
csums = []
|
2013-09-25 21:50:03 +00:00
|
|
|
csums.append('sha256:{0}'.format(h.hexdigest()))
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
try:
|
2014-10-20 17:11:33 +00:00
|
|
|
if requires_tarsum:
|
|
|
|
tmp.seek(0)
|
2018-09-24 22:57:27 +00:00
|
|
|
csums.append(checksums.compute_tarsum(tmp, layer.v1_metadata_string))
|
2014-10-20 17:11:33 +00:00
|
|
|
tmp.close()
|
2015-07-15 21:25:41 +00:00
|
|
|
except (IOError, checksums.TarError) as exc:
|
|
|
|
logger.debug('put_image_layer: Error when computing tarsum %s', exc)
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
# If there was already a precomputed checksum, validate against it now.
|
|
|
|
if builder.get_layer_checksums(layer):
|
|
|
|
checksum = builder.get_layer_checksums(layer)[0]
|
|
|
|
if not builder.validate_layer_checksum(layer, checksum):
|
|
|
|
logger.debug('put_image_checksum: Wrong checksum. Given: %s and expected: %s', checksum,
|
|
|
|
builder.get_layer_checksums(layer))
|
|
|
|
abort(400, 'Checksum mismatch for image: %(image_id)s', issue='checksum-mismatch',
|
|
|
|
image_id=image_id)
|
|
|
|
|
|
|
|
# Assign the blob to the layer in the manifest.
|
|
|
|
if not builder.assign_layer_blob(layer, uploaded_blob, csums):
|
|
|
|
abort(500, 'Something went wrong')
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
# Send a job to the work queue to replicate the image layer.
|
|
|
|
# TODO: move this into a better place.
|
|
|
|
queue_storage_replication(namespace, uploaded_blob)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('true', 200)
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/checksum', methods=['PUT'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2019-03-05 21:50:56 +00:00
|
|
|
@check_v1_push_enabled()
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2015-06-02 19:56:44 +00:00
|
|
|
@anon_protect
|
2013-09-26 00:00:22 +00:00
|
|
|
def put_image_checksum(namespace, repository, image_id):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if not permission.can():
|
|
|
|
abort(403)
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(403)
|
2017-03-22 18:30:33 +00:00
|
|
|
|
2014-10-20 17:11:33 +00:00
|
|
|
# Docker Version < 0.10 (tarsum+sha):
|
|
|
|
old_checksum = request.headers.get('X-Docker-Checksum')
|
|
|
|
|
|
|
|
# Docker Version >= 0.10 (sha):
|
|
|
|
new_checksum = request.headers.get('X-Docker-Checksum-Payload')
|
|
|
|
|
|
|
|
checksum = new_checksum or old_checksum
|
2013-09-25 21:50:03 +00:00
|
|
|
if not checksum:
|
2014-06-11 19:37:45 +00:00
|
|
|
abort(400, "Missing checksum for image %(image_id)s", issue='missing-checksum',
|
|
|
|
image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
logger.debug('Checking for image in manifest builder')
|
2018-11-12 21:27:49 +00:00
|
|
|
builder = lookup_manifest_builder(repository_ref, session.get('manifest_builder'), store)
|
2018-09-24 22:57:27 +00:00
|
|
|
if builder is None:
|
|
|
|
abort(400)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
layer = builder.lookup_layer(image_id)
|
|
|
|
if layer is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if old_checksum:
|
|
|
|
builder.save_precomputed_checksum(layer, checksum)
|
|
|
|
return make_response('true', 200)
|
|
|
|
|
|
|
|
if not builder.validate_layer_checksum(layer, checksum):
|
|
|
|
logger.debug('put_image_checksum: Wrong checksum. Given: %s and expected: %s', checksum,
|
|
|
|
builder.get_layer_checksums(layer))
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(400, 'Checksum mismatch for image: %(image_id)s', issue='checksum-mismatch',
|
|
|
|
image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('true', 200)
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/json', methods=['GET'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2013-09-25 21:50:03 +00:00
|
|
|
@require_completion
|
|
|
|
@set_cache_headers
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2013-09-26 00:00:22 +00:00
|
|
|
def get_image_json(namespace, repository, image_id, headers):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
|
|
|
if not permission.can() and not (repository_ref is not None and repository_ref.is_public):
|
2013-09-26 17:42:24 +00:00
|
|
|
abort(403)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2018-09-24 22:57:27 +00:00
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id, include_blob=True)
|
|
|
|
if legacy_image is None:
|
2014-02-26 21:03:00 +00:00
|
|
|
flask_abort(404)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
size = legacy_image.blob.compressed_size
|
2015-08-12 20:39:32 +00:00
|
|
|
if size is not None:
|
2015-10-14 18:11:18 +00:00
|
|
|
# Note: X-Docker-Size is optional and we *can* end up with a NULL image_size,
|
|
|
|
# so handle this case rather than failing.
|
2015-08-12 20:39:32 +00:00
|
|
|
headers['X-Docker-Size'] = str(size)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
response = make_response(legacy_image.v1_metadata_string, 200)
|
2013-09-25 21:50:03 +00:00
|
|
|
response.headers.extend(headers)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/ancestry', methods=['GET'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2013-09-25 21:50:03 +00:00
|
|
|
@require_completion
|
|
|
|
@set_cache_headers
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2013-09-26 00:00:22 +00:00
|
|
|
def get_image_ancestry(namespace, repository, image_id, headers):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
|
|
|
if not permission.can() and not (repository_ref is not None and repository_ref.is_public):
|
2013-09-26 17:42:24 +00:00
|
|
|
abort(403)
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
logger.debug('Looking up repo image')
|
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id, include_parents=True)
|
|
|
|
if legacy_image is None:
|
2015-08-24 15:58:43 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
# NOTE: We can not use jsonify here because we are returning a list not an object.
|
|
|
|
ancestor_ids = ([legacy_image.docker_image_id] +
|
|
|
|
[a.docker_image_id for a in legacy_image.parents])
|
|
|
|
response = make_response(json.dumps(ancestor_ids), 200)
|
2013-09-25 21:50:03 +00:00
|
|
|
response.headers.extend(headers)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v1_bp.route('/images/<image_id>/json', methods=['PUT'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2013-09-26 00:00:22 +00:00
|
|
|
@extract_namespace_repo_from_session
|
2019-03-05 21:50:56 +00:00
|
|
|
@check_v1_push_enabled()
|
2018-05-22 22:36:04 +00:00
|
|
|
@ensure_namespace_enabled
|
2015-06-02 19:56:44 +00:00
|
|
|
@anon_protect
|
2013-09-26 00:00:22 +00:00
|
|
|
def put_image_json(namespace, repository, image_id):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2013-09-26 17:42:24 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if not permission.can():
|
|
|
|
abort(403)
|
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace, repository, kind_filter='image')
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(403)
|
|
|
|
|
2018-11-12 21:27:49 +00:00
|
|
|
builder = lookup_manifest_builder(repository_ref, session.get('manifest_builder'), store)
|
2018-09-24 22:57:27 +00:00
|
|
|
if builder is None:
|
|
|
|
abort(400)
|
2017-03-22 18:30:33 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Parsing image JSON')
|
2013-09-25 21:50:03 +00:00
|
|
|
try:
|
2016-07-11 22:51:28 +00:00
|
|
|
uploaded_metadata = request.data
|
|
|
|
data = json.loads(uploaded_metadata.decode('utf8'))
|
2014-06-11 19:37:45 +00:00
|
|
|
except ValueError:
|
2013-09-25 21:50:03 +00:00
|
|
|
pass
|
2014-06-11 19:37:45 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
if not data or not isinstance(data, dict):
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(400, 'Invalid JSON for image: %(image_id)s\nJSON: %(json)s', issue='invalid-request',
|
|
|
|
image_id=image_id, json=request.data)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
if 'id' not in data:
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(400, 'Missing key `id` in JSON for image: %(image_id)s', issue='invalid-request',
|
|
|
|
image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2016-07-11 22:51:28 +00:00
|
|
|
if image_id != data['id']:
|
2017-06-29 17:24:00 +00:00
|
|
|
abort(400, 'JSON data contains invalid id for image: %(image_id)s', issue='invalid-request',
|
|
|
|
image_id=image_id)
|
2016-07-11 22:51:28 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2018-09-24 22:57:27 +00:00
|
|
|
location_pref = store.preferred_locations[0]
|
|
|
|
username = get_authenticated_user() and get_authenticated_user().username
|
|
|
|
layer = builder.start_layer(image_id, uploaded_metadata, location_pref, username,
|
|
|
|
app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
|
|
|
|
if layer is None:
|
|
|
|
abort(400, 'Image %(image_id)s has invalid metadata',
|
|
|
|
issue='invalid-request', image_id=image_id)
|
2014-08-18 17:35:03 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('true', 200)
|