Keycloak¶
Keycloak provides identity and single sign-on for the Skatzi platform.
At a glance¶
| URL | https://keycloak.prod.skatzi.com |
| Upstream version | 26.8.2 (Quay) |
| Deployed image | harbor.prod.skatzi.com/skatzi/keycloak-kodexet:<tag> |
| Operator | Keycloak Operator (k8s.keycloak.org/v2beta1; v2alpha1 is deprecated but still served with the same schema) |
| Namespace | keycloak |
| Database | CNPG Cluster keycloak-db (PostgreSQL 15), backed up to the backup store |
| Realms | skatzi (platform users and admins) · emiliero |
Repository layout¶
components/keycloak/
├── Dockerfile # Bakes the kodexet theme into Keycloak
├── base/
│ ├── keycloak-instance.yaml # The Keycloak CR consumed by the operator
│ ├── skatzi-realm.yaml # Disaster-recovery snapshot — NOT live config
│ ├── emiliero-realm.yaml # Disaster-recovery snapshot — NOT live config
│ ├── httproute.yaml # Gateway API route for keycloak.prod.skatzi.com
│ └── ...
├── database/ # CNPG Cluster keycloak-db, daily backup, ObjectStore
├── backup/ # Upgrade gate keycloak-preupgrade, weekly restore drill
├── rollback/ # Post-upgrade smoke check and rollback by restore
├── theme/kodexet/ # Custom login theme (CSS-only override)
└── secrets/ # ExternalSecret definitions
Image composition¶
The deployed image is a thin wrapper around the upstream Keycloak image — it only adds the kodexet login theme:
FROM quay.io/keycloak/keycloak:26.8.2
COPY theme/kodexet /opt/keycloak/themes/kodexet
RUN /opt/keycloak/bin/kc.sh build
The kc.sh build step pre-compiles the theme so startup is fast. Because build-time options are baked into the image, the Keycloak CR sets startOptimized: false — that forces kc.sh build to re-run on pod boot whenever any build-time option (db, health-enabled, metrics-enabled, …) differs from the image. Without that flag the pod crash-loops after every option change.
Realm configuration — important¶
The KeycloakRealmImport CRDs in components/keycloak/base/*-realm.yaml are disaster-recovery snapshots only. The Keycloak Operator does not reconcile them against a running realm — it only applies them the first time the realm is created. Removing an import does not delete an already imported realm; that requires an admin-console action.
Live realm changes are made in the admin console, or for skatzi through the Admin REST API with ops-agent-realm-admin (see Admin REST API for agents). Either way, mirror every change into base/*-realm.yaml by hand: the YAML files are the target state, so that a rebuild from scratch reproduces the configuration.
See the upgrade guide for the full workflow for changing the image, the theme, the Keycloak version, or realm settings.
Admin REST API for agents¶
The operator changes the realm skatzi through the Admin REST API with the client ops-agent-realm-admin. That client has only realm-management → realm-admin in skatzi, so it cannot reach master or emiliero, nor /admin/serverinfo. It uses the client-credentials grant against https://keycloak.prod.skatzi.com/realms/skatzi. Its credentials are in OpenBao hetzner-mgmt/keycloak/ops-agent-realm-admin and in the Secret keycloak/ops-agent-realm-admin (keys client_id, client_secret). Read them with kubectl into a variable, and never print them.
Since Keycloak 26.8.2 the Admin REST API checks the token's audience: it accepts only a token with the kc-admin-api client scope. The upgrade's migration creates that scope and assigns it only to admin-cli and security-admin-console, so the other clients that call the API have it as a default client scope: skatzi/ops-agent-realm-admin, skatzi/taskbase-service and emiliero/emiliero-admin (#2631). A new client that calls the Admin REST API needs it too, or it gets 401. The DR snapshots keep it (#3361): skatzi-realm.yaml lists kc-admin-api in clientScopes as exported and in those clients' defaultClientScopes. emiliero-realm.yaml lists no clientScopes, so on a rebuild Keycloak creates the scope with its built-in ones, and the file lists it for emiliero-admin. admin-cli and security-admin-console are not in either file and get it from Keycloak.
Taskbase stops when taskbase-service loses Admin REST API access. Taskbase reads every caller's groups through the Admin REST API with taskbase-service (Taskbase authentication). When that call fails, Taskbase rejects every authenticated request, from people and agents alike: the API logs auth: identity resolution failed: keycloak get user: status 401 and MCP calls answer 500, while /api/health stays 200. An agent then cannot call Taskbase at all, so a drained merge run cannot resume_organization until access is back. After access is restored, Taskbase keeps using its cached service token until it expires, up to the realm's 300 s access-token lifespan (accessTokenLifespan in skatzi-realm.yaml). On 2026-11-18 the last 401 came 30 s after the fix (#1260 upgrade evidence).
The master bootstrap admin Secret can be stale. The operator creates the Secret keycloak/keycloak-initial-admin and creates the bootstrap user temp-admin from it only when master has no admin user. It never changes the password of a user that already exists. The Secret was recreated on 2026-07-02, but temp-admin dates from 2026-01-03, so on 2026-11-18 the login with the Secret's password failed with Invalid user credentials [invalid_grant] (incident report, PR #1537). ops-agent-realm-admin reaches only skatzi, so without a working master admin no identity can change master or emiliero, or fix an Admin REST API client that has lost access. A plan that needs the master login must test it read-only before the change that needs it, for example before merging. Restoring a working master admin is #3366. When no admin login works, Assign a default client scope without an admin login is the recovery step.
When you mirror a change into skatzi-realm.yaml, keep every flow in authenticationFlows and every entry in authenticatorConfig, the built-in ones included. A realm import that lists flows is not expected to add Keycloak's built-in flows, so leaving them out could rebuild the realm without its browser flow. Export them with POST /admin/realms/skatzi/partial-export; the export masks client secrets.
Keep every client scope in clientScopes too, the built-in ones included, with the realm's defaultDefaultClientScopes, defaultOptionalClientScopes and the scopeMappings entry of offline_access. A realm import that lists clientScopes creates none of Keycloak's built-in scopes (profile, email, roles, web-origins, acr, basic, service_account, the SAML role_list, …): in Keycloak 26.8.1, RealmManager.isCreateDefaultClientScopes creates them only when clientScopes is absent. The only custom scope is groups (an oidc-group-membership-mapper, claim groups, full.path false), a realm default scope (#2854).
Every client in skatzi-realm.yaml lists its defaultClientScopes and optionalClientScopes as they are live. A client without the lists gets the realm's default scopes when it is created, so it would gain groups on a rebuild even where it has no groups scope live. A client that is older than that default carries its own groups mapper instead. Copy a new client's lists from the export. Inside components/keycloak/base, write a Keycloak placeholder such as ${rolesScopeConsentText} as $${rolesScopeConsentText}, so that Flux's strict substitution keeps it as it is (see Flux). Clients and groups that are live but left out of the YAML are named in a comment in it, with the task that decides them.
Webshops in the realm skatzi¶
The central webshop admin and the shops' agents log in through skatzi, as decided in eloro/webshop-platform docs/design/15-central-admin.md §5 (#2828):
| What | Content |
|---|---|
shop-<name> (per shop: shop-demo, and shop-eloro since eloro #3441) |
Client without flows that owns the client roles owner, staff and agent. It is the token audience: Audience Resolve adds it to aud for a token that has one of its roles |
shop-<name>-agent |
Confidential client, service account only. Its service account has shop-<name>'s role agent. Full scope is off, and its role scope covers shop-<name>'s roles only. Access token 5 minutes, no refresh token. Secret in OpenBao webshop/shop-<name>/agent |
webshop-admin |
The admin at admin.eloro.dk: confidential, authorization code with PKCE S256, back-channel logout. Full scope is off, and its role scope covers owner and staff of every shop-<name>, never agent. Access token 5 minutes, client session idle 30 minutes and at most 8 hours. Secret in OpenBao webshop/webshop-admin/oidc |
webshop-owners, webshop-staff |
Groups with owner or staff on every shop-<name> |
shop-demo-e2e-owner, shop-demo-e2e-staff |
Demo-only test identities for the staging gate's admin journeys (owner decision 2026-11-12, eloro #2747, #2886). Confidential, service account only, like shop-<name>-agent. Each service account has only shop-demo's role owner or staff, and nothing on any other shop-* client. Full scope is off, and the role scope covers shop-demo's roles only. Access token 5 minutes, no refresh token. Secrets in OpenBao webshop/shop-demo/e2e-owner and webshop/shop-demo/e2e-staff (client_id, client_secret), and as the eloro org Actions secrets SHOP_DEMO_E2E_OWNER_CLIENT_ID/_SECRET and SHOP_DEMO_E2E_STAFF_CLIENT_ID/_SECRET |
MFA is not required for webshop-admin (owner decision 2026-11-12, #2870). The owner prefers the fast single sign-on login. Revisit it when a second person joins webshop-owners or webshop-staff, or when a shop goes live with real payments. The client has no browser flow override, so it uses the realm's browser flow: the password, then OTP or WebAuthn only for a person who has set one up in the account console (https://keycloak.prod.skatzi.com/realms/skatzi/account → Signing in). The client maps acr with {"password":1,"mfa":2} and has no minimum or default acr. Every login, a single sign-on included, gets acr password, even when the admin asks for mfa, so the admin's OIDC_ACR_VALUES is password. A token for a member of webshop-owners still carries resource_access["shop-<name>"].roles owner. The client policy webshop-admin (profile webshop-admin-mfa, selected by the client attribute webshop.client=admin) still refuses any update that turns off PKCE S256 or the confidential client, or that turns on full scope. The profile keeps its name from #2828; it never selected the flow.
To require MFA again, bind the flow webshop-admin browser, which is kept in the realm, as the client's browser flow override: password at level 1, then OTP or WebAuthn at level 2 on every login (loa-max-age 0). Also set the client's minimum and default acr to mfa, and set the admin's OIDC_ACR_VALUES to mfa. That flow never sets a second factor up: a person without OTP or WebAuthn gets "We are sorry…" (LOGIN_ERROR invalid_user_credentials in the events), so every member enrolls first. Keycloak 26.8 has no client-policy executor that selects a flow, so the flow is bound by the override.
The e2e clients exist on shop-demo only. They hold owner and staff without a person behind them, which the owner accepted for the demo shop alone. Never give these roles to a test client on a real shop, and do not copy the e2e clients when adding one. If a real shop ever needs end-to-end tests of its admin, that is a new owner decision.
A new shop adds shop-<name> with its three roles and shop-<name>-agent. It also adds its roles to webshop-admin's scope and to both groups, both live and in skatzi-realm.yaml.
The realm saves login and credential events for 90 days, and admin events without representations.
Database¶
Keycloak's database is the CloudNativePG Cluster keycloak-db (components/keycloak/database, Flux Kustomization keycloak-database). Keycloak connects to its keycloak-db-rw Service with the credentials in keycloak-db-secret (OpenBao hetzner-mgmt/keycloak/database), and the same secret sets the database owner's password.
- Backups: WAL archiving and a daily base backup at 02:20 UTC to
s3://skatzi-backups/hetzner-mgmt/cnpg/keycloak/, kept 30 days, with point-in-time restore. The weekly drillkeycloak-restore-drill(Sundays 09:00 UTC) restores the latest backup incnpg-drill, starts the production image on it and checks every realm's discovery endpoint, includingskatziandemiliero. See the CloudNativePG page. - Upgrades: a new image is applied only after the gate
keycloak-preupgradehas backed up the database with Keycloak stopped and restored it; a failed post-upgrade smoke check restores that backup and pins the old image. See Keycloak backups, upgrade gate and rollback. - PostgreSQL 15: the major version of the StatefulSet it replaced. Keycloak 26.4 supports PostgreSQL 13 to 17, so the operator's default image (18) is not used.
- Owner name: CNPG requires the Cluster's
ownerto equal theusernamein its secret, and takes the import source's user only as a literal. Both arekeycloak, the username in OpenBaohetzner-mgmt/keycloak/database. Changing that username means changingcomponents/keycloak/database/postgres-cluster.yamltoo.
Move from the StatefulSet (task #1379)¶
Until task #1379, the database was a plain postgres:15 StatefulSet postgresql-db (Service postgres-db, PVC keycloak-database-pvc on Hetzner volume keycloak-database-volume) without backups. keycloak-db was created from it by a CNPG microservice import: CNPG runs pg_dump against postgres-db and pg_restore into the new Cluster, once, when the Cluster is created. Flux orders the move: keycloak dependsOn keycloak-database, so the Keycloak CR switches to keycloak-db-rw, and postgresql-db scales to 0, only after the import has finished and the Cluster is ready.
The import reads a snapshot taken when it starts. Changes Keycloak makes after that (sessions, admin changes) are not copied, so the move is done in a planned window with Keycloak stopped:
- Announce a short Keycloak downtime: every SSO login (Grafana, Gitea, Taskbase, OpenBao, Kubernetes OIDC) fails while it lasts, and the Keycloak probe alerts.
-
Stop Keycloak, leaving the old database running:
-
Merge the pull request, run
flux --context admin@mgmt-cluster reconcile kustomization flux-system --with-source(it creates thekeycloak-databaseKustomization), and wait for the import:kubectl --context admin@mgmt-cluster -n keycloak get cluster keycloak-dbuntil it reportsCluster in healthy state. flux --context admin@mgmt-cluster resume kustomization keycloak. Flux applies the CR again (one instance, hostkeycloak-db-rw) and scalespostgresql-dbto 0.- Check the realms load:
https://keycloak.prod.skatzi.com/realms/<realm>/.well-known/openid-configurationforskatziandemiliero; then log in to Grafana, Gitea and Taskbase through Keycloak. - Check the first backup:
kubectl --context admin@mgmt-cluster -n keycloak get backups.postgresql.cnpg.ioshows acompletedbackup fromkeycloak-db-daily(it runs once at creation), then run the drill once:kubectl --context admin@mgmt-cluster -n cnpg-drill create job --from=cronjob/keycloak-restore-drill keycloak-drill-manual.
Without step 2 the move still works, but whatever Keycloak writes during the import is lost.
After the move (task #1393): the StatefulSet postgresql-db, its Service and the import section of postgres-cluster.yaml are removed; a re-created keycloak-db starts empty and is filled by a restore from the backup store, not by an import. The old database's PVC keycloak-database-pvc, its PersistentVolume and the Hetzner volume keycloak-database-volume were removed on 2026-11-01 (task #1903); Keycloak's data is only in keycloak-db and its backups.
Related docs¶
- Keycloak API management — scripted config changes via the Admin REST API