feat(keycloak): run keycloak and its own postgres as bitborg-id behind a flag #709

Sammanfogat
supernaut sammanfogade 4 incheckningar från feat/keycloak-isolation in i main 2026-10-13 08:39:59 +00:00
Ägare

Refs #670

Moves Keycloak to its own rootless user, bitborg-id, with its own PostgreSQL. This is the role change from ADR 0042. A flag gates it: keycloak_isolated, default false. With the default, every template renders byte-identical on prod and staging. No host changes until an environment sets the flag.

Design

  • Own Postgres. keycloak-postgres runs under bitborg-id on the internal bitborg-id-data network. It is not published. It uses the shared Postgres image pin, so one Renovate bump moves both. Superuser postgres with the new vault_keycloak_postgres_password. The keycloak app role owns the keycloak database and has no superuser rights. max_connections 50, MemoryMax 512M. Notify=healthy with a TCP pg_isready, so the start returns only after a first-run initdb.
  • Keycloak under bitborg-id. Same image, ports and secrets. It joins bitborg-id and bitborg-id-data, reads keycloak-postgres, and trusts forwarded headers from the bitborg-id subnet. It publishes 127.0.0.1:18080 and 10.90.0.1:8080/:9000 as before, so Caddy, the portal, billing, vmagent, the reconciler, the health gate and the admin forward do not change.
  • One code path. The role picks the run user, Quadlet dir, networks, Postgres container, superuser and reload handler from the flag. Every task keeps its shape.
  • Order of the move. keycloak-postgres publishes no ports, so it starts beside the old Keycloak. The role creates the role and database and restores the dump. Only then, and only when the Keycloak image is in bitborg-id's store, does it stop and remove the main user's Keycloak unit and config directory. A bad dump path, a failed restore or a failed pull leaves the old Keycloak in place. The old database and Podman secrets stay as the rollback copy. Dropping them is a later change.
  • Restore input. -e keycloak_restore_from=<dump> restores a custom-format dump with pg_restore --no-owner --no-privileges --role=keycloak --single-transaction. Guards:
    • the move fails at once without it;
    • bitborg-id must be able to read it (test -r, also in check mode);
    • a dump passed for a database that already has a realm fails the run, so it is never silently unused;
    • an empty keycloak-postgres without a dump fails, whatever vault_keycloak_bootstrap_client_secret holds. That secret is empty in both vaults today, so the existing bootstrap assert already blocked this. The new guard keeps it blocked if the secret is ever set again.
  • Monitoring and gate. keycloak_os_units becomes keycloak-postgres and keycloak. The main user's unit metrics and health-gate list drop keycloak. The bitborg-id copies (from #687) pick both up. Alert names do not change.
  • Prod guard. The role refuses the flag on prod until the backup jobs take Keycloak from bitborg-id. Until then they would dump the stale shared copy. The backup handoff is the next change.

Runbook

New section "Move Keycloak to bitborg-id": preconditions, a fresh dump, the cutover, verification and rollback. Staging first, then prod.

Proof that prod is unchanged

  • Rendered keycloak.container, both bitborg-unit-metrics.sh copies and the computed lists (keycloak_os_units, monitoring_core_units, health_check_services, health_check_id_services, keycloak_internal_url, keycloak_config_dir) with the prod and staging inventories, at main and at this branch. Identical for both hosts.
  • The default-flag values resolve to the old literals: user gitborg, uid 2000, Postgres postgres as forgejo, handler Reload bitborg user systemd.
  • ansible-playbook site.yml --syntax-check passes for both inventories. ansible-lint passes on the production profile.
  • Staging --check --diff --tags keycloak with the default flag: changed=0, failed=0.

Staging preview (check mode)

--check --diff --tags keycloak,monitoring-agent -e keycloak_isolated=true, with a readable placeholder for keycloak_restore_from (check mode never restores): changed=9, failed=0. In order, it pulls Postgres as bitborg-id, installs the keycloak-pgdata volume and keycloak-postgres units, creates the config dir and health script, and pulls Keycloak. Then it stops and removes the main user's Keycloak unit and config, and installs the new unit with the new networks, DB URL and trusted subnet.

