Keycloak role, database and Caddy vhost #570

Stängd
öppnade 2026-10-08 14:52:37 +00:00 av supernaut · 1 kommentar
Ägare

Run Keycloak 26 on staging behind Caddy with its own database role.

  • Role keycloak: Quadlet unit, image tag with Renovate annotation, MemoryHigh=896M, MemoryMax=1G, DropCapability=ALL, env from vault, start --optimized.
  • Env: KC_DB=postgres, KC_HOSTNAME, KC_HTTP_ENABLED=true (internal network only), KC_PROXY_HEADERS=xforwarded, health and metrics on the management port (not published).
  • Container health check on /health/ready (closes the class of infra#475).
  • Bootstrap: temporary admin from vault, create a master-realm service client for OpenTofu with kc.sh bootstrap-admin service, then remove the temporary admin. Document break-glass in the runbook.
  • Postgres: database keycloak and role keycloak, password in vault, own pg_hba entry, no access to other databases.
  • Caddy vhost for the auth host: reverse_proxy keycloak:8080. Block /admin* and /metrics. Keep the auth and auth_credentials rate-limit zones, retargeted to /realms/bitborg/login-actions/* and the token endpoint.

Done when: Keycloak is ready on staging, /admin returns blocked from outside, and the database role cannot reach other databases.

Depends on: #568
Depends on: bitborg/bitborg-keycloak#1
Epic: bitborg/bitborg-docs#127

Run Keycloak 26 on staging behind Caddy with its own database role. - [ ] Role `keycloak`: Quadlet unit, image tag with Renovate annotation, `MemoryHigh=896M`, `MemoryMax=1G`, `DropCapability=ALL`, env from vault, `start --optimized`. - [ ] Env: `KC_DB=postgres`, `KC_HOSTNAME`, `KC_HTTP_ENABLED=true` (internal network only), `KC_PROXY_HEADERS=xforwarded`, health and metrics on the management port (not published). - [ ] Container health check on `/health/ready` (closes the class of infra#475). - [ ] Bootstrap: temporary admin from vault, create a master-realm service client for OpenTofu with `kc.sh bootstrap-admin service`, then remove the temporary admin. Document break-glass in the runbook. - [ ] Postgres: database `keycloak` and role `keycloak`, password in vault, own `pg_hba` entry, no access to other databases. - [ ] Caddy vhost for the auth host: `reverse_proxy keycloak:8080`. Block `/admin*` and `/metrics`. Keep the `auth` and `auth_credentials` rate-limit zones, retargeted to `/realms/bitborg/login-actions/*` and the token endpoint. Done when: Keycloak is ready on staging, `/admin` returns blocked from outside, and the database role cannot reach other databases. Depends on: #568 Depends on: bitborg/bitborg-keycloak#1 Epic: bitborg/bitborg-docs#127
Upphovsperson
Ägare

Applied to staging from main (#599, #600) and verified. Prod is unchanged: keycloak_enabled is false there.

  • Apply --tags keycloak,caddy: failed=0, and the health gate passes with keycloak. A second apply gives changed=0.
  • Apply --tags base: failed=0. The sshd forward assertion passes.
  • Image and runtime:
    • bitborg-keycloak:26.8.0-1 runs with ReadonlyRootfs=true.
    • Heap max is about 718 MiB. Memory is 521 MiB current and 626 MiB peak, against MemoryHigh 896M and MemoryMax 1G.
  • Ports: only 127.0.0.1:18080 listens. Port 9000 is not published.
  • Admin API over loopback: a client_credentials token for bootstrap-opentofu returns 200, so sslRequired=external accepts the rootless port forward.
    • Keycloak sees the forwarded source as 10.89.0.16. That address is outside KC_PROXY_TRUSTED_ADDRESSES, so X-Forwarded-For sent through the tunnel is ignored.
  • SSH forward from the operator machine:
    • -L …:127.0.0.1:18080 returns 200.
    • -L …:127.0.0.1:5432 is refused with administratively prohibited.
  • Edge (kc.stg.bitborg.se):
    • /admin/, /realms/master/, /metrics, /health/ready and / get Caddy's empty 404.
    • /realms/bitborg/ is proxied. Keycloak returns JSON 404 until the realm exists (#571).

Still open, carried to #571:

  • replace the bootstrap client and empty the vault value;
  • the account console framing test (X-Frame-Options);
  • the realm well-known check in the health gate.

The keycloak_credentials zone stays off (caddy_ratelimit_auth_credentials_enabled defaults to false) until login traffic has been measured.

Applied to staging from `main` (#599, #600) and verified. Prod is unchanged: `keycloak_enabled` is false there. - Apply `--tags keycloak,caddy`: `failed=0`, and the health gate passes with `keycloak`. A second apply gives `changed=0`. - Apply `--tags base`: `failed=0`. The sshd forward assertion passes. - **Image and runtime:** - `bitborg-keycloak:26.8.0-1` runs with `ReadonlyRootfs=true`. - Heap max is about 718 MiB. Memory is 521 MiB current and 626 MiB peak, against `MemoryHigh` 896M and `MemoryMax` 1G. - **Ports:** only `127.0.0.1:18080` listens. Port 9000 is not published. - **Admin API over loopback:** a `client_credentials` token for `bootstrap-opentofu` returns `200`, so `sslRequired=external` accepts the rootless port forward. - Keycloak sees the forwarded source as `10.89.0.16`. That address is outside `KC_PROXY_TRUSTED_ADDRESSES`, so `X-Forwarded-For` sent through the tunnel is ignored. - **SSH forward from the operator machine:** - `-L …:127.0.0.1:18080` returns `200`. - `-L …:127.0.0.1:5432` is refused with `administratively prohibited`. - **Edge (`kc.stg.bitborg.se`):** - `/admin/`, `/realms/master/`, `/metrics`, `/health/ready` and `/` get Caddy's empty 404. - `/realms/bitborg/` is proxied. Keycloak returns JSON 404 until the realm exists (#571). Still open, carried to #571: - replace the bootstrap client and empty the vault value; - the account console framing test (`X-Frame-Options`); - the realm well-known check in the health gate. The `keycloak_credentials` zone stays off (`caddy_ratelimit_auth_credentials_enabled` defaults to false) until login traffic has been measured.
Logga in för att delta i denna konversation.
Ingen milstolpe
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#570
Ingen beskrivning angiven.