Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-48326: Avoid commas in generated auth-url annotation #1204

Merged
merged 1 commit into from
Jan 9, 2025
Merged
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
3 changes: 3 additions & 0 deletions changelog.d/20250108_223210_rra_DM_48326.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Bug fixes

- Pass multiple delegate scopes to the `/auth` route by repeating the `delegate_scope` query parameter instead of passing a comma-separated list as a single value. ingress-nginx 4.12.0 no longer allows `%` in the `auth-url` annotation and `,` was not initially allowed, and this matches how `scope` was handled.
17 changes: 6 additions & 11 deletions src/gafaelfawr/handlers/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,14 @@ def auth_config(
),
] = None,
delegate_scope: Annotated[
str | None,
list[str] | None,
Query(
title="Scope of delegated token",
description=(
"Comma-separated list of scopes to add to the delegated token."
" All listed scopes are implicitly added to the scope"
" requirements for authorization."
"Scopes to add to the delegated token if present in the"
" token used for authentication"
),
examples=["read:all,write:all"],
examples=[["read:all", "write:all"]],
),
] = None,
minimum_lifetime: Annotated[
Expand Down Expand Up @@ -195,7 +194,7 @@ def auth_config(
"If given more than once, meaning is determined by the"
" `satisfy` parameter"
),
examples=["read:all"],
examples=[["read:all"]],
),
] = None,
service: Annotated[
Expand Down Expand Up @@ -263,15 +262,11 @@ def auth_config(
if username:
context.rebind_logger(required_user=username)

if delegate_scope:
delegate_scopes = {s.strip() for s in delegate_scope.split(",")}
else:
delegate_scopes = set()
if not minimum_lifetime and (notebook or delegate_to):
minimum_lifetime = MINIMUM_LIFETIME
return AuthConfig(
auth_type=auth_type,
delegate_scopes=delegate_scopes,
delegate_scopes=set(delegate_scope) if delegate_scope else set(),
delegate_to=delegate_to,
minimum_lifetime=minimum_lifetime,
only_services=set(only_service) if only_service else None,
Expand Down
6 changes: 4 additions & 2 deletions src/gafaelfawr/models/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,10 @@ def to_auth_query(self) -> list[tuple[str, str]]:
elif self.delegate.internal:
service = self.delegate.internal.service
query.append(("delegate_to", service))
scopes = ",".join(self.delegate.internal.scopes)
query.append(("delegate_scope", scopes))
query.extend(
("delegate_scope", s)
for s in self.delegate.internal.scopes
)
if self.delegate.minimum_lifetime:
minimum_lifetime = self.delegate.minimum_lifetime
minimum_str = str(int(minimum_lifetime.total_seconds()))
Expand Down
2 changes: 1 addition & 1 deletion tests/data/kubernetes/output/ingresses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ metadata:
annotations:
nginx.ingress.kubernetes.io/auth-method: GET
nginx.ingress.kubernetes.io/auth-response-headers: "Authorization,Cookie,X-Auth-Request-Email,X-Auth-Request-Service,X-Auth-Request-Token,X-Auth-Request-User"
nginx.ingress.kubernetes.io/auth-url: "http://gafaelfawr.gafaelfawr.svc.cluster.local:8080/ingress/auth?scope=read:all&scope=read:some&delegate_to=some-service&delegate_scope=read:all%2Cread:some&auth_type=basic"
nginx.ingress.kubernetes.io/auth-url: "http://gafaelfawr.gafaelfawr.svc.cluster.local:8080/ingress/auth?scope=read:all&scope=read:some&delegate_to=some-service&delegate_scope=read:all&delegate_scope=read:some&auth_type=basic"
nginx.ingress.kubernetes.io/configuration-snippet: |
{snippet}
creationTimestamp: {any}
Expand Down
35 changes: 19 additions & 16 deletions tests/handlers/ingress_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,13 @@ async def test_internal(client: AsyncClient, factory: Factory) -> None:

r = await client.get(
"/ingress/auth",
params={
"scope": "exec:admin",
"service": "a-service",
"delegate_to": "a-service",
"delegate_scope": " read:some ,read:all ",
},
params=[
("scope", "exec:admin"),
("service", "a-service"),
("delegate_to", "a-service"),
("delegate_scope", "read:some"),
("delegate_scope", "read:all"),
],
headers={"Authorization": f"Bearer {token_data.token}"},
)
assert r.status_code == 200
Expand Down Expand Up @@ -365,11 +366,12 @@ async def test_internal(client: AsyncClient, factory: Factory) -> None:
# Requesting a token with the same parameters returns the same token.
r = await client.get(
"/ingress/auth",
params={
"scope": "exec:admin",
"delegate_to": "a-service",
"delegate_scope": "read:all,read:some",
},
params=(
("scope", "exec:admin"),
("delegate_to", "a-service"),
("delegate_scope", "read:all"),
("delegate_scope", "read:some"),
),
headers={"Authorization": f"Bearer {token_data.token}"},
)
assert r.status_code == 200
Expand All @@ -384,11 +386,12 @@ async def test_internal_scopes(client: AsyncClient, factory: Factory) -> None:

r = await client.get(
"/ingress/auth",
params={
"scope": "read:some",
"delegate_to": "a-service",
"delegate_scope": "read:all,read:some",
},
params=(
("scope", "read:some"),
("delegate_to", "a-service"),
("delegate_scope", "read:all"),
("delegate_scope", "read:some"),
),
headers={"Authorization": f"Bearer {token_data.token}"},
)
assert r.status_code == 200
Expand Down
Loading