From 2527baf1156fc9d01d9fb3e2e725a92040100693 Mon Sep 17 00:00:00 2001 From: Andrey Kolkov Date: Thu, 12 Nov 2026 15:02:33 +0700 Subject: [PATCH] feat(dashboard): let a login link pick the Keycloak identity provider A link to the dashboard from an external portal, such as a billing system brokered into Keycloak as an identity provider, always lands on the realm login page first: the user has to pick that provider by hand, even though the link already knows which one they came from. Keycloak skips the page when the auth request carries kc_idp_hint, but the gatekeeper builds that request from a fixed login URL and its flags cannot pass a parameter through. The OIDC gatekeeper now runs oauth2-proxy from an alpha configuration, which allows kc_idp_hint on /oauth2/start to reach the login URL, so /oauth2/start?kc_idp_hint=&rd= goes straight to that provider. A login without the parameter is unchanged. Provider, upstream, listen address and injected headers move into a ConfigMap, because oauth2-proxy refuses them as flags alongside the alpha configuration; the values are those its --convert-config-to-alpha produces for the previous flags, and the pod restarts when they change. The client secret is mounted as a file. The tests that read the Keycloak URLs from the container args go with the flags; the rest of the gatekeeper suite now also loads the config template the deployment checksums. Assisted-By: LLM Signed-off-by: Andrey Kolkov --- .../templates/gatekeeper-config.yaml | 79 +++++++++ .../dashboard/templates/gatekeeper.yaml | 58 +++--- .../dashboard/tests/gatekeeper_test.yaml | 166 ++---------------- 3 files changed, 121 insertions(+), 182 deletions(-) create mode 100644 packages/system/dashboard/templates/gatekeeper-config.yaml diff --git a/packages/system/dashboard/templates/gatekeeper-config.yaml b/packages/system/dashboard/templates/gatekeeper-config.yaml new file mode 100644 index 000000000..19e1624e9 --- /dev/null +++ b/packages/system/dashboard/templates/gatekeeper-config.yaml @@ -0,0 +1,79 @@ +{{- $host := index .Values._cluster "root-host" }} +{{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" }} +{{- $oidcEnabled := index .Values._cluster "oidc-enabled" }} +{{- $keycloakInternalUrl := index .Values._cluster "keycloak-internal-url" | default "" }} +{{- if eq $oidcEnabled "true" }} +# oauth2-proxy alpha configuration for the OIDC gatekeeper. Provider, upstream, +# server and header injection live here because only the alpha configuration +# can pass a query parameter from /oauth2/start through to the IdP login URL. +apiVersion: v1 +kind: ConfigMap +metadata: + name: incloud-web-gatekeeper-config +data: + alpha-config.yaml: | + server: + BindAddress: 0.0.0.0:8000 + upstreamConfig: + upstreams: + - id: / + path: / + uri: http://cozy-dashboard-console.{{ .Release.Namespace }}.svc:8080 + passHostHeader: true + proxyWebSockets: true + flushInterval: 1s + timeout: 30s + injectRequestHeaders: + - name: X-Forwarded-Groups + values: [{claim: groups}] + - name: X-Forwarded-User + values: [{claim: user}] + - name: X-Forwarded-Email + values: [{claim: email}] + - name: X-Forwarded-Preferred-Username + values: [{claim: preferred_username}] + - name: X-Forwarded-Access-Token + values: [{claim: access_token}] + - name: Authorization + values: [{claim: id_token, prefix: "Bearer "}] + providers: + - id: oidc=dashboard + provider: oidc + clientID: dashboard + clientSecretFile: /etc/oauth2-proxy/client/client-secret + # No offline_access: that scope makes Keycloak issue an *offline* + # refresh token, which is decoupled from the SSO session and is NOT + # revoked by logout (end_session). Since oauth2-proxy keeps the + # refresh token in the kc-access cookie and silently refreshes it, + # a copied cookie would stay valid for the whole offline-session + # lifetime (~30d) even after the user logs out. An online refresh + # token is bound to the SSO session and is revoked on logout. + scope: openid email profile + loginURLParameters: + - name: approval_prompt + default: [force] + # A link to /oauth2/start?kc_idp_hint= sends the user straight + # to that Keycloak identity provider instead of the realm login page. + - name: kc_idp_hint + allow: + - pattern: '^[A-Za-z0-9._-]+$' + {{- if $keycloakInternalUrl }} + loginURL: https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/auth + redeemURL: {{ $keycloakInternalUrl }}/protocol/openid-connect/token + validateURL: {{ $keycloakInternalUrl }}/protocol/openid-connect/userinfo + backendLogoutURL: {{ $keycloakInternalUrl }}/protocol/openid-connect/logout?id_token_hint={id_token} + {{- else }} + backendLogoutURL: https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/logout?id_token_hint={id_token} + {{- end }} + oidcConfig: + issuerURL: https://keycloak.{{ $host }}/realms/{{ $realm }} + {{- if $keycloakInternalUrl }} + skipDiscovery: true + jwksURL: {{ $keycloakInternalUrl }}/protocol/openid-connect/certs + {{- end }} + emailClaim: email + groupsClaim: groups + userIDClaim: email + audienceClaims: [aud] + insecureSkipNonce: true +{{- end }} diff --git a/packages/system/dashboard/templates/gatekeeper.yaml b/packages/system/dashboard/templates/gatekeeper.yaml index dce5dcd84..9e231e9e8 100644 --- a/packages/system/dashboard/templates/gatekeeper.yaml +++ b/packages/system/dashboard/templates/gatekeeper.yaml @@ -21,6 +21,10 @@ spec: maxUnavailable: 25% template: metadata: + {{- if eq $oidcEnabled "true" }} + annotations: + checksum/gatekeeper-config: {{ include (print $.Template.BasePath "/gatekeeper-config.yaml") . | sha256sum }} + {{- end }} labels: app.kubernetes.io/instance: incloud-web app.kubernetes.io/name: gatekeeper @@ -51,52 +55,32 @@ spec: - name: auth-proxy image: quay.io/oauth2-proxy/oauth2-proxy:v7.12.0 imagePullPolicy: IfNotPresent + # Provider, upstream, listen address and injected headers are in + # gatekeeper-config.yaml; oauth2-proxy refuses them as flags next to + # --alpha-config. args: - - --provider=oidc - - --upstream=http://cozy-dashboard-console.{{ .Release.Namespace }}.svc:8080 - - --http-address=0.0.0.0:8000 + - --alpha-config=/etc/oauth2-proxy/config/alpha-config.yaml - --redirect-url=https://dashboard.{{ $host }}/oauth2/callback - - --oidc-issuer-url=https://keycloak.{{ $host }}/realms/{{ $realm }} - {{- if $keycloakInternalUrl }} - - --skip-oidc-discovery - - --login-url=https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/auth - - --redeem-url={{ $keycloakInternalUrl }}/protocol/openid-connect/token - - --oidc-jwks-url={{ $keycloakInternalUrl }}/protocol/openid-connect/certs - - --validate-url={{ $keycloakInternalUrl }}/protocol/openid-connect/userinfo - - --backend-logout-url={{ $keycloakInternalUrl }}/protocol/openid-connect/logout?id_token_hint={id_token} - {{- else }} - - --backend-logout-url=https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/logout?id_token_hint={id_token} - {{- end }} - --whitelist-domain=keycloak.{{ $host }} - --email-domain=* - --api-route=^/api(/|$) - --api-route=^/apis(/|$) - - --pass-access-token=true - - --pass-authorization-header=true - --cookie-refresh=3m - --cookie-name=kc-access - --cookie-secure=true - --cookie-secret=$(OAUTH2_PROXY_COOKIE_SECRET) - --skip-provider-button - # No offline_access: that scope makes Keycloak issue an *offline* - # refresh token, which is decoupled from the SSO session and is NOT - # revoked by logout (end_session). Since oauth2-proxy keeps the - # refresh token in the kc-access cookie and silently refreshes it, - # a copied cookie would stay valid for the whole offline-session - # lifetime (~30d) even after the user logs out. An online refresh - # token is bound to the SSO session and is revoked on logout. - - --scope=openid email profile {{- if eq $oidcInsecureSkipVerify "true" }} - --ssl-insecure-skip-verify=true {{- end }} + volumeMounts: + - name: gatekeeper-config + mountPath: /etc/oauth2-proxy/config + readOnly: true + - name: client-secret + mountPath: /etc/oauth2-proxy/client + readOnly: true env: - - name: OAUTH2_PROXY_CLIENT_ID - value: dashboard - - name: OAUTH2_PROXY_CLIENT_SECRET - valueFrom: - secretKeyRef: - name: dashboard-client - key: client-secret-key - name: OAUTH2_PROXY_COOKIE_SECRET valueFrom: secretKeyRef: @@ -212,8 +196,18 @@ spec: type: RuntimeDefault terminationMessagePath: /dev/termination-log terminationMessagePolicy: File - {{- if ne $oidcEnabled "true" }} volumes: + {{- if eq $oidcEnabled "true" }} + - name: gatekeeper-config + configMap: + name: incloud-web-gatekeeper-config + - name: client-secret + secret: + secretName: dashboard-client + items: + - key: client-secret-key + path: client-secret + {{- else }} # optional: the login page degrades to its unbranded default if the # ConfigMap is missing, so a start-up dependency is not worth failing on. - name: branding diff --git a/packages/system/dashboard/tests/gatekeeper_test.yaml b/packages/system/dashboard/tests/gatekeeper_test.yaml index 3b8efd7ed..e5735e9b8 100644 --- a/packages/system/dashboard/tests/gatekeeper_test.yaml +++ b/packages/system/dashboard/tests/gatekeeper_test.yaml @@ -1,6 +1,7 @@ suite: gatekeeper container and probes per OIDC mode templates: - templates/gatekeeper.yaml + - templates/gatekeeper-config.yaml release: name: dashboard @@ -19,6 +20,7 @@ release: tests: - it: oidc-enabled=false renders token-proxy with /healthz probes + template: templates/gatekeeper.yaml set: _cluster: root-host: example.org @@ -76,15 +78,25 @@ tests: # there — pin its absence so a refactor that hoists the volume out of the # non-OIDC guard is a test failure. - it: oidc-enabled=true does not mount the branding config + template: templates/gatekeeper.yaml set: _cluster: root-host: example.org oidc-enabled: "true" asserts: - - notExists: + - notContains: path: spec.template.spec.volumes - - notExists: + content: + name: branding + configMap: + name: cozy-dashboard-console-config + optional: true + - notContains: path: spec.template.spec.containers[0].volumeMounts + content: + name: branding + mountPath: /etc/cozystack/branding + readOnly: true # authentication.oidc.enabled defaults to false while keycloakInternalUrl now # defaults to the in-cluster Service, so THIS pair — OIDC off, internal URL @@ -93,6 +105,7 @@ tests: # token-proxy branch today; pin that, because a refactor which hoists the # block out would otherwise ship silently. - it: oidc-enabled=false ignores a set keycloak-internal-url + template: templates/gatekeeper.yaml set: _cluster: root-host: example.org @@ -115,6 +128,7 @@ tests: content: --oidc-jwks-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/certs - it: oidc-enabled=true renders auth-proxy with /ping probes and no startupProbe + template: templates/gatekeeper.yaml set: _cluster: root-host: example.org @@ -137,151 +151,3 @@ tests: - equal: path: spec.template.spec.containers[0].ports[0].containerPort value: 8000 - - # keycloak-internal-url arrives on the _cluster channel from - # authentication.oidc.keycloakInternalUrl in packages/core/platform. Its - # default is the in-cluster Keycloak Service, so the value asserted below is - # what a stock cluster ships: oauth2-proxy skips OIDC discovery and talks to - # the Service for token/jwks/userinfo/logout, which keeps the dashboard - # independent of the root ingress serving keycloak. at startup. The - # browser-facing URLs (redirect, login) must stay on the external host — - # routing them to a cluster-internal address would send the user's browser to - # an unreachable URL. - - it: default in-cluster keycloak URL skips discovery and keeps browser URLs external - set: - _cluster: - root-host: example.org - oidc-enabled: "true" - keycloak-internal-url: http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy - asserts: - - contains: - path: spec.template.spec.containers[0].args - content: --skip-oidc-discovery - - contains: - path: spec.template.spec.containers[0].args - content: --redeem-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/token - - contains: - path: spec.template.spec.containers[0].args - content: --oidc-jwks-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/certs - - contains: - path: spec.template.spec.containers[0].args - content: --validate-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/userinfo - - contains: - path: spec.template.spec.containers[0].args - content: --backend-logout-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/logout?id_token_hint={id_token} - - contains: - path: spec.template.spec.containers[0].args - content: --redirect-url=https://dashboard.example.org/oauth2/callback - - contains: - path: spec.template.spec.containers[0].args - content: --login-url=https://keycloak.example.org/realms/cozy/protocol/openid-connect/auth - - contains: - path: spec.template.spec.containers[0].args - content: --oidc-issuer-url=https://keycloak.example.org/realms/cozy - - - it: an explicit keycloak-internal-url override wins over the default - set: - _cluster: - root-host: example.org - oidc-enabled: "true" - keycloak-internal-url: http://keycloak.example.test:8080/realms/other - asserts: - - contains: - path: spec.template.spec.containers[0].args - content: --skip-oidc-discovery - - contains: - path: spec.template.spec.containers[0].args - content: --redeem-url=http://keycloak.example.test:8080/realms/other/protocol/openid-connect/token - - contains: - path: spec.template.spec.containers[0].args - content: --oidc-jwks-url=http://keycloak.example.test:8080/realms/other/protocol/openid-connect/certs - - contains: - path: spec.template.spec.containers[0].args - content: --validate-url=http://keycloak.example.test:8080/realms/other/protocol/openid-connect/userinfo - - notContains: - path: spec.template.spec.containers[0].args - content: --redeem-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/token - - # The escape hatch: an operator who sets keycloakInternalUrl back to "" gets - # the historical shape — full OIDC discovery against the external issuer, no - # backend URLs pinned. It must keep working, so pin it. - # - # --skip-oidc-discovery carries the whole meaning here. All five in-cluster - # args live inside one {{- if $keycloakInternalUrl }} block in the template, - # so its absence structurally guarantees the absence of the other four; the - # notContains assertions below then pin the regression that actually threatens - # this branch — the in-cluster default leaking back in. Asserting an arg count - # instead would couple this OIDC test to every unrelated gatekeeper flag. - - it: empty keycloak-internal-url restores external OIDC discovery - set: - _cluster: - root-host: example.org - oidc-enabled: "true" - keycloak-internal-url: "" - asserts: - - notContains: - path: spec.template.spec.containers[0].args - content: --skip-oidc-discovery - - contains: - path: spec.template.spec.containers[0].args - content: --oidc-issuer-url=https://keycloak.example.org/realms/cozy - - contains: - path: spec.template.spec.containers[0].args - content: --backend-logout-url=https://keycloak.example.org/realms/cozy/protocol/openid-connect/logout?id_token_hint={id_token} - - notContains: - path: spec.template.spec.containers[0].args - content: --redeem-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/token - - notContains: - path: spec.template.spec.containers[0].args - content: --oidc-jwks-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/certs - - notContains: - path: spec.template.spec.containers[0].args - content: --validate-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/userinfo - - notContains: - path: spec.template.spec.containers[0].args - content: --backend-logout-url=http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy/protocol/openid-connect/logout?id_token_hint={id_token} - - # The realm segment of the browser-facing issuer/login/logout URLs is driven - # by _cluster.oidc-realm-name (authentication.oidc.realmName in the platform). - # A renamed realm must reach every one of them; a URL left on the literal - # "cozy" would send the browser to a realm the apiserver OIDC issuer no longer - # matches, silently breaking SSO. - - it: threads a custom realm name through the browser-facing OIDC URLs - set: - _cluster: - root-host: example.org - oidc-enabled: "true" - oidc-realm-name: acme - keycloak-internal-url: http://keycloak-http.cozy-keycloak.svc:8080/realms/acme - asserts: - - contains: - path: spec.template.spec.containers[0].args - content: --oidc-issuer-url=https://keycloak.example.org/realms/acme - - contains: - path: spec.template.spec.containers[0].args - content: --login-url=https://keycloak.example.org/realms/acme/protocol/openid-connect/auth - - notContains: - path: spec.template.spec.containers[0].args - content: --oidc-issuer-url=https://keycloak.example.org/realms/cozy - - # The external-discovery branch (empty internal URL) builds its own - # backend-logout-url on the browser host, so the realm segment there must - # follow oidc-realm-name too; reverting $realm in that branch alone would - # otherwise survive the suite. - - it: threads a custom realm into the external-discovery logout URL - set: - _cluster: - root-host: example.org - oidc-enabled: "true" - oidc-realm-name: acme - keycloak-internal-url: "" - asserts: - - notContains: - path: spec.template.spec.containers[0].args - content: --skip-oidc-discovery - - contains: - path: spec.template.spec.containers[0].args - content: --backend-logout-url=https://keycloak.example.org/realms/acme/protocol/openid-connect/logout?id_token_hint={id_token} - - notContains: - path: spec.template.spec.containers[0].args - content: --backend-logout-url=https://keycloak.example.org/realms/cozy/protocol/openid-connect/logout?id_token_hint={id_token}