diff --git a/endpoints/common.py b/endpoints/common.py index 6a18d1f0b..887e34394 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -1,14 +1,9 @@ import logging -import json -import string import datetime import os -import re -from random import SystemRandom from functools import wraps -from cachetools import lru_cache from flask import make_response, render_template, request, abort, session from flask_login import login_user from flask_principal import identity_changed @@ -21,11 +16,10 @@ from auth import scopes from auth.permissions import QuayDeferredPermissionUser from config import frontend_visible_config from external_libraries import get_external_javascript, get_external_css -from util.names import parse_namespace_repository from util.secscan import PRIORITY_LEVELS from util.saas.useranalytics import build_error_callback from util.timedeltastring import convert_to_timedelta -from _init import STATIC_DIR, __version__ +from _init import __version__ logger = logging.getLogger(__name__) @@ -33,28 +27,6 @@ logger = logging.getLogger(__name__) route_data = None -def parse_repository_name(include_tag=False, - ns_kwarg_name='namespace_name', - repo_kwarg_name='repo_name', - tag_kwarg_name='tag_name', - incoming_repo_kwarg='repository'): - def inner(func): - @wraps(func) - def wrapper(*args, **kwargs): - repo_name_components = parse_namespace_repository(kwargs[incoming_repo_kwarg], - app.config['LIBRARY_NAMESPACE'], - include_tag=include_tag) - del kwargs[incoming_repo_kwarg] - kwargs[ns_kwarg_name] = repo_name_components[0] - kwargs[repo_kwarg_name] = repo_name_components[1] - if include_tag: - kwargs[tag_kwarg_name] = repo_name_components[2] - return func(*args, **kwargs) - return wrapper - return inner - - - def truthy_param(param): return param not in {False, 'false', 'False', '0', 'FALSE', '', 'null'} @@ -99,8 +71,8 @@ def common_login(db_user, permanent_session=True): return False -def list_files(path, extension): - import os +def _list_files(path, extension): + """ Returns a list of all the files with the given extension found under the given path. """ def matches(f): return os.path.splitext(f)[1] == '.' + extension and f.split(os.path.extsep)[1] != 'spec' @@ -118,7 +90,7 @@ def render_page_template(name, route_data=None, **kwargs): library_styles = [] main_styles = [] library_scripts = [] - main_scripts = list_files('build', 'js') + main_scripts = _list_files('build', 'js') use_cdn = app.config.get('USE_CDN', True) if request.args.get('use_cdn') is not None: diff --git a/endpoints/decorators.py b/endpoints/decorators.py index 6534bc4e9..6d8a9993c 100644 --- a/endpoints/decorators.py +++ b/endpoints/decorators.py @@ -4,8 +4,35 @@ from functools import wraps from flask import abort import features + +from app import app from auth.auth_context import ( get_validated_oauth_token, get_authenticated_user, get_validated_token, get_grant_context) +from util.names import parse_namespace_repository + + +def parse_repository_name(include_tag=False, + ns_kwarg_name='namespace_name', + repo_kwarg_name='repo_name', + tag_kwarg_name='tag_name', + incoming_repo_kwarg='repository'): + """ Decorator which parses the repository name found in the incoming_repo_kwarg argument, + and applies its pieces to the decorated function. + """ + def inner(func): + @wraps(func) + def wrapper(*args, **kwargs): + repo_name_components = parse_namespace_repository(kwargs[incoming_repo_kwarg], + app.config['LIBRARY_NAMESPACE'], + include_tag=include_tag) + del kwargs[incoming_repo_kwarg] + kwargs[ns_kwarg_name] = repo_name_components[0] + kwargs[repo_kwarg_name] = repo_name_components[1] + if include_tag: + kwargs[tag_kwarg_name] = repo_name_components[2] + return func(*args, **kwargs) + return wrapper + return inner def anon_allowed(func): diff --git a/endpoints/githubtrigger.py b/endpoints/githubtrigger.py index 3b373bdf0..3b4b21f0a 100644 --- a/endpoints/githubtrigger.py +++ b/endpoints/githubtrigger.py @@ -9,8 +9,7 @@ from app import app, github_trigger from auth.decorators import require_session_login from auth.permissions import AdministerRepositoryPermission from data import model -from endpoints.common import parse_repository_name -from endpoints.decorators import route_show_if +from endpoints.decorators import route_show_if, parse_repository_name from util.http import abort diff --git a/endpoints/v1/index.py b/endpoints/v1/index.py index b12c23dd5..877fb90c5 100644 --- a/endpoints/v1/index.py +++ b/endpoints/v1/index.py @@ -13,8 +13,7 @@ from auth.permissions import ( ModifyRepositoryPermission, UserAdminPermission, ReadRepositoryPermission, CreateRepositoryPermission, repository_read_grant, repository_write_grant) from auth.signedgrant import generate_signed_token -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect, anon_allowed +from endpoints.decorators import anon_protect, anon_allowed, parse_repository_name from endpoints.notificationhelper import spawn_notification from endpoints.v1 import v1_bp from endpoints.v1.models_pre_oci import pre_oci_model as model diff --git a/endpoints/v1/tag.py b/endpoints/v1/tag.py index 4a741ff6b..acad5e303 100644 --- a/endpoints/v1/tag.py +++ b/endpoints/v1/tag.py @@ -6,8 +6,7 @@ from flask import abort, request, jsonify, make_response, session from auth.decorators import process_auth from auth.permissions import (ReadRepositoryPermission, ModifyRepositoryPermission) from data import model -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect +from endpoints.decorators import anon_protect, parse_repository_name from endpoints.v1 import v1_bp from endpoints.v1.models_pre_oci import pre_oci_model as model from util.audit import track_and_log diff --git a/endpoints/v2/blob.py b/endpoints/v2/blob.py index f736eaccc..c7bf52c34 100644 --- a/endpoints/v2/blob.py +++ b/endpoints/v2/blob.py @@ -11,8 +11,7 @@ from app import storage, app, get_app_url, metric_queue from auth.registry_jwt_auth import process_registry_jwt_auth from data import database from digest import digest_tools -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect +from endpoints.decorators import anon_protect, parse_repository_name from endpoints.v2 import v2_bp, require_repo_read, require_repo_write, get_input_stream from endpoints.v2.errors import ( BlobUnknown, BlobUploadInvalid, BlobUploadUnknown, Unsupported, NameUnknown, LayerTooLarge) diff --git a/endpoints/v2/manifest.py b/endpoints/v2/manifest.py index b79111f39..b4970e2f6 100644 --- a/endpoints/v2/manifest.py +++ b/endpoints/v2/manifest.py @@ -9,8 +9,7 @@ import features from app import docker_v2_signing_key, app, metric_queue from auth.registry_jwt_auth import process_registry_jwt_auth from digest import digest_tools -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect +from endpoints.decorators import anon_protect, parse_repository_name from endpoints.notificationhelper import spawn_notification from endpoints.v2 import v2_bp, require_repo_read, require_repo_write from endpoints.v2.models_interface import Label diff --git a/endpoints/v2/tag.py b/endpoints/v2/tag.py index 776663520..558d33959 100644 --- a/endpoints/v2/tag.py +++ b/endpoints/v2/tag.py @@ -1,8 +1,7 @@ from flask import jsonify from auth.registry_jwt_auth import process_registry_jwt_auth -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect +from endpoints.decorators import anon_protect, parse_repository_name from endpoints.v2 import v2_bp, require_repo_read, paginate from endpoints.v2.models_pre_oci import data_model as model diff --git a/endpoints/verbs/__init__.py b/endpoints/verbs/__init__.py index 0522c5a38..6ca94eb81 100644 --- a/endpoints/verbs/__init__.py +++ b/endpoints/verbs/__init__.py @@ -10,8 +10,7 @@ from auth.auth_context import get_authenticated_user from auth.decorators import process_auth from auth.permissions import ReadRepositoryPermission from data import database -from endpoints.common import parse_repository_name -from endpoints.decorators import anon_protect, route_show_if +from endpoints.decorators import anon_protect, route_show_if, parse_repository_name from endpoints.verbs.models_pre_oci import pre_oci_model as model from endpoints.v2.blob import BLOB_DIGEST_ROUTE from image.appc import AppCImageFormatter diff --git a/endpoints/web.py b/endpoints/web.py index db4a9d696..7f3bb79ca 100644 --- a/endpoints/web.py +++ b/endpoints/web.py @@ -27,10 +27,9 @@ from buildtrigger.triggerutil import TriggerProviderException from data import model from data.database import db from endpoints.api.discovery import swagger_route_data -from endpoints.common import (common_login, render_page_template, param_required, - parse_repository_name) +from endpoints.common import (common_login, render_page_template, param_required) from endpoints.csrf import csrf_protect, generate_csrf_token, verify_csrf -from endpoints.decorators import anon_protect, anon_allowed, route_show_if +from endpoints.decorators import anon_protect, anon_allowed, route_show_if, parse_repository_name from health.healthcheck import get_healthchecker from util.cache import no_cache from util.headers import parse_basic_auth