feat(keycloak): scheduled realm drift check with alerts #934
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!934
Läser in…
Hänvisa till i nytt ärende
Ingen beskrivning angiven.
Ta bort grenen "feat/realm-drift-check"
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?
feat(keycloak): scheduled realm drift check with alerts
Stack:
feat/realm-state-passphrase, thenfeat/realm-state-passphrase-cleanup, then this PR(3 of 3). Merge and deploy in that order. This PR needs the realm state on its own passphrase,
so the main root's passphrase never reaches the services host.
What
A timer on the prod services host runs
tofu plan -detailed-exitcodeforopentofu/keycloakevery 6 hours and writes the exit code to the textfile collector. Three alerts read it:
KeycloakRealmDriftKeycloakRealmDriftCheckFailedKeycloakRealmDriftCheckStaleThe plan covers everything the root declares, so a changed flow, execution, required action,
client, scope or permission raises
KeycloakRealmDrift. So does a merged realm change that wasnever applied.
Design
bitborg-id, in the OpenTofu container (ghcr.io/opentofu/opentofu,pulled through the registry mirror). The admin API listens only on that host's loopback.
opentofu. A read-only client cannot plan this root. The provider refreshesevery
keycloak_openid_clientwithGET /clients/<id>/client-secret, and Keycloak allows thatonly with manage rights on the client. A client allowed to read the
opentofusecret can act asopentofu, so a narrower client would not be narrower.bitborg-idowns Keycloak's database,which already holds every client secret, so the job gives no account anything it lacked.
--tags keycloakcopies the.tofufiles, lock file,env/prod.tfvarsand theworkspace state (still encrypted) from the checkout that runs Ansible, and templates the
TF_VAR_*inputs into a 0600 env file.scripts/keycloak-tofu-env.py --self-testnow fails if therole and the script read different vault keys.
vault_keycloak_opentofu_state_passphrase, the realm state'sown key, and never the main root's.
keycloak-tofu-env.py --self-testfails if the role's inputsdiffer from the script's or include the main root's passphrase.
secret on that host for results nobody reads. The role also skips the check while the realm
passphrase or the
opentofusecret is unset.HEADon the host, and each run'sjournal line names it. The role warns, without failing, when that checkout is not at
origin/main.after 5 minutes, at most twice an hour, so a race with a Keycloak restart or a boot clears itself.
Rejected:
plan -refresh-only: needs no secrets, but misses merged changes that were never applied.Operator steps
The first two PRs' steps come first: the realm vault keys, and every workspace rewritten with its
own passphrase.
bitborg/opentofu:1.13.9exists in the registry.--tags keycloakon prod.bitborg-keycloak-realm-drift.serviceasbitborg-id. Expect status 0.and
KeycloakRealmDrift. Apply the realm to undo it.After every realm apply, run
--tags keycloakfrom the same checkout, or the check plans the oldstate and code and fires.
Proof against a local Keycloak 26.8.2
The root was applied to a throwaway Keycloak (stock theme in place of
bitborg), then the renderedscript ran with the host layout and the encrypted state:
No changes, status 0;DISABLEDover the admin API:~ requirement = "DISABLED" -> "ALTERNATIVE",status 2, unit exit 0;
podman: no state gives status 1 and skips the plan; a failed run followed by asuccessful retry leaves status 0 with a fresh timestamp.
Checks
scripts/test-alert-rules.py,scripts/check-alert-rules.pyscripts/check-metric-names.py,scripts/check-renovate-annotations.py,scripts/lint-rendered-templates.py,scripts/check-podman-run-log-driver.pyscripts/keycloak-tofu-env.py --self-testansible-lint,ansible-playbook site.yml --syntax-check, shellcheck, ruffCloses #576