feat(keycloak): declare the bitborg realm in opentofu #622

Sammanfogat
supernaut sammanfogade 6 incheckningar från feat/keycloak-realm in i main 2026-10-08 21:20:34 +00:00
Ägare

Declares the whole bitborg Keycloak realm in a new OpenTofu root, opentofu/keycloak/, with its own encrypted state (ADR 0044). It reaches the admin API over the SSH forward from #598. Prod is locked (env/prod.tfvars locked = true), so only staging can be planned.

What

  • Realm: sv default and en, registration off, reset on, usernames immutable, verified email, 24 h SSO session, brute-force detection, 30-day login events, admin events. The user profile has username, email and one displayName attribute emitted as name. firstName and lastName are removed, because accounts created without them cannot sign in.
  • Sign-in: a passkey alone, or a password plus a second factor. A person without a factor enrols TOTP before any session. One browser flow per client denies people without the client's access role: forgejo_users for Forgejo and the portal, grafana_admins for Grafana.
  • Reset: the email link asks for an existing factor (OTP, security key, passkey or recovery code) before a new password is set. A person with no factor is refused and uses the portal's setup link instead.
  • Password grants: refused realm-wide (direct_grant_flow is a deny flow), so the default admin-cli client cannot issue a token for a password alone.
  • Entitlements: flat groups and ent_* realm roles. tier_pro and tier_trial map to ent_runners_shared and ent_runners_priority, as the current nesting does. Tokens carry a groups claim (bare names) and an entitlements claim (effective roles).
  • Service accounts: with fine-grained admin permissions v2:
    • bitborg-web-admin: creates people, writes tier_participant, tier_trial and forgejo_users, maps ent_renovate.
    • bitborg-billing: writes tier_pro, maps the paid roles.
    • bitborg-reconciler: read only.
    • No writer holds a realm-management role.
  • Bootstrap: the first apply authenticates as the temporary bootstrap client and creates the permanent opentofu client. The runbook covers deleting the bootstrap client.
  • Other files:
    • scripts/keycloak-tofu-env.py exports TF_VAR_* from the vault, with a self-test.
    • The health gate checks that the portal client can write each portal group and cannot write tier_pro.
    • CI and lefthook validate the new root.

Verified locally

These ran against bitborg-keycloak:26.8.0-1 with a fresh Postgres:

  • Apply: a from-scratch apply, then a second plan with no changes. The switch from the bootstrap client to the permanent one works.
  • Permissions and claims: 42/42 checks pass, covering each service account's allowed and refused calls and the token claims.
  • Browser flow: 11/11 pass. A password-only user lands on TOTP enrolment, the next sign-in asks for OTP, and the access gate denies a user outside the group.
  • Reset: 9/9 pass. A user with TOTP gets the OTP prompt before the new password, a user with no factor is refused, a passkey-only user gets the passkey prompt, and a new user enrols through the setup link.
  • Password grant: to admin-cli returns 401.
  • lefthook run pre-push passes.

Not verified

  • A real passkey ceremony.
  • Real SMTP.
  • Forgejo and Grafana against the new clients. Staging covers these.

Known gap

keycloak/keycloak 5.9.0 reads a client's secret when it looks the client up, and Keycloak refuses that for realm-management under v2 permissions. The root therefore uses hashicorp/http to look up that client's id. State is encrypted.

Refs #571

