diff --git a/packages/apps/kubernetes/templates/_helpers.tpl b/packages/apps/kubernetes/templates/_helpers.tpl index e27f5ccdb..f9b706baa 100644 --- a/packages/apps/kubernetes/templates/_helpers.tpl +++ b/packages/apps/kubernetes/templates/_helpers.tpl @@ -120,12 +120,14 @@ scope from packages/system/keycloak-configure. {{- end }} {{/* -Issuer URL for `mode: System`. Resolves to the platform Keycloak realm -`cozy`, served at the root host published in the per-namespace bundle by -cozystack-basics. +Issuer URL for `mode: System`. Resolves to the platform Keycloak realm, +served at the keycloak-host published in the per-namespace bundle by +cozystack-basics (keycloak. when the platform does not set one). */}} {{- define "kubernetes.oidc.systemIssuerURL" -}} -{{- printf "https://keycloak.%s/realms/%s" (dig "root-host" "" (.Values._cluster | default dict)) (dig "oidc-realm-name" "cozy" (.Values._cluster | default dict) | toString) }} +{{- $cluster := .Values._cluster | default dict }} +{{- $keycloakHost := dig "keycloak-host" "" $cluster | default (printf "keycloak.%s" (dig "root-host" "" $cluster)) }} +{{- printf "https://%s/realms/%s" $keycloakHost (dig "oidc-realm-name" "cozy" $cluster | toString) }} {{- end }} {{/* diff --git a/packages/apps/kubernetes/tests/oidc_test.yaml b/packages/apps/kubernetes/tests/oidc_test.yaml index 55a34aba9..1413e2cfb 100644 --- a/packages/apps/kubernetes/tests/oidc_test.yaml +++ b/packages/apps/kubernetes/tests/oidc_test.yaml @@ -2929,3 +2929,23 @@ tests: - notMatchRegex: path: stringData["config.yaml"] pattern: "group in the cozy realm" + + # The guest apiserver must trust the issuer Keycloak actually signs with, + # which is https://<_cluster.keycloak-host>/realms/. + - it: System mode points the issuer at a custom keycloak-host + release: + name: kubernetes-app + namespace: tenant-foo + template: templates/oidc-authn-config.yaml + set: + _cluster.oidc-enabled: "true" + _cluster.root-host: example.org + _cluster.keycloak-host: auth.example.org + oidc.mode: System + asserts: + - matchRegex: + path: stringData["config.yaml"] + pattern: "url: https://auth.example.org/realms/cozy" + - notMatchRegex: + path: stringData["config.yaml"] + pattern: "keycloak.example.org" diff --git a/packages/core/platform/templates/apps.yaml b/packages/core/platform/templates/apps.yaml index db6c7109b..9e41f2e7a 100644 --- a/packages/core/platform/templates/apps.yaml +++ b/packages/core/platform/templates/apps.yaml @@ -146,6 +146,27 @@ stringData: {{- fail "authentication.oidc.realmName must not be master: that realm holds the Keycloak super-admin, so platform clients and end users would share it. Pick any other name, e.g. realmName: \"cozy\"" }} {{- end }} oidc-realm-name: {{ $realmName | quote }} + {{- /* Resolved here so every consumer reads one ready-made value instead + of re-deriving keycloak.. An explicit value is spliced + into URLs and an oauth2-proxy --whitelist-domain flag, so only a + bare lowercase DNS name within the RFC 1123 length limits is + accepted: a scheme, port or path would yield an issuer that + matches nothing. The kind is checked before `default`, which would + otherwise launder false or 0 into the derived host. The derived + keycloak. is not validated, so clusters that never set + this key render exactly as before. */}} + {{- $keycloakHost := .Values.authentication.oidc.keycloakHost }} + {{- if not (or (kindIs "invalid" $keycloakHost) (kindIs "string" $keycloakHost)) }} + {{- fail (printf "authentication.oidc.keycloakHost must be a string (got %s)" (kindOf $keycloakHost)) }} + {{- end }} + {{- if $keycloakHost }} + {{- if or (gt (len $keycloakHost) 253) (not (regexMatch "^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$" $keycloakHost)) }} + {{- fail (printf "authentication.oidc.keycloakHost %q is invalid: give a bare lowercase hostname such as auth.example.org, without scheme, port or path, with labels of at most 63 characters and 253 in total" $keycloakHost) }} + {{- end }} + {{- else }} + {{- $keycloakHost = printf "keycloak.%s" $rootHost }} + {{- end }} + keycloak-host: {{ $keycloakHost | quote }} {{- /* null keycloakInternalUrl derives the in-cluster Service URL for the configured realm; "" is the external-discovery escape hatch and an explicit value is passed through, so distinguish null from "" with diff --git a/packages/core/platform/tests/apps_oidc_keycloak_host_test.yaml b/packages/core/platform/tests/apps_oidc_keycloak_host_test.yaml new file mode 100644 index 000000000..c7dd6b3d0 --- /dev/null +++ b/packages/core/platform/tests/apps_oidc_keycloak_host_test.yaml @@ -0,0 +1,102 @@ +suite: apps.yaml OIDC keycloak-host platform → tenant value wiring +templates: + - templates/apps.yaml + +release: + name: cozy-platform + namespace: cozy-system + +# authentication.oidc.keycloakHost is the single source for the host of the +# Keycloak issuer. It rides the _cluster channel as keycloak-host, already +# resolved, so the Keycloak chart and every OIDC consumer agree on it without +# each re-deriving keycloak.. + +tests: + - it: defaults the keycloak host to keycloak. + set: + publishing.host: example.org + asserts: + - matchRegex: + path: stringData["values.yaml"] + pattern: 'keycloak-host:\s*"keycloak\.example\.org"' + + - it: propagates a custom keycloak host onto the _cluster channel + set: + publishing.host: example.org + authentication.oidc.keycloakHost: auth.example.org + asserts: + - matchRegex: + path: stringData["values.yaml"] + pattern: 'keycloak-host:\s*"auth\.example\.org"' + + # The host is spliced after "https://" and into --whitelist-domain, so a + # scheme would yield https://https://... and a path would shift the realm. + - it: rejects a keycloak host carrying a scheme + set: + authentication.oidc.keycloakHost: https://auth.example.org + asserts: + - failedTemplate: + errorPattern: 'authentication.oidc.keycloakHost "https://auth.example.org" is invalid' + + - it: rejects a keycloak host carrying a path + set: + authentication.oidc.keycloakHost: auth.example.org/realms + asserts: + - failedTemplate: + errorPattern: 'authentication.oidc.keycloakHost "auth.example.org/realms" is invalid' + + - it: rejects a keycloak host carrying a port + set: + authentication.oidc.keycloakHost: auth.example.org:8443 + asserts: + - failedTemplate: + errorPattern: 'authentication.oidc.keycloakHost "auth.example.org:8443" is invalid' + + # `default` treats false and 0 as unset; checking the kind first keeps a + # mistyped value from silently falling back to keycloak.. + - it: rejects a non-string keycloak host instead of falling back to the default + set: + authentication.oidc.keycloakHost: false + asserts: + - failedTemplate: + errorMessage: 'authentication.oidc.keycloakHost must be a string (got bool)' + + - it: rejects a DNS label longer than 63 characters + set: + authentication.oidc.keycloakHost: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.example.org + asserts: + - failedTemplate: + errorPattern: 'authentication.oidc.keycloakHost ".*" is invalid' + + - it: rejects a hostname longer than 253 characters + set: + authentication.oidc.keycloakHost: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.org + asserts: + - failedTemplate: + errorPattern: 'authentication.oidc.keycloakHost ".*" is invalid' + + - it: accepts a 63-character label + set: + authentication.oidc.keycloakHost: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.example.org + asserts: + - matchRegex: + path: stringData["values.yaml"] + pattern: 'keycloak-host:\s*"a{63}\.example\.org"' + + # Both are valid DNS labels, so they must survive validation and reach the + # _cluster channel as strings. + - it: accepts an all-digit keycloak host + set: + authentication.oidc.keycloakHost: "123" + asserts: + - matchRegex: + path: stringData["values.yaml"] + pattern: 'keycloak-host:\s*"123"' + + - it: accepts a boolean-looking keycloak host + set: + authentication.oidc.keycloakHost: "true" + asserts: + - matchRegex: + path: stringData["values.yaml"] + pattern: 'keycloak-host:\s*"true"' diff --git a/packages/core/platform/values.yaml b/packages/core/platform/values.yaml index 22dc05e80..f2046cf94 100644 --- a/packages/core/platform/values.yaml +++ b/packages/core/platform/values.yaml @@ -528,6 +528,27 @@ authentication: # it here would put the platform clients, the cluster-admin roles and every # end user inside it. realmName: "cozy" + # Public hostname Keycloak is served on, without scheme or path. Empty means + # keycloak.. It is the host of the OIDC issuer + # (https:///realms/), so the Keycloak Ingress and + # KC_HOSTNAME, the dashboard and linstor-gui gatekeepers, Grafana SSO, the + # info kubeconfig and tenant Kubernetes clusters in oidc.mode System all + # follow it. As with realmName, the apiserver OIDC issuer is set out-of-band + # via talm oidcIssuerUrl and must name the same host. + # + # Changing it on a running cluster changes the issuer of every token minted + # afterwards: sessions and refresh tokens issued under the old host stop + # being accepted, and the apiserver must trust the new issuer first. + # + # Certificates that cover the whole apex do not stretch to a host outside + # it. In wildcard mode (publishing.certificates.wildcard or + # wildcardSecretName) the Keycloak Ingress relies on the wildcard + # certificate, which covers *. only; on the Gateway path + # the dns01, existingSecret and edge cert modes render listeners for the + # apex alone, so the Keycloak HTTPRoute finds no listener. Pick a host one + # label under publishing.host, or provide a certificate and listener that + # cover it. + keycloakHost: "" # In-cluster URL of the KeyCloak realm, used for backend-to-backend requests. oauth2-proxy runs # with --skip-oidc-discovery and sends every backend call (token, jwks, userinfo, logout) to this # URL, while browser-facing redirects keep pointing at the external host. diff --git a/packages/extra/info/templates/kubeconfig.yaml b/packages/extra/info/templates/kubeconfig.yaml index e8fea935d..9df00b375 100644 --- a/packages/extra/info/templates/kubeconfig.yaml +++ b/packages/extra/info/templates/kubeconfig.yaml @@ -1,5 +1,6 @@ {{- $host := index .Values._cluster "root-host" }} {{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" }} +{{- $keycloakHost := index .Values._cluster "keycloak-host" | default (printf "keycloak.%s" $host) }} {{- $oidcEnabled := index .Values._cluster "oidc-enabled" }} {{- if $oidcEnabled }} {{- $apiServerEndpoint := index .Values._cluster "api-server-endpoint" }} @@ -36,7 +37,7 @@ stringData: args: - oidc-login - get-token - - --oidc-issuer-url=https://keycloak.{{ $host }}/realms/{{ $realm }} + - --oidc-issuer-url=https://{{ $keycloakHost }}/realms/{{ $realm }} - --oidc-client-id=kubernetes - --skip-open-browser command: kubectl diff --git a/packages/extra/info/tests/kubeconfig_test.yaml b/packages/extra/info/tests/kubeconfig_test.yaml index 6486e4055..2c3e46cad 100644 --- a/packages/extra/info/tests/kubeconfig_test.yaml +++ b/packages/extra/info/tests/kubeconfig_test.yaml @@ -41,3 +41,19 @@ tests: - notMatchRegex: path: stringData.kubeconfig pattern: '/realms/cozy' + + - it: points the issuer at a custom keycloak-host + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + keycloak-host: auth.example.org + api-server-endpoint: https://api.example.org:6443 + kube-root-ca: dGVzdA== + asserts: + - matchRegex: + path: stringData.kubeconfig + pattern: '--oidc-issuer-url=https://auth\.example\.org/realms/cozy' + - notMatchRegex: + path: stringData.kubeconfig + pattern: 'keycloak\.example\.org' diff --git a/packages/system/dashboard/templates/gatekeeper-config.yaml b/packages/system/dashboard/templates/gatekeeper-config.yaml index 19e1624e9..614b3272c 100644 --- a/packages/system/dashboard/templates/gatekeeper-config.yaml +++ b/packages/system/dashboard/templates/gatekeeper-config.yaml @@ -1,5 +1,6 @@ {{- $host := index .Values._cluster "root-host" }} {{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" }} +{{- $keycloakHost := index .Values._cluster "keycloak-host" | default (printf "keycloak.%s" $host) }} {{- $oidcEnabled := index .Values._cluster "oidc-enabled" }} {{- $keycloakInternalUrl := index .Values._cluster "keycloak-internal-url" | default "" }} {{- if eq $oidcEnabled "true" }} @@ -58,15 +59,15 @@ data: allow: - pattern: '^[A-Za-z0-9._-]+$' {{- if $keycloakInternalUrl }} - loginURL: https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/auth + loginURL: https://{{ $keycloakHost }}/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} + backendLogoutURL: https://{{ $keycloakHost }}/realms/{{ $realm }}/protocol/openid-connect/logout?id_token_hint={id_token} {{- end }} oidcConfig: - issuerURL: https://keycloak.{{ $host }}/realms/{{ $realm }} + issuerURL: https://{{ $keycloakHost }}/realms/{{ $realm }} {{- if $keycloakInternalUrl }} skipDiscovery: true jwksURL: {{ $keycloakInternalUrl }}/protocol/openid-connect/certs diff --git a/packages/system/dashboard/templates/gatekeeper.yaml b/packages/system/dashboard/templates/gatekeeper.yaml index 9e231e9e8..0f90dd039 100644 --- a/packages/system/dashboard/templates/gatekeeper.yaml +++ b/packages/system/dashboard/templates/gatekeeper.yaml @@ -1,5 +1,6 @@ {{- $host := index .Values._cluster "root-host" }} {{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" }} +{{- $keycloakHost := index .Values._cluster "keycloak-host" | default (printf "keycloak.%s" $host) }} {{- $oidcEnabled := index .Values._cluster "oidc-enabled" }} {{- $oidcInsecureSkipVerify := index .Values._cluster "oidc-insecure-skip-verify" }} {{- $keycloakInternalUrl := index .Values._cluster "keycloak-internal-url" | default "" }} @@ -61,7 +62,7 @@ spec: args: - --alpha-config=/etc/oauth2-proxy/config/alpha-config.yaml - --redirect-url=https://dashboard.{{ $host }}/oauth2/callback - - --whitelist-domain=keycloak.{{ $host }} + - --whitelist-domain={{ $keycloakHost }} - --email-domain=* - --api-route=^/api(/|$) - --api-route=^/apis(/|$) diff --git a/packages/system/dashboard/tests/gatekeeper_test.yaml b/packages/system/dashboard/tests/gatekeeper_test.yaml index e5735e9b8..4ac7d5ab9 100644 --- a/packages/system/dashboard/tests/gatekeeper_test.yaml +++ b/packages/system/dashboard/tests/gatekeeper_test.yaml @@ -151,3 +151,57 @@ tests: - equal: path: spec.template.spec.containers[0].ports[0].containerPort value: 8000 + + # The browser-facing issuer, login URL, logout URL and redirect whitelist + # follow _cluster.keycloak-host (authentication.oidc.keycloakHost in the + # platform), so moving Keycloak off keycloak. keeps dashboard SSO + # working. + - it: builds the browser-facing OIDC URLs on a custom keycloak-host + template: templates/gatekeeper-config.yaml + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + keycloak-host: auth.example.org + keycloak-internal-url: http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy + asserts: + - matchRegex: + path: data["alpha-config.yaml"] + pattern: 'issuerURL: https://auth\.example\.org/realms/cozy\n' + - matchRegex: + path: data["alpha-config.yaml"] + pattern: 'loginURL: https://auth\.example\.org/realms/cozy/protocol/openid-connect/auth\n' + - notMatchRegex: + path: data["alpha-config.yaml"] + pattern: 'keycloak\.example\.org' + + - it: builds the external-discovery logout URL on a custom keycloak-host + template: templates/gatekeeper-config.yaml + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + keycloak-host: auth.example.org + keycloak-internal-url: "" + asserts: + - matchRegex: + path: data["alpha-config.yaml"] + pattern: 'backendLogoutURL: https://auth\.example\.org/realms/cozy/protocol/openid-connect/logout\?id_token_hint=\{id_token\}\n' + - notMatchRegex: + path: data["alpha-config.yaml"] + pattern: 'keycloak\.example\.org' + + - it: whitelists the custom keycloak-host for the post-logout redirect + template: templates/gatekeeper.yaml + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + keycloak-host: auth.example.org + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --whitelist-domain=auth.example.org + - notContains: + path: spec.template.spec.containers[0].args + content: --whitelist-domain=keycloak.example.org diff --git a/packages/system/keycloak/templates/_helpers.tpl b/packages/system/keycloak/templates/_helpers.tpl index 311a26f2e..1364ea7e8 100644 --- a/packages/system/keycloak/templates/_helpers.tpl +++ b/packages/system/keycloak/templates/_helpers.tpl @@ -1,3 +1,22 @@ +{{- /* Public hostname Keycloak serves on and issues tokens for. + ingress.host keeps precedence for deployments that set it before the + platform-wide authentication.oidc.keycloakHost existed. Setting both to + different hosts is refused: KC_HOSTNAME would follow ingress.host while + every OIDC consumer builds its issuer from _cluster.keycloak-host, so + every login would fail issuer validation. A keycloak-host equal to the + derived keycloak. is the platform default, not a choice, and + does not conflict. */}} +{{- define "keycloak.host" -}} +{{- $cluster := .Values._cluster | default dict }} +{{- $derived := printf "keycloak.%s" (dig "root-host" "" $cluster) }} +{{- $platformHost := dig "keycloak-host" "" $cluster }} +{{- $override := .Values.ingress.host }} +{{- if and $override $platformHost (ne $platformHost $derived) (ne $platformHost $override) }} +{{- fail (printf "ingress.host %q conflicts with the platform authentication.oidc.keycloakHost %q: OIDC consumers would expect an issuer Keycloak does not sign with. Drop ingress.host and keep only the platform value" $override $platformHost) }} +{{- end }} +{{- $override | default $platformHost | default $derived }} +{{- end }} + {{- /* Realms published on the login host in the narrowed (ingress.adminHost) mode, as a YAML list — callers parse it with fromYamlArray. diff --git a/packages/system/keycloak/templates/httproute.yaml b/packages/system/keycloak/templates/httproute.yaml index 3e3910dd3..78b6bb6da 100644 --- a/packages/system/keycloak/templates/httproute.yaml +++ b/packages/system/keycloak/templates/httproute.yaml @@ -1,5 +1,4 @@ -{{- $host := index .Values._cluster "root-host" }} -{{- $ingressHost := .Values.ingress.host | default (printf "keycloak.%s" $host) }} +{{- $ingressHost := include "keycloak.host" . }} {{- $exposeIngress := (index .Values._cluster "expose-ingress") | default "tenant-root" }} {{- $adminExposeIngress := (index .Values._cluster "expose-ingress-admin") | default $exposeIngress }} {{- $gatewayEnabled := (index .Values._cluster "gateway-enabled") | default "false" }} diff --git a/packages/system/keycloak/templates/ingress.yaml b/packages/system/keycloak/templates/ingress.yaml index aa7b7f966..5eab0e710 100644 --- a/packages/system/keycloak/templates/ingress.yaml +++ b/packages/system/keycloak/templates/ingress.yaml @@ -1,5 +1,5 @@ {{- $host := index .Values._cluster "root-host" }} -{{- $ingressHost := .Values.ingress.host | default (printf "keycloak.%s" $host) }} +{{- $ingressHost := include "keycloak.host" . }} {{- $solver := (index .Values._cluster "solver") | default "http01" }} {{- $clusterIssuer := (index .Values._cluster "issuer-name") | default "letsencrypt-prod" }} {{- $exposeIngress := (index .Values._cluster "expose-ingress") | default "tenant-root" }} @@ -35,7 +35,7 @@ spec: ingressClassName: {{ $exposeIngress }} tls: - hosts: - - {{ $ingressHost }} + - {{ $ingressHost | quote }} {{- /* Operator wildcard mode: no per-host secret — ingress-nginx falls back to the default SSL certificate while the tls section keeps the HTTP-to-HTTPS redirect enforced. */}} @@ -43,7 +43,7 @@ spec: secretName: web-tls {{- end }} rules: - - host: {{ $ingressHost }} + - host: {{ $ingressHost | quote }} http: paths: {{- if $adminHost }} diff --git a/packages/system/keycloak/templates/sts.yaml b/packages/system/keycloak/templates/sts.yaml index c74381919..c28642b53 100644 --- a/packages/system/keycloak/templates/sts.yaml +++ b/packages/system/keycloak/templates/sts.yaml @@ -4,8 +4,7 @@ {{- include "keycloak.truststores.validate" . -}} -{{- $host := index .Values._cluster "root-host" }} -{{- $ingressHost := .Values.ingress.host | default (printf "keycloak.%s" $host) }} +{{- $ingressHost := include "keycloak.host" . }} {{- $clusterDomain := (index .Values._cluster "cluster-domain") | default "cozy.local" }} {{- /* diff --git a/packages/system/keycloak/tests/keycloak_host_test.yaml b/packages/system/keycloak/tests/keycloak_host_test.yaml new file mode 100644 index 000000000..516b4da5f --- /dev/null +++ b/packages/system/keycloak/tests/keycloak_host_test.yaml @@ -0,0 +1,128 @@ +suite: keycloak hostname from the platform keycloak-host + +release: + name: keycloak + namespace: cozy-keycloak + +# KC_HOSTNAME fixes the issuer of every token Keycloak mints, so it must follow +# the same _cluster.keycloak-host the OIDC consumers build their issuer from. +# ingress.host stays as a per-chart override and wins when set. +tests: + - it: serves and issues on the platform keycloak-host + template: templates/sts.yaml + documentSelector: + path: kind + value: StatefulSet + set: + _cluster: + root-host: example.com + keycloak-host: auth.example.com + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: KC_HOSTNAME + value: https://auth.example.com + + - it: routes the Ingress to the platform keycloak-host + template: templates/ingress.yaml + set: + _cluster: + root-host: example.com + keycloak-host: auth.example.com + asserts: + - equal: + path: spec.rules[0].host + value: auth.example.com + - equal: + path: spec.tls[0].hosts + value: [auth.example.com] + + - it: routes the HTTPRoute to the platform keycloak-host + template: templates/httproute.yaml + set: + _cluster: + root-host: example.com + keycloak-host: auth.example.com + gateway-enabled: "true" + asserts: + - contains: + path: spec.hostnames + content: auth.example.com + + # Legacy deployments set only ingress.host; the platform then publishes the + # derived keycloak., which is not a competing choice. + - it: keeps a legacy ingress.host when the platform host is the derived default + template: templates/sts.yaml + documentSelector: + path: kind + value: StatefulSet + set: + _cluster: + root-host: example.com + keycloak-host: keycloak.example.com + ingress: + host: sso.example.com + asserts: + - contains: + path: spec.template.spec.containers[0].env + content: + name: KC_HOSTNAME + value: https://sso.example.com + + - it: refuses an ingress.host that conflicts with an explicit platform host + template: templates/sts.yaml + set: + _cluster: + root-host: example.com + keycloak-host: auth.example.com + ingress: + host: sso.example.com + asserts: + - failedTemplate: + errorPattern: 'ingress.host "sso.example.com" conflicts with the platform authentication.oidc.keycloakHost "auth.example.com"' + + # Platform validation accepts all-digit labels and words YAML would read as + # booleans. Unquoted, "123" and "true" render as a number and a bool, and the + # Ingress no longer decodes as networking.k8s.io/v1. + - it: keeps an all-digit host a string in the Ingress + template: templates/ingress.yaml + set: + _cluster: + root-host: example.com + keycloak-host: "123" + asserts: + - isAPIVersion: + of: networking.k8s.io/v1 + - equal: + path: spec.rules[0].host + value: "123" + - equal: + path: spec.tls[0].hosts + value: ["123"] + + - it: keeps a boolean-looking host a string in the Ingress + template: templates/ingress.yaml + set: + _cluster: + root-host: example.com + keycloak-host: "true" + asserts: + - equal: + path: spec.rules[0].host + value: "true" + - equal: + path: spec.tls[0].hosts + value: ["true"] + + - it: keeps a boolean-looking host a string in the HTTPRoute + template: templates/httproute.yaml + set: + _cluster: + root-host: example.com + keycloak-host: "true" + gateway-enabled: "true" + asserts: + - equal: + path: spec.hostnames + value: ["true"] diff --git a/packages/system/keycloak/values.yaml b/packages/system/keycloak/values.yaml index b85d00061..e54925812 100644 --- a/packages/system/keycloak/values.yaml +++ b/packages/system/keycloak/values.yaml @@ -5,9 +5,12 @@ _cluster: {} image: quay.io/keycloak/keycloak:26.8.2-3 ingress: - # Custom hostname for the Keycloak Ingress. - # If set, this value will be used as the Ingress hostname (e.g., "auth.example.com"). - # If left empty, defaults to "keycloak." based on the cluster root-host setting. + # Custom hostname for the Keycloak Ingress (e.g., "auth.example.com"). + # Prefer the platform-wide authentication.oidc.keycloakHost: it moves the + # Keycloak Ingress and KC_HOSTNAME together with every OIDC consumer + # (dashboard, linstor-gui, Grafana, info kubeconfig, tenant Kubernetes). + # This value moves Keycloak alone, so those consumers keep the old issuer. + # If left empty, the platform keycloak-host is used, then "keycloak.". host: "" # Optional separate hostname for the Keycloak admin console (opt-in). # When set (e.g., "keycloak-admin.example.com"): diff --git a/packages/system/linstor-gui/templates/gatekeeper.yaml b/packages/system/linstor-gui/templates/gatekeeper.yaml index ef1c2521e..9f2c75732 100644 --- a/packages/system/linstor-gui/templates/gatekeeper.yaml +++ b/packages/system/linstor-gui/templates/gatekeeper.yaml @@ -1,5 +1,6 @@ {{- $host := index .Values._cluster "root-host" }} {{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" }} +{{- $keycloakHost := index .Values._cluster "keycloak-host" | default (printf "keycloak.%s" $host) }} {{- $oidcEnabled := index .Values._cluster "oidc-enabled" }} {{- $oidcInsecureSkipVerify := index .Values._cluster "oidc-insecure-skip-verify" }} {{- $keycloakInternalUrl := index .Values._cluster "keycloak-internal-url" | default "" }} @@ -70,18 +71,18 @@ spec: - --upstream=http://linstor-gui.{{ .Release.Namespace }}.svc:80 - --http-address=0.0.0.0:8000 - --redirect-url=https://linstor-gui.{{ $host }}/oauth2/callback - - --oidc-issuer-url=https://keycloak.{{ $host }}/realms/{{ $realm }} + - --oidc-issuer-url=https://{{ $keycloakHost }}/realms/{{ $realm }} {{- if $keycloakInternalUrl }} - --skip-oidc-discovery - - --login-url=https://keycloak.{{ $host }}/realms/{{ $realm }}/protocol/openid-connect/auth + - --login-url=https://{{ $keycloakHost }}/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} + - --backend-logout-url=https://{{ $keycloakHost }}/realms/{{ $realm }}/protocol/openid-connect/logout?id_token_hint={id_token} {{- end }} - - --whitelist-domain=keycloak.{{ $host }} + - --whitelist-domain={{ $keycloakHost }} - --email-domain=* - --pass-access-token=true - --pass-authorization-header=true diff --git a/packages/system/linstor-gui/tests/ingress_auth_test.yaml b/packages/system/linstor-gui/tests/ingress_auth_test.yaml index e714e4917..1e2c9b80a 100644 --- a/packages/system/linstor-gui/tests/ingress_auth_test.yaml +++ b/packages/system/linstor-gui/tests/ingress_auth_test.yaml @@ -345,3 +345,24 @@ tests: - 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} + + # Same issuer host as the dashboard: _cluster.keycloak-host. + - it: builds the browser-facing OIDC URLs on a custom keycloak-host + template: templates/gatekeeper.yaml + set: + _cluster: + root-host: example.org + oidc-enabled: "true" + keycloak-host: auth.example.org + keycloak-internal-url: http://keycloak-http.cozy-keycloak.svc:8080/realms/cozy + expose-services: "linstor-gui" + asserts: + - contains: + path: spec.template.spec.containers[0].args + content: --oidc-issuer-url=https://auth.example.org/realms/cozy + - contains: + path: spec.template.spec.containers[0].args + content: --login-url=https://auth.example.org/realms/cozy/protocol/openid-connect/auth + - contains: + path: spec.template.spec.containers[0].args + content: --whitelist-domain=auth.example.org diff --git a/packages/system/monitoring/templates/_helpers.tpl b/packages/system/monitoring/templates/_helpers.tpl index 389720959..d4cbaa3e0 100644 --- a/packages/system/monitoring/templates/_helpers.tpl +++ b/packages/system/monitoring/templates/_helpers.tpl @@ -45,7 +45,8 @@ {{- define "monitoring.oidc.systemIssuerURL" -}} {{- $host := index .Values._cluster "root-host" -}} {{- $realm := index .Values._cluster "oidc-realm-name" | default "cozy" | toString -}} -{{- printf "https://keycloak.%s/realms/%s" $host $realm -}} +{{- $keycloakHost := index .Values._cluster "keycloak-host" | default (printf "keycloak.%s" $host) -}} +{{- printf "https://%s/realms/%s" $keycloakHost $realm -}} {{- end -}} {{- /* diff --git a/packages/system/monitoring/tests/oidc_test.yaml b/packages/system/monitoring/tests/oidc_test.yaml index 917ce85a4..c7740731b 100644 --- a/packages/system/monitoring/tests/oidc_test.yaml +++ b/packages/system/monitoring/tests/oidc_test.yaml @@ -800,3 +800,25 @@ tests: - equal: path: spec.config["auth.generic_oauth"].auth_url value: "https://keycloak.example.org/realms/acme/protocol/openid-connect/auth" + + # Grafana calls the token and userinfo endpoints on the public host, so all + # three URLs must move with _cluster.keycloak-host. + - it: mode=System grafana.yaml builds the OIDC URLs on a custom keycloak-host + template: templates/grafana/grafana.yaml + set: + oidc.mode: System + _cluster.oidc-enabled: "true" + _cluster.keycloak-host: auth.example.org + oidc.users: + - email: alice@example.com + role: Admin + asserts: + - equal: + path: spec.config["auth.generic_oauth"].auth_url + value: "https://auth.example.org/realms/cozy/protocol/openid-connect/auth" + - equal: + path: spec.config["auth.generic_oauth"].token_url + value: "https://auth.example.org/realms/cozy/protocol/openid-connect/token" + - equal: + path: spec.config["auth.generic_oauth"].api_url + value: "https://auth.example.org/realms/cozy/protocol/openid-connect/userinfo"