Guards seen on staging: without keycloak_restore_from the run fails before any change. With a path that does not exist yet it fails at the test -r check, also before any change.

Operator steps

  1. Merge. Nothing changes on any host.
  2. Staging: add vault_keycloak_postgres_password, set keycloak_isolated: true in group_vars/staging/vars.yml, then follow the runbook section. Reboot once.
  3. Prod: after the backup handoff lands and staging has soaked, the same steps in a planned window.

Not in this change

  • Backup handoff from bitborg-id (next change). Prod waits for it.
  • Dropping the old keycloak database and role from the shared Postgres.
Refs #670 Moves Keycloak to its own rootless user, `bitborg-id`, with its own PostgreSQL. This is the role change from ADR 0042. A flag gates it: `keycloak_isolated`, default `false`. With the default, every template renders byte-identical on prod and staging. No host changes until an environment sets the flag. ## Design - **Own Postgres.** `keycloak-postgres` runs under `bitborg-id` on the internal `bitborg-id-data` network. It is not published. It uses the shared Postgres image pin, so one Renovate bump moves both. Superuser `postgres` with the new `vault_keycloak_postgres_password`. The `keycloak` app role owns the `keycloak` database and has no superuser rights. `max_connections` 50, `MemoryMax` 512M. `Notify=healthy` with a TCP `pg_isready`, so the start returns only after a first-run initdb. - **Keycloak under bitborg-id.** Same image, ports and secrets. It joins `bitborg-id` and `bitborg-id-data`, reads `keycloak-postgres`, and trusts forwarded headers from the `bitborg-id` subnet. It publishes `127.0.0.1:18080` and `10.90.0.1:8080`/`:9000` as before, so Caddy, the portal, billing, vmagent, the reconciler, the health gate and the admin forward do not change. - **One code path.** The role picks the run user, Quadlet dir, networks, Postgres container, superuser and reload handler from the flag. Every task keeps its shape. - **Order of the move.** `keycloak-postgres` publishes no ports, so it starts beside the old Keycloak. The role creates the role and database and restores the dump. Only then, and only when the Keycloak image is in `bitborg-id`'s store, does it stop and remove the main user's Keycloak unit and config directory. A bad dump path, a failed restore or a failed pull leaves the old Keycloak in place. The old database and Podman secrets stay as the rollback copy. Dropping them is a later change. - **Restore input.** `-e keycloak_restore_from=<dump>` restores a custom-format dump with `pg_restore --no-owner --no-privileges --role=keycloak --single-transaction`. Guards: - the move fails at once without it; - `bitborg-id` must be able to read it (`test -r`, also in check mode); - a dump passed for a database that already has a realm fails the run, so it is never silently unused; - an empty `keycloak-postgres` without a dump fails, whatever `vault_keycloak_bootstrap_client_secret` holds. That secret is empty in both vaults today, so the existing bootstrap assert already blocked this. The new guard keeps it blocked if the secret is ever set again. - **Monitoring and gate.** `keycloak_os_units` becomes `keycloak-postgres` and `keycloak`. The main user's unit metrics and health-gate list drop `keycloak`. The bitborg-id copies (from #687) pick both up. Alert names do not change. - **Prod guard.** The role refuses the flag on prod until the backup jobs take Keycloak from `bitborg-id`. Until then they would dump the stale shared copy. The backup handoff is the next change. ## Runbook New section "Move Keycloak to bitborg-id": preconditions, a fresh dump, the cutover, verification and rollback. Staging first, then prod. ## Proof that prod is unchanged - Rendered `keycloak.container`, both `bitborg-unit-metrics.sh` copies and the computed lists (`keycloak_os_units`, `monitoring_core_units`, `health_check_services`, `health_check_id_services`, `keycloak_internal_url`, `keycloak_config_dir`) with the prod and staging inventories, at `main` and at this branch. Identical for both hosts. - The default-flag values resolve to the old literals: user `gitborg`, uid 2000, Postgres `postgres` as `forgejo`, handler `Reload bitborg user systemd`. - `ansible-playbook site.yml --syntax-check` passes for both inventories. `ansible-lint` passes on the `production` profile. - Staging `--check --diff --tags keycloak` with the default flag: `changed=0`, `failed=0`. ## Staging preview (check mode) `--check --diff --tags keycloak,monitoring-agent -e keycloak_isolated=true`, with a readable placeholder for `keycloak_restore_from` (check mode never restores): `changed=9`, `failed=0`. In order, it pulls Postgres as `bitborg-id`, installs the `keycloak-pgdata` volume and `keycloak-postgres` units, creates the config dir and health script, and pulls Keycloak. Then it stops and removes the main user's Keycloak unit and config, and installs the new unit with the new networks, DB URL and trusted subnet. Guards seen on staging: without `keycloak_restore_from` the run fails before any change. With a path that does not exist yet it fails at the `test -r` check, also before any change. ## Operator steps 1. Merge. Nothing changes on any host. 2. Staging: add `vault_keycloak_postgres_password`, set `keycloak_isolated: true` in `group_vars/staging/vars.yml`, then follow the runbook section. Reboot once. 3. Prod: after the backup handoff lands and staging has soaked, the same steps in a planned window. ## Not in this change - Backup handoff from `bitborg-id` (next change). Prod waits for it. - Dropping the old `keycloak` database and role from the shared Postgres.
supernaut lade till 2 incheckningar 2026-10-13 06:35:30 +00:00
keycloak_isolated (default false) moves Keycloak to the bitborg-id user with a
keycloak-postgres instance on bitborg-id-data. The default renders every
template unchanged, so no host changes until an environment sets the flag.