Declares the whole `bitborg` Keycloak realm in a new OpenTofu root, `opentofu/keycloak/`, with its own encrypted state (ADR 0044). It reaches the admin API over the SSH forward from #598. Prod is locked (`env/prod.tfvars` `locked = true`), so only staging can be planned. ## What - **Realm:** `sv` default and `en`, registration off, reset on, usernames immutable, verified email, 24 h SSO session, brute-force detection, 30-day login events, admin events. The user profile has `username`, `email` and one `displayName` attribute emitted as `name`. firstName and lastName are removed, because accounts created without them cannot sign in. - **Sign-in:** a passkey alone, or a password plus a second factor. A person without a factor enrols TOTP before any session. One browser flow per client denies people without the client's `access` role: `forgejo_users` for Forgejo and the portal, `grafana_admins` for Grafana. - **Reset:** the email link asks for an existing factor (OTP, security key, passkey or recovery code) before a new password is set. A person with no factor is refused and uses the portal's setup link instead. - **Password grants:** refused realm-wide (`direct_grant_flow` is a deny flow), so the default `admin-cli` client cannot issue a token for a password alone. - **Entitlements:** flat groups and `ent_*` realm roles. `tier_pro` and `tier_trial` map to `ent_runners_shared` and `ent_runners_priority`, as the current nesting does. Tokens carry a `groups` claim (bare names) and an `entitlements` claim (effective roles). - **Service accounts:** with fine-grained admin permissions v2: - `bitborg-web-admin`: creates people, writes `tier_participant`, `tier_trial` and `forgejo_users`, maps `ent_renovate`. - `bitborg-billing`: writes `tier_pro`, maps the paid roles. - `bitborg-reconciler`: read only. - No writer holds a realm-management role. - **Bootstrap:** the first apply authenticates as the temporary bootstrap client and creates the permanent `opentofu` client. The runbook covers deleting the bootstrap client. - **Other files:** - `scripts/keycloak-tofu-env.py` exports `TF_VAR_*` from the vault, with a self-test. - The health gate checks that the portal client can write each portal group and cannot write `tier_pro`. - CI and lefthook validate the new root. ## Verified locally These ran against `bitborg-keycloak:26.8.0-1` with a fresh Postgres: - **Apply:** a from-scratch apply, then a second plan with no changes. The switch from the bootstrap client to the permanent one works. - **Permissions and claims:** 42/42 checks pass, covering each service account's allowed and refused calls and the token claims. - **Browser flow:** 11/11 pass. A password-only user lands on TOTP enrolment, the next sign-in asks for OTP, and the access gate denies a user outside the group. - **Reset:** 9/9 pass. A user with TOTP gets the OTP prompt before the new password, a user with no factor is refused, a passkey-only user gets the passkey prompt, and a new user enrols through the setup link. - **Password grant:** to `admin-cli` returns 401. - `lefthook run pre-push` passes. ## Not verified - A real passkey ceremony. - Real SMTP. - Forgejo and Grafana against the new clients. Staging covers these. ## Known gap `keycloak/keycloak` 5.9.0 reads a client's secret when it looks the client up, and Keycloak refuses that for `realm-management` under v2 permissions. The root therefore uses `hashicorp/http` to look up that client's id. State is encrypted. Refs #571
supernaut lade till 6 incheckningar 2026-10-08 21:17:57 +00:00
New root opentofu/keycloak with its own encrypted state per workspace and the
keycloak/keycloak provider pinned at 5.9.0. It declares the realm, the
passkey-or-password-plus-second-factor browser flows, the email-only reset flow,
required actions, user profile, sign-in clients with group-scoped access, the
groups claim and entitlement roles, the portal, billing and reconciler service
accounts with fine-grained admin permissions v2, SMTP and Swedish overrides.
The permanent opentofu admin client replaces the bootstrap one.

Refs #571
Behind keycloak_enabled and once the web-admin client secret is in the vault,
the gate reads the groups as bitborg-web-admin and asserts it may manage the
membership of every portal group and may not manage tier_pro. It reads the
access map Keycloak returns, so it writes nothing and runs under --check.

Refs #571
Staging has no Grafana of its own, so it needs neither the client nor its
vault secret. grafana_client_enabled defaults to true for prod.

Refs #571
The reset-credentials flow signed a person in from the email link and a new
password alone. It now verifies an existing second factor (OTP, security key
or recovery code) before the new password is set, and never resets it.

The stock direct-grant flow issued tokens for a password alone through the
public admin-cli client, and the account API accepted them. The realm's
direct grant flow is now a single deny step.

Refs #571
parse() cut every value at #, quoted or not. Quoted values are now taken
whole, and # starts a comment only after whitespace.

Refs #571
fix(keycloak): require an existing factor to finish a password reset
Alla kontroller lyckades
ci / ci (pull_request) Successful in 2m5s
0f961f99c4
The reset step was conditional on a configured second factor, so a person
with only a passkey, or with no factor at all, got a new password and a
session from the email link alone. The step is now required and accepts an
OTP, a security key, a passkey or a recovery code. A person with no factor is
refused and gets a setup link from the portal instead.

Refs #571
supernaut schemalade den här ändringsförfrågan för automatisk sammanfogning när alla kontroller lyckas 2026-10-08 21:19:00 +00:00
supernaut sammanfogade incheckning 51c8cbc909 till main 2026-10-08 21:20:34 +00:00
supernaut tog bort grenen feat/keycloak-realm 2026-10-08 21:20:34 +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!622
Ingen beskrivning angiven.