Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/user/guides/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,24 @@ credential on index and download requests, and rejects these requests unless the
`WWW-Authenticate: Cargo` challenge, which tells Cargo to retry with its credential.

The download endpoint redirects to the content app, which this setting does not cover. To
protect the crate files there as well, also attach a content guard that accepts the credential
Cargo sends.
protect the crate files there as well, attach a content redirect guard to the distribution:

```bash
http POST http://<pulp-host>/pulp/api/v3/contentguards/core/content_redirect/ \
-a admin:password \
name=private-crates
http PATCH http://<pulp-host>/pulp/api/v3/distributions/rust/rust/<uuid>/ \
-a admin:password \
content_guard=<content-guard-href>
```

The download endpoint then checks the credential, including the distributions and actions a
Cargo token is scoped to, and redirects to a signed URL of the crate file that expires after an
hour. The content app only accepts the crate file with that signature. A signed URL stays valid
until it expires, even if the user loses access in the meantime. The download endpoint only
signs URLs while `cargo_auth_required` is set, as it checks nothing otherwise. A content guard that
checks the user instead, such as an RBAC content guard, does not work with Cargo tokens: the
content app does not know them.

### Scoping a Token

Expand Down
10 changes: 9 additions & 1 deletion pulp_rust/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,15 @@ def get_full_path(self, base_path, pulp_domain=None):
def redirect_to_content_app(self, distribution, relative_path, request):
full_path = self.get_full_path(distribution.base_path)
content_path = f"{settings.CONTENT_PATH_PREFIX}{full_path}/{relative_path}"
return redirect(request.build_absolute_uri(content_path))
url = request.build_absolute_uri(content_path)
# The content app does not know Cargo tokens. A guard that can preauthenticate a
# URL (the content redirect guard) lets it trust this view's access check instead.
# Only when that check ran: without cargo_auth_required, anyone would get a signature.
if distribution.cargo_auth_required and distribution.content_guard:
guard = distribution.content_guard.cast()
if hasattr(guard, "preauthenticate_url"):
url = guard.preauthenticate_url(url)
return redirect(url)

def get_distribution(self):
return get_object_or_404(
Expand Down
90 changes: 90 additions & 0 deletions pulp_rust/tests/functional/api/test_auth.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tests for Cargo token authentication on Cargo API endpoints."""

import base64
import uuid
from urllib.parse import urljoin

import pytest
Expand Down Expand Up @@ -447,3 +448,92 @@ def test_private_distribution_honors_token_distribution_scope(
allow_redirects=False,
)
assert response.status_code == 403


# --- A content redirect guard lets Cargo tokens download crates ---


@pytest.fixture
def redirect_guarded_repo(
private_repo, pulpcore_bindings, gen_object_with_cleanup, rust_distro_api_client, monitor_task
):
"""The private repository, its crate files guarded by a content redirect guard."""
guard = gen_object_with_cleanup(
pulpcore_bindings.ContentguardsContentRedirectApi, {"name": str(uuid.uuid4())}
)
# The bindings send the default of every field left out, so cargo_auth_required would
# fall back to False if it were not repeated here.
monitor_task(
rust_distro_api_client.partial_update(
private_repo["distribution"].pulp_href,
{"content_guard": guard.pulp_href, "cargo_auth_required": True},
).task
)
return private_repo


def test_download_redirect_is_preauthenticated(
redirect_guarded_repo, gen_user, rust_token_factory, rust_distro_api_client
):
"""The content app does not know Cargo tokens: the redirect carries the authorization."""
user = gen_user()
rust_distro_api_client.add_role(
redirect_guarded_repo["distribution"].pulp_href,
{"role": "rust.rustdistribution_viewer", "users": [user.username]},
)
download_url = urljoin(redirect_guarded_repo["base_url"], "api/v1/crates/itoa/1.0.0/download")

response = cargo_api_request(
"GET",
download_url,
headers={"Authorization": rust_token_factory(user).token},
allow_redirects=False,
)
assert response.status_code == 302
signed_url = response.headers["Location"]
assert "validate_token=" in signed_url

# Fetched without any credential, as the signature is the authorization.
crate = cargo_api_request("GET", signed_url)
assert crate.status_code == 200
assert crate.content

unsigned_url = signed_url.split("?", 1)[0]
assert cargo_api_request("GET", unsigned_url).status_code == 403


def test_download_redirect_still_checks_the_token(redirect_guarded_repo, gen_user):
"""Signing happens after the access check: a user without a role gets no signed URL."""
user = gen_user()
creds = base64.b64encode(f"{user.username}:{user.password}".encode()).decode()

response = cargo_api_request(
"GET",
urljoin(redirect_guarded_repo["base_url"], "api/v1/crates/itoa/1.0.0/download"),
headers={"Authorization": f"Basic {creds}"},
allow_redirects=False,
)
assert response.status_code == 403


def test_download_redirect_is_not_signed_without_auth_required(
redirect_guarded_repo, rust_distro_api_client, monitor_task
):
"""Without cargo_auth_required nothing is checked, so the guard still applies."""
href = redirect_guarded_repo["distribution"].pulp_href
guard = rust_distro_api_client.read(href).content_guard
assert guard
monitor_task(
rust_distro_api_client.partial_update(
href, {"cargo_auth_required": False, "content_guard": guard}
).task
)

response = cargo_api_request(
"GET",
urljoin(redirect_guarded_repo["base_url"], "api/v1/crates/itoa/1.0.0/download"),
allow_redirects=False,
)
assert response.status_code == 302
assert "validate_token=" not in response.headers["Location"]
assert cargo_api_request("GET", response.headers["Location"]).status_code == 403
Loading