1
0
mirror of https://github.com/quay/quay.git synced 2025-04-18 10:44:06 +03:00
quay/path_converters.py
Kenny Lee Sin Cheong 72f7c64ed6
chore: update werkzeug and related package versions (PROJQUAY-5098) (#1982)
* chore: update werkzeug and related package versions (PROJQUAY-5098)

Path converter related change reference: https://github.com/pallets/werkzeug/issues/2506

* Update query count
2023-09-12 11:51:09 -04:00

120 lines
3.6 KiB
Python

from werkzeug.routing import BaseConverter
import features
class QuayBaseConverter(BaseConverter):
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
if "part_isolating" not in cls.__dict__:
cls.part_isolating = "/" not in cls.regex
class APIRepositoryPathConverter(QuayBaseConverter):
"""
Converter for handling repository paths.
Does not handle library paths.
"""
def __init__(self, url_map):
super().__init__(url_map)
self.weight = 200
self.regex = r"([^/]+(/[^/]+)+)"
# TODO(kleesc): Remove after fully deprecating V1 push/pull
class V1CreateRepositoryPathConverter(QuayBaseConverter):
"""
Converter for handling PUT repository path.
Handles both library and non-library paths (if configured).
This is needed so that v1.create_repository does not match possibly
nested path from other routes.
For example:
PUT /repositories/<repopath:repository>/tags/<tag> when no tag is given
should 404, and not fallback to v1.create_repository route.
"""
def __init__(self, url_map):
super().__init__(url_map)
self.weight = 200
if features.LIBRARY_SUPPORT:
# Allow names without namespaces.
self.regex = r"[^/]+(/[^/]+)*(?<!auth)(?<!tags)(?<!images)"
else:
self.regex = r"([^/]+(/[^/]+)+)(?<!auth)(?<!tags)(?<!images)"
class RepositoryPathConverter(QuayBaseConverter):
"""
Converter for handling repository paths.
Handles both library and non-library paths (if configured).
Supports names with or without slashes (nested paths).
"""
def __init__(self, url_map):
super().__init__(url_map)
self.weight = 200
if features.LIBRARY_SUPPORT:
# Allow names without namespaces.
self.regex = r"[^/]+(/[^/]+)*"
else:
self.regex = r"([^/]+(/[^/]+)+)"
class RegexConverter(QuayBaseConverter):
"""
Converter for handling custom regular expression patterns in paths.
"""
def __init__(self, url_map, regex_value):
super().__init__(url_map)
self.regex = regex_value
class RepositoryPathRedirectConverter(QuayBaseConverter):
"""
Converter for handling redirect paths that don't match any other routes.
This needs to be separate from RepositoryPathConverter with the updated regex for
extended repo names support, otherwise, a nonexistent repopath resource would fallback
to redirecting to the repository web route.
For example:
/v2/devtable/testrepo/nested/manifests/somedigest" would previously have (correctly) returned
a 404, due to the path not matching any routes. With the regex supporting nested path for extended
repo names, Werkzeug would now (incorrectly) match to redirect to the web page of a repository with
the above path. See endpoints.web.redirect_to_repository.
"""
RESERVED_PREFIXES = [
"v1/",
"v2/",
"cnr/",
"customtrigger/setup/",
"bitbucket/setup/",
"repository/",
"github/callback/trigger/",
"push/",
]
def __init__(self, url_map):
super().__init__(url_map)
self.weight = 200
if features.LIBRARY_SUPPORT:
# Allow names without namespaces.
self.regex = r"(?!{})[^/]+(/[^/]+)*".format(
"|".join(RepositoryPathRedirectConverter.RESERVED_PREFIXES)
)
else:
self.regex = r"((?!{})[^/]+(/[^/]+)+)".format(
"|".join(RepositoryPathRedirectConverter.RESERVED_PREFIXES)
)