2015-06-22 21:37:13 +00:00
|
|
|
import logging
|
2015-07-06 19:00:07 +00:00
|
|
|
import jwt.utils
|
2015-08-12 20:39:32 +00:00
|
|
|
import json
|
2016-01-21 20:40:51 +00:00
|
|
|
import features
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2015-11-24 23:38:29 +00:00
|
|
|
from peewee import IntegrityError
|
2016-02-11 20:40:00 +00:00
|
|
|
from flask import make_response, request, url_for, abort
|
2015-08-12 20:39:32 +00:00
|
|
|
from collections import namedtuple, OrderedDict
|
2015-10-20 06:08:45 +00:00
|
|
|
from jwkest.jws import SIGNER_ALGS, keyrep
|
2015-08-12 20:39:32 +00:00
|
|
|
from datetime import datetime
|
2016-02-09 19:16:39 +00:00
|
|
|
from functools import wraps
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
from app import docker_v2_signing_key, app
|
2015-12-09 20:07:37 +00:00
|
|
|
from auth.registry_jwt_auth import process_registry_jwt_auth
|
2015-06-22 21:37:13 +00:00
|
|
|
from endpoints.decorators import anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
from endpoints.v2 import v2_bp, require_repo_read, require_repo_write
|
2016-01-21 20:40:51 +00:00
|
|
|
from endpoints.v2.errors import (BlobUnknown, ManifestInvalid, ManifestUnknown, TagInvalid,
|
|
|
|
NameInvalid)
|
2015-08-25 20:02:21 +00:00
|
|
|
from endpoints.trackhelper import track_and_log
|
|
|
|
from endpoints.notificationhelper import spawn_notification
|
2015-07-06 19:00:07 +00:00
|
|
|
from digest import digest_tools
|
2015-08-12 20:39:32 +00:00
|
|
|
from data import model
|
2015-12-03 21:04:17 +00:00
|
|
|
from data.database import RepositoryTag
|
2016-01-21 20:40:51 +00:00
|
|
|
from endpoints.common import parse_repository_name
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
VALID_TAG_PATTERN = r'[\w][\w.-]{0,127}'
|
2015-08-12 20:39:32 +00:00
|
|
|
|
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)
|
|
|
|
|
2016-01-25 21:39:59 +00:00
|
|
|
# From: https://github.com/docker/distribution/blob/47a064d4195a9b56133891bbb13620c3ac83a827/manifest/schema1/manifest.go#L18
|
|
|
|
MANIFEST_CONTENT_TYPE = 'application/vnd.docker.distribution.manifest.v1+prettyjws'
|
2016-02-09 19:16:39 +00:00
|
|
|
MANIFEST2_SCHEMA2_CONTENT_TYPES = ['application/vnd.docker.distribution.manifest.v2+json',
|
|
|
|
'application/vnd.docker.distribution.manifest.list.v2+json']
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
ISO_DATETIME_FORMAT_ZULU = '%Y-%m-%dT%H:%M:%SZ'
|
|
|
|
JWS_ALGORITHM = 'RS256'
|
|
|
|
|
|
|
|
|
|
|
|
ImageMetadata = namedtuple('ImageMetadata', ['digest', 'v1_metadata', 'v1_metadata_str'])
|
|
|
|
ExtractedV1Metadata = namedtuple('ExtractedV1Metadata', ['docker_id', 'parent', 'created',
|
|
|
|
'comment', 'command'])
|
|
|
|
|
|
|
|
|
|
|
|
_SIGNATURES_KEY = 'signatures'
|
|
|
|
_PROTECTED_KEY = 'protected'
|
|
|
|
_FORMAT_LENGTH_KEY = 'formatLength'
|
|
|
|
_FORMAT_TAIL_KEY = 'formatTail'
|
|
|
|
_REPO_NAME_KEY = 'name'
|
|
|
|
_REPO_TAG_KEY = 'tag'
|
|
|
|
_FS_LAYERS_KEY = 'fsLayers'
|
|
|
|
_HISTORY_KEY = 'history'
|
|
|
|
_BLOB_SUM_KEY = 'blobSum'
|
|
|
|
_V1_COMPAT_KEY = 'v1Compatibility'
|
|
|
|
_ARCH_KEY = 'architecture'
|
|
|
|
_SCHEMA_VER = 'schemaVersion'
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
|
2015-07-06 19:00:07 +00:00
|
|
|
class SignedManifest(object):
|
|
|
|
|
|
|
|
def __init__(self, manifest_bytes):
|
|
|
|
self._bytes = manifest_bytes
|
|
|
|
|
2015-09-24 20:17:42 +00:00
|
|
|
self._parsed = json.loads(manifest_bytes)
|
2015-08-12 20:39:32 +00:00
|
|
|
self._signatures = self._parsed[_SIGNATURES_KEY]
|
|
|
|
self._tag = self._parsed[_REPO_TAG_KEY]
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
repo_name_tuple = self._parsed[_REPO_NAME_KEY].split('/')
|
|
|
|
if len(repo_name_tuple) > 1:
|
|
|
|
self._namespace, self._repo_name = repo_name_tuple
|
|
|
|
elif len(repo_name_tuple) == 1:
|
|
|
|
self._namespace = ''
|
|
|
|
self._repo_name = repo_name_tuple[0]
|
|
|
|
else:
|
|
|
|
raise ValueError('repo_name has too many or too few pieces')
|
|
|
|
|
2015-07-06 19:00:07 +00:00
|
|
|
self._validate()
|
|
|
|
|
|
|
|
def _validate(self):
|
2015-10-20 06:08:45 +00:00
|
|
|
for signature in self._signatures:
|
|
|
|
bytes_to_verify = '{0}.{1}'.format(signature['protected'], jwt.utils.base64url_encode(self.payload))
|
|
|
|
signer = SIGNER_ALGS[signature['header']['alg']]
|
|
|
|
key = keyrep(signature['header']['jwk'])
|
|
|
|
gk = key.get_key()
|
|
|
|
sig = jwt.utils.base64url_decode(signature['signature'].encode('utf-8'))
|
|
|
|
verified = signer.verify(bytes_to_verify, sig, gk)
|
|
|
|
if not verified:
|
|
|
|
raise ValueError('manifest file failed signature verification')
|
2015-07-06 19:00:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def signatures(self):
|
|
|
|
return self._signatures
|
|
|
|
|
|
|
|
@property
|
|
|
|
def namespace(self):
|
|
|
|
return self._namespace
|
|
|
|
|
|
|
|
@property
|
|
|
|
def repo_name(self):
|
|
|
|
return self._repo_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tag(self):
|
|
|
|
return self._tag
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@property
|
|
|
|
def bytes(self):
|
|
|
|
return self._bytes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def digest(self):
|
|
|
|
return digest_tools.sha256_digest(self.payload)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def layers(self):
|
|
|
|
""" Returns a generator of objects that have the blobSum and v1Compatibility keys in them,
|
2015-09-29 21:53:39 +00:00
|
|
|
starting from the leaf image and working toward the base node.
|
2015-08-12 20:39:32 +00:00
|
|
|
"""
|
|
|
|
for blob_sum_obj, history_obj in reversed(zip(self._parsed[_FS_LAYERS_KEY],
|
|
|
|
self._parsed[_HISTORY_KEY])):
|
2015-09-29 19:02:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
image_digest = digest_tools.Digest.parse_digest(blob_sum_obj[_BLOB_SUM_KEY])
|
|
|
|
except digest_tools.InvalidDigestException:
|
|
|
|
raise ManifestInvalid()
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
metadata_string = history_obj[_V1_COMPAT_KEY]
|
|
|
|
|
2015-09-24 20:17:42 +00:00
|
|
|
v1_metadata = json.loads(metadata_string)
|
2015-08-12 20:39:32 +00:00
|
|
|
command_list = v1_metadata.get('container_config', {}).get('Cmd', None)
|
|
|
|
command = json.dumps(command_list) if command_list else None
|
|
|
|
|
|
|
|
extracted = ExtractedV1Metadata(v1_metadata['id'], v1_metadata.get('parent'),
|
|
|
|
v1_metadata.get('created'), v1_metadata.get('comment'),
|
|
|
|
command)
|
|
|
|
yield ImageMetadata(image_digest, extracted, metadata_string)
|
|
|
|
|
2015-07-06 19:00:07 +00:00
|
|
|
@property
|
|
|
|
def payload(self):
|
2015-09-24 20:17:42 +00:00
|
|
|
protected = str(self._signatures[0][_PROTECTED_KEY])
|
|
|
|
|
|
|
|
parsed_protected = json.loads(jwt.utils.base64url_decode(protected))
|
2015-07-06 19:00:07 +00:00
|
|
|
logger.debug('parsed_protected: %s', parsed_protected)
|
2015-09-24 20:17:42 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
signed_content_head = self._bytes[:parsed_protected[_FORMAT_LENGTH_KEY]]
|
2015-07-06 19:00:07 +00:00
|
|
|
logger.debug('signed content head: %s', signed_content_head)
|
2015-09-24 20:17:42 +00:00
|
|
|
|
|
|
|
signed_content_tail = jwt.utils.base64url_decode(str(parsed_protected[_FORMAT_TAIL_KEY]))
|
2015-07-06 19:00:07 +00:00
|
|
|
logger.debug('signed content tail: %s', signed_content_tail)
|
|
|
|
return signed_content_head + signed_content_tail
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
class SignedManifestBuilder(object):
|
|
|
|
""" Class which represents a manifest which is currently being built.
|
|
|
|
"""
|
|
|
|
def __init__(self, namespace, repo_name, tag, architecture='amd64', schema_ver=1):
|
2016-01-21 20:40:51 +00:00
|
|
|
repo_name_key = '{0}/{1}'.format(namespace, repo_name)
|
|
|
|
if namespace == '':
|
|
|
|
repo_name_key = repo_name
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
self._base_payload = {
|
|
|
|
_REPO_TAG_KEY: tag,
|
2016-01-21 20:40:51 +00:00
|
|
|
_REPO_NAME_KEY: repo_name_key,
|
2015-08-12 20:39:32 +00:00
|
|
|
_ARCH_KEY: architecture,
|
|
|
|
_SCHEMA_VER: schema_ver,
|
|
|
|
}
|
|
|
|
|
|
|
|
self._fs_layer_digests = []
|
|
|
|
self._history = []
|
|
|
|
|
|
|
|
def add_layer(self, layer_digest, v1_json_metadata):
|
|
|
|
self._fs_layer_digests.append({
|
|
|
|
_BLOB_SUM_KEY: layer_digest,
|
|
|
|
})
|
|
|
|
self._history.append({
|
|
|
|
_V1_COMPAT_KEY: v1_json_metadata,
|
|
|
|
})
|
|
|
|
|
|
|
|
def build(self, json_web_key):
|
|
|
|
""" Build the payload and sign it, returning a SignedManifest object.
|
|
|
|
"""
|
|
|
|
payload = OrderedDict(self._base_payload)
|
|
|
|
payload.update({
|
|
|
|
_HISTORY_KEY: self._history,
|
|
|
|
_FS_LAYERS_KEY: self._fs_layer_digests,
|
|
|
|
})
|
|
|
|
|
|
|
|
payload_str = json.dumps(payload, indent=3)
|
|
|
|
|
|
|
|
split_point = payload_str.rfind('\n}')
|
|
|
|
|
|
|
|
protected_payload = {
|
|
|
|
'formatTail': jwt.utils.base64url_encode(payload_str[split_point:]),
|
|
|
|
'formatLength': split_point,
|
|
|
|
'time': datetime.utcnow().strftime(ISO_DATETIME_FORMAT_ZULU),
|
|
|
|
}
|
|
|
|
protected = jwt.utils.base64url_encode(json.dumps(protected_payload))
|
|
|
|
logger.debug('Generated protected block: %s', protected)
|
|
|
|
|
|
|
|
bytes_to_sign = '{0}.{1}'.format(protected, jwt.utils.base64url_encode(payload_str))
|
|
|
|
|
|
|
|
signer = SIGNER_ALGS[JWS_ALGORITHM]
|
|
|
|
signature = jwt.utils.base64url_encode(signer.sign(bytes_to_sign, json_web_key.get_key()))
|
|
|
|
logger.debug('Generated signature: %s', signature)
|
|
|
|
|
2015-10-26 20:40:19 +00:00
|
|
|
public_members = set(json_web_key.public_members)
|
|
|
|
public_key = {comp: value for comp, value in json_web_key.to_dict().items()
|
|
|
|
if comp in public_members}
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
signature_block = {
|
|
|
|
'header': {
|
2015-10-26 20:40:19 +00:00
|
|
|
'jwk': public_key,
|
2015-08-12 20:39:32 +00:00
|
|
|
'alg': JWS_ALGORITHM,
|
|
|
|
},
|
|
|
|
'signature': signature,
|
|
|
|
_PROTECTED_KEY: protected,
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug('Encoded signature block: %s', json.dumps(signature_block))
|
|
|
|
|
|
|
|
payload.update({
|
|
|
|
_SIGNATURES_KEY: [signature_block],
|
|
|
|
})
|
|
|
|
|
|
|
|
return SignedManifest(json.dumps(payload, indent=3))
|
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route(MANIFEST_TAGNAME_ROUTE, methods=['GET'])
|
2015-12-09 20:07:37 +00:00
|
|
|
@process_registry_jwt_auth
|
2016-01-21 20:40:51 +00:00
|
|
|
@parse_repository_name
|
2015-08-12 20:39:32 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
|
|
|
def fetch_manifest_by_tagname(namespace, repo_name, manifest_ref):
|
|
|
|
try:
|
|
|
|
manifest = model.tag.load_tag_manifest(namespace, repo_name, manifest_ref)
|
|
|
|
except model.InvalidManifestException:
|
2015-12-03 21:04:17 +00:00
|
|
|
try:
|
|
|
|
model.tag.get_active_tag(namespace, repo_name, manifest_ref)
|
|
|
|
except RepositoryTag.DoesNotExist:
|
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
try:
|
|
|
|
manifest = _generate_and_store_manifest(namespace, repo_name, manifest_ref)
|
|
|
|
except model.DataModelException:
|
|
|
|
logger.exception('Exception when generating manifest for %s/%s:%s', namespace, repo_name,
|
|
|
|
manifest_ref)
|
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2015-11-21 02:29:57 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repo_name)
|
|
|
|
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)
|
2015-11-21 02:29:57 +00:00
|
|
|
|
2015-11-23 18:56:34 +00:00
|
|
|
response = make_response(manifest.json_data, 200)
|
2016-01-25 21:39:59 +00:00
|
|
|
response.headers['Content-Type'] = MANIFEST_CONTENT_TYPE
|
2015-11-23 18:56:34 +00:00
|
|
|
response.headers['Docker-Content-Digest'] = manifest.digest
|
|
|
|
return response
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['GET'])
|
2015-12-09 20:07:37 +00:00
|
|
|
@process_registry_jwt_auth
|
2016-01-21 20:40:51 +00:00
|
|
|
@parse_repository_name
|
2015-06-22 21:37:13 +00:00
|
|
|
@require_repo_read
|
|
|
|
@anon_protect
|
2015-08-12 20:39:32 +00:00
|
|
|
def fetch_manifest_by_digest(namespace, repo_name, manifest_ref):
|
|
|
|
try:
|
|
|
|
manifest = model.tag.load_manifest_by_digest(namespace, repo_name, manifest_ref)
|
|
|
|
except model.InvalidManifestException:
|
|
|
|
# Without a tag name to reference, we can't make an attempt to generate the manifest
|
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2015-11-21 02:29:57 +00:00
|
|
|
repo = model.repository.get_repository(namespace, repo_name)
|
|
|
|
if repo is not None:
|
|
|
|
track_and_log('pull_repo', repo)
|
|
|
|
|
2015-11-23 18:56:34 +00:00
|
|
|
response = make_response(manifest.json_data, 200)
|
2016-01-25 21:39:59 +00:00
|
|
|
response.headers['Content-Type'] = MANIFEST_CONTENT_TYPE
|
2015-11-23 18:56:34 +00:00
|
|
|
response.headers['Docker-Content-Digest'] = manifest.digest
|
|
|
|
return response
|
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):
|
|
|
|
if request.content_type in MANIFEST2_SCHEMA2_CONTENT_TYPES:
|
|
|
|
raise ManifestInvalid(detail={'message': 'manifest schema version not supported'},
|
|
|
|
http_status_code=415)
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_TAGNAME_ROUTE, methods=['PUT'])
|
2015-12-09 20:07:37 +00:00
|
|
|
@process_registry_jwt_auth
|
2016-01-21 20:40:51 +00:00
|
|
|
@parse_repository_name
|
2015-08-12 20:39:32 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-02-09 19:16:39 +00:00
|
|
|
@_reject_manifest2_schema2
|
2015-08-12 20:39:32 +00:00
|
|
|
def write_manifest_by_tagname(namespace, repo_name, manifest_ref):
|
2015-10-02 18:01:12 +00:00
|
|
|
try:
|
|
|
|
manifest = SignedManifest(request.data)
|
2016-01-21 20:40:51 +00:00
|
|
|
except ValueError as ve:
|
2015-10-02 18:01:12 +00:00
|
|
|
raise ManifestInvalid()
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
if manifest.tag != manifest_ref:
|
|
|
|
raise TagInvalid()
|
|
|
|
|
|
|
|
return _write_manifest(namespace, 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'])
|
2015-12-09 20:07:37 +00:00
|
|
|
@process_registry_jwt_auth
|
2016-01-21 20:40:51 +00:00
|
|
|
@parse_repository_name
|
2015-06-22 21:37:13 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
2016-02-09 19:16:39 +00:00
|
|
|
@_reject_manifest2_schema2
|
2015-08-12 20:39:32 +00:00
|
|
|
def write_manifest_by_digest(namespace, repo_name, manifest_ref):
|
2015-10-02 18:01:12 +00:00
|
|
|
try:
|
|
|
|
manifest = SignedManifest(request.data)
|
|
|
|
except ValueError:
|
|
|
|
raise ManifestInvalid()
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
if manifest.digest != manifest_ref:
|
|
|
|
raise ManifestInvalid()
|
|
|
|
|
|
|
|
return _write_manifest(namespace, repo_name, manifest)
|
|
|
|
|
|
|
|
|
|
|
|
def _write_manifest(namespace, repo_name, manifest):
|
2016-01-21 20:40:51 +00:00
|
|
|
# Ensure that the manifest is for this repository. If the manifest's namespace is empty, then
|
|
|
|
# it is for the library namespace and we need an extra check.
|
|
|
|
if (manifest.namespace == '' and features.LIBRARY_SUPPORT and
|
|
|
|
namespace == app.config['LIBRARY_NAMESPACE']):
|
|
|
|
# This is a library manifest. All good.
|
|
|
|
pass
|
|
|
|
elif manifest.namespace != namespace:
|
|
|
|
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.
|
|
|
|
repo = model.repository.get_repository(namespace, repo_name)
|
|
|
|
if repo is None:
|
|
|
|
raise NameInvalid()
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
layers = list(manifest.layers)
|
|
|
|
|
2015-11-19 09:03:14 +00:00
|
|
|
docker_image_ids = {mdata.v1_metadata.docker_id for mdata in layers}
|
|
|
|
parent_image_ids = {mdata.v1_metadata.parent for mdata in layers
|
|
|
|
if mdata.v1_metadata.parent}
|
|
|
|
all_image_ids = list(docker_image_ids | parent_image_ids)
|
2015-09-29 21:53:39 +00:00
|
|
|
|
|
|
|
images_query = model.image.lookup_repository_images(repo, all_image_ids)
|
|
|
|
images_map = {image.docker_image_id: image for image in images_query}
|
|
|
|
|
|
|
|
# Lookup the storages associated with each blob in the manifest.
|
2015-11-19 09:03:14 +00:00
|
|
|
checksums = list({str(mdata.digest) for mdata in manifest.layers})
|
2015-11-06 23:18:29 +00:00
|
|
|
storage_query = model.storage.lookup_repo_storages_by_content_checksum(repo, checksums)
|
|
|
|
storage_map = {storage.content_checksum: storage for storage in storage_query}
|
2015-09-29 21:53:39 +00:00
|
|
|
|
|
|
|
# Synthesize the V1 metadata for each layer.
|
2015-08-12 20:39:32 +00:00
|
|
|
manifest_digest = manifest.digest
|
|
|
|
tag_name = manifest.tag
|
|
|
|
|
2015-09-29 21:53:39 +00:00
|
|
|
for mdata in layers:
|
|
|
|
digest_str = str(mdata.digest)
|
|
|
|
v1_mdata = mdata.v1_metadata
|
|
|
|
|
|
|
|
# If there is already a V1 image for this layer, nothing more to do.
|
|
|
|
if v1_mdata.docker_id in images_map:
|
2016-02-11 20:40:00 +00:00
|
|
|
# Ensure that the V1 image's storage matches the V2 blob. If not, we've found
|
|
|
|
# a data inconsistency and need to fail.
|
|
|
|
v1_image = images_map[v1_mdata.docker_id]
|
|
|
|
if v1_image.storage.content_checksum != digest_str:
|
|
|
|
logger.error('Checksum mismatch on V1 layer %s (#%s): Expected digest %s, found %s',
|
|
|
|
v1_mdata.docker_id, v1_image.id, digest_str, v1_image.storage.content_checksum)
|
|
|
|
abort(500)
|
2015-09-29 21:53:39 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Lookup the parent image for the layer, if any.
|
|
|
|
parent_image = None
|
|
|
|
if v1_mdata.parent is not None:
|
|
|
|
parent_image = images_map.get(v1_mdata.parent)
|
|
|
|
if parent_image is None:
|
|
|
|
msg = 'Parent not found with docker image id {0}'.format(v1_mdata.parent)
|
|
|
|
raise ManifestInvalid(detail={'message': msg})
|
|
|
|
|
|
|
|
# Synthesize and store the v1 metadata in the db.
|
|
|
|
blob_storage = storage_map.get(digest_str)
|
|
|
|
if blob_storage is None:
|
|
|
|
raise BlobUnknown(detail={'digest': digest_str})
|
|
|
|
|
|
|
|
image = model.image.synthesize_v1_image(repo, blob_storage, v1_mdata.docker_id,
|
|
|
|
v1_mdata.created, v1_mdata.comment, v1_mdata.command,
|
|
|
|
mdata.v1_metadata_str, parent_image)
|
|
|
|
|
|
|
|
images_map[v1_mdata.docker_id] = image
|
|
|
|
|
|
|
|
if not layers:
|
2015-08-12 20:39:32 +00:00
|
|
|
# The manifest doesn't actually reference any layers!
|
|
|
|
raise ManifestInvalid(detail={'message': 'manifest does not reference any layers'})
|
|
|
|
|
2015-09-29 21:53:39 +00:00
|
|
|
# Store the manifest pointing to the tag.
|
2015-11-12 21:22:19 +00:00
|
|
|
leaf_layer = layers[-1]
|
2015-08-12 20:39:32 +00:00
|
|
|
model.tag.store_tag_manifest(namespace, repo_name, tag_name, leaf_layer.v1_metadata.docker_id,
|
|
|
|
manifest_digest, request.data)
|
2015-07-06 19:00:07 +00:00
|
|
|
|
2015-08-25 20:02:21 +00:00
|
|
|
# Spawn the repo_push event.
|
2015-09-29 21:53:39 +00:00
|
|
|
event_data = {
|
|
|
|
'updated_tags': [tag_name],
|
|
|
|
}
|
2015-08-25 20:02:21 +00:00
|
|
|
|
2015-09-29 21:53:39 +00:00
|
|
|
track_and_log('push_repo', repo)
|
|
|
|
spawn_notification(repo, 'repo_push', event_data)
|
2015-08-25 20:02:21 +00:00
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
response = make_response('OK', 202)
|
2015-07-06 19:00:07 +00:00
|
|
|
response.headers['Docker-Content-Digest'] = manifest_digest
|
2016-01-21 20:40:51 +00:00
|
|
|
response.headers['Location'] = url_for('v2.fetch_manifest_by_digest',
|
|
|
|
repository='%s/%s' % (namespace, repo_name),
|
|
|
|
manifest_ref=manifest_digest)
|
2015-06-22 21:37:13 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
@v2_bp.route(MANIFEST_DIGEST_ROUTE, methods=['DELETE'])
|
2015-12-09 20:07:37 +00:00
|
|
|
@process_registry_jwt_auth
|
2016-01-21 20:40:51 +00:00
|
|
|
@parse_repository_name
|
2015-08-12 20:39:32 +00:00
|
|
|
@require_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_manifest_by_digest(namespace, repo_name, manifest_ref):
|
|
|
|
""" 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.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
manifest = model.tag.load_manifest_by_digest(namespace, repo_name, manifest_ref)
|
|
|
|
except model.InvalidManifestException:
|
|
|
|
# Without a tag name to reference, we can't make an attempt to generate the manifest
|
|
|
|
raise ManifestUnknown()
|
|
|
|
|
2015-12-10 20:15:24 +00:00
|
|
|
# Mark the tag as no longer alive.
|
|
|
|
try:
|
|
|
|
model.tag.delete_tag(namespace, repo_name, manifest.tag.name)
|
|
|
|
except model.DataModelException:
|
|
|
|
# Tag is not alive.
|
|
|
|
raise ManifestUnknown()
|
2015-08-12 20:39:32 +00:00
|
|
|
|
2015-12-10 20:15:24 +00:00
|
|
|
track_and_log('delete_tag', manifest.tag.repository,
|
|
|
|
tag=manifest.tag.name, digest=manifest_ref)
|
2015-11-21 02:29:57 +00:00
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
return make_response('', 202)
|
|
|
|
|
|
|
|
|
|
|
|
def _generate_and_store_manifest(namespace, repo_name, tag_name):
|
|
|
|
# First look up the tag object and its ancestors
|
|
|
|
image = model.tag.get_tag_image(namespace, repo_name, tag_name)
|
|
|
|
parents = model.image.get_parent_images(namespace, repo_name, image)
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
# If the manifest is being generated under the library namespace, then we make its namespace
|
|
|
|
# empty.
|
|
|
|
manifest_namespace = namespace
|
|
|
|
if features.LIBRARY_SUPPORT and namespace == app.config['LIBRARY_NAMESPACE']:
|
|
|
|
manifest_namespace = ''
|
|
|
|
|
2015-08-12 20:39:32 +00:00
|
|
|
# Create and populate the manifest builder
|
2016-01-21 20:40:51 +00:00
|
|
|
builder = SignedManifestBuilder(manifest_namespace, repo_name, tag_name)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
# Add the leaf layer
|
2015-11-06 23:18:29 +00:00
|
|
|
builder.add_layer(image.storage.content_checksum, image.v1_json_metadata)
|
2015-08-12 20:39:32 +00:00
|
|
|
|
|
|
|
for parent in parents:
|
2015-11-06 23:18:29 +00:00
|
|
|
builder.add_layer(parent.storage.content_checksum, parent.v1_json_metadata)
|
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
|
|
|
|
2015-11-24 23:38:29 +00:00
|
|
|
# Write the manifest to the DB. If an existing manifest already exists, return the
|
|
|
|
# one found.
|
|
|
|
try:
|
|
|
|
return model.tag.associate_generated_tag_manifest(namespace, repo_name, tag_name,
|
|
|
|
manifest.digest, manifest.bytes)
|
|
|
|
except IntegrityError:
|
|
|
|
try:
|
|
|
|
return model.tag.load_tag_manifest(namespace, repo_name, tag_name)
|
|
|
|
except model.InvalidManifestException:
|
|
|
|
raise model.DataModelException('Could not load or generate manifest')
|
|
|
|
|