This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/v2/manifest.py

260 lines
9.5 KiB
Python
Raw Normal View History

2015-06-22 21:37:13 +00:00
import logging
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
from app import docker_v2_signing_key, app, metric_queue
from auth.registry_jwt_auth import process_registry_jwt_auth
2016-07-25 22:56:25 +00:00
from data import model
from digest import digest_tools
2016-03-09 21:20:28 +00:00
from endpoints.common import parse_repository_name
2015-06-22 21:37:13 +00:00
from endpoints.decorators import anon_protect
from endpoints.v2 import v2_bp, require_repo_read, require_repo_write
from endpoints.v2.errors import (BlobUnknown, ManifestInvalid, ManifestUnknown, TagInvalid,
2016-07-25 22:56:25 +00:00
NameInvalid)
from endpoints.trackhelper import track_and_log
from endpoints.notificationhelper import spawn_notification
from image.docker import ManifestException
from image.docker.schema1 import DockerSchema1Manifest, DockerSchema1ManifestBuilder
from image.docker.schema2 import DOCKER_SCHEMA2_CONTENT_TYPES
from util.registry.replication import queue_storage_replication
from util.names import VALID_TAG_PATTERN
2015-06-22 21:37:13 +00:00
logger = logging.getLogger(__name__)
BASE_MANIFEST_ROUTE = '/<repopath:repository>/manifests/<regex("{0}"):manifest_ref>'
MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN)
MANIFEST_TAGNAME_ROUTE = BASE_MANIFEST_ROUTE.format(VALID_TAG_PATTERN)
@v2_bp.route(MANIFEST_TAGNAME_ROUTE, methods=['GET'])
2016-03-09 21:20:28 +00:00
@parse_repository_name()
@process_registry_jwt_auth(scopes=['pull'])
@require_repo_read
@anon_protect
2016-07-25 22:56:25 +00:00
def fetch_manifest_by_tagname(namespace_name, repo_name, tag_name):
manifest = v2.get_manifest_by_tag(namespace_name, repo_name, tag_name)
if manifest is None:
tag = v2.get_active_tag(namespace_name, repo_name, tag_name)
if tag is None:
2016-03-09 21:20:28 +00:00
raise ManifestUnknown()
2016-07-25 22:56:25 +00:00
manifest = _generate_and_store_manifest(namespace_name, repo_name, tag_name)
if manifest is None:
raise ManifestUnknown()
2016-07-25 22:56:25 +00:00
repo = v2.get_repository(namespace_name, repo_name)
2015-11-21 02:29:57 +00:00
if repo is not None:
2015-12-14 22:41:16 +00:00
track_and_log('pull_repo', repo, analytics_name='pull_repo_100x', analytics_sample=0.01)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
2015-11-21 02:29:57 +00:00
2016-08-09 16:28:00 +00:00
return Response(
manifest.bytes,
status=200,
headers={'Content-Type': manifest.content_type, 'Docker-Content-Digest': manifest.digest},
)
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['GET'])
2016-03-09 21:20:28 +00:00
@parse_repository_name()
@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-07-25 22:56:25 +00:00
manifest = model.tag.load_manifest_by_digest(namespace_name, repo_name, manifest_ref)
if manifest is None:
# Without a tag name to reference, we can't make an attempt to generate the manifest
raise ManifestUnknown()
2016-07-25 22:56:25 +00:00
repo = v2.get_repository(namespace_name, repo_name)
2015-11-21 02:29:57 +00:00
if repo is not None:
track_and_log('pull_repo', repo)
metric_queue.repository_pull.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
2015-11-21 02:29:57 +00:00
2016-08-09 16:28:00 +00:00
return Response(manifest.json, status=200, headers={'Content-Type': manifest.content_type,
'Docker-Content-Digest': manifest.digest})
def _reject_manifest2_schema2(func):
@wraps(func)
def wrapped(*args, **kwargs):
2016-07-25 22:56:25 +00:00
if request.content_type in DOCKER_SCHEMA2_CONTENT_TYPES:
raise ManifestInvalid(detail={'message': 'manifest schema version not supported'},
http_status_code=415)
return func(*args, **kwargs)
return wrapped
@v2_bp.route(MANIFEST_TAGNAME_ROUTE, methods=['PUT'])
@_reject_manifest2_schema2
2016-03-09 21:20:28 +00:00
@parse_repository_name()
@process_registry_jwt_auth(scopes=['pull', 'push'])
@require_repo_write
@anon_protect
2016-07-25 22:56:25 +00:00
def write_manifest_by_tagname(namespace_name, repo_name, tag_name):
try:
2016-07-25 22:56:25 +00:00
manifest = DockerSchema1Manifest(request.data)
except ManifestException as me:
raise ManifestInvalid(detail={'message': me.message})
2016-07-25 22:56:25 +00:00
if manifest.tag != tag_name:
raise TagInvalid()
2016-03-09 21:20:28 +00:00
return _write_manifest(namespace_name, repo_name, manifest)
2015-06-22 21:37:13 +00:00
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['PUT'])
@_reject_manifest2_schema2
2016-03-09 21:20:28 +00:00
@parse_repository_name()
@process_registry_jwt_auth(scopes=['pull', 'push'])
2015-06-22 21:37:13 +00:00
@require_repo_write
@anon_protect
2016-07-25 22:56:25 +00:00
def write_manifest_by_digest(namespace_name, repo_name, digest):
try:
2016-07-25 22:56:25 +00:00
manifest = DockerSchema1Manifest(request.data)
except ManifestException as me:
raise ManifestInvalid(detail={'message': me.message})
2016-07-25 22:56:25 +00:00
if manifest.digest != digest:
raise ManifestInvalid(detail={'message': 'manifest digest mismatch'})
2016-03-09 21:20:28 +00:00
return _write_manifest(namespace_name, repo_name, manifest)
2016-07-25 22:56:25 +00:00
def _write_manifest(namespace_name, repo_name, manifest):
if (manifest.namespace == '' and
features.LIBRARY_SUPPORT and
2016-03-09 21:20:28 +00:00
namespace_name == app.config['LIBRARY_NAMESPACE']):
pass
2016-03-09 21:20:28 +00:00
elif manifest.namespace != namespace_name:
raise NameInvalid()
if manifest.repo_name != repo_name:
raise NameInvalid()
# Ensure that the repository exists.
2016-07-25 22:56:25 +00:00
repo = v2.get_repository(namespace_name, repo_name)
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'})
2016-07-25 22:56:25 +00:00
# Ensure all the blobs in the manifest exist.
storage_query = model.storage.lookup_repo_storages_by_content_checksum(repo, manifest.checksums)
storage_map = {storage.content_checksum: storage for storage in storage_query}
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:
raise BlobUnknown(detail={'digest': digest_str})
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.
all_image_ids = list(manifest.docker_image_ids | manifest.parent_image_ids)
2016-08-03 18:00:40 +00:00
images_map = v2.get_docker_v1_metadata_by_image_id(namespace_name, repo_name, all_image_ids)
2016-07-25 22:56:25 +00:00
# Rewrite any v1 image IDs that do not match the checksum in the database.
try:
2016-07-25 22:56:25 +00:00
rewritten_images = manifest.rewrite_invalid_image_ids(images_map)
for rewritten_image in rewritten_images:
image = v2.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,
)
images_map[image.image_id] = image
except ManifestException as me:
raise ManifestInvalid(detail={'message': me.message})
# Store the manifest pointing to the tag.
leaf_layer_id = images_map[manifest.leaf_layer.v1_metadata.image_id].image_id
2016-07-25 22:56:25 +00:00
v2.save_manifest(namespace_name, repo_name, tag_name, leaf_layer_id, manifest.digest, manifest.bytes)
# Queue all blob manifests for replication.
# TODO(jschorr): Find a way to optimize this insertion.
if features.STORAGE_REPLICATION:
for layer in manifest.layers:
digest_str = str(layer.digest)
2016-07-25 22:56:25 +00:00
queue_storage_replication(namespace_name, storage_map[digest_str])
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]})
metric_queue.repository_push.Inc(labelvalues=[namespace_name, repo_name, 'v2'])
2016-08-09 16:28:00 +00:00
return Response(
'OK',
status=202,
headers={
'Docker-Content-Digest': manifest.digest,
'Location': url_for('v2.fetch_manifest_by_digest',
repository='%s/%s' % (namespace_name, repo_name),
manifest_ref=manifest.digest),
},
)
2015-06-22 21:37:13 +00:00
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['DELETE'])
2016-03-09 21:20:28 +00:00
@parse_repository_name()
@process_registry_jwt_auth(scopes=['pull', 'push'])
@require_repo_write
@anon_protect
2016-07-25 22:56:25 +00:00
def delete_manifest_by_digest(namespace_name, repo_name, digest):
"""
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.
"""
tag = v2.get_tag_by_manifest_digest(namespace_name, repo_name, digest)
if tag is None:
# TODO(jzelinskie): disambiguate between no manifest and no tag
raise ManifestUnknown()
# Mark the tag as no longer alive.
2016-07-25 22:56:25 +00:00
deleted = v2.delete_tag(namespace_name, repo_name, tag.name)
if not deleted:
# Tag was not alive.
raise ManifestUnknown()
2016-07-25 22:56:25 +00:00
track_and_log('delete_tag', tag.repository, tag=tag.name, digest=digest)
2015-11-21 02:29:57 +00:00
2016-08-09 16:28:00 +00:00
return Response(status=202)
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-03 18:00:40 +00:00
v1_metadata = v2.get_docker_v1_metadata_by_tag(namespace_name, repo_name, tag_name)
2016-07-25 22:56:25 +00:00
parents_v1_metadata = v2.get_parents_docker_v1_metadata(namespace_name, repo_name,
v1_metadata.image_id)
# 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']:
manifest_namespace = ''
# Create and populate the manifest builder
2016-07-25 22:56:25 +00:00
builder = DockerSchema1ManifestBuilder(manifest_namespace, repo_name, tag_name)
# Add the leaf layer
2016-07-25 22:56:25 +00:00
builder.add_layer(v1_metadata.content_checksum, v1_metadata.compat_json)
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)
# Sign the manifest with our signing key.
manifest = builder.build(docker_v2_signing_key)
2016-07-25 22:56:25 +00:00
# Write the manifest to the DB.
v2.create_manifest_and_update_tag(namespace_name, repo_name, tag_name, manifest.digest,
manifest.bytes)
return manifest