Repository navigation
Restrict index access when cargo_auth_required is set - #71
Conversation
d9587e1 to
a15cb10
Compare
|
Hey @dralley can I have a review when it's still fresh in my mind ? :) |
|
Yup. Looking |
| "action": ["retrieve"], | ||
| "principal": "*", | ||
| "effect": "allow", | ||
| "condition": "can_read_index", |
There was a problem hiding this comment.
Given that this is now it's own separate condition, and that in theory users can customize the access policies, it might be a good idea to document that any custom access policies should take this into consideration, OR else just keep it separate from the access policy system.
@gerrod3 Your opinion here would be welcome as you have more experience with access policies.
Unless we forsee a need to customize rules around index access, I'm slightly partial to "keep it separate", which I think would look something like this.
diff --git a/pulp_rust/app/global_access_conditions.py b/pulp_rust/app/global_access_conditions.py
index 6317f14..08c71cf 100644
--- a/pulp_rust/app/global_access_conditions.py
+++ b/pulp_rust/app/global_access_conditions.py
@@ -32,8 +32,3 @@ def can_read_distribution(request, distribution):
if user.has_perm(permission) or user.has_perm(permission, distribution):
return True
return settings.DOMAIN_ENABLED and user.has_perm(permission, obj=request.pulp_domain)
-
-
-def can_read_index(request, view, action):
- """Access condition for the Cargo index views, which expose their distribution."""
- return can_read_distribution(request, view.distribution)
diff --git a/pulp_rust/app/views.py b/pulp_rust/app/views.py
index 5bec56d..1c40a51 100644
--- a/pulp_rust/app/views.py
+++ b/pulp_rust/app/views.py
@@ -65,6 +65,15 @@ def cargo_error(detail, status=400):
)
+def require_distribution_read(request, distribution):
+ """Raise the appropriate response when a request cannot read a distribution."""
+ if can_read_distribution(request, distribution):
+ return
+ if request.user.is_authenticated:
+ raise PermissionDenied()
+ raise NotAuthenticated()
+
+
def repository_write_error(distro):
"""Return a Cargo error response if the distribution can't be written to, else None.
@@ -175,6 +184,7 @@ class ApiMixin:
def initial(self, request, *args, **kwargs):
"""Perform common initialization tasks for API endpoints."""
super().initial(request, *args, **kwargs)
+ require_distribution_read(request, self.distribution)
domain_name = get_domain().name
repo = self.kwargs["repo"]
if settings.DOMAIN_ENABLED:
@@ -202,7 +212,6 @@ class CargoIndexApiViewSet(CargoAuthMixin, ApiMixin, ViewSet):
"action": ["retrieve"],
"principal": "*",
"effect": "allow",
- "condition": "can_read_index",
},
],
}
@@ -330,7 +339,6 @@ class IndexRoot(CargoAuthMixin, ApiMixin, ViewSet):
"action": ["retrieve"],
"principal": "*",
"effect": "allow",
- "condition": "can_read_index",
},
],
}
@@ -505,10 +513,7 @@ class CargoDownloadApiView(CargoAuthMixin, APIView):
- api/v1/crates/{name}/{version}/readme - not yet implemented
"""
distro = self.get_distribution()
- if not can_read_distribution(request, distro):
- if request.user.is_authenticated:
- raise PermissionDenied()
- raise NotAuthenticated()
+ require_distribution_read(request, distro)
if rest == "download":
relative_path = f"{name}/{name}-{version}.crate"There was a problem hiding this comment.
I have no opinion on this. In pulp-python we have custom access policy conditions for reading the python index that users can modify. https://pulpproject.org/pulp_python/docs/admin/guides/rbac/#index-specific-access-conditions.
When cargo_auth_required is set on a distribution, the index config.json, the sparse index files and the download endpoint now require the rust.view_rustdistribution permission on the distribution. Anonymous requests get a 401 with a "WWW-Authenticate: Cargo" challenge, so Cargo retries with its credential. A Cargo token scoped to other distributions cannot read this one. The check runs in the views rather than in the access policies, so a customized access policy cannot bypass it. The index views now accept the same credentials as the other Cargo endpoints: the Cargo token first, then the instance's default authentication classes. The download endpoint redirects to the content app, which this does not cover; a content guard is still needed to protect the crate files. Assisted By: Claude Opus 5.5
a15cb10 to
3b8d4e7
Compare
|
Thank you both of you for your reviews 👍 I had address the request changes. |
📜 Checklist
Follow-up of #61, as discussed there: when
cargo_auth_requiredis set, reading the index requires the view permission on the distribution.config.json, the sparse index and the download endpoint requirerust.view_rustdistributionon the distribution (e.g. therust.rustdistribution_viewerrole).401withWWW-Authenticate: Cargo, so Cargo retries with its credential; authenticated users without the permission get a403..cratefiles still need a content guard. Happy to discuss how to handle that in a separate PR.See: Pull Request Walkthrough