This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/endpoints/api/__init__.py

384 lines
11 KiB
Python
Raw Normal View History

import logging
import datetime
import json
from enum import Enum
from app import app, metric_queue
from flask import Blueprint, Response, request, make_response, jsonify, session, url_for
from flask.ext.restful import Resource, abort, Api, reqparse
from flask.ext.restful.utils.cors import crossdomain
from calendar import timegm
from email.utils import formatdate
2014-03-11 03:54:55 +00:00
from functools import partial, wraps
from jsonschema import validate, ValidationError
from data import model
from util.names import parse_namespace_repository
from auth.permissions import (ReadRepositoryPermission, ModifyRepositoryPermission,
AdministerRepositoryPermission, UserReadPermission,
UserAdminPermission)
2014-03-12 16:37:06 +00:00
from auth import scopes
from auth.auth_context import get_authenticated_user, get_validated_oauth_token
from auth.auth import process_oauth
from endpoints.csrf import csrf_protect
2016-04-15 20:50:01 +00:00
from endpoints.exception import ApiException, Unauthorized, InvalidRequest, InvalidResponse, FreshLoginRequired
from endpoints.decorators import check_anon_protection
from util.saas.metricqueue import time_decorator
2016-02-09 22:25:33 +00:00
from util.pagination import encrypt_page_token, decrypt_page_token
logger = logging.getLogger(__name__)
api_bp = Blueprint('api', __name__)
2014-03-17 19:23:49 +00:00
api = Api()
api.init_app(api_bp)
api.decorators = [csrf_protect,
crossdomain(origin='*', headers=['Authorization', 'Content-Type']),
process_oauth, time_decorator(api_bp.name, metric_queue)]
@api_bp.app_errorhandler(ApiException)
@crossdomain(origin='*', headers=['Authorization', 'Content-Type'])
def handle_api_error(error):
response = Response(json.dumps(error.to_dict()), error.status_code, mimetype='application/json')
2016-04-13 13:01:42 +00:00
if error.status_code == 401:
response.headers['WWW-Authenticate'] = ('Bearer error="%s" error_description="%s"' %
2016-04-13 13:01:42 +00:00
(error.error_type.value, error.error_description))
return response
def resource(*urls, **kwargs):
def wrapper(api_resource):
if not api_resource:
return None
api.add_resource(api_resource, *urls, **kwargs)
return api_resource
return wrapper
def show_if(value):
def f(inner):
if not value:
return None
return inner
return f
def hide_if(value):
def f(inner):
if value:
return None
return inner
return f
def truthy_bool(param):
return param not in {False, 'false', 'False', '0', 'FALSE', '', 'null'}
def format_date(date):
""" Output an RFC822 date format. """
if date is None:
return None
return formatdate(timegm(date.utctimetuple()))
def add_method_metadata(name, value):
def modifier(func):
if func is None:
return None
if '__api_metadata' not in dir(func):
2014-03-11 03:54:55 +00:00
func.__api_metadata = {}
func.__api_metadata[name] = value
return func
return modifier
def method_metadata(func, name):
if func is None:
return None
if '__api_metadata' in dir(func):
2014-03-11 03:54:55 +00:00
return func.__api_metadata.get(name, None)
return None
nickname = partial(add_method_metadata, 'nickname')
related_user_resource = partial(add_method_metadata, 'related_user_resource')
2014-03-14 22:07:03 +00:00
internal_only = add_method_metadata('internal', True)
2014-08-06 21:47:32 +00:00
def path_param(name, description):
def add_param(func):
if not func:
return func
2014-11-25 21:08:01 +00:00
2014-08-06 21:47:32 +00:00
if '__api_path_params' not in dir(func):
func.__api_path_params = {}
func.__api_path_params[name] = {
'name': name,
'description': description
}
return func
return add_param
def query_param(name, help_str, type=reqparse.text_type, default=None,
choices=(), required=False):
def add_param(func):
if '__api_query_params' not in dir(func):
func.__api_query_params = []
func.__api_query_params.append({
'name': name,
'type': type,
'help': help_str,
'default': default,
'choices': choices,
'required': required,
'location': ('args')
})
return func
return add_param
def page_support(page_token_kwarg='page_token', parsed_args_kwarg='parsed_args'):
def inner(func):
""" Adds pagination support to an API endpoint. The decorated API will have an
added query parameter named 'next_page'. Works in tandem with the
modelutil paginate method.
"""
@wraps(func)
@query_param('next_page', 'The page token for the next page', type=str)
def wrapper(self, *args, **kwargs):
# Note: if page_token is None, we'll receive the first page of results back.
2016-02-09 22:25:33 +00:00
page_token = decrypt_page_token(kwargs[parsed_args_kwarg]['next_page'])
kwargs[page_token_kwarg] = page_token
2016-02-09 22:25:33 +00:00
(result, next_page_token) = func(self, *args, **kwargs)
if next_page_token is not None:
2016-02-09 22:25:33 +00:00
result['next_page'] = encrypt_page_token(next_page_token)
return result
return wrapper
return inner
def parse_args(kwarg_name='parsed_args'):
def inner(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
if '__api_query_params' not in dir(func):
abort(500)
parser = reqparse.RequestParser()
for arg_spec in func.__api_query_params:
parser.add_argument(**arg_spec)
kwargs[kwarg_name] = parser.parse_args()
return func(self, *args, **kwargs)
return wrapper
return inner
2014-03-11 03:54:55 +00:00
def parse_repository_name(func):
@wraps(func)
def wrapper(repository, *args, **kwargs):
(namespace, repository) = parse_namespace_repository(repository, app.config['LIBRARY_NAMESPACE'])
2014-03-11 03:54:55 +00:00
return func(namespace, repository, *args, **kwargs)
return wrapper
class ApiResource(Resource):
method_decorators = [check_anon_protection]
def options(self):
return None, 200
class RepositoryParamResource(ApiResource):
method_decorators = [check_anon_protection, parse_repository_name]
2014-03-11 03:54:55 +00:00
2014-03-12 16:37:06 +00:00
def require_repo_permission(permission_class, scope, allow_public=False):
2014-03-11 03:54:55 +00:00
def wrapper(func):
2014-03-12 16:37:06 +00:00
@add_method_metadata('oauth2_scope', scope)
2014-03-11 03:54:55 +00:00
@wraps(func)
def wrapped(self, namespace, repository, *args, **kwargs):
logger.debug('Checking permission %s for repo: %s/%s', permission_class, namespace,
repository)
2014-03-11 03:54:55 +00:00
permission = permission_class(namespace, repository)
if (permission.can() or
(allow_public and
model.repository.repository_is_public(namespace, repository))):
2014-03-11 03:54:55 +00:00
return func(self, namespace, repository, *args, **kwargs)
raise Unauthorized()
2014-03-11 03:54:55 +00:00
return wrapped
return wrapper
2014-03-12 16:37:06 +00:00
require_repo_read = require_repo_permission(ReadRepositoryPermission, scopes.READ_REPO, True)
require_repo_write = require_repo_permission(ModifyRepositoryPermission, scopes.WRITE_REPO)
require_repo_admin = require_repo_permission(AdministerRepositoryPermission, scopes.ADMIN_REPO)
2014-03-11 03:54:55 +00:00
def require_user_permission(permission_class, scope=None):
def wrapper(func):
@add_method_metadata('oauth2_scope', scope)
@wraps(func)
def wrapped(self, *args, **kwargs):
user = get_authenticated_user()
if not user:
2014-03-19 17:57:36 +00:00
raise Unauthorized()
logger.debug('Checking permission %s for user %s', permission_class, user.username)
permission = permission_class(user.username)
if permission.can():
return func(self, *args, **kwargs)
raise Unauthorized()
return wrapped
return wrapper
2014-03-19 17:57:36 +00:00
require_user_read = require_user_permission(UserReadPermission, scopes.READ_USER)
require_user_admin = require_user_permission(UserAdminPermission, scopes.ADMIN_USER)
def verify_not_prod(func):
@add_method_metadata('enterprise_only', True)
@wraps(func)
def wrapped(*args, **kwargs):
# Verify that we are not running on a production (i.e. hosted) stack. If so, we fail.
# This should never happen (because of the feature-flag on SUPER_USERS), but we want to be
# absolutely sure.
if app.config['SERVER_HOSTNAME'].find('quay.io') >= 0:
logger.error('!!! Super user method called IN PRODUCTION !!!')
raise NotFound()
return func(*args, **kwargs)
return wrapped
def require_fresh_login(func):
@add_method_metadata('requires_fresh_login', True)
@wraps(func)
def wrapped(*args, **kwargs):
user = get_authenticated_user()
if not user:
raise Unauthorized()
oauth_token = get_validated_oauth_token()
if oauth_token:
return func(*args, **kwargs)
logger.debug('Checking fresh login for user %s', user.username)
last_login = session.get('login_time', datetime.datetime.min)
valid_span = datetime.datetime.now() - datetime.timedelta(minutes=10)
if not user.password_hash or last_login >= valid_span:
return func(*args, **kwargs)
2014-11-25 21:08:01 +00:00
raise FreshLoginRequired()
return wrapped
def require_scope(scope_object):
def wrapper(func):
@add_method_metadata('oauth2_scope', scope_object)
@wraps(func)
def wrapped(*args, **kwargs):
return func(*args, **kwargs)
return wrapped
return wrapper
2014-03-11 03:54:55 +00:00
def validate_json_request(schema_name):
def wrapper(func):
@add_method_metadata('request_schema', schema_name)
@wraps(func)
def wrapped(self, *args, **kwargs):
2014-03-11 03:54:55 +00:00
schema = self.schemas[schema_name]
try:
json_data = request.get_json()
if json_data is None:
raise InvalidRequest('Missing JSON body')
validate(json_data, schema)
return func(self, *args, **kwargs)
2014-03-11 03:54:55 +00:00
except ValidationError as ex:
2014-03-18 18:22:14 +00:00
raise InvalidRequest(ex.message)
2014-03-11 03:54:55 +00:00
return wrapped
return wrapper
def request_error(exception=None, **kwargs):
data = kwargs.copy()
message = 'Request error.'
if exception:
message = exception.message
2016-04-11 20:34:40 +00:00
message = data.pop('message', message)
raise InvalidRequest(message, data)
def license_error(exception=None):
raise ExceedsLicenseException()
def log_action(kind, user_or_orgname, metadata=None, repo=None):
if not metadata:
metadata = {}
oauth_token = get_validated_oauth_token()
if oauth_token:
metadata['oauth_token_id'] = oauth_token.id
metadata['oauth_token_application_id'] = oauth_token.application.client_id
metadata['oauth_token_application'] = oauth_token.application.name
performer = get_authenticated_user()
model.log.log_action(kind, user_or_orgname, performer=performer, ip=request.remote_addr,
metadata=metadata, repository=repo)
2014-03-11 03:54:55 +00:00
2014-10-02 19:08:32 +00:00
def define_json_response(schema_name):
def wrapper(func):
@add_method_metadata('response_schema', schema_name)
@wraps(func)
def wrapped(self, *args, **kwargs):
schema = self.schemas[schema_name]
resp = func(self, *args, **kwargs)
if app.config['TESTING']:
try:
validate(resp, schema)
except ValidationError as ex:
raise InvalidResponse(ex.message)
return resp
return wrapped
return wrapper
2014-03-14 19:35:20 +00:00
import endpoints.api.billing
import endpoints.api.build
2014-03-14 17:24:01 +00:00
import endpoints.api.discovery
import endpoints.api.error
import endpoints.api.image
2014-03-14 20:02:13 +00:00
import endpoints.api.logs
import endpoints.api.organization
2014-03-14 17:24:01 +00:00
import endpoints.api.permission
import endpoints.api.prototype
2014-03-14 17:24:01 +00:00
import endpoints.api.repository
import endpoints.api.repositorynotification
import endpoints.api.repoemail
2014-03-14 17:24:01 +00:00
import endpoints.api.repotoken
2014-03-14 20:02:13 +00:00
import endpoints.api.robot
2014-03-14 17:24:01 +00:00
import endpoints.api.search
2015-01-04 19:38:41 +00:00
import endpoints.api.suconfig
import endpoints.api.superuser
import endpoints.api.tag
2014-03-14 18:20:51 +00:00
import endpoints.api.team
2014-03-14 17:24:01 +00:00
import endpoints.api.trigger
import endpoints.api.user
import endpoints.api.secscan
2015-01-04 19:38:41 +00:00