2015-05-14 20:47:38 +00:00
|
|
|
""" Manage the tags of a repository. """
|
2018-11-14 13:59:05 +00:00
|
|
|
import json
|
2015-05-14 20:47:38 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
from datetime import datetime
|
2015-04-15 19:21:09 +00:00
|
|
|
from flask import request, abort
|
2014-03-26 23:42:29 +00:00
|
|
|
|
2018-11-12 21:27:49 +00:00
|
|
|
from app import storage
|
2017-06-27 14:01:20 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2018-08-22 22:03:26 +00:00
|
|
|
from data.registry_model import registry_model
|
2017-06-27 18:24:23 +00:00
|
|
|
from endpoints.api import (resource, nickname, require_repo_read, require_repo_write,
|
|
|
|
RepositoryParamResource, log_action, validate_json_request, path_param,
|
2018-09-14 19:30:54 +00:00
|
|
|
parse_args, query_param, truthy_bool, disallow_for_app_repositories,
|
|
|
|
format_date)
|
2018-08-22 22:03:26 +00:00
|
|
|
from endpoints.api.image import image_dict
|
2017-07-17 13:36:05 +00:00
|
|
|
from endpoints.exception import NotFound, InvalidRequest
|
2015-10-05 20:36:33 +00:00
|
|
|
from util.names import TAG_ERROR, TAG_REGEX
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
def _tag_dict(tag):
|
|
|
|
tag_info = {
|
|
|
|
'name': tag.name,
|
|
|
|
'reversion': tag.reversion,
|
|
|
|
}
|
|
|
|
|
|
|
|
if tag.lifetime_start_ts > 0:
|
|
|
|
tag_info['start_ts'] = tag.lifetime_start_ts
|
|
|
|
|
|
|
|
if tag.lifetime_end_ts > 0:
|
|
|
|
tag_info['end_ts'] = tag.lifetime_end_ts
|
|
|
|
|
2018-11-14 13:59:05 +00:00
|
|
|
# TODO(jschorr): Remove this once fully on OCI data model.
|
|
|
|
if tag.legacy_image_if_present:
|
2018-08-22 22:03:26 +00:00
|
|
|
tag_info['docker_image_id'] = tag.legacy_image.docker_image_id
|
2018-09-14 19:30:54 +00:00
|
|
|
tag_info['image_id'] = tag.legacy_image.docker_image_id
|
|
|
|
tag_info['size'] = tag.legacy_image.aggregate_size
|
|
|
|
|
2018-11-14 13:59:05 +00:00
|
|
|
# TODO(jschorr): Remove this check once fully on OCI data model.
|
|
|
|
if tag.manifest_digest:
|
|
|
|
tag_info['manifest_digest'] = tag.manifest_digest
|
|
|
|
if tag.manifest:
|
|
|
|
try:
|
|
|
|
tag_info['manifest'] = json.loads(tag.manifest.manifest_bytes)
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
tag_info['is_manifest_list'] = tag.manifest.is_manifest_list
|
|
|
|
|
|
|
|
if 'size' not in tag_info:
|
|
|
|
tag_info['size'] = tag.manifest.layers_compressed_size
|
|
|
|
|
2018-09-14 19:30:54 +00:00
|
|
|
if tag.lifetime_start_ts > 0:
|
2018-11-07 21:51:49 +00:00
|
|
|
last_modified = format_date(datetime.utcfromtimestamp(tag.lifetime_start_ts))
|
2018-09-14 19:30:54 +00:00
|
|
|
tag_info['last_modified'] = last_modified
|
|
|
|
|
|
|
|
if tag.lifetime_end_ts is not None:
|
2018-11-07 21:51:49 +00:00
|
|
|
expiration = format_date(datetime.utcfromtimestamp(tag.lifetime_end_ts))
|
2018-09-14 19:30:54 +00:00
|
|
|
tag_info['expiration'] = expiration
|
2018-08-22 22:03:26 +00:00
|
|
|
|
|
|
|
return tag_info
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/')
|
2015-04-15 19:21:09 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
class ListRepositoryTags(RepositoryParamResource):
|
2015-06-30 08:38:58 +00:00
|
|
|
""" Resource for listing full repository tag history, alive *and dead*. """
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2015-08-05 21:07:35 +00:00
|
|
|
@require_repo_read
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-04-16 21:18:00 +00:00
|
|
|
@query_param('specificTag', 'Filters the tags to the specific tag.', type=str, default='')
|
2017-05-04 15:33:36 +00:00
|
|
|
@query_param('limit', 'Limit to the number of results to return per page. Max 100.', type=int,
|
|
|
|
default=50)
|
2015-06-30 08:38:58 +00:00
|
|
|
@query_param('page', 'Page index for the results. Default 1.', type=int, default=1)
|
2018-09-14 19:30:54 +00:00
|
|
|
@query_param('onlyActiveTags', 'Filter to only active tags.', type=truthy_bool, default=False)
|
2015-04-15 19:21:09 +00:00
|
|
|
@nickname('listRepoTags')
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, parsed_args):
|
|
|
|
specific_tag = parsed_args.get('specificTag') or None
|
|
|
|
page = max(1, parsed_args.get('page', 1))
|
|
|
|
limit = min(100, max(1, parsed_args.get('limit', 50)))
|
2018-09-14 19:30:54 +00:00
|
|
|
active_tags_only = parsed_args.get('onlyActiveTags')
|
2017-06-27 14:01:20 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
2017-06-27 14:01:20 +00:00
|
|
|
raise NotFound()
|
2015-06-30 08:38:58 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
history, has_more = registry_model.list_repository_tag_history(repo_ref, page=page,
|
|
|
|
size=limit,
|
2018-09-14 19:30:54 +00:00
|
|
|
specific_tag_name=specific_tag,
|
|
|
|
active_tags_only=active_tags_only)
|
2015-06-30 08:38:58 +00:00
|
|
|
return {
|
2018-08-22 22:03:26 +00:00
|
|
|
'tags': [_tag_dict(tag) for tag in history],
|
2015-06-30 08:38:58 +00:00
|
|
|
'page': page,
|
2018-08-22 22:03:26 +00:00
|
|
|
'has_additional': has_more,
|
2015-06-30 08:38:58 +00:00
|
|
|
}
|
2015-04-15 19:21:09 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTag(RepositoryParamResource):
|
|
|
|
""" Resource for managing repository tags. """
|
2014-03-26 23:42:29 +00:00
|
|
|
schemas = {
|
2017-07-17 13:36:05 +00:00
|
|
|
'ChangeTag': {
|
2014-03-26 23:42:29 +00:00
|
|
|
'type': 'object',
|
2017-06-22 01:33:26 +00:00
|
|
|
'description': 'Makes changes to a specific tag',
|
2014-03-26 23:42:29 +00:00
|
|
|
'properties': {
|
|
|
|
'image': {
|
2017-06-22 01:33:26 +00:00
|
|
|
'type': ['string', 'null'],
|
|
|
|
'description': '(If specified) Image identifier to which the tag should point',
|
|
|
|
},
|
2017-07-17 13:36:05 +00:00
|
|
|
'expiration': {
|
2017-06-22 01:33:26 +00:00
|
|
|
'type': ['number', 'null'],
|
|
|
|
'description': '(If specified) The expiration for the image',
|
2014-03-26 23:42:29 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-07-17 13:36:05 +00:00
|
|
|
@nickname('changeTag')
|
|
|
|
@validate_json_request('ChangeTag')
|
2014-03-26 23:42:29 +00:00
|
|
|
def put(self, namespace, repository, tag):
|
|
|
|
""" Change which image a tag points to or create a new tag."""
|
2015-10-05 20:36:33 +00:00
|
|
|
if not TAG_REGEX.match(tag):
|
|
|
|
abort(400, TAG_ERROR)
|
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
2014-03-26 23:42:29 +00:00
|
|
|
raise NotFound()
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2017-06-22 01:33:26 +00:00
|
|
|
if 'expiration' in request.get_json():
|
2018-08-22 22:03:26 +00:00
|
|
|
tag_ref = registry_model.get_repo_tag(repo_ref, tag)
|
|
|
|
if tag_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2017-06-22 01:33:26 +00:00
|
|
|
expiration = request.get_json().get('expiration')
|
|
|
|
expiration_date = None
|
|
|
|
if expiration is not None:
|
2018-08-22 22:03:26 +00:00
|
|
|
try:
|
|
|
|
expiration_date = datetime.utcfromtimestamp(float(expiration))
|
|
|
|
except ValueError:
|
|
|
|
abort(400)
|
2017-06-22 01:33:26 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
if expiration_date <= datetime.now():
|
|
|
|
abort(400)
|
2017-06-22 01:33:26 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
existing_end_ts, ok = registry_model.change_repository_tag_expiration(tag_ref,
|
|
|
|
expiration_date)
|
2017-06-22 01:33:26 +00:00
|
|
|
if ok:
|
2018-08-22 22:03:26 +00:00
|
|
|
if not (existing_end_ts is None and expiration_date is None):
|
|
|
|
log_action('change_tag_expiration', namespace, {
|
|
|
|
'username': get_authenticated_user().username,
|
|
|
|
'repo': repository,
|
|
|
|
'tag': tag,
|
|
|
|
'namespace': namespace,
|
|
|
|
'expiration_date': expiration_date,
|
|
|
|
'old_expiration_date': existing_end_ts
|
|
|
|
}, repo_name=repository)
|
2017-06-22 01:33:26 +00:00
|
|
|
else:
|
2017-07-17 13:36:05 +00:00
|
|
|
raise InvalidRequest('Could not update tag expiration; Tag has probably changed')
|
2017-06-22 01:33:26 +00:00
|
|
|
|
2018-11-20 12:01:35 +00:00
|
|
|
if 'image' in request.get_json() or 'manifest_digest' in request.get_json():
|
2018-08-22 22:03:26 +00:00
|
|
|
existing_tag = registry_model.get_repo_tag(repo_ref, tag, include_legacy_image=True)
|
|
|
|
|
2018-11-20 12:01:35 +00:00
|
|
|
manifest_or_image = None
|
|
|
|
image_id = None
|
|
|
|
manifest_digest = None
|
|
|
|
|
|
|
|
if 'image' in request.get_json():
|
|
|
|
image_id = request.get_json()['image']
|
|
|
|
manifest_or_image = registry_model.get_legacy_image(repo_ref, image_id)
|
|
|
|
else:
|
|
|
|
manifest_digest = request.get_json()['manifest_digest']
|
|
|
|
manifest_or_image = registry_model.lookup_manifest_by_digest(repo_ref, manifest_digest)
|
|
|
|
|
|
|
|
if manifest_or_image is None:
|
2017-07-17 13:36:05 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2018-11-20 12:01:35 +00:00
|
|
|
# TODO(jschorr): Remove this check once fully on V22
|
|
|
|
existing_manifest_digest = None
|
|
|
|
if existing_tag:
|
|
|
|
existing_manifest = registry_model.get_manifest_for_tag(existing_tag)
|
|
|
|
existing_manifest_digest = existing_manifest.digest if existing_manifest else None
|
|
|
|
|
|
|
|
if not registry_model.retarget_tag(repo_ref, tag, manifest_or_image, storage):
|
2018-08-22 22:03:26 +00:00
|
|
|
raise InvalidRequest('Could not move tag')
|
2017-06-22 01:33:26 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
2018-08-22 22:03:26 +00:00
|
|
|
|
|
|
|
log_action('move_tag' if existing_tag else 'create_tag', namespace, {
|
2017-06-22 01:33:26 +00:00
|
|
|
'username': username,
|
|
|
|
'repo': repository,
|
|
|
|
'tag': tag,
|
|
|
|
'namespace': namespace,
|
|
|
|
'image': image_id,
|
2018-11-20 12:01:35 +00:00
|
|
|
'manifest_digest': manifest_digest,
|
|
|
|
'original_image': (existing_tag.legacy_image.docker_image_id
|
|
|
|
if existing_tag and existing_tag.legacy_image_if_present
|
|
|
|
else None),
|
|
|
|
'original_manifest_digest': existing_manifest_digest,
|
2017-06-22 01:33:26 +00:00
|
|
|
}, repo_name=repository)
|
2018-08-22 22:03:26 +00:00
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
return 'Updated', 201
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2014-03-26 23:42:29 +00:00
|
|
|
@require_repo_write
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2014-03-14 17:06:58 +00:00
|
|
|
@nickname('deleteFullTag')
|
|
|
|
def delete(self, namespace, repository, tag):
|
|
|
|
""" Delete the specified repository tag. """
|
2018-08-22 22:03:26 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
|
|
|
registry_model.delete_tag(repo_ref, tag)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
username = get_authenticated_user().username
|
|
|
|
log_action('delete_tag', namespace,
|
2017-05-04 15:33:36 +00:00
|
|
|
{'username': username,
|
|
|
|
'repo': repository,
|
|
|
|
'namespace': namespace,
|
2017-07-10 18:45:23 +00:00
|
|
|
'tag': tag}, repo_name=repository)
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2016-12-06 21:26:28 +00:00
|
|
|
return '', 204
|
2014-03-14 17:06:58 +00:00
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>/images')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2014-03-14 17:06:58 +00:00
|
|
|
class RepositoryTagImages(RepositoryParamResource):
|
|
|
|
""" Resource for listing the images in a specific repository tag. """
|
2017-05-04 15:33:36 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
@require_repo_read
|
|
|
|
@nickname('listTagImages')
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-11-03 23:15:23 +00:00
|
|
|
@query_param('owned', 'If specified, only images wholely owned by this tag are returned.',
|
|
|
|
type=truthy_bool, default=False)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, tag, parsed_args):
|
2014-03-14 17:06:58 +00:00
|
|
|
""" List the images for the specified repository tag. """
|
2018-08-22 22:03:26 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
tag_ref = registry_model.get_repo_tag(repo_ref, tag, include_legacy_image=True)
|
|
|
|
if tag_ref is None:
|
2017-07-10 18:45:23 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2018-11-19 21:33:37 +00:00
|
|
|
if tag_ref.legacy_image_if_present is None:
|
|
|
|
return {'images': []}
|
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
image_id = tag_ref.legacy_image.docker_image_id
|
2014-03-14 17:06:58 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
all_images = None
|
2016-01-26 21:27:36 +00:00
|
|
|
if parsed_args['owned']:
|
2018-11-05 18:03:08 +00:00
|
|
|
# TODO(jschorr): Remove the `owned` image concept once we are fully on V2_2.
|
2018-08-22 22:03:26 +00:00
|
|
|
all_images = registry_model.get_legacy_images_owned_by_tag(tag_ref)
|
|
|
|
else:
|
|
|
|
image_with_parents = registry_model.get_legacy_image(repo_ref, image_id, include_parents=True)
|
|
|
|
if image_with_parents is None:
|
|
|
|
raise NotFound()
|
2015-11-03 23:15:23 +00:00
|
|
|
|
2018-08-22 22:03:26 +00:00
|
|
|
all_images = [image_with_parents] + image_with_parents.parents
|
2015-11-03 23:15:23 +00:00
|
|
|
|
2014-03-14 17:06:58 +00:00
|
|
|
return {
|
2018-08-22 22:03:26 +00:00
|
|
|
'images': [image_dict(image) for image in all_images],
|
2014-03-14 17:06:58 +00:00
|
|
|
}
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/tag/<tag>/restore')
|
2015-04-16 21:18:00 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
@path_param('tag', 'The name of the tag')
|
2017-03-03 22:23:23 +00:00
|
|
|
class RestoreTag(RepositoryParamResource):
|
|
|
|
""" Resource for restoring a repository tag back to a previous image. """
|
2015-04-16 21:18:00 +00:00
|
|
|
schemas = {
|
2017-03-03 22:23:23 +00:00
|
|
|
'RestoreTag': {
|
2015-04-16 21:18:00 +00:00
|
|
|
'type': 'object',
|
2017-03-03 22:23:23 +00:00
|
|
|
'description': 'Restores a tag to a specific image',
|
2017-07-10 13:46:02 +00:00
|
|
|
'required': ['image',],
|
2015-04-16 21:18:00 +00:00
|
|
|
'properties': {
|
|
|
|
'image': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'Image identifier to which the tag should point',
|
|
|
|
},
|
2017-03-03 22:23:23 +00:00
|
|
|
'manifest_digest': {
|
|
|
|
'type': 'string',
|
|
|
|
'description': 'If specified, the manifest digest that should be used',
|
|
|
|
},
|
2015-04-16 21:18:00 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
@require_repo_write
|
2017-03-22 18:30:13 +00:00
|
|
|
@disallow_for_app_repositories
|
2017-03-03 22:23:23 +00:00
|
|
|
@nickname('restoreTag')
|
|
|
|
@validate_json_request('RestoreTag')
|
2015-04-16 21:18:00 +00:00
|
|
|
def post(self, namespace, repository, tag):
|
2017-03-03 22:23:23 +00:00
|
|
|
""" Restores a repository tag back to a previous image in the repository. """
|
2018-08-22 22:03:26 +00:00
|
|
|
repo_ref = registry_model.lookup_repository(namespace, repository)
|
|
|
|
if repo_ref is None:
|
|
|
|
raise NotFound()
|
2015-04-16 21:18:00 +00:00
|
|
|
|
2017-03-03 22:23:23 +00:00
|
|
|
# Restore the tag back to the previous image.
|
2015-04-16 21:18:00 +00:00
|
|
|
image_id = request.get_json()['image']
|
2017-03-03 22:23:23 +00:00
|
|
|
manifest_digest = request.get_json().get('manifest_digest', None)
|
2015-04-16 21:18:00 +00:00
|
|
|
|
2017-05-02 20:13:21 +00:00
|
|
|
# Data for logging the reversion/restoration.
|
2015-04-16 21:18:00 +00:00
|
|
|
username = get_authenticated_user().username
|
2017-03-03 22:23:23 +00:00
|
|
|
log_data = {
|
|
|
|
'username': username,
|
|
|
|
'repo': repository,
|
|
|
|
'tag': tag,
|
|
|
|
'image': image_id,
|
|
|
|
}
|
2018-08-22 22:03:26 +00:00
|
|
|
|
|
|
|
manifest_or_legacy_image = None
|
2017-05-02 20:13:21 +00:00
|
|
|
if manifest_digest is not None:
|
2018-08-22 22:03:26 +00:00
|
|
|
manifest_or_legacy_image = registry_model.lookup_manifest_by_digest(repo_ref, manifest_digest,
|
|
|
|
allow_dead=True)
|
2017-05-02 20:13:21 +00:00
|
|
|
else:
|
2018-08-22 22:03:26 +00:00
|
|
|
manifest_or_legacy_image = registry_model.get_legacy_image(repo_ref, image_id)
|
|
|
|
|
|
|
|
if manifest_or_legacy_image is None:
|
|
|
|
raise NotFound()
|
|
|
|
|
2018-11-12 21:27:49 +00:00
|
|
|
if not registry_model.retarget_tag(repo_ref, tag, manifest_or_legacy_image, storage,
|
|
|
|
is_reversion=True):
|
2018-08-22 22:03:26 +00:00
|
|
|
raise InvalidRequest('Could not restore tag')
|
2017-05-02 20:13:21 +00:00
|
|
|
|
2017-07-10 18:45:23 +00:00
|
|
|
log_action('revert_tag', namespace, log_data, repo_name=repository)
|
2015-04-16 21:18:00 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
'image_id': image_id,
|
|
|
|
}
|