2015-06-22 21:37:13 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
|
2016-07-26 22:41:51 +00:00
|
|
|
from cachetools import lru_cache
|
2018-01-05 21:27:03 +00:00
|
|
|
from flask import request, jsonify
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2017-04-15 12:26:33 +00:00
|
|
|
import features
|
2016-05-31 20:48:19 +00:00
|
|
|
from app import app, userevents, instance_keys
|
2018-01-05 21:27:03 +00:00
|
|
|
from auth.auth_context import get_authenticated_context, get_authenticated_user
|
2017-03-16 21:06:05 +00:00
|
|
|
from auth.decorators import process_basic_auth
|
2015-06-22 21:37:13 +00:00
|
|
|
from auth.permissions import (ModifyRepositoryPermission, ReadRepositoryPermission,
|
2016-11-18 19:46:11 +00:00
|
|
|
CreateRepositoryPermission, AdministerRepositoryPermission)
|
2016-05-04 21:40:09 +00:00
|
|
|
from endpoints.decorators import anon_protect
|
2017-03-16 21:06:05 +00:00
|
|
|
from endpoints.v2 import v2_bp
|
2017-03-30 20:12:27 +00:00
|
|
|
from endpoints.v2.errors import InvalidLogin, NameInvalid, InvalidRequest, Unsupported, Unauthorized
|
2017-06-26 22:10:39 +00:00
|
|
|
from endpoints.v2.models_pre_oci import data_model as model
|
2015-06-22 21:37:13 +00:00
|
|
|
from util.cache import no_cache
|
2015-10-05 18:19:52 +00:00
|
|
|
from util.names import parse_namespace_repository, REPOSITORY_NAME_REGEX
|
2017-06-26 22:16:15 +00:00
|
|
|
from util.security.registry_jwt import (generate_bearer_token, build_context_and_subject,
|
|
|
|
QUAY_TUF_ROOT, SIGNER_TUF_ROOT, DISABLED_TUF_ROOT)
|
2016-05-04 21:40:09 +00:00
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-09-10 16:24:33 +00:00
|
|
|
TOKEN_VALIDITY_LIFETIME_S = 60 * 60 # 1 hour
|
2017-03-30 20:12:27 +00:00
|
|
|
SCOPE_REGEX_TEMPLATE = r'^repository:((?:{}\/)?((?:[\.a-zA-Z0-9_\-]+\/)*[\.a-zA-Z0-9_\-]+)):((?:push|pull|\*)(?:,(?:push|pull|\*))*)$'
|
2016-05-23 20:36:48 +00:00
|
|
|
|
2017-06-26 22:16:15 +00:00
|
|
|
|
2016-05-23 20:36:48 +00:00
|
|
|
@lru_cache(maxsize=1)
|
|
|
|
def get_scope_regex():
|
|
|
|
hostname = re.escape(app.config['SERVER_HOSTNAME'])
|
|
|
|
scope_regex_string = SCOPE_REGEX_TEMPLATE.format(hostname)
|
|
|
|
return re.compile(scope_regex_string)
|
|
|
|
|
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
@v2_bp.route('/auth')
|
2017-03-16 21:06:05 +00:00
|
|
|
@process_basic_auth
|
2015-06-22 21:37:13 +00:00
|
|
|
@no_cache
|
2015-08-27 18:55:33 +00:00
|
|
|
@anon_protect
|
2017-03-16 21:06:05 +00:00
|
|
|
def generate_registry_jwt(auth_result):
|
2016-08-02 00:59:39 +00:00
|
|
|
"""
|
|
|
|
This endpoint will generate a JWT conforming to the Docker Registry v2 Auth Spec:
|
|
|
|
https://docs.docker.com/registry/spec/auth/token/
|
2015-07-16 19:49:06 +00:00
|
|
|
"""
|
2015-06-22 21:37:13 +00:00
|
|
|
audience_param = request.args.get('service')
|
|
|
|
logger.debug('Request audience: %s', audience_param)
|
|
|
|
|
2015-12-09 20:07:37 +00:00
|
|
|
scope_param = request.args.get('scope') or ''
|
2015-06-22 21:37:13 +00:00
|
|
|
logger.debug('Scope request: %s', scope_param)
|
|
|
|
|
2017-03-16 21:06:05 +00:00
|
|
|
auth_header = request.headers.get('authorization', '')
|
|
|
|
auth_credentials_sent = bool(auth_header)
|
2018-01-05 21:27:03 +00:00
|
|
|
|
|
|
|
has_valid_auth_context = False
|
|
|
|
if get_authenticated_context():
|
|
|
|
has_valid_auth_context = not get_authenticated_context().is_anonymous
|
|
|
|
|
|
|
|
if auth_credentials_sent and not has_valid_auth_context:
|
2016-01-22 21:49:32 +00:00
|
|
|
# The auth credentials sent for the user are invalid.
|
2017-03-16 21:06:05 +00:00
|
|
|
raise InvalidLogin(auth_result.error_message)
|
2016-01-22 21:49:32 +00:00
|
|
|
|
2015-06-22 21:37:13 +00:00
|
|
|
access = []
|
2016-01-19 19:01:43 +00:00
|
|
|
user_event_data = {
|
2017-12-08 22:05:59 +00:00
|
|
|
'action': 'login',
|
|
|
|
}
|
2017-04-15 12:26:33 +00:00
|
|
|
tuf_root = DISABLED_TUF_ROOT
|
2015-12-09 20:07:37 +00:00
|
|
|
|
|
|
|
if len(scope_param) > 0:
|
2016-05-23 20:36:48 +00:00
|
|
|
match = get_scope_regex().match(scope_param)
|
2015-08-27 18:55:33 +00:00
|
|
|
if match is None:
|
2015-06-22 21:37:13 +00:00
|
|
|
logger.debug('Match: %s', match)
|
|
|
|
logger.debug('len: %s', len(scope_param))
|
|
|
|
logger.warning('Unable to decode repository and actions: %s', scope_param)
|
2017-03-30 20:12:27 +00:00
|
|
|
raise InvalidRequest('Unable to decode repository and actions: %s' % scope_param)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
|
|
|
logger.debug('Match: %s', match.groups())
|
|
|
|
|
2016-05-23 20:36:48 +00:00
|
|
|
registry_and_repo = match.group(1)
|
|
|
|
namespace_and_repo = match.group(2)
|
2016-01-21 20:40:51 +00:00
|
|
|
actions = match.group(3).split(',')
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
lib_namespace = app.config['LIBRARY_NAMESPACE']
|
|
|
|
namespace, reponame = parse_namespace_repository(namespace_and_repo, lib_namespace)
|
2015-10-05 18:19:52 +00:00
|
|
|
|
|
|
|
# Ensure that we are never creating an invalid repository.
|
|
|
|
if not REPOSITORY_NAME_REGEX.match(reponame):
|
2016-01-22 21:49:32 +00:00
|
|
|
logger.debug('Found invalid repository name in auth flow: %s', reponame)
|
2017-03-30 20:12:27 +00:00
|
|
|
if len(namespace_and_repo.split('/')) > 1:
|
|
|
|
msg = 'Nested repositories are not supported. Found: %s' % namespace_and_repo
|
|
|
|
raise NameInvalid(message=msg)
|
|
|
|
|
|
|
|
raise NameInvalid(message='Invalid repository name: %s' % namespace_and_repo)
|
2015-10-05 18:19:52 +00:00
|
|
|
|
2015-11-24 04:46:05 +00:00
|
|
|
final_actions = []
|
|
|
|
|
2017-03-22 20:31:07 +00:00
|
|
|
repo = model.get_repository(namespace, reponame)
|
|
|
|
|
|
|
|
repo_is_public = repo is not None and repo.is_public
|
|
|
|
invalid_repo_message = ''
|
|
|
|
if repo is not None and repo.kind != 'image':
|
2017-06-26 22:16:15 +00:00
|
|
|
invalid_repo_message = ((
|
|
|
|
'This repository is for managing %s resources ' + 'and not container images.') % repo.kind)
|
2017-03-22 20:31:07 +00:00
|
|
|
|
2015-11-24 04:46:05 +00:00
|
|
|
if 'push' in actions:
|
2018-01-05 21:27:03 +00:00
|
|
|
# Check if there is a valid user or token, as otherwise the repository cannot be
|
2015-11-24 04:46:05 +00:00
|
|
|
# accessed.
|
2018-01-05 21:27:03 +00:00
|
|
|
if has_valid_auth_context:
|
2016-01-22 21:49:32 +00:00
|
|
|
# Lookup the repository. If it exists, make sure the entity has modify
|
|
|
|
# permission. Otherwise, make sure the entity has create permission.
|
|
|
|
if repo:
|
|
|
|
if ModifyRepositoryPermission(namespace, reponame).can():
|
2017-03-22 20:31:07 +00:00
|
|
|
if repo.kind != 'image':
|
2017-03-30 20:12:27 +00:00
|
|
|
raise Unsupported(message=invalid_repo_message)
|
2017-03-22 20:31:07 +00:00
|
|
|
|
2016-01-22 21:49:32 +00:00
|
|
|
final_actions.append('push')
|
|
|
|
else:
|
|
|
|
logger.debug('No permission to modify repository %s/%s', namespace, reponame)
|
|
|
|
else:
|
2018-01-05 21:27:03 +00:00
|
|
|
user = get_authenticated_user()
|
2016-01-22 21:49:32 +00:00
|
|
|
if CreateRepositoryPermission(namespace).can() and user is not None:
|
|
|
|
logger.debug('Creating repository: %s/%s', namespace, reponame)
|
2016-08-30 19:05:15 +00:00
|
|
|
model.create_repository(namespace, reponame, user)
|
2016-01-22 21:49:32 +00:00
|
|
|
final_actions.append('push')
|
|
|
|
else:
|
|
|
|
logger.debug('No permission to create repository %s/%s', namespace, reponame)
|
2015-11-24 04:46:05 +00:00
|
|
|
|
|
|
|
if 'pull' in actions:
|
2016-01-22 21:49:32 +00:00
|
|
|
# Grant pull if the user can read the repo or it is public.
|
2017-03-22 20:31:07 +00:00
|
|
|
if ReadRepositoryPermission(namespace, reponame).can() or repo_is_public:
|
|
|
|
if repo is not None and repo.kind != 'image':
|
2017-03-30 20:12:27 +00:00
|
|
|
raise Unsupported(message=invalid_repo_message)
|
2017-03-22 20:31:07 +00:00
|
|
|
|
2015-11-24 04:46:05 +00:00
|
|
|
final_actions.append('pull')
|
|
|
|
else:
|
2016-01-22 21:49:32 +00:00
|
|
|
logger.debug('No permission to pull repository %s/%s', namespace, reponame)
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2016-11-18 19:46:11 +00:00
|
|
|
if '*' in actions:
|
|
|
|
# Grant * user is admin
|
2017-03-22 20:31:07 +00:00
|
|
|
if AdministerRepositoryPermission(namespace, reponame).can():
|
|
|
|
if repo is not None and repo.kind != 'image':
|
2017-03-30 20:12:27 +00:00
|
|
|
raise Unsupported(message=invalid_repo_message)
|
2017-03-22 20:31:07 +00:00
|
|
|
|
2016-11-18 19:46:11 +00:00
|
|
|
final_actions.append('*')
|
|
|
|
else:
|
|
|
|
logger.debug("No permission to administer repository %s/%s", namespace, reponame)
|
2017-03-22 20:31:07 +00:00
|
|
|
|
2016-01-19 19:01:43 +00:00
|
|
|
# Add the access for the JWT.
|
2015-06-22 21:37:13 +00:00
|
|
|
access.append({
|
|
|
|
'type': 'repository',
|
2016-05-23 20:36:48 +00:00
|
|
|
'name': registry_and_repo,
|
2017-12-08 22:05:59 +00:00
|
|
|
'actions': final_actions,
|
|
|
|
})
|
2015-06-22 21:37:13 +00:00
|
|
|
|
2016-01-19 19:01:43 +00:00
|
|
|
# Set the user event data for the auth.
|
|
|
|
if 'push' in final_actions:
|
|
|
|
user_action = 'push_start'
|
|
|
|
elif 'pull' in final_actions:
|
|
|
|
user_action = 'pull_start'
|
2016-01-22 21:49:32 +00:00
|
|
|
else:
|
|
|
|
user_action = 'login'
|
2016-01-19 19:01:43 +00:00
|
|
|
|
|
|
|
user_event_data = {
|
|
|
|
'action': user_action,
|
|
|
|
'repository': reponame,
|
2017-12-08 22:05:59 +00:00
|
|
|
'namespace': namespace,
|
|
|
|
}
|
2017-04-15 12:26:33 +00:00
|
|
|
tuf_root = get_tuf_root(repo, namespace, reponame)
|
2016-01-19 19:01:43 +00:00
|
|
|
|
2018-01-05 21:27:03 +00:00
|
|
|
elif not has_valid_auth_context:
|
2015-11-20 23:35:02 +00:00
|
|
|
# In this case, we are doing an auth flow, and it's not an anonymous pull
|
2016-01-20 23:06:46 +00:00
|
|
|
logger.debug('No user and no token sent for empty scope list')
|
2017-03-30 20:12:27 +00:00
|
|
|
raise Unauthorized()
|
2015-11-20 23:35:02 +00:00
|
|
|
|
2016-01-19 19:01:43 +00:00
|
|
|
# Send the user event.
|
2018-01-05 21:27:03 +00:00
|
|
|
if get_authenticated_user() is not None:
|
|
|
|
event = userevents.get_event(get_authenticated_user().username)
|
2016-01-19 19:01:43 +00:00
|
|
|
event.publish_event_data('docker-cli', user_event_data)
|
|
|
|
|
2018-01-05 21:27:03 +00:00
|
|
|
# Build the signed JWT.
|
|
|
|
context, subject = build_context_and_subject(get_authenticated_context(), tuf_root=tuf_root)
|
2016-05-31 20:48:19 +00:00
|
|
|
token = generate_bearer_token(audience_param, subject, context, access,
|
|
|
|
TOKEN_VALIDITY_LIFETIME_S, instance_keys)
|
|
|
|
return jsonify({'token': token})
|
2017-02-22 19:35:11 +00:00
|
|
|
|
|
|
|
|
2017-04-15 12:26:33 +00:00
|
|
|
def get_tuf_root(repo, namespace, reponame):
|
|
|
|
if not features.SIGNING or repo is None or not repo.trust_enabled:
|
|
|
|
return DISABLED_TUF_ROOT
|
2017-06-26 22:10:39 +00:00
|
|
|
|
2017-03-22 11:38:52 +00:00
|
|
|
# Users with write access to a repo will see signer-rooted TUF metadata
|
|
|
|
if ModifyRepositoryPermission(namespace, reponame).can():
|
2017-03-22 20:14:56 +00:00
|
|
|
return SIGNER_TUF_ROOT
|
|
|
|
return QUAY_TUF_ROOT
|