2013-09-25 21:50:03 +00:00
|
|
|
import logging
|
2013-09-30 20:14:48 +00:00
|
|
|
import json
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
from flask import abort, request, jsonify, make_response, session
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2018-11-12 21:27:49 +00:00
|
|
|
from app import storage
|
2017-03-16 20:50:09 +00:00
|
|
|
from auth.decorators import process_auth
|
2017-06-29 17:24:00 +00:00
|
|
|
from auth.permissions import (ReadRepositoryPermission, ModifyRepositoryPermission)
|
2018-09-24 22:57:27 +00:00
|
|
|
from data.registry_model import registry_model
|
|
|
|
from data.registry_model.manifestbuilder import lookup_manifest_builder
|
2017-07-20 15:31:22 +00:00
|
|
|
from endpoints.decorators import anon_protect, parse_repository_name
|
2015-06-22 21:37:13 +00:00
|
|
|
from endpoints.v1 import v1_bp
|
2017-05-11 17:33:18 +00:00
|
|
|
from util.audit import track_and_log
|
|
|
|
from util.names import TAG_ERROR, TAG_REGEX
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v1_bp.route('/repositories/<repopath:repository>/tags', methods=['GET'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
|
|
|
def get_tags(namespace_name, repo_name):
|
|
|
|
permission = ReadRepositoryPermission(namespace_name, repo_name)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name, kind_filter='image')
|
|
|
|
if permission.can() or (repository_ref is not None and repository_ref.is_public):
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(404)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2018-11-14 12:05:06 +00:00
|
|
|
tag_map = registry_model.get_legacy_tags_map(repository_ref, storage)
|
2013-09-25 21:50:03 +00:00
|
|
|
return jsonify(tag_map)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['GET'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2015-05-19 21:52:44 +00:00
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
|
|
|
def get_tag(namespace_name, repo_name, tag):
|
|
|
|
permission = ReadRepositoryPermission(namespace_name, repo_name)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name, kind_filter='image')
|
|
|
|
if permission.can() or (repository_ref is not None and repository_ref.is_public):
|
|
|
|
if repository_ref is None:
|
|
|
|
abort(404)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2019-01-10 18:36:05 +00:00
|
|
|
image_id = registry_model.get_tag_legacy_image_id(repository_ref, tag, storage)
|
|
|
|
if image_id is None:
|
2015-09-29 19:02:03 +00:00
|
|
|
abort(404)
|
|
|
|
|
2019-01-10 18:36:05 +00:00
|
|
|
resp = make_response('"%s"' % image_id)
|
2013-11-12 01:00:01 +00:00
|
|
|
resp.headers['Content-Type'] = 'application/json'
|
|
|
|
return resp
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['PUT'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2015-06-02 19:56:44 +00:00
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
|
|
|
def put_tag(namespace_name, repo_name, tag):
|
|
|
|
permission = ModifyRepositoryPermission(namespace_name, repo_name)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name, kind_filter='image')
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
if permission.can() and repository_ref is not None:
|
2015-10-05 20:36:33 +00:00
|
|
|
if not TAG_REGEX.match(tag):
|
|
|
|
abort(400, TAG_ERROR)
|
|
|
|
|
2016-07-12 17:48:44 +00:00
|
|
|
image_id = json.loads(request.data)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
# Check for the image ID first in a builder (for an in-progress push).
|
2018-11-12 21:27:49 +00:00
|
|
|
builder = lookup_manifest_builder(repository_ref, session.get('manifest_builder'), storage)
|
2018-09-24 22:57:27 +00:00
|
|
|
if builder is not None:
|
|
|
|
layer = builder.lookup_layer(image_id)
|
|
|
|
if layer is not None:
|
|
|
|
commited_tag = builder.commit_tag_and_manifest(tag, layer)
|
|
|
|
if commited_tag is None:
|
|
|
|
abort(400)
|
2014-10-22 18:14:56 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
return make_response('Created', 200)
|
|
|
|
|
|
|
|
# Check if there is an existing image we should use (for PUT calls outside of a normal push
|
|
|
|
# operation).
|
|
|
|
legacy_image = registry_model.get_legacy_image(repository_ref, image_id)
|
|
|
|
if legacy_image is None:
|
|
|
|
abort(400)
|
|
|
|
|
2018-11-12 21:27:49 +00:00
|
|
|
if registry_model.retarget_tag(repository_ref, tag, legacy_image, storage) is None:
|
2018-09-24 22:57:27 +00:00
|
|
|
abort(400)
|
2014-10-22 18:14:56 +00:00
|
|
|
|
2013-09-25 21:50:03 +00:00
|
|
|
return make_response('Created', 200)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@v1_bp.route('/repositories/<repopath:repository>/tags/<tag>', methods=['DELETE'])
|
2013-09-25 21:50:03 +00:00
|
|
|
@process_auth
|
2015-06-02 19:56:44 +00:00
|
|
|
@anon_protect
|
2016-03-09 21:20:28 +00:00
|
|
|
@parse_repository_name()
|
|
|
|
def delete_tag(namespace_name, repo_name, tag):
|
|
|
|
permission = ModifyRepositoryPermission(namespace_name, repo_name)
|
2018-09-24 22:57:27 +00:00
|
|
|
repository_ref = registry_model.lookup_repository(namespace_name, repo_name, kind_filter='image')
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
if permission.can() and repository_ref is not None:
|
|
|
|
if not registry_model.delete_tag(repository_ref, tag):
|
|
|
|
abort(404)
|
2017-03-22 18:30:33 +00:00
|
|
|
|
2018-09-24 22:57:27 +00:00
|
|
|
track_and_log('delete_tag', repository_ref, tag=tag)
|
2014-02-06 22:33:50 +00:00
|
|
|
return make_response('Deleted', 200)
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
abort(403)
|