2013-09-25 21:50:03 +00:00
|
|
|
import logging
|
2013-09-30 20:14:48 +00:00
|
|
|
import json
|
2015-06-28 10:29:22 +00:00
|
|
|
import features
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import make_response, request, session, Response, redirect, abort as flask_abort
|
2013-09-25 21:50:03 +00:00
|
|
|
from functools import wraps
|
|
|
|
from datetime import datetime
|
|
|
|
from time import time
|
|
|
|
|
2015-11-13 17:42:12 +00:00
|
|
|
from app import storage as store, image_replication_queue, app
|
2014-01-24 20:01:40 +00:00
|
|
|
from auth.auth import process_auth, extract_namespace_repo_from_session
|
2015-12-09 21:10:39 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2015-12-09 20:07:37 +00:00
|
|
|
from auth.registry_jwt_auth import get_granted_username
|
2015-07-06 19:00:07 +00:00
|
|
|
from digest import checksums
|
2014-06-11 20:55:38 +00:00
|
|
|
from util.http import abort, exact_abort
|
2015-08-12 20:39:32 +00:00
|
|
|
from util.registry.filelike import SocketReader
|
2013-09-26 17:42:24 +00:00
|
|
|
from auth.permissions import (ReadRepositoryPermission,
|
|
|
|
ModifyRepositoryPermission)
|
2014-11-06 23:00:52 +00:00
|
|
|
from data import model, database
|
2015-08-03 19:49:10 +00:00
|
|
|
from util.registry import gzipstream
|
2016-01-05 17:14:52 +00:00
|
|
|
from util.registry.torrent import PieceHasher
|
2015-06-22 21:37:13 +00:00
|
|
|
from endpoints.v1 import v1_bp
|
2015-05-19 21:52:44 +00:00
|
|
|
from endpoints.decorators import anon_protect
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
|
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
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
def image_is_uploading(repo_image):
|
|
|
|
if repo_image is None:
|
|
|
|
return False
|
|
|
|
|
2014-06-12 23:27:43 +00:00
|
|
|
return repo_image.storage.uploading
|
2014-05-02 20:59:46 +00:00
|
|
|
|
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
def set_uploading_flag(repo_image, is_image_uploading):
|
|
|
|
repo_image.storage.uploading = is_image_uploading
|
|
|
|
repo_image.storage.save()
|
|
|
|
|
2014-05-02 20:59:46 +00:00
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
def _finish_image(namespace, repository, repo_image):
|
|
|
|
# Checksum is ok, we remove the marker
|
|
|
|
set_uploading_flag(repo_image, False)
|
|
|
|
|
|
|
|
# Send a job to the work queue to replicate the image layer.
|
|
|
|
if features.STORAGE_REPLICATION:
|
2015-11-13 17:42:12 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-06-28 10:29:22 +00:00
|
|
|
image_replication_queue.put([repo_image.storage.uuid], json.dumps({
|
|
|
|
'namespace_user_id': repo.namespace_user.id,
|
|
|
|
'storage_id': repo_image.storage.uuid,
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
def require_completion(f):
|
|
|
|
"""This make sure that the image push correctly finished."""
|
|
|
|
@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']
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-06-11 19:37:45 +00:00
|
|
|
if image_is_uploading(repo_image):
|
2014-01-24 22:00:42 +00:00
|
|
|
abort(400, 'Image %(image_id)s is being uploaded, retry later',
|
2014-01-28 23:29:45 +00:00
|
|
|
issue='upload-in-progress', image_id=kwargs['image_id'])
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2013-09-26 00:00:22 +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."""
|
|
|
|
@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,
|
|
|
|
'Last-Modified': 'Thu, 01 Jan 1970 00:00:00 GMT',
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
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
|
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)
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2015-07-15 21:25:41 +00:00
|
|
|
if permission.can() or model.repository.repository_is_public(namespace, repository):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-07-07 20:21:27 +00:00
|
|
|
if not repo_image:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Image not found')
|
2014-07-07 20:21:27 +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-07-02 04:39:59 +00:00
|
|
|
extra_headers = {}
|
|
|
|
|
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.
|
2014-09-09 19:54:03 +00:00
|
|
|
if store.get_supports_resumable_downloads(repo_image.storage.locations):
|
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
|
|
|
|
@require_completion
|
|
|
|
@set_cache_headers
|
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)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking repo permissions')
|
2015-07-15 21:25:41 +00:00
|
|
|
if permission.can() or model.repository.repository_is_public(namespace, repository):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2015-01-28 17:39:00 +00:00
|
|
|
if not repo_image:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Image not found')
|
2015-01-28 17:39:00 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image',
|
|
|
|
image_id=image_id)
|
2014-07-02 04:39:59 +00:00
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
try:
|
2015-11-16 18:51:44 +00:00
|
|
|
path = model.storage.get_layer_path(repo_image.storage)
|
|
|
|
logger.debug('Looking up the direct download URL for path: %s', path)
|
2014-06-17 20:03:43 +00:00
|
|
|
direct_download_url = store.get_direct_download_url(repo_image.storage.locations, path)
|
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
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Streaming layer data')
|
2014-11-05 17:27:38 +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)
|
|
|
|
|
2014-07-07 22:19:52 +00:00
|
|
|
return Response(store.stream_read(repo_image.storage.locations, 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')
|
2014-02-16 22:38:47 +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
|
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)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Retrieving image')
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2013-09-25 21:50:03 +00:00
|
|
|
try:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Retrieving image data')
|
2014-06-11 19:37:45 +00:00
|
|
|
uuid = repo_image.storage.uuid
|
2015-10-21 19:29:57 +00:00
|
|
|
json_data = repo_image.v1_json_metadata
|
|
|
|
except (AttributeError):
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.exception('Exception when retrieving image data')
|
2015-10-21 19:29:57 +00:00
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2015-08-13 21:14:17 +00:00
|
|
|
uuid = repo_image.storage.uuid
|
2015-08-12 20:39:32 +00:00
|
|
|
layer_path = store.v1_image_layer_path(uuid)
|
|
|
|
logger.info('Storing layer at v1 path: %s', layer_path)
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2014-06-17 20:03:43 +00:00
|
|
|
if (store.exists(repo_image.storage.locations, layer_path) and not
|
2014-06-11 19:37:45 +00:00
|
|
|
image_is_uploading(repo_image)):
|
2014-06-11 20:55:38 +00:00
|
|
|
exact_abort(409, 'Image already exists')
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Storing layer data')
|
2014-05-02 01:19:52 +00:00
|
|
|
|
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
|
|
|
|
2014-09-29 21:00:47 +00:00
|
|
|
# Create a socket reader to read the input stream containing the layer data.
|
2013-09-25 21:50:03 +00:00
|
|
|
sr = SocketReader(input_stream)
|
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.
|
|
|
|
requires_tarsum = session.get('checksum_format') == 'tarsum'
|
|
|
|
if requires_tarsum:
|
|
|
|
tmp, tmp_hndlr = store.temp_store_handler()
|
|
|
|
sr.add_handler(tmp_hndlr)
|
|
|
|
|
|
|
|
# Add a handler to compute the compressed and uncompressed sizes of the layer.
|
|
|
|
size_info, size_hndlr = gzipstream.calculate_size_handler()
|
2014-09-29 21:00:47 +00:00
|
|
|
sr.add_handler(size_hndlr)
|
|
|
|
|
2016-01-05 17:14:52 +00:00
|
|
|
# Add a handler to hash the chunks of the upload for torrenting
|
|
|
|
piece_hasher = PieceHasher(app.config['TORRENT_PIECE_SIZE'])
|
|
|
|
sr.add_handler(piece_hasher.update)
|
|
|
|
|
2014-09-29 21:00:47 +00:00
|
|
|
# Add a handler which computes the checksum.
|
2013-09-25 21:50:03 +00:00
|
|
|
h, sum_hndlr = checksums.simple_checksum_handler(json_data)
|
|
|
|
sr.add_handler(sum_hndlr)
|
2014-09-29 21:00:47 +00:00
|
|
|
|
2015-11-04 21:18:53 +00:00
|
|
|
# Add a handler which computes the content checksum only
|
|
|
|
ch, content_sum_hndlr = checksums.content_checksum_handler()
|
|
|
|
sr.add_handler(content_sum_hndlr)
|
|
|
|
|
2014-09-29 21:00:47 +00:00
|
|
|
# Stream write the data to storage.
|
2014-11-06 23:00:52 +00:00
|
|
|
with database.CloseForLongOperation(app.config):
|
2015-09-01 19:53:32 +00:00
|
|
|
try:
|
|
|
|
store.stream_write(repo_image.storage.locations, layer_path, sr)
|
|
|
|
except IOError:
|
|
|
|
logger.exception('Exception when writing image data')
|
|
|
|
abort(520, 'Image %(image_id)s could not be written. Please try again.', image_id=image_id)
|
2014-09-29 21:00:47 +00:00
|
|
|
|
2015-10-14 18:11:18 +00:00
|
|
|
# Save the size of the image.
|
2016-01-05 17:14:52 +00:00
|
|
|
updated_storage = model.storage.set_image_storage_metadata(image_id, namespace, repository,
|
|
|
|
size_info.compressed_size,
|
|
|
|
size_info.uncompressed_size)
|
|
|
|
pieces_bytes = piece_hasher.piece_hashes + piece_hasher.hash_fragment.digest()
|
|
|
|
model.storage.save_torrent_info(updated_storage, app.config['TORRENT_PIECE_SIZE'], pieces_bytes)
|
2015-10-14 18:11:18 +00:00
|
|
|
|
2014-09-29 21:00:47 +00:00
|
|
|
# Append the computed checksum.
|
|
|
|
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)
|
|
|
|
csums.append(checksums.compute_tarsum(tmp, json_data))
|
|
|
|
tmp.close()
|
2014-01-03 21:42:38 +00:00
|
|
|
|
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
|
|
|
|
2015-11-06 21:08:14 +00:00
|
|
|
if repo_image.v1_checksum is None:
|
2013-09-25 21:50:03 +00:00
|
|
|
# We don't have a checksum stored yet, that's fine skipping the check.
|
|
|
|
# Not removing the mark though, image is not downloadable yet.
|
|
|
|
session['checksum'] = csums
|
2015-11-04 21:18:53 +00:00
|
|
|
session['content_checksum'] = 'sha256:{0}'.format(ch.hexdigest())
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('true', 200)
|
2014-01-28 23:29:45 +00:00
|
|
|
|
2015-11-06 21:08:14 +00:00
|
|
|
checksum = repo_image.v1_checksum
|
2014-06-12 23:27:43 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
# We check if the checksums provided matches one the one we computed
|
|
|
|
if checksum not in csums:
|
2013-12-31 21:22:27 +00:00
|
|
|
logger.warning('put_image_layer: Wrong checksum')
|
2014-01-28 23:29:45 +00:00
|
|
|
abort(400, 'Checksum mismatch; ignoring the layer for image %(image_id)s',
|
|
|
|
issue='checksum-mismatch', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
# Mark the image as uploaded.
|
|
|
|
_finish_image(namespace, repository, repo_image)
|
2013-10-18 21:27:09 +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
|
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)
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
# Store whether we need to calculate the tarsum.
|
|
|
|
if new_checksum:
|
|
|
|
session['checksum_format'] = 'sha256'
|
|
|
|
else:
|
|
|
|
session['checksum_format'] = 'tarsum'
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
if not session.get('checksum'):
|
2014-05-08 15:50:38 +00:00
|
|
|
abort(400, 'Checksum not found in Cookie for image %(image_id)s',
|
2014-01-28 23:29:45 +00:00
|
|
|
issue='missing-checksum-cookie', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-10-17 21:57:10 +00:00
|
|
|
if not repo_image or not repo_image.storage:
|
|
|
|
abort(404, 'Image not found: %(image_id)s', issue='unknown-image', image_id=image_id)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo layer data')
|
2015-10-21 19:29:57 +00:00
|
|
|
if not repo_image.v1_json_metadata:
|
2014-01-28 23:29:45 +00:00
|
|
|
abort(404, 'Image not found: %(image_id)s', issue='unknown-image', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Marking image path')
|
2014-06-11 19:37:45 +00:00
|
|
|
if not image_is_uploading(repo_image):
|
2014-01-28 23:29:45 +00:00
|
|
|
abort(409, 'Cannot set checksum for image %(image_id)s',
|
|
|
|
issue='image-write-error', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-11-04 21:18:53 +00:00
|
|
|
logger.debug('Storing image and content checksums')
|
|
|
|
content_checksum = session.get('content_checksum', None)
|
|
|
|
err = store_checksum(repo_image, checksum, content_checksum)
|
2013-09-25 21:50:03 +00:00
|
|
|
if err:
|
2014-01-24 19:12:04 +00:00
|
|
|
abort(400, err)
|
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
if checksum not in session.get('checksum', []):
|
2015-07-15 21:25:41 +00:00
|
|
|
logger.debug('session checksums: %s', session.get('checksum', []))
|
|
|
|
logger.debug('client supplied checksum: %s', checksum)
|
2014-06-12 02:00:37 +00:00
|
|
|
logger.debug('put_image_checksum: Wrong checksum')
|
2014-01-28 23:29:45 +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
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
# Mark the image as uploaded.
|
|
|
|
_finish_image(namespace, repository, repo_image)
|
2013-10-18 21:27:09 +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
|
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)
|
2015-07-15 21:25:41 +00:00
|
|
|
if not permission.can() and not model.repository.repository_is_public(namespace, repository):
|
2013-09-26 17:42:24 +00:00
|
|
|
abort(403)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2015-10-21 19:29:57 +00:00
|
|
|
if repo_image is None:
|
2014-02-26 21:03:00 +00:00
|
|
|
flask_abort(404)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo layer size')
|
2014-06-12 23:51:01 +00:00
|
|
|
size = repo_image.storage.image_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
|
|
|
|
2015-10-21 19:29:57 +00:00
|
|
|
response = make_response(repo_image.v1_json_metadata, 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
|
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)
|
2015-07-15 21:25:41 +00:00
|
|
|
if not permission.can() and not model.repository.repository_is_public(namespace, repository):
|
2013-09-26 17:42:24 +00:00
|
|
|
abort(403)
|
|
|
|
|
2015-08-24 15:58:43 +00:00
|
|
|
try:
|
|
|
|
image = model.image.get_image_by_id(namespace, repository, image_id)
|
|
|
|
except model.InvalidImageException:
|
|
|
|
abort(404, 'Image %(image_id)s not found', issue='unknown-image', image_id=image_id)
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
parents = model.image.get_parent_images(namespace, repository, image)
|
2014-02-16 22:38:47 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
ancestry_docker_ids = [image.docker_image_id]
|
2015-11-06 22:47:08 +00:00
|
|
|
ancestry_docker_ids.extend([parent.docker_image_id for parent in parents])
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
# We can not use jsonify here because we are returning a list not an object
|
|
|
|
response = make_response(json.dumps(ancestry_docker_ids), 200)
|
2013-09-25 21:50:03 +00:00
|
|
|
response.headers.extend(headers)
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-11-04 21:18:53 +00:00
|
|
|
def store_checksum(image_with_storage, checksum, content_checksum):
|
2013-09-25 21:50:03 +00:00
|
|
|
checksum_parts = checksum.split(':')
|
|
|
|
if len(checksum_parts) != 2:
|
|
|
|
return 'Invalid checksum format'
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
# We store the checksum
|
2015-11-04 21:18:53 +00:00
|
|
|
image_with_storage.storage.content_checksum = content_checksum
|
|
|
|
image_with_storage.storage.save()
|
|
|
|
|
|
|
|
image_with_storage.v1_checksum = checksum
|
|
|
|
image_with_storage.save()
|
2014-06-11 19:37:45 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
|
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
|
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)
|
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Parsing image JSON')
|
2013-09-25 21:50:03 +00:00
|
|
|
try:
|
2015-08-12 20:39:32 +00:00
|
|
|
v1_metadata = request.data
|
|
|
|
data = json.loads(v1_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):
|
2014-01-24 22:00:42 +00:00
|
|
|
abort(400, 'Invalid JSON for image: %(image_id)s\nJSON: %(json)s',
|
2014-01-28 23:29:45 +00:00
|
|
|
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:
|
2014-01-24 22:00:42 +00:00
|
|
|
abort(400, 'Missing key `id` in JSON for image: %(image_id)s',
|
2014-01-28 23:29:45 +00:00
|
|
|
issue='invalid-request', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up repo image')
|
2015-02-19 21:46:01 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repository)
|
2015-02-19 21:46:01 +00:00
|
|
|
if repo is None:
|
|
|
|
abort(404, 'Repository does not exist: %(namespace)s/%(repository)s', issue='no-repo',
|
|
|
|
namespace=namespace, repository=repository)
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.get_repo_image_extended(namespace, repository, image_id)
|
2014-07-03 21:18:14 +00:00
|
|
|
if not repo_image:
|
2015-12-09 21:10:39 +00:00
|
|
|
username = get_authenticated_user() and get_authenticated_user().username
|
|
|
|
if not username:
|
|
|
|
username = get_granted_username()
|
|
|
|
|
2015-02-23 20:07:02 +00:00
|
|
|
logger.debug('Image not found, creating image with initiating user context: %s', username)
|
2015-07-15 21:25:41 +00:00
|
|
|
repo_image = model.image.find_create_or_link_image(image_id, repo, username, {},
|
|
|
|
store.preferred_locations[0])
|
2014-07-03 21:18:14 +00:00
|
|
|
|
2015-02-18 21:37:38 +00:00
|
|
|
# Create a temporary tag to prevent this image from getting garbage collected while the push
|
|
|
|
# is in progress.
|
2015-07-15 21:25:41 +00:00
|
|
|
model.tag.create_temporary_hidden_tag(repo, repo_image,
|
|
|
|
app.config['PUSH_TEMP_TAG_EXPIRATION_SEC'])
|
2015-02-18 21:37:38 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
if image_id != data['id']:
|
2014-01-24 22:00:42 +00:00
|
|
|
abort(400, 'JSON data contains invalid id for image: %(image_id)s',
|
2014-01-28 23:29:45 +00:00
|
|
|
issue='invalid-request', image_id=image_id)
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
parent_id = data.get('parent', None)
|
2014-02-16 22:38:47 +00:00
|
|
|
|
|
|
|
parent_image = None
|
|
|
|
if parent_id:
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Looking up parent image')
|
2015-07-15 21:25:41 +00:00
|
|
|
parent_image = model.image.get_repo_image_extended(namespace, repository, parent_id)
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
if not parent_image or parent_image.storage.uploading:
|
|
|
|
abort(400, 'Image %(image_id)s depends on non existing parent image %(parent_id)s',
|
|
|
|
issue='invalid-request', image_id=image_id, parent_id=parent_id)
|
2014-05-02 20:59:46 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Checking if image already exists')
|
2015-10-21 19:29:57 +00:00
|
|
|
if repo_image.v1_json_metadata and not image_is_uploading(repo_image):
|
2014-06-11 20:55:38 +00:00
|
|
|
exact_abort(409, 'Image already exists')
|
2014-01-24 19:12:04 +00:00
|
|
|
|
2014-06-11 19:37:45 +00:00
|
|
|
set_uploading_flag(repo_image, True)
|
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
# If we reach that point, it means that this is a new image or a retry
|
2015-08-12 20:39:32 +00:00
|
|
|
# on a failed push, save the metadata
|
2014-01-13 21:32:51 +00:00
|
|
|
command_list = data.get('container_config', {}).get('Cmd', None)
|
|
|
|
command = json.dumps(command_list) if command_list else None
|
2014-05-02 01:19:52 +00:00
|
|
|
|
2015-02-11 19:15:18 +00:00
|
|
|
logger.debug('Setting image metadata')
|
2015-07-15 21:25:41 +00:00
|
|
|
model.image.set_image_metadata(image_id, namespace, repository, data.get('created'),
|
2015-08-12 20:39:32 +00:00
|
|
|
data.get('comment'), command, v1_metadata, parent_image)
|
2014-08-18 17:35:03 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('true', 200)
|
2013-10-01 16:13:25 +00:00
|
|
|
|