The flag stops and removes the main user's Keycloak unit, starts
keycloak-postgres, creates the keycloak role and database there, and restores
keycloak_restore_from into it when it has no realm. Unit metrics and the
health gate check both units as bitborg-id. The role refuses the flag on prod
until the backups take Keycloak from bitborg-id.

Runbook: "Move Keycloak to bitborg-id" with the dump, cutover, checks and
rollback.

Refs #670
fix(keycloak): keep the old keycloak serving until the move is safe
En del kontroller misslyckades
ci / ci (pull_request) Failing after 52s
42fc5e8847
The main user's Keycloak now stops only after keycloak-postgres is up, the
dump is restored and the image is present. A bad dump path or a failed pull
leaves it running. keycloak-postgres publishes no ports, so it starts beside
it.

- Check bitborg-id can read keycloak_restore_from, also in check mode.
- Fail when a dump is passed for a database that already has a realm.
- Refuse an empty keycloak-postgres without a dump, whatever the bootstrap
  secret holds.
- Runbook: host steps run in an SSH session with /tmp helpers, the apply runs
  from the control node with the vault env and a log, and a failed move says
  how to retry.

Refs #670
supernaut lade till 1 incheckning 2026-10-13 06:44:46 +00:00
fix(keycloak): hide the restore task's postgres password from apply output
Alla kontroller lyckades
ci / ci (pull_request) Successful in 1m54s
1f0ca7ec06
The secret-output check flags PGPASSWORD in the task environment without no_log.

Refs #670
supernaut lade till 1 incheckning 2026-10-13 08:36:27 +00:00
Merge remote-tracking branch 'origin/main' into feat/keycloak-isolation
Alla kontroller lyckades
ci / ci (pull_request) Successful in 3m0s
9e0517fa4f
# Conflicts:
#	ansible/site.yml
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-13 08:37:04 +00:00
supernaut avbröt automatisk sammanfogning av den här ändringsförfrågan när alla kontroller lyckas 2026-10-13 08:38:03 +00:00
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-13 08:38:47 +00:00
supernaut sammanfogade incheckning 723db69fdb till main 2026-10-13 08:39:59 +00:00
supernaut tog bort grenen feat/keycloak-isolation 2026-10-13 08:39:59 +00:00
Logga in för att delta i denna konversation.
Inga granskare
Ingen milstolpe
Inget projekt
Inga tilldelade
1 deltagare
Notiser
Förfallodatum
Förfallodatumet är ogiltigt eller utanför gränserna. Använd formatet "åååå-mm-dd".

Inget förfallodatum satt.

Beroenden

Inga beroenden satta

Referens
bitborg/bitborg-infra!709
Ingen beskrivning angiven.