feat(keycloak): own realm state passphrase and scheduled realm drift check #932
Inga granskare
Etiketter
Inga etiketter
area/backups
area/ci
area/control-panel
area/identity
area/infra
area/observability
area/payments
area/security
area/storage
area/web
blocked
needs-info
needs-triage
priority
high
priority
low
priority
medium
ready-for-implementation
type
bug
type
chore
type
docs
type
epic
type
feature
type
task
wontfix
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Inget förfallodatum satt.
Beroenden
Inga beroenden satta
Referens
bitborg/bitborg-infra!932
Läser in…
Hänvisa till i nytt ärende
Ingen beskrivning angiven.
Ta bort grenen "feat/realm-state-passphrase"
Borttagning av en gren är permanent. Även om den borttagna grenen kan fortsätta existera en kort tid innan den faktiskt tas bort, kan det INTE ångras i de flesta fall. Vill du fortsätta?
This PR carries the whole #576 stack. #934 merged into #933's branch and #933 into this one, so the
three changes land together:
Both realm workspaces (staging, prod) were re-encrypted from this branch's first two commits, which
still had the fallback, before merge. Both now read
key_provider.pbkdf2.realm. The vault keys arein #935.
Own passphrase for the realm state
Each environment's realm state (
opentofu/keycloak) is encrypted with its ownvault_keycloak_opentofu_state_passphraseinstead of the main root's passphrase. The drift checkneeds the realm state on the services host, and the host must never hold the main root's key.
encryption.tofu: only thepbkdf2.realmkey andaes_gcm.realmmethod, for state and plans.variables.tofu:realm_state_passphrase(sensitive, at least 16 characters).state_encryption_passphraseis gone from the realm root.scripts/keycloak-tofu-env.py: exportsTF_VAR_realm_state_passphrasefrom the environment's ownvault and reads no other vault. The self-test checks that it never exports the main root's
passphrase.
vault.example.ymland the runbook ("Keycloak realm", "Realm state passphrase") document the key.The main root keeps
vault_opentofu_state_encryption_passphraseunchanged.Proof on a throwaway root with the fallback config, then the final config: the fallback read the old
state (
No changes, exit 0),apply -refresh-onlyrewrote it tokey_provider.pbkdf2.realm, andthe final config read it with only the realm passphrase. The old config could no longer read it.
Realm drift check
A timer on the prod services host runs
tofu plan -detailed-exitcodeforopentofu/keycloakevery6 hours and writes the exit code to the textfile collector. Three alerts read it:
KeycloakRealmDriftKeycloakRealmDriftCheckFailedKeycloakRealmDriftCheckStaleThe plan covers everything the root declares. A changed flow, execution, required action, client,
scope or permission raises
KeycloakRealmDrift, and so does a merged realm change that was neverapplied.
bitborg-id, in the OpenTofu container(
ghcr.io/opentofu/opentofu, through the registry mirror). The admin API listens only on thathost's loopback.
opentofu. A read-only client cannot plan this root: the provider refreshesevery
keycloak_openid_clientwithGET /clients/<id>/client-secret, which needs manage rights.bitborg-idowns Keycloak's database, which already holds every client secret, so the job givesno account anything it lacked.
--tags keycloakcopies the.tofufiles, lock file,env/prod.tfvarsand theencrypted workspace state from the checkout that runs Ansible, and templates the
TF_VAR_*inputsinto a 0600 env file. The self-test fails if the role's inputs differ from the script's or include
the main root's passphrase.
passphrase or the
opentofusecret is unset.HEADon the host, and each run'sjournal line names it. The role warns when that checkout is not at
origin/main.at most twice an hour.
Rejected: CI (needs the admin secret and an SSH path into prod), the monitoring VM (a second host
with the admin secret), a read-only client (cannot refresh clients),
plan -refresh-only(missesmerged changes never applied).
Proof against a local Keycloak 26.8.2: clean realm gives status 0; an OTP execution set to
DISABLEDgives status 2; Keycloak stopped gives status 1 and keeps the last success timestamp.Operator steps after merge
eval "$(./scripts/keycloak-tofu-env.py <env>)", thentofu plan -detailed-exitcodemustexit 0.
bitborg/opentofu:1.13.9exists in the registry.--tags keycloakon prod.bitborg-keycloak-realm-drift.serviceasbitborg-id. Expect status 0.Apply the realm to undo it, then run
--tags keycloakagain.After every realm apply, run
--tags keycloakfrom the same checkout, or the check plans stalestate and fires.
Checks
tofu fmt -check,tofu validate,keycloak-tofu-env.py --self-test, ruff, shellcheck,test-alert-rules.py,check-alert-rules.py,check-metric-names.py,check-renovate-annotations.py,lint-rendered-templates.py,check-podman-run-log-driver.py,ansible-lint,site.yml --syntax-check, prettier, markdownlint.Closes #576
5d16a7ebe215e7584df6feat(keycloak): give the realm state its own passphrasetill feat(keycloak): own realm state passphrase and scheduled realm drift check