mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
feat(dashboard): let a login link pick the Keycloak identity provider (#5286)
## What this PR does A login link into the dashboard can now name the Keycloak identity provider, so a user who comes from a portal brokered into Keycloak skips the realm login page: ``` https://dashboard.<host>/oauth2/start?kc_idp_hint=<idp-alias>&rd=/admin/tenants ``` The motivating case is a billing portal registered in the realm as an OIDC identity provider. Its "Open dashboard" button today lands every client on "Sign in to <realm>", where they must pick the portal's provider by hand on every new browser or session. Keycloak skips that page when the auth request carries `kc_idp_hint`. The gatekeeper, however, builds the request from a fixed `--login-url`, and oauth2-proxy's flags forward only `acr_values`, `prompt` and `approval_prompt` from `/oauth2/start`. Putting the hint into `--login-url` would force every dashboard login, administrators included, through that one provider. oauth2-proxy can forward an allowed parameter through `providers[].loginURLParameters`, but only in its alpha configuration. The OIDC gatekeeper therefore now runs from one: - `templates/gatekeeper-config.yaml` (new, OIDC only) holds the provider, upstream, listen address and injected headers. The values are exactly what `oauth2-proxy --convert-config-to-alpha` (v7.12.0) produces for the previous flags, both the in-cluster Keycloak branch and the external-discovery branch. On top of those, `kc_idp_hint` is allowed when it matches `^[A-Za-z0-9._-]+$`. That rule and `clientSecretFile` in place of the inline `clientSecret` are the only hand-written lines; everything else, `scope`, `approval_prompt`, `insecureSkipNonce`, `flushInterval` and `timeout` included, is the converter output unchanged. - `templates/gatekeeper.yaml` keeps the flags the alpha configuration does not own (redirect URL, whitelist domain, email domain, API routes, cookie options, `--skip-provider-button`, `--ssl-insecure-skip-verify`). It mounts the config and the `dashboard-client` secret as files (`clientSecretFile`). A checksum annotation restarts the pod when the config changes. - The token-proxy branch (OIDC disabled) is unchanged. A login without the parameter behaves as before. oauth2-proxy logs a warning on start that the alpha configuration format may change between releases. Tests: - **Chart suite.** The gatekeeper suite loads the config template as well, because the deployment checksums it. The "no branding volume" test now asserts the branding volume is absent, not that the pod has no volumes. The five tests that read Keycloak URLs from the container args are removed: those values now sit in the alpha configuration. `helm unittest .` passes, 23 tests. - **E2E.** After the gatekeeper rolls out with OIDC enabled, the in-tree install suite requests `/oauth2/start` through a port-forward and checks the redirect: `kc_idp_hint=e2e-idp` reaches the `cozy` realm's auth URL, and a value outside the allow pattern (`e2e%26prompt%3Dnone`) is dropped. This covers the realm path and the allow rule that the removed chart tests no longer pin. - **Gatekeeper login (`hack/dashboard-gatekeeper-login.bats`).** Renders the chart for a realm other than `cozy`, starts the auth-proxy image with the rendered args and alpha config, and checks the `/oauth2/start` redirect: on the in-cluster Keycloak branch the hint reaches that realm's auth URL, a value outside the allow pattern is dropped, and a login without it carries none; on the external-discovery branch the gatekeeper discovers that realm from a local Keycloak stub and passes the hint. The image is read from the render, so an oauth2-proxy bump re-checks the alpha configuration. Needs docker, like `hack/etcd-grpc-alerts.bats`. - **Real oauth2-proxy v7.12.0 on the rendered config.** Ran with the rendered args and checked: - `/ping` answers OK; - `/oauth2/start?kc_idp_hint=whmcs` puts `kc_idp_hint=whmcs` into the Keycloak auth URL; - `/oauth2/start` without the parameter, or with a value outside the pattern, does not; - a protected path redirects without it; - `/apis/*` stays 401; - `--ssl-insecure-skip-verify` starts fine alongside `--alpha-config`. - **Live cluster.** Swapped in the rendered gatekeeper with the HelmRelease suspended, then reverted: - a brokered user who was already signed in at the portal went from the hinted link straight to the dashboard; - a plain visit still showed the realm login page; - a hinted visit without a portal session went straight to the portal's own login; - API calls from the console worked, so the `Authorization` header injection is intact. ### Screenshots N/A — no console UI changes. The change removes the Keycloak login page from the hinted flow. ### Downstream repositories - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: The diff touches only the dashboard gatekeeper templates and their tests. It adds no value, schema, package or platform component. I walked the trigger map against it, and no entry matches. ### Release note ```release-note feat(dashboard): a dashboard login link can name the Keycloak identity provider with `/oauth2/start?kc_idp_hint=<alias>`, so users coming from a brokered portal skip the realm login page. The OIDC gatekeeper now runs oauth2-proxy from an alpha configuration kept in a ConfigMap; logins without the parameter are unchanged. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * OIDC-enabled clusters now use updated dashboard sign-in settings, forwarding identity and token information to the dashboard. * Authentication requests use Keycloak’s internal URL when configured; otherwise, logout uses the public Keycloak URL. * Sign-in requests include the OpenID, email, and profile scopes. * Supported identity-provider hints are preserved in sign-in redirects, while invalid hints are excluded. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
5 files changed
+325
-182
No files matched your search
@@ -0,0 +1,162 @@
|
||||
#!/usr/bin/env bats
|
||||
# -----------------------------------------------------------------------------
|
||||
# The dashboard gatekeeper's login redirect, checked against the oauth2-proxy
|
||||
# binary the chart ships rather than against the rendered config text: the
|
||||
# chart is rendered for a non-default realm, the auth-proxy container runs with
|
||||
# the image, args and alpha config from that render, and /oauth2/start must
|
||||
# redirect to that realm with an allowed kc_idp_hint passed through. The image
|
||||
# comes from the render, so bumping it re-runs these cases on the new binary.
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
load test_helper
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
|
||||
STUB_IMAGE="python:3.13-alpine@sha256:9ef1a599f863231c3d91e941389bfdc4a2d6070557243d54df1a4e57015b960e"
|
||||
HOST=example.org
|
||||
REALM=acme
|
||||
|
||||
setup() {
|
||||
strict_setup
|
||||
docker info >/dev/null 2>&1 || {
|
||||
echo "docker is required to run the gatekeeper image" >&2
|
||||
return 1
|
||||
}
|
||||
work=$(mktemp -d)
|
||||
mkdir -p "$work/config" "$work/client"
|
||||
printf 'unused' > "$work/client/client-secret"
|
||||
}
|
||||
|
||||
teardown() {
|
||||
[ -n "${work:-}" ] || return 0
|
||||
for f in "$work"/cid-*; do
|
||||
if [ -e "$f" ]; then docker rm -f "$(cat "$f")" >/dev/null 2>&1 || true; fi
|
||||
done
|
||||
if [ -e "$work/network" ]; then docker network rm "$(cat "$work/network")" >/dev/null 2>&1 || true; fi
|
||||
rm -rf "$work"
|
||||
}
|
||||
|
||||
# render <keycloak-internal-url>
|
||||
render() {
|
||||
helm template dashboard "$REPO_ROOT/packages/system/dashboard" --namespace cozy-dashboard \
|
||||
--set-string "_cluster.root-host=$HOST" \
|
||||
--set-string '_cluster.oidc-enabled=true' \
|
||||
--set-string "_cluster.oidc-realm-name=$REALM" \
|
||||
--set-string "_cluster.keycloak-internal-url=$1" > "$work/rendered.yaml"
|
||||
yq 'select(.kind == "ConfigMap" and .metadata.name == "incloud-web-gatekeeper-config") | .data["alpha-config.yaml"]' \
|
||||
"$work/rendered.yaml" > "$work/config/alpha-config.yaml"
|
||||
yq 'select(.kind == "Deployment" and .metadata.name == "incloud-web-gatekeeper") | .spec.template.spec.containers[] | select(.name == "auth-proxy") | .image' \
|
||||
"$work/rendered.yaml" > "$work/image"
|
||||
yq 'select(.kind == "Deployment" and .metadata.name == "incloud-web-gatekeeper") | .spec.template.spec.containers[] | select(.name == "auth-proxy") | .args[]' \
|
||||
"$work/rendered.yaml" > "$work/args"
|
||||
[ -s "$work/config/alpha-config.yaml" ]
|
||||
[ -s "$work/image" ]
|
||||
}
|
||||
|
||||
# start_proxy [docker run options...]; prints the published host:port
|
||||
start_proxy() {
|
||||
local cookie_secret args=() a
|
||||
cookie_secret=$(head -c 16 /dev/urandom | od -An -tx1 | tr -d ' \n')
|
||||
while IFS= read -r a; do
|
||||
args+=("${a/\$(OAUTH2_PROXY_COOKIE_SECRET)/$cookie_secret}")
|
||||
done < "$work/args"
|
||||
docker run -d "$@" -p 127.0.0.1::8000 \
|
||||
-v "$work/config:/etc/oauth2-proxy/config:ro" \
|
||||
-v "$work/client:/etc/oauth2-proxy/client:ro" \
|
||||
"$(cat "$work/image")" "${args[@]}" > "$work/cid-proxy"
|
||||
local addr
|
||||
addr=$(docker port "$(cat "$work/cid-proxy")" 8000/tcp | head -n1)
|
||||
timeout 60 sh -c "until curl -sf -o /dev/null http://$addr/ping; do sleep 0.5; done" || {
|
||||
echo "gatekeeper did not become ready; its log:" >&2
|
||||
docker logs "$(cat "$work/cid-proxy")" >&2 || true
|
||||
return 1
|
||||
}
|
||||
echo "$addr"
|
||||
}
|
||||
|
||||
# login_redirect <host:port> <query>
|
||||
login_redirect() {
|
||||
curl -sS -o /dev/null -w '%{redirect_url}' "http://$1/oauth2/start?$2"
|
||||
}
|
||||
|
||||
auth_url="https://keycloak.$HOST/realms/$REALM/protocol/openid-connect/auth"
|
||||
|
||||
# expect_redirect <location> <must contain>... ; a leading "!" means must not
|
||||
# contain. A function, not [[ ]]: a failing [[ ]] mid-test does not stop the
|
||||
# test under bash 3.2.
|
||||
expect_redirect() {
|
||||
local location=$1 want
|
||||
shift
|
||||
case "$location" in
|
||||
"$auth_url?"*) ;;
|
||||
*) echo "expected a redirect to $auth_url, got: ${location:-<none>}" >&2; return 1 ;;
|
||||
esac
|
||||
for want in "$@"; do
|
||||
case "$want" in
|
||||
!*)
|
||||
case "$location" in
|
||||
*"${want#!}"*) echo "unexpected ${want#!} in: $location" >&2; return 1 ;;
|
||||
esac
|
||||
;;
|
||||
*)
|
||||
case "$location" in
|
||||
*"$want"*) ;;
|
||||
*) echo "expected $want in: $location" >&2; return 1 ;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
@test "in-cluster Keycloak URL: login redirects to the configured realm and passes an allowed kc_idp_hint" {
|
||||
render "http://keycloak-http.cozy-keycloak.svc:8080/realms/$REALM"
|
||||
addr=$(start_proxy --network bridge)
|
||||
|
||||
expect_redirect "$(login_redirect "$addr" 'kc_idp_hint=portal')" '&kc_idp_hint=portal&'
|
||||
expect_redirect "$(login_redirect "$addr" 'kc_idp_hint=x%26prompt%3Dnone')" '!kc_idp_hint' '!prompt=none'
|
||||
expect_redirect "$(login_redirect "$addr" '')" '!kc_idp_hint'
|
||||
}
|
||||
|
||||
@test "OIDC discovery: the gatekeeper discovers the configured realm and passes an allowed kc_idp_hint" {
|
||||
render ""
|
||||
issuer="https://keycloak.$HOST/realms/$REALM"
|
||||
|
||||
mkdir -p "$work/stub/realms/$REALM/.well-known"
|
||||
cat > "$work/stub/realms/$REALM/.well-known/openid-configuration" <<EOF
|
||||
{
|
||||
"issuer": "$issuer",
|
||||
"authorization_endpoint": "$auth_url",
|
||||
"token_endpoint": "$issuer/protocol/openid-connect/token",
|
||||
"userinfo_endpoint": "$issuer/protocol/openid-connect/userinfo",
|
||||
"jwks_uri": "$issuer/protocol/openid-connect/certs",
|
||||
"end_session_endpoint": "$issuer/protocol/openid-connect/logout",
|
||||
"response_types_supported": ["code"],
|
||||
"subject_types_supported": ["public"],
|
||||
"id_token_signing_alg_values_supported": ["RS256"]
|
||||
}
|
||||
EOF
|
||||
openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=keycloak.$HOST" \
|
||||
-addext "subjectAltName=DNS:keycloak.$HOST" \
|
||||
-keyout "$work/stub/tls.key" -out "$work/stub/tls.crt" 2>/dev/null
|
||||
|
||||
network="gatekeeper-login-$(basename "$work" | tr -cd 'a-zA-Z0-9')"
|
||||
docker network create "$network" >/dev/null
|
||||
echo "$network" > "$work/network"
|
||||
docker run -d --network "$network" --network-alias "keycloak.$HOST" \
|
||||
-v "$work/stub:/stub:ro" -w /stub "$STUB_IMAGE" python3 -c '
|
||||
import http.server, ssl
|
||||
server = http.server.ThreadingHTTPServer(("", 443), http.server.SimpleHTTPRequestHandler)
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ctx.load_cert_chain("tls.crt", "tls.key")
|
||||
server.socket = ctx.wrap_socket(server.socket, server_side=True)
|
||||
server.serve_forever()
|
||||
' > "$work/cid-stub"
|
||||
# Discovery runs once at startup and the gatekeeper exits if it fails.
|
||||
timeout 60 sh -c "until docker exec $(cat "$work/cid-stub") python3 -c 'import socket; socket.create_connection((\"127.0.0.1\", 443), 1)' 2>/dev/null; do sleep 0.5; done" || {
|
||||
docker logs "$(cat "$work/cid-stub")" >&2 || true
|
||||
false
|
||||
}
|
||||
|
||||
addr=$(start_proxy --network "$network" -v "$work/stub/tls.crt:/stub-ca.crt:ro" -e SSL_CERT_FILE=/stub-ca.crt)
|
||||
|
||||
expect_redirect "$(login_redirect "$addr" 'kc_idp_hint=portal')" '&kc_idp_hint=portal&'
|
||||
}
|
||||
@@ -367,6 +367,48 @@
|
||||
# has necessarily been re-reconciled, so Ready here means the whole upgrade
|
||||
# converged -- not just the one Deployment this test watched.
|
||||
kubectl wait hr/dashboard -n cozy-dashboard --timeout=10m --for=condition=ready
|
||||
|
||||
# The login redirect oauth2-proxy builds from the alpha config: the realm path
|
||||
# must reach the browser-facing auth URL, an allowed kc_idp_hint must be passed
|
||||
# through to Keycloak, and a value outside the allow pattern must be dropped.
|
||||
# Asserted after the port-forward is gone: on failure set -e would skip a kill
|
||||
# placed after the assertion, and a trap would hide the TAP line.
|
||||
pf_log=$(mktemp)
|
||||
kubectl -n cozy-dashboard port-forward deploy/incloud-web-gatekeeper 18000:8000 >"$pf_log" 2>&1 &
|
||||
pf_pid=$!
|
||||
hinted=""
|
||||
rejected=""
|
||||
if timeout 30 sh -ec 'until nc -z localhost 18000; do sleep 0.2; done'; then
|
||||
hinted=$(curl -sS -o /dev/null -w '%{redirect_url}' 'http://localhost:18000/oauth2/start?kc_idp_hint=e2e-idp') || true
|
||||
rejected=$(curl -sS -o /dev/null -w '%{redirect_url}' 'http://localhost:18000/oauth2/start?kc_idp_hint=e2e%26prompt%3Dnone') || true
|
||||
fi
|
||||
kill "$pf_pid" 2>/dev/null || true
|
||||
wait "$pf_pid" 2>/dev/null || true
|
||||
|
||||
case "$hinted" in
|
||||
https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth\?*\&kc_idp_hint=e2e-idp\&*|https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth\?*\&kc_idp_hint=e2e-idp) ;;
|
||||
*)
|
||||
echo "=== /oauth2/start?kc_idp_hint=e2e-idp did not redirect to the cozy realm with the hint ==="
|
||||
echo "Location: ${hinted:-<none>}"
|
||||
cat "$pf_log"
|
||||
false
|
||||
;;
|
||||
esac
|
||||
case "$rejected" in
|
||||
https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth\?*kc_idp_hint*|https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth\?*prompt=none*)
|
||||
echo "=== a kc_idp_hint outside the allow pattern reached the Keycloak URL ==="
|
||||
echo "Location: $rejected"
|
||||
false
|
||||
;;
|
||||
https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth\?*) ;;
|
||||
*)
|
||||
echo "=== /oauth2/start with a rejected kc_idp_hint did not redirect to Keycloak ==="
|
||||
echo "Location: ${rejected:-<none>}"
|
||||
cat "$pf_log"
|
||||
false
|
||||
;;
|
||||
esac
|
||||
rm -f "$pf_log"
|
||||
}
|
||||
|
||||
@test "Aggregated API rejects Tenant name with dashes" {
|
||||
|
||||
Reference in new issue
Block a user