2015-06-22 21:37:13 +00:00
|
|
|
import logging
|
|
|
|
|
2016-02-09 19:16:39 +00:00
|
|
|
from functools import wraps
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
from flask import request, url_for, Response
|
2016-03-09 21:20:28 +00:00
|
|
|
|
|
|
|
import features
|
|
|
|
|
2016-09-09 19:13:58 +00:00
|
|
|
from app import docker_v2_signing_key, app, metric_queue
|
2015-12-09 20:07:37 +00:00
|
|
|
from auth.registry_jwt_auth import process_registry_jwt_auth
|
2016-07-25 22:56:25 +00:00
|
|
|
from digest import digest_tools
|
2017-07-20 15:31:22 +00:00
|
|
|
from endpoints.decorators import anon_protect, parse_repository_name
|
2017-06-26 22:10:39 +00:00
|
|
|
from endpoints.v2 import v2_bp, require_repo_read, require_repo_write
|
|
|
|
from endpoints.v2.models_interface import Label
|
|
|
|
from endpoints.v2.models_pre_oci import data_model as model
|
2017-06-22 01:58:16 +00:00
|
|
|
from endpoints.v2.errors import (BlobUnknown, ManifestInvalid, ManifestUnknown, TagInvalid,
|
|
|
|
NameInvalid, TagExpired)
|
2017-06-19 23:03:10 +00:00
|
|
|
from endpoints.v2.labelhandlers import handle_label
|
2016-08-02 22:45:30 +00:00
|
|
|
from image.docker import ManifestException
|
|
|
|
from image.docker.schema1 import DockerSchema1Manifest, DockerSchema1ManifestBuilder
|
2017-12-20 16:02:34 +00:00
|
|
|
from image.docker.schema2 import DOCKER_SCHEMA2_CONTENT_TYPES, OCI_CONTENT_TYPES
|
2017-07-14 13:29:59 +00:00
|
|
|
from notifications import spawn_notification
|
2017-05-11 17:33:18 +00:00
|
|
|
from util.audit import track_and_log
|
2016-07-26 00:50:35 +00:00
|
|
|
from util.names import VALID_TAG_PATTERN
|
2016-12-21 17:54:50 +00:00
|
|
|
from util.registry.replication import queue_replication_batch
|
2016-09-21 02:09:25 +00:00
|
|
|
from util.validation import is_json
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
BASE_MANIFEST_ROUTE = '/<repopath:repository>/manifests/<regex("{0}"):manifest_ref>'
|
2015-08-12 20:39:32 +00:00
|
|
|
MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN)
|
|
|
|
MANIFEST_TAGNAME_ROUTE = BASE_MANIFEST_ROUTE.format(VALID_TAG_PATTERN)
|
|
|
|
|
2017-06-26 22:16:15 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_TAGNAME_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-08-12 20:39:32 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
2016-08-16 19:23:00 +00:00
|
|
|
def fetch_manifest_by_tagname(namespace_name, repo_name, manifest_ref):
|
2016-08-30 19:05:15 +00:00
|
|
|
manifest = model.get_manifest_by_tag(namespace_name, repo_name, manifest_ref)
|
2016-07-25 22:56:25 +00:00
|
|
|
if manifest is None:
|
2016-08-30 19:05:15 +00:00
|
|
|
has_tag = model.has_active_tag(namespace_name, repo_name, manifest_ref)
|
2016-08-16 19:23:00 +00:00
|
|
|
if not has_tag:
|
2017-06-22 01:58:16 +00:00
|
|
|
has_expired_tag = model.has_tag(namespace_name, repo_name, manifest_ref)
|
|
|
|
if has_expired_tag:
|
|
|
|
logger.debug('Found expired tag %s for repository %s/%s', manifest_ref, namespace_name,
|
|
|
|
repo_name)
|
|
|
|
msg = 'Tag %s was deleted or has expired. To pull, revive via time machine' % manifest_ref
|
|
|
|
raise TagExpired(msg)
|
|
|
|
else:
|
|
|
|
raise ManifestUnknown()
|
2015-12-03 21:04:17 +00:00
|
|
|
|
2016-08-16 19:23:00 +00:00
|
|
|
manifest = _generate_and_store_manifest(namespace_name, repo_name, manifest_ref)
|
2016-07-25 22:56:25 +00:00
|
|
|
if manifest is None:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2016-08-30 19:05:15 +00:00
|
|
|
repo = model.get_repository(namespace_name, repo_name)
|
2015-11-21 02:29:57 +00:00
|
|
|
if repo is not None:
|
2016-11-21 17:29:59 +00:00
|
|
|
track_and_log('pull_repo', repo, analytics_name='pull_repo_100x', analytics_sample=0.01,
|
2017-06-26 22:16:15 +00:00
|
|
|
tag=manifest_ref)
|
2016-11-03 19:28:40 +00:00
|
|
|
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2', True])
|
2015-11-21 02:29:57 +00:00
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
2016-08-16 19:23:00 +00:00
|
|
|
manifest.json,
|
2016-08-09 16:28:00 +00:00
|
|
|
status=200,
|
2017-06-26 22:16:15 +00:00
|
|
|
headers={'Content-Type': manifest.media_type,
|
|
|
|
'Docker-Content-Digest': manifest.digest},)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route(MANIFEST_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-06-22 21:37:13 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
def fetch_manifest_by_digest(namespace_name, repo_name, manifest_ref):
|
2016-08-30 19:05:15 +00:00
|
|
|
manifest = model.get_manifest_by_digest(namespace_name, repo_name, manifest_ref)
|
2016-07-25 22:56:25 +00:00
|
|
|
if manifest is None:
|
2015-08-12 20:39:32 +00:00
|
|
|
# Without a tag name to reference, we can't make an attempt to generate the manifest
|
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2016-08-30 19:05:15 +00:00
|
|
|
repo = model.get_repository(namespace_name, repo_name)
|
2015-11-21 02:29:57 +00:00
|
|
|
if repo is not None:
|
2016-11-21 17:29:59 +00:00
|
|
|
track_and_log('pull_repo', repo, manifest_digest=manifest_ref)
|
2016-11-03 19:28:40 +00:00
|
|
|
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2', True])
|
2015-11-21 02:29:57 +00:00
|
|
|
|
2017-06-26 22:16:15 +00:00
|
|
|
return Response(manifest.json, status=200, headers={
|
|
|
|
'Content-Type': manifest.media_type,
|
|
|
|
'Docker-Content-Digest': manifest.digest})
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-02-09 19:16:39 +00:00
|
|
|
def _reject_manifest2_schema2(func):
|
|
|
|
@wraps(func)
|
|
|
|
def wrapped(*args, **kwargs):
|
2017-12-20 16:02:34 +00:00
|
|
|
if request.content_type in (DOCKER_SCHEMA2_CONTENT_TYPES | OCI_CONTENT_TYPES):
|
2016-02-09 19:16:39 +00:00
|
|
|
raise ManifestInvalid(detail={'message': 'manifest schema version not supported'},
|
|
|
|
http_status_code=415)
|
|
|
|
return func(*args, **kwargs)
|
2017-06-26 22:16:15 +00:00
|
|
|
|
2016-02-09 19:16:39 +00:00
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_TAGNAME_ROUTE, methods=['PUT'])
|
2016-06-02 16:46:20 +00:00
|
|
|
@_reject_manifest2_schema2
|
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-08-12 20:39:32 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-08-16 19:23:00 +00:00
|
|
|
def write_manifest_by_tagname(namespace_name, repo_name, manifest_ref):
|
2015-10-02 18:01:12 +00:00
|
|
|
try:
|
2016-07-25 22:56:25 +00:00
|
|
|
manifest = DockerSchema1Manifest(request.data)
|
|
|
|
except ManifestException as me:
|
2016-10-03 14:25:53 +00:00
|
|
|
logger.exception("failed to parse manifest when writing by tagname")
|
2016-10-03 14:13:39 +00:00
|
|
|
raise ManifestInvalid(detail={'message': 'failed to parse manifest: %s' % me.message})
|
2015-10-02 18:01:12 +00:00
|
|
|
|
2016-08-16 19:23:00 +00:00
|
|
|
if manifest.tag != manifest_ref:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise TagInvalid()
|
|
|
|
|
2016-09-01 23:00:11 +00:00
|
|
|
return _write_manifest_and_log(namespace_name, repo_name, manifest)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['PUT'])
|
2016-06-02 16:46:20 +00:00
|
|
|
@_reject_manifest2_schema2
|
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-06-22 21:37:13 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-09-01 23:00:11 +00:00
|
|
|
def write_manifest_by_digest(namespace_name, repo_name, manifest_ref):
|
2015-10-02 18:01:12 +00:00
|
|
|
try:
|
2016-07-25 22:56:25 +00:00
|
|
|
manifest = DockerSchema1Manifest(request.data)
|
|
|
|
except ManifestException as me:
|
2016-10-03 14:25:53 +00:00
|
|
|
logger.exception("failed to parse manifest when writing by digest")
|
2016-10-03 14:13:39 +00:00
|
|
|
raise ManifestInvalid(detail={'message': 'failed to parse manifest: %s' % me.message})
|
2015-10-02 18:01:12 +00:00
|
|
|
|
2016-09-01 23:00:11 +00:00
|
|
|
if manifest.digest != manifest_ref:
|
2016-02-12 15:39:27 +00:00
|
|
|
raise ManifestInvalid(detail={'message': 'manifest digest mismatch'})
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-09-01 23:00:11 +00:00
|
|
|
return _write_manifest_and_log(namespace_name, repo_name, manifest)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
def _write_manifest(namespace_name, repo_name, manifest):
|
2017-06-26 22:16:15 +00:00
|
|
|
if (manifest.namespace == '' and features.LIBRARY_SUPPORT and
|
2016-03-09 21:20:28 +00:00
|
|
|
namespace_name == app.config['LIBRARY_NAMESPACE']):
|
2016-01-21 20:40:51 +00:00
|
|
|
pass
|
2016-03-09 21:20:28 +00:00
|
|
|
elif manifest.namespace != namespace_name:
|
2016-01-21 20:40:51 +00:00
|
|
|
raise NameInvalid()
|
|
|
|
|
|
|
|
if manifest.repo_name != repo_name:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise NameInvalid()
|
|
|
|
|
2015-09-29 21:53:39 +00:00
|
|
|
# Ensure that the repository exists.
|
2016-08-30 19:05:15 +00:00
|
|
|
repo = model.get_repository(namespace_name, repo_name)
|
2015-09-29 21:53:39 +00:00
|
|
|
if repo is None:
|
|
|
|
raise NameInvalid()
|
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
if not manifest.layers:
|
|
|
|
raise ManifestInvalid(detail={'message': 'manifest does not reference any layers'})
|
2015-09-29 21:53:39 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
# Ensure all the blobs in the manifest exist.
|
2018-01-11 21:25:38 +00:00
|
|
|
storage_map = model.lookup_blobs_by_digest(repo, manifest.checksums)
|
2016-08-02 22:45:30 +00:00
|
|
|
for layer in manifest.layers:
|
|
|
|
digest_str = str(layer.digest)
|
2016-07-25 22:56:25 +00:00
|
|
|
if digest_str not in storage_map:
|
2016-02-12 15:39:27 +00:00
|
|
|
raise BlobUnknown(detail={'digest': digest_str})
|
2015-09-29 21:53:39 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
# Lookup all the images and their parent images (if any) inside the manifest.
|
|
|
|
# This will let us know which v1 images we need to synthesize and which ones are invalid.
|
2016-08-16 19:23:00 +00:00
|
|
|
all_image_ids = list(manifest.parent_image_ids | manifest.image_ids)
|
2018-01-11 21:25:38 +00:00
|
|
|
images_map = model.get_docker_v1_metadata_by_image_id(repo, all_image_ids)
|
2016-08-29 15:58:18 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
# Rewrite any v1 image IDs that do not match the checksum in the database.
|
2016-08-29 15:58:18 +00:00
|
|
|
try:
|
2018-01-11 21:25:38 +00:00
|
|
|
# TODO: make this batch and read the parent image from the previous iteration, rather than
|
|
|
|
# reloading it.
|
2016-08-16 19:23:00 +00:00
|
|
|
rewritten_images = list(manifest.rewrite_invalid_image_ids(images_map))
|
2016-07-25 22:56:25 +00:00
|
|
|
for rewritten_image in rewritten_images:
|
2018-01-11 21:25:38 +00:00
|
|
|
if not rewritten_image.image_id in images_map:
|
|
|
|
model.synthesize_v1_image(
|
|
|
|
repo,
|
|
|
|
storage_map[rewritten_image.content_checksum],
|
|
|
|
rewritten_image.image_id,
|
|
|
|
rewritten_image.created,
|
|
|
|
rewritten_image.comment,
|
|
|
|
rewritten_image.command,
|
|
|
|
rewritten_image.compat_json,
|
|
|
|
rewritten_image.parent_image_id,
|
|
|
|
)
|
2016-07-25 22:56:25 +00:00
|
|
|
except ManifestException as me:
|
2016-10-03 14:25:53 +00:00
|
|
|
logger.exception("exception when rewriting v1 metadata")
|
2016-10-03 14:13:39 +00:00
|
|
|
raise ManifestInvalid(detail={'message': 'failed synthesizing v1 metadata: %s' % me.message})
|
2016-07-25 22:56:25 +00:00
|
|
|
|
|
|
|
# Store the manifest pointing to the tag.
|
2016-08-16 19:23:00 +00:00
|
|
|
leaf_layer_id = rewritten_images[-1].image_id
|
2018-01-11 21:25:38 +00:00
|
|
|
newly_created = model.save_manifest(repo, manifest.tag, leaf_layer_id, manifest.digest,
|
|
|
|
manifest.bytes)
|
2016-09-21 02:09:25 +00:00
|
|
|
if newly_created:
|
2018-01-11 21:25:38 +00:00
|
|
|
# TODO: make this batch
|
2016-09-21 02:09:25 +00:00
|
|
|
labels = []
|
|
|
|
for key, value in manifest.layers[-1].v1_metadata.labels.iteritems():
|
|
|
|
media_type = 'application/json' if is_json(value) else 'text/plain'
|
|
|
|
labels.append(Label(key=key, value=value, source_type='manifest', media_type=media_type))
|
2017-06-19 23:03:10 +00:00
|
|
|
handle_label(key, value, namespace_name, repo_name, manifest.digest)
|
|
|
|
|
2016-09-21 02:09:25 +00:00
|
|
|
model.create_manifest_labels(namespace_name, repo_name, manifest.digest, labels)
|
2016-05-31 20:43:49 +00:00
|
|
|
|
2016-09-01 23:00:11 +00:00
|
|
|
return repo, storage_map
|
|
|
|
|
|
|
|
|
|
|
|
def _write_manifest_and_log(namespace_name, repo_name, manifest):
|
|
|
|
repo, storage_map = _write_manifest(namespace_name, repo_name, manifest)
|
|
|
|
|
2016-05-31 20:43:49 +00:00
|
|
|
# Queue all blob manifests for replication.
|
|
|
|
if features.STORAGE_REPLICATION:
|
2016-12-21 17:54:50 +00:00
|
|
|
with queue_replication_batch(namespace_name) as queue_storage_replication:
|
|
|
|
for layer in manifest.layers:
|
|
|
|
digest_str = str(layer.digest)
|
|
|
|
queue_storage_replication(storage_map[digest_str])
|
2015-08-25 20:02:21 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
track_and_log('push_repo', repo, tag=manifest.tag)
|
|
|
|
spawn_notification(repo, 'repo_push', {'updated_tags': [manifest.tag]})
|
2016-11-03 19:28:40 +00:00
|
|
|
metric_queue.repository_push.Inc(labelvalues=[namespace_name, repo_name, 'v2', True])
|
2015-08-25 20:02:21 +00:00
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(
|
|
|
|
'OK',
|
|
|
|
status=202,
|
|
|
|
headers={
|
2018-01-11 21:25:38 +00:00
|
|
|
'Docker-Content-Digest': manifest.digest,
|
2017-06-26 22:16:15 +00:00
|
|
|
'Location':
|
|
|
|
url_for('v2.fetch_manifest_by_digest', repository='%s/%s' % (namespace_name, repo_name),
|
2018-01-11 21:25:38 +00:00
|
|
|
manifest_ref=manifest.digest),
|
|
|
|
},
|
|
|
|
)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['DELETE'])
|
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-08-12 20:39:32 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-08-16 19:23:00 +00:00
|
|
|
def delete_manifest_by_digest(namespace_name, repo_name, manifest_ref):
|
2015-08-12 20:39:32 +00:00
|
|
|
"""
|
2016-07-25 22:56:25 +00:00
|
|
|
Delete the manifest specified by the digest.
|
|
|
|
|
|
|
|
Note: there is no equivalent method for deleting by tag name because it is
|
|
|
|
forbidden by the spec.
|
|
|
|
"""
|
2016-08-30 19:05:15 +00:00
|
|
|
tags = model.delete_manifest_by_digest(namespace_name, repo_name, manifest_ref)
|
2016-08-16 19:23:00 +00:00
|
|
|
if not tags:
|
2015-08-12 20:39:32 +00:00
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2016-08-16 19:23:00 +00:00
|
|
|
for tag in tags:
|
|
|
|
track_and_log('delete_tag', tag.repository, tag=tag.name, digest=manifest_ref)
|
2015-11-21 02:29:57 +00:00
|
|
|
|
2016-08-09 16:28:00 +00:00
|
|
|
return Response(status=202)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
2016-03-09 21:20:28 +00:00
|
|
|
def _generate_and_store_manifest(namespace_name, repo_name, tag_name):
|
2016-07-25 22:56:25 +00:00
|
|
|
# Find the v1 metadata for this image and its parents.
|
2016-08-30 19:05:15 +00:00
|
|
|
v1_metadata = model.get_docker_v1_metadata_by_tag(namespace_name, repo_name, tag_name)
|
|
|
|
parents_v1_metadata = model.get_parents_docker_v1_metadata(namespace_name, repo_name,
|
|
|
|
v1_metadata.image_id)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
# If the manifest is being generated under the library namespace, then we make its namespace
|
|
|
|
# empty.
|
2016-03-09 21:20:28 +00:00
|
|
|
manifest_namespace = namespace_name
|
|
|
|
if features.LIBRARY_SUPPORT and namespace_name == app.config['LIBRARY_NAMESPACE']:
|
2016-01-21 20:40:51 +00:00
|
|
|
manifest_namespace = ''
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
# Create and populate the manifest builder
|
2016-07-25 22:56:25 +00:00
|
|
|
builder = DockerSchema1ManifestBuilder(manifest_namespace, repo_name, tag_name)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
# Add the leaf layer
|
2016-07-25 22:56:25 +00:00
|
|
|
builder.add_layer(v1_metadata.content_checksum, v1_metadata.compat_json)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
for parent_v1_metadata in parents_v1_metadata:
|
|
|
|
builder.add_layer(parent_v1_metadata.content_checksum, parent_v1_metadata.compat_json)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2015-09-28 19:43:20 +00:00
|
|
|
# Sign the manifest with our signing key.
|
|
|
|
manifest = builder.build(docker_v2_signing_key)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2016-07-25 22:56:25 +00:00
|
|
|
# Write the manifest to the DB.
|
2016-08-30 19:05:15 +00:00
|
|
|
model.create_manifest_and_update_tag(namespace_name, repo_name, tag_name, manifest.digest,
|
|
|
|
manifest.bytes)
|
2016-07-25 22:56:25 +00:00
|
|
|
return manifest
|