2017-03-23 01:52:45 +00:00
|
|
|
import logging
|
|
|
|
from base64 import b64encode
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
import cnr
|
|
|
|
from cnr.api.impl import registry as cnr_registry
|
|
|
|
from cnr.api.registry import _pull, repo_name
|
|
|
|
from cnr.exception import (
|
|
|
|
ChannelNotFound, CnrException, Forbidden, InvalidParams, InvalidRelease, InvalidUsage,
|
2017-04-13 12:25:47 +00:00
|
|
|
PackageAlreadyExists, PackageNotFound, PackageReleaseNotFound, UnableToLockResource,
|
|
|
|
UnauthorizedAccess, Unsupported)
|
|
|
|
from flask import jsonify, request
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
from auth.auth_context import get_authenticated_user
|
2017-10-27 18:55:49 +00:00
|
|
|
from auth.credentials import validate_credentials
|
2017-03-23 19:42:27 +00:00
|
|
|
from auth.decorators import process_auth
|
2017-07-26 00:41:55 +00:00
|
|
|
from auth.permissions import CreateRepositoryPermission, ModifyRepositoryPermission
|
|
|
|
from endpoints.appr import appr_bp, require_app_repo_read, require_app_repo_write
|
2017-08-10 00:45:46 +00:00
|
|
|
from endpoints.appr.cnr_backend import Blob, Channel, Package, User
|
2017-03-23 04:01:37 +00:00
|
|
|
from endpoints.appr.decorators import disallow_for_image_repository
|
2018-05-23 21:08:00 +00:00
|
|
|
from endpoints.appr.models_cnr import model
|
2017-03-23 01:52:45 +00:00
|
|
|
from endpoints.decorators import anon_allowed, anon_protect
|
2017-03-23 04:55:36 +00:00
|
|
|
from util.names import REPOSITORY_NAME_REGEX, TAG_REGEX
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.errorhandler(Unsupported)
|
|
|
|
@appr_bp.errorhandler(PackageAlreadyExists)
|
|
|
|
@appr_bp.errorhandler(InvalidRelease)
|
2017-03-24 11:01:11 +00:00
|
|
|
@appr_bp.errorhandler(Forbidden)
|
2017-03-23 01:52:45 +00:00
|
|
|
@appr_bp.errorhandler(UnableToLockResource)
|
|
|
|
@appr_bp.errorhandler(UnauthorizedAccess)
|
|
|
|
@appr_bp.errorhandler(PackageNotFound)
|
|
|
|
@appr_bp.errorhandler(PackageReleaseNotFound)
|
2017-08-10 00:45:46 +00:00
|
|
|
@appr_bp.errorhandler(CnrException)
|
2017-03-23 01:52:45 +00:00
|
|
|
@appr_bp.errorhandler(InvalidUsage)
|
|
|
|
@appr_bp.errorhandler(InvalidParams)
|
|
|
|
@appr_bp.errorhandler(ChannelNotFound)
|
|
|
|
def render_error(error):
|
|
|
|
response = jsonify({"error": error.to_dict()})
|
|
|
|
response.status_code = error.status_code
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/version")
|
|
|
|
@anon_allowed
|
|
|
|
def version():
|
2017-08-10 00:45:46 +00:00
|
|
|
return jsonify({"cnr-api": cnr.__version__})
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/users/login", methods=['POST'])
|
|
|
|
@anon_allowed
|
|
|
|
def login():
|
2017-03-23 19:07:46 +00:00
|
|
|
values = request.get_json(force=True, silent=True) or {}
|
|
|
|
username = values.get('user', {}).get('username')
|
|
|
|
password = values.get('user', {}).get('password')
|
|
|
|
if not username or not password:
|
|
|
|
raise InvalidUsage('Missing username or password')
|
|
|
|
|
2017-10-27 18:55:49 +00:00
|
|
|
result, _ = validate_credentials(username, password)
|
|
|
|
if not result.auth_valid:
|
|
|
|
raise UnauthorizedAccess(result.error_message)
|
2017-03-23 19:07:46 +00:00
|
|
|
|
2017-10-27 18:55:49 +00:00
|
|
|
return jsonify({'token': "basic " + b64encode("%s:%s" % (username, password))})
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
# @TODO: Redirect to S3 url
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/blobs/sha256/<string:digest>",
|
|
|
|
methods=['GET'],
|
2017-04-13 14:00:50 +00:00
|
|
|
strict_slashes=False,)
|
2017-03-23 16:41:56 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
2017-03-23 01:52:45 +00:00
|
|
|
def blobs(namespace, package_name, digest):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
data = cnr_registry.pull_blob(reponame, digest, blob_class=Blob)
|
2017-03-23 01:52:45 +00:00
|
|
|
json_format = request.args.get('format', None) == 'json'
|
|
|
|
return _pull(data, json_format=json_format)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/packages", methods=['GET'], strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@anon_protect
|
|
|
|
def list_packages():
|
|
|
|
namespace = request.args.get('namespace', None)
|
|
|
|
media_type = request.args.get('media_type', None)
|
|
|
|
query = request.args.get('query', None)
|
|
|
|
user = get_authenticated_user()
|
|
|
|
username = None
|
|
|
|
if user:
|
|
|
|
username = user.username
|
2017-08-10 00:45:46 +00:00
|
|
|
result_data = cnr_registry.list_packages(namespace, package_class=Package, search=query,
|
2017-04-13 14:00:50 +00:00
|
|
|
media_type=media_type, username=username)
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result_data)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>",
|
|
|
|
methods=['DELETE'], strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@require_app_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_package(namespace, package_name, release, media_type):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.delete_package(reponame, release, media_type, package_class=Package)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('delete_tag', namespace, repo_name=package_name,
|
|
|
|
metadata={'release': release, 'mediatype': media_type})
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>",
|
2017-04-13 14:00:50 +00:00
|
|
|
methods=['GET'], strict_slashes=False)
|
2017-03-23 04:01:37 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
2017-03-23 16:41:56 +00:00
|
|
|
@anon_protect
|
2017-03-23 01:52:45 +00:00
|
|
|
def show_package(namespace, package_name, release, media_type):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.show_package(reponame, release, media_type, channel_class=Channel,
|
2017-03-23 01:52:45 +00:00
|
|
|
package_class=Package)
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/packages/<string:namespace>/<string:package_name>", methods=['GET'],
|
|
|
|
strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
|
|
|
def show_package_releases(namespace, package_name):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
|
|
|
media_type = request.args.get('media_type', None)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.show_package_releases(reponame, media_type=media_type,
|
2017-03-23 01:52:45 +00:00
|
|
|
package_class=Package)
|
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>",
|
|
|
|
methods=['GET'], strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
2017-05-11 17:33:18 +00:00
|
|
|
def show_package_release_manifests(namespace, package_name, release):
|
2017-03-23 01:52:45 +00:00
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.show_package_manifests(reponame, release, package_class=Package)
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/<string:release>/<string:media_type>/pull",
|
|
|
|
methods=['GET'],
|
2017-04-13 14:00:50 +00:00
|
|
|
strict_slashes=False,)
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
|
|
|
def pull(namespace, package_name, release, media_type):
|
2018-01-04 18:05:50 +00:00
|
|
|
logger.debug('Pull of release %s of app repository %s/%s', release, namespace, package_name)
|
2017-03-23 01:52:45 +00:00
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
data = cnr_registry.pull(reponame, release, media_type, Package, blob_class=Blob)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('pull_repo', namespace, repo_name=package_name,
|
|
|
|
metadata={'release': release, 'mediatype': media_type})
|
2017-05-19 10:48:02 +00:00
|
|
|
json_format = request.args.get('format', None) == 'json'
|
|
|
|
return _pull(data, json_format)
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/packages/<string:namespace>/<string:package_name>", methods=['POST'],
|
|
|
|
strict_slashes=False)
|
2017-03-23 04:01:37 +00:00
|
|
|
@disallow_for_image_repository()
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@anon_protect
|
|
|
|
def push(namespace, package_name):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-03-23 04:51:54 +00:00
|
|
|
|
|
|
|
if not REPOSITORY_NAME_REGEX.match(package_name):
|
|
|
|
logger.debug('Found invalid repository name CNR push: %s', reponame)
|
2017-04-13 12:25:47 +00:00
|
|
|
raise InvalidUsage('invalid repository name: %s' % reponame)
|
2017-03-23 04:51:54 +00:00
|
|
|
|
2017-03-23 17:14:12 +00:00
|
|
|
values = request.get_json(force=True, silent=True) or {}
|
2017-04-05 18:11:06 +00:00
|
|
|
private = values.get('visibility', 'private')
|
2017-03-23 17:14:12 +00:00
|
|
|
|
2017-03-23 01:52:45 +00:00
|
|
|
owner = get_authenticated_user()
|
|
|
|
if not Package.exists(reponame):
|
|
|
|
if not CreateRepositoryPermission(namespace).can():
|
2017-03-24 11:01:11 +00:00
|
|
|
raise Forbidden("Unauthorized access for: %s" % reponame,
|
2017-04-13 14:00:50 +00:00
|
|
|
{"package": reponame,
|
|
|
|
"scopes": ['create']})
|
2017-03-23 01:52:45 +00:00
|
|
|
Package.create_repository(reponame, private, owner)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('create_repo', namespace, repo_name=package_name)
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
if not ModifyRepositoryPermission(namespace, package_name).can():
|
2017-03-24 11:01:11 +00:00
|
|
|
raise Forbidden("Unauthorized access for: %s" % reponame,
|
2017-04-13 14:00:50 +00:00
|
|
|
{"package": reponame,
|
|
|
|
"scopes": ['push']})
|
2017-03-23 01:52:45 +00:00
|
|
|
|
2017-03-23 17:14:12 +00:00
|
|
|
if not 'release' in values:
|
|
|
|
raise InvalidUsage('Missing release')
|
|
|
|
|
2018-01-12 22:14:05 +00:00
|
|
|
if not 'media_type' in values:
|
|
|
|
raise InvalidUsage('Missing media_type')
|
|
|
|
|
|
|
|
if not 'blob' in values:
|
|
|
|
raise InvalidUsage('Missing blob')
|
|
|
|
|
|
|
|
release_version = str(values['release'])
|
2017-03-23 17:14:12 +00:00
|
|
|
media_type = values['media_type']
|
|
|
|
force = request.args.get('force', 'false') == 'true'
|
|
|
|
|
2017-03-23 01:52:45 +00:00
|
|
|
blob = Blob(reponame, values['blob'])
|
2017-08-10 00:45:46 +00:00
|
|
|
app_release = cnr_registry.push(reponame, release_version, media_type, blob, force,
|
2017-03-23 01:52:45 +00:00
|
|
|
package_class=Package, user=owner, visibility=private)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('push_repo', namespace, repo_name=package_name,
|
|
|
|
metadata={'release': release_version})
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(app_release)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route("/api/v1/packages/search", methods=['GET'], strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@anon_protect
|
|
|
|
def search_packages():
|
|
|
|
query = request.args.get("q")
|
|
|
|
user = get_authenticated_user()
|
|
|
|
username = None
|
|
|
|
if user:
|
|
|
|
username = user.username
|
|
|
|
|
2017-08-10 00:45:46 +00:00
|
|
|
search_results = cnr_registry.search(query, Package, username=username)
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(search_results)
|
|
|
|
|
|
|
|
|
|
|
|
# CHANNELS
|
|
|
|
@appr_bp.route("/api/v1/packages/<string:namespace>/<string:package_name>/channels",
|
|
|
|
methods=['GET'], strict_slashes=False)
|
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
|
|
|
def list_channels(namespace, package_name):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
return jsonify(cnr_registry.list_channels(reponame, channel_class=Channel))
|
2017-03-23 01:52:45 +00:00
|
|
|
|
|
|
|
|
2017-04-13 14:00:50 +00:00
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>",
|
|
|
|
methods=['GET'], strict_slashes=False)
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_read
|
|
|
|
@anon_protect
|
|
|
|
def show_channel(namespace, package_name, channel_name):
|
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
channel = cnr_registry.show_channel(reponame, channel_name, channel_class=Channel)
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(channel)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>/<string:release>",
|
|
|
|
methods=['POST'],
|
2017-04-13 14:00:50 +00:00
|
|
|
strict_slashes=False,)
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def add_channel_release(namespace, package_name, channel_name, release):
|
2017-04-13 12:25:47 +00:00
|
|
|
_check_channel_name(channel_name, release)
|
2017-03-23 01:52:45 +00:00
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.add_channel_release(reponame, channel_name, release, channel_class=Channel,
|
2017-03-23 01:52:45 +00:00
|
|
|
package_class=Package)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('create_tag', namespace, repo_name=package_name,
|
|
|
|
metadata={'channel': channel_name, 'release': release})
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result)
|
|
|
|
|
2017-04-13 14:00:50 +00:00
|
|
|
|
2017-04-13 12:25:47 +00:00
|
|
|
def _check_channel_name(channel_name, release=None):
|
|
|
|
if not TAG_REGEX.match(channel_name):
|
|
|
|
logger.debug('Found invalid channel name CNR add channel release: %s', channel_name)
|
|
|
|
raise InvalidUsage("Found invalid channelname %s" % release,
|
|
|
|
{'name': channel_name,
|
2017-05-11 17:33:18 +00:00
|
|
|
'release': release})
|
2017-04-13 12:25:47 +00:00
|
|
|
|
|
|
|
if release is not None and not TAG_REGEX.match(release):
|
|
|
|
logger.debug('Found invalid release name CNR add channel release: %s', release)
|
2017-05-11 17:33:18 +00:00
|
|
|
raise InvalidUsage('Found invalid channel release name %s' % release,
|
2017-04-13 12:25:47 +00:00
|
|
|
{'name': channel_name,
|
2017-05-11 17:33:18 +00:00
|
|
|
'release': release})
|
2017-03-23 01:52:45 +00:00
|
|
|
|
2017-04-13 14:00:50 +00:00
|
|
|
|
2017-03-23 01:52:45 +00:00
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>/<string:release>",
|
|
|
|
methods=['DELETE'],
|
2017-04-13 14:00:50 +00:00
|
|
|
strict_slashes=False,)
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_channel_release(namespace, package_name, channel_name, release):
|
2017-04-13 12:25:47 +00:00
|
|
|
_check_channel_name(channel_name, release)
|
2017-03-23 01:52:45 +00:00
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.delete_channel_release(reponame, channel_name, release,
|
2017-03-23 01:52:45 +00:00
|
|
|
channel_class=Channel, package_class=Package)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('delete_tag', namespace, repo_name=package_name,
|
|
|
|
metadata={'channel': channel_name, 'release': release})
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result)
|
|
|
|
|
|
|
|
|
|
|
|
@appr_bp.route(
|
|
|
|
"/api/v1/packages/<string:namespace>/<string:package_name>/channels/<string:channel_name>",
|
|
|
|
methods=['DELETE'],
|
2017-04-13 14:00:50 +00:00
|
|
|
strict_slashes=False,)
|
2017-03-23 01:52:45 +00:00
|
|
|
@process_auth
|
|
|
|
@require_app_repo_write
|
|
|
|
@anon_protect
|
|
|
|
def delete_channel(namespace, package_name, channel_name):
|
2017-04-13 12:25:47 +00:00
|
|
|
_check_channel_name(channel_name)
|
2017-03-23 01:52:45 +00:00
|
|
|
reponame = repo_name(namespace, package_name)
|
2017-08-10 00:45:46 +00:00
|
|
|
result = cnr_registry.delete_channel(reponame, channel_name, channel_class=Channel)
|
2017-05-11 17:33:18 +00:00
|
|
|
model.log_action('delete_tag', namespace, repo_name=package_name,
|
|
|
|
metadata={'channel': channel_name})
|
2017-03-23 01:52:45 +00:00
|
|
|
return jsonify(result)
|