2016-07-18 22:20:00 +00:00
|
|
|
""" Manage the manifests of a repository. """
|
2018-12-10 20:33:59 +00:00
|
|
|
import json
|
2018-12-10 22:10:54 +00:00
|
|
|
import logging
|
2018-12-10 20:33:59 +00:00
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
from flask import request
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2018-12-10 20:33:59 +00:00
|
|
|
from app import label_validator, storage
|
2018-08-21 21:26:32 +00:00
|
|
|
from data.model import InvalidLabelKeyException, InvalidMediaTypeException
|
|
|
|
from data.registry_model import registry_model
|
|
|
|
from digest import digest_tools
|
2016-07-18 22:20:00 +00:00
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
|
|
|
RepositoryParamResource, log_action, validate_json_request,
|
2017-07-20 18:24:43 +00:00
|
|
|
path_param, parse_args, query_param, abort, api,
|
2018-12-10 20:33:59 +00:00
|
|
|
disallow_for_app_repositories, format_date)
|
2018-08-21 21:26:32 +00:00
|
|
|
from endpoints.api.image import image_dict
|
2016-07-18 22:20:00 +00:00
|
|
|
from endpoints.exception import NotFound
|
2017-06-20 19:50:46 +00:00
|
|
|
from util.validation import VALID_LABEL_KEY_REGEX
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
BASE_MANIFEST_ROUTE = '/v1/repository/<apirepopath:repository>/manifest/<regex("{0}"):manifestref>'
|
|
|
|
MANIFEST_DIGEST_ROUTE = BASE_MANIFEST_ROUTE.format(digest_tools.DIGEST_PATTERN)
|
|
|
|
ALLOWED_LABEL_MEDIA_TYPES = ['text/plain', 'application/json']
|
|
|
|
|
2018-12-10 22:10:54 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
def _label_dict(label):
|
|
|
|
return {
|
|
|
|
'id': label.uuid,
|
|
|
|
'key': label.key,
|
|
|
|
'value': label.value,
|
|
|
|
'source_type': label.source_type_name,
|
|
|
|
'media_type': label.media_type_name,
|
|
|
|
}
|
|
|
|
|
2018-12-10 20:33:59 +00:00
|
|
|
|
|
|
|
def _layer_dict(manifest_layer, index):
|
2018-12-10 22:10:54 +00:00
|
|
|
# NOTE: The `command` in the layer is either a JSON string of an array (schema 1) or
|
|
|
|
# a single string (schema 2). The block below normalizes it to have the same format.
|
2018-12-10 20:33:59 +00:00
|
|
|
try:
|
|
|
|
command = json.loads(manifest_layer.command)
|
|
|
|
except (TypeError, ValueError):
|
2018-12-10 22:10:54 +00:00
|
|
|
command = [manifest_layer.command]
|
2018-12-10 20:33:59 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'index': index,
|
|
|
|
'compressed_size': manifest_layer.compressed_size,
|
|
|
|
'is_remote': manifest_layer.is_remote,
|
|
|
|
'urls': manifest_layer.urls,
|
|
|
|
'command': command,
|
|
|
|
'blob_digest': str(manifest_layer.blob_digest),
|
|
|
|
'created_datetime': format_date(manifest_layer.created_datetime),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
def _manifest_dict(manifest):
|
|
|
|
image = None
|
2018-12-10 20:33:59 +00:00
|
|
|
if manifest.legacy_image_if_present is not None:
|
2018-08-21 21:26:32 +00:00
|
|
|
image = image_dict(manifest.legacy_image, with_history=True)
|
|
|
|
|
2018-12-10 20:33:59 +00:00
|
|
|
layers = registry_model.list_manifest_layers(manifest, storage)
|
2018-12-10 22:10:54 +00:00
|
|
|
if layers is None and not manifest.is_manifest_list:
|
|
|
|
logger.debug('Missing layers for manifest `%s`', manifest.digest)
|
|
|
|
abort(404)
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
return {
|
|
|
|
'digest': manifest.digest,
|
2018-12-10 20:33:59 +00:00
|
|
|
'is_manifest_list': manifest.is_manifest_list,
|
2018-08-21 21:26:32 +00:00
|
|
|
'manifest_data': manifest.manifest_bytes,
|
|
|
|
'image': image,
|
2018-12-10 20:33:59 +00:00
|
|
|
'layers': ([_layer_dict(lyr.layer_info, idx) for idx, lyr in enumerate(layers)]
|
|
|
|
if layers else None),
|
2018-08-21 21:26:32 +00:00
|
|
|
}
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2018-03-28 20:03:18 +00:00
|
|
|
@resource(MANIFEST_DIGEST_ROUTE)
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
class RepositoryManifest(RepositoryParamResource):
|
|
|
|
""" Resource for retrieving a specific repository manifest. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('getRepoManifest')
|
|
|
|
@disallow_for_app_repositories
|
|
|
|
def get(self, namespace_name, repository_name, manifestref):
|
2018-08-21 21:26:32 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace_name, repository_name)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
manifest = registry_model.lookup_manifest_by_digest(repo_ref, manifestref,
|
|
|
|
include_legacy_image=True)
|
2018-03-28 20:03:18 +00:00
|
|
|
if manifest is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
return _manifest_dict(manifest)
|
2018-03-28 20:03:18 +00:00
|
|
|
|
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
@resource(MANIFEST_DIGEST_ROUTE + '/labels')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
class RepositoryManifestLabels(RepositoryParamResource):
|
|
|
|
""" Resource for listing the labels on a specific repository manifest. """
|
|
|
|
schemas = {
|
|
|
|
'AddLabel': {
|
|
|
|
'type': 'object',
|
|
|
|
'description': 'Adds a label to a manifest',
|
|
|
|
'required': [
|
|
|
|
'key',
|
|
|
|
'value',
|
|
|
|
'media_type',
|
|
|
|
],
|
|
|
|
'properties': {
|
|
|
|
'key': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The key for the label',
|
|
|
|
},
|
|
|
|
'value': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'The value for the label',
|
|
|
|
},
|
|
|
|
'media_type': {
|
2017-03-10 00:04:35 +00:00
|
|
|
'type': ['string', 'null'],
|
2016-07-18 22:20:00 +00:00
|
|
|
'description': 'The media type for this label',
|
2017-03-10 00:04:35 +00:00
|
|
|
'enum': ALLOWED_LABEL_MEDIA_TYPES + [None],
|
2016-07-18 22:20:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('listManifestLabels')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-07-18 22:20:00 +00:00
|
|
|
@parse_args()
|
|
|
|
@query_param('filter', 'If specified, only labels matching the given prefix will be returned',
|
|
|
|
type=str, default=None)
|
2017-07-20 18:24:43 +00:00
|
|
|
def get(self, namespace_name, repository_name, manifestref, parsed_args):
|
2018-08-21 21:26:32 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace_name, repository_name)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
manifest = registry_model.lookup_manifest_by_digest(repo_ref, manifestref)
|
|
|
|
if manifest is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
labels = registry_model.list_manifest_labels(manifest, parsed_args['filter'])
|
2017-07-20 18:24:43 +00:00
|
|
|
if labels is None:
|
2016-07-18 22:20:00 +00:00
|
|
|
raise NotFound()
|
2018-08-21 21:26:32 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
return {
|
2018-08-21 21:26:32 +00:00
|
|
|
'labels': [_label_dict(label) for label in labels]
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('addManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-07-18 22:20:00 +00:00
|
|
|
@validate_json_request('AddLabel')
|
2017-07-20 18:24:43 +00:00
|
|
|
def post(self, namespace_name, repository_name, manifestref):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Adds a new label into the tag manifest. """
|
|
|
|
label_data = request.get_json()
|
|
|
|
|
|
|
|
# Check for any reserved prefixes.
|
|
|
|
if label_validator.has_reserved_prefix(label_data['key']):
|
|
|
|
abort(400, message='Label has a reserved prefix')
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace_name, repository_name)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
manifest = registry_model.lookup_manifest_by_digest(repo_ref, manifestref)
|
|
|
|
if manifest is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2017-06-20 19:50:46 +00:00
|
|
|
label = None
|
|
|
|
try:
|
2018-08-21 21:26:32 +00:00
|
|
|
label = registry_model.create_manifest_label(manifest,
|
|
|
|
label_data['key'],
|
|
|
|
label_data['value'],
|
|
|
|
'api',
|
|
|
|
label_data['media_type'])
|
2017-07-20 18:24:43 +00:00
|
|
|
except InvalidLabelKeyException:
|
2018-08-21 21:26:32 +00:00
|
|
|
message = ('Label is of an invalid format or missing please ' +
|
|
|
|
'use %s format for labels' % VALID_LABEL_KEY_REGEX)
|
|
|
|
abort(400, message=message)
|
2017-07-20 18:24:43 +00:00
|
|
|
except InvalidMediaTypeException:
|
2018-08-21 21:26:32 +00:00
|
|
|
message = 'Media type is invalid please use a valid media type: text/plain, application/json'
|
|
|
|
abort(400, message=message)
|
2017-06-20 19:50:46 +00:00
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
if label is None:
|
|
|
|
raise NotFound()
|
2018-08-21 21:26:32 +00:00
|
|
|
|
2016-07-18 22:20:00 +00:00
|
|
|
metadata = {
|
|
|
|
'id': label.uuid,
|
2017-07-20 18:24:43 +00:00
|
|
|
'key': label.key,
|
|
|
|
'value': label.value,
|
2016-07-18 22:20:00 +00:00
|
|
|
'manifest_digest': manifestref,
|
2017-07-20 18:24:43 +00:00
|
|
|
'media_type': label.media_type_name,
|
|
|
|
'namespace': namespace_name,
|
|
|
|
'repo': repository_name,
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
log_action('manifest_label_add', namespace_name, metadata, repo_name=repository_name)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
resp = {'label': _label_dict(label)}
|
2017-07-20 18:24:43 +00:00
|
|
|
repo_string = '%s/%s' % (namespace_name, repository_name)
|
2016-07-18 22:20:00 +00:00
|
|
|
headers = {
|
|
|
|
'Location': api.url_for(ManageRepositoryManifestLabel, repository=repo_string,
|
|
|
|
manifestref=manifestref, labelid=label.uuid),
|
|
|
|
}
|
|
|
|
return resp, 201, headers
|
|
|
|
|
|
|
|
|
|
|
|
@resource(MANIFEST_DIGEST_ROUTE + '/labels/<labelid>')
|
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('manifestref', 'The digest of the manifest')
|
|
|
|
@path_param('labelid', 'The ID of the label')
|
|
|
|
class ManageRepositoryManifestLabel(RepositoryParamResource):
|
|
|
|
""" Resource for managing the labels on a specific repository manifest. """
|
|
|
|
@require_repo_read
|
|
|
|
@nickname('getManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-20 18:24:43 +00:00
|
|
|
def get(self, namespace_name, repository_name, manifestref, labelid):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Retrieves the label with the specific ID under the manifest. """
|
2018-08-21 21:26:32 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace_name, repository_name)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
manifest = registry_model.lookup_manifest_by_digest(repo_ref, manifestref)
|
|
|
|
if manifest is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
label = registry_model.get_manifest_label(manifest, labelid)
|
2016-07-18 22:20:00 +00:00
|
|
|
if label is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2018-08-21 21:26:32 +00:00
|
|
|
return _label_dict(label)
|
2016-07-18 22:20:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_repo_write
|
|
|
|
@nickname('deleteManifestLabel')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-20 18:24:43 +00:00
|
|
|
def delete(self, namespace_name, repository_name, manifestref, labelid):
|
2016-07-18 22:20:00 +00:00
|
|
|
""" Deletes an existing label from a manifest. """
|
2018-08-21 21:26:32 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace_name, repository_name)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
manifest = registry_model.lookup_manifest_by_digest(repo_ref, manifestref)
|
|
|
|
if manifest is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
deleted = registry_model.delete_manifest_label(manifest, labelid)
|
2016-07-18 22:20:00 +00:00
|
|
|
if deleted is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
metadata = {
|
|
|
|
'id': labelid,
|
|
|
|
'key': deleted.key,
|
|
|
|
'value': deleted.value,
|
2017-02-14 19:55:24 +00:00
|
|
|
'manifest_digest': manifestref,
|
2017-07-20 18:24:43 +00:00
|
|
|
'namespace': namespace_name,
|
|
|
|
'repo': repository_name,
|
2016-07-18 22:20:00 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 18:24:43 +00:00
|
|
|
log_action('manifest_label_delete', namespace_name, metadata, repo_name=repository_name)
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|