Skip to content

Commit

Permalink
Define a custom API root endpoint class
Browse files Browse the repository at this point in the history
closes pulp#2340
  • Loading branch information
lubosmj committed Jun 15, 2022
1 parent 1d1cea4 commit 54a36a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/2340.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made the API root endpoint accessible for anonymous users once again.
19 changes: 16 additions & 3 deletions pulpcore/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SpectacularSwaggerView,
)
from rest_framework_nested import routers
from rest_framework.routers import APIRootView

from pulpcore.app.apps import pulp_plugin_configs
from pulpcore.app.views import OrphansView, PulpImporterImportCheckView, RepairView, StatusView
Expand Down Expand Up @@ -104,6 +105,19 @@ def __repr__(self):
return str(self.viewset)


class PulpAPIRootView(APIRootView):
"""A Pulp-defined APIRootView class with no authentication requirements."""

authentication_classes = []
permission_classes = []


class PulpDefaultRouter(routers.DefaultRouter):
"""A DefaultRouter class that benefits from the customized PulpAPIRootView class."""

APIRootView = PulpAPIRootView


all_viewsets = []
plugin_patterns = []
# Iterate over each app, including pulpcore and the plugins.
Expand All @@ -118,9 +132,6 @@ def __repr__(self):
for viewset in sorted_by_depth:
vs_tree.add_decendent(ViewSetNode(viewset))

#: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API.
root_router = routers.DefaultRouter()

urlpatterns = [
path(f"{API_ROOT}repair/", RepairView.as_view()),
path(f"{API_ROOT}status/", StatusView.as_view()),
Expand Down Expand Up @@ -184,6 +195,8 @@ def __repr__(self):
)
)

#: The Pulp Platform v3 API router, which can be used to manually register ViewSets with the API.
root_router = PulpDefaultRouter()

all_routers = [root_router] + vs_tree.register_with(root_router)
for router in all_routers:
Expand Down
5 changes: 5 additions & 0 deletions pulpcore/tests/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ def pulp_api_v3_path(cli_client):
return v3_api_root


@pytest.fixture(scope="session")
def pulp_api_v3_root_endpoint(pulp_cfg, pulp_api_v3_path):
return f"{pulp_cfg.get_base_url()}{pulp_api_v3_path}"


@pytest.fixture
def random_artifact(random_artifact_factory):
return random_artifact_factory()
Expand Down
10 changes: 10 additions & 0 deletions pulpcore/tests/functional/api/test_root_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import requests
import pytest

from pulpcore.tests.functional.utils import NoAuth

@pytest.mark.parallel
@pytest.mark.from_pulpcore_for_all_plugins
def test_anonymous_access_to_root(pulp_api_v3_root_endpoint):
response = requests.get(pulp_api_v3_root_endpoint, auth=NoAuth())
assert response.status_code == 200
14 changes: 14 additions & 0 deletions pulpcore/tests/functional/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Utilities for Pulpcore tests."""
import requests

from functools import partial
from unittest import SkipTest

Expand All @@ -7,6 +9,18 @@
from pulpcore.client.pulpcore import ApiClient


class NoAuth(requests.auth.AuthBase):
"""A class using no authentication credentials to override .netrc configuration.
If there is a need to bypass .netrc and send requests without any authentication credentials,
this class should suffice. If no .netrc file is present in the filesystem, the class has no
added value.
"""

def __call__(self, r):
return r


skip_if = partial(selectors.skip_if, exc=SkipTest) # pylint:disable=invalid-name
"""The ``@skip_if`` decorator, customized for unittest.
Expand Down

0 comments on commit 54a36a1

Please sign in to comment.