Files
aevolution/docs/adr/0131-keycloak-login-and-grant-broker.md
T
dingoandClaude Opus 5.5 40426777a0
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 3m47s
Foundation / image (push) Successful in 5s
Foundation / postgres-tools (push) Successful in 5s
Foundation / server-image (push) Successful in 5s
ADR-0131: grant broker reachable only through HTTPS 443
The broker NetworkPolicy no longer admits the node subnet. Port 8080 is the
in-cluster backend for ingress-nginx pods only; players use
https://grants.aeternalisgames.com on 443.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 12:19:46 -04:00

5.3 KiB

ADR-0131: Keycloak login and the grant broker

Status

Accepted (2026-10-15). DEV first; stage and World0 need their own realm, issuer key and review.

Context

Region servers accept 160-byte session grants signed with an ECDSA P-256 issuer key (ADR-0091). Grants are single-use per account: each carries a serial that must exceed the newest one the world accepted, and a session can be extended with a newer grant (SessionRefresh). Until now grants were minted offline by evo-session-probe grants with the issuer key on the operator's host, for hand-made accounts. Players need to register, log in and log out, and grants must never be minted on a client.

Decision

Identity

  • Keycloak realm aeternalis at auth.aeternalisgames.com is the identity provider: registration, email verification, password reset and MFA live there. Users sign in with their email.
  • The game client is the public client aevolution-game: authorization code with PKCE S256 in the system browser, redirect http://127.0.0.1:47600/callback (RFC 8252 loopback). Device authorization is enabled for test tools.
  • An audience mapper adds aevolution-grants to access tokens. Access tokens live 5 minutes; refresh tokens rotate and are single-use.

The grant broker (evo-grant-broker)

  • A small HTTP service behind grants.aeternalisgames.com (TLS at the ingress). It holds the issuer private key (Secret aevolution-grant-issuer) and a database login; it never calls Keycloak. It verifies access tokens itself against the realm's public keys, pinned from the realm's JWKS in Git (a Keycloak key rotation is a Git change and a sync).
  • POST /grant with Authorization: Bearer <access token>: the token must be an RS256 access token (typ Bearer) from the realm issuer, for audience aevolution-grants, authorized party aevolution-game, unexpired (30 s leeway), with a subject and a verified email. The broker maps (issuer, subject) to a stable game account, created on first use, so registering is the first login. It takes the next grant serial for that account (above any serial the world has accepted), routes to the region holding the account's active life or else the configured default region, and returns a 10-minute grant with the region's endpoint.
  • POST /logout with the same bearer: the account's accepted serial is raised past every serial issued so far and its current control is expired. Outstanding grants are then stale, and a live session closes at its next control renewal (at most 5 s). Refused tokens get 401 with a JSON reason (invalid_token, token_expired, email_not_verified, token_not_for_this_service); a second grant for the same account within 2 s, 429; an unreachable database or unrouted region, 503.
  • GET /healthz for probes.
  • Responses carry no secret but the grant itself; nothing logs tokens or grants.

Database (schema v39)

  • account_identities(issuer, subject, account_id, last_serial, created_at).
  • issue_account_grant(world, issuer, subject) and revoke_account_grants(world, issuer, subject), SECURITY DEFINER and granted to the runtime group role. The broker logs in with its own login in that group (DEV); a dedicated broker role is a later least-privilege step.

Client contract

  1. Sign in (authorization code + PKCE), keep the refresh token in the OS credential store, never on disk in clear.
  2. POST https://grants.aeternalisgames.com/grant with the access token. The JSON answer is {"grant":"<base64>","account":"<uuid>","region":N,"endpoint":"host:port", "server_name":"name","expires_at":unix_seconds}.
  3. Connect with the grant. About one minute before expires_at, refresh the access token if needed, fetch a new grant and send SessionRefresh.
  4. On logout: POST /logout, then revoke the refresh token at Keycloak's revocation endpoint.

Deployment (DEV)

  • deploy/kubernetes/grant-broker-dev, a component of region/dev so it runs the same pinned server image (/usr/local/bin/evo-grant-broker). One replica, non-root, read-only root filesystem, port 8080, probes on /healthz.
  • Out of band in evolution-dev: Secret aevolution-grant-issuer (key issuer.key, the raw 32-byte dev issuer scalar), Secret aevolution-grant-broker-pg (pg_service.conf with service [evolution], pgpass, ca.crt) and the Ingress for grants.aeternalisgames.com (the Argo project admits no Ingress).
  • Public access is HTTPS on 443 only, terminated by ingress-nginx. Port 8080 is the in-cluster backend: a ClusterIP Service, and a NetworkPolicy that admits only the ingress-nginx pods and nothing on the LAN. Egress goes to PostgreSQL only.
  • The grant lifetime (600 s) must stay within the region's REGION_M10_MAX_GRANT_LIFETIME.

Alternatives

  • Verify Keycloak tokens in every region: rejected; it changes the Hello wire format, puts identity mapping in every region and spreads key rotation.
  • Long-lived grants without refresh: rejected; a stolen grant would stay usable.
  • Our own password store: rejected; Keycloak already provides it properly.

Consequences

Players log in with an email account; the issuer key leaves the operator's host for a Secret; hand-minted grants remain for tests. A new login supersedes the old session (unchanged). Revocation is immediate within one renewal.