ADR-0131: Keycloak grant broker and schema v39 account identities
Foundation / checks (checks) (push) Successful in 42m13s
Foundation / checks (deploy) (push) Successful in 4m5s
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (network) (push) Successful in 19m18s
Foundation / checks (postgres) (push) Successful in 44m19s
Foundation / checks (tsan) (push) Successful in 9m48s
Foundation / checks (client) (push) Failing after 12m6s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

evo-grant-broker verifies realm aeternalis access tokens offline (RS256 against
the pinned JWKS; audience aevolution-grants, client aevolution-game, verified
email) and issues 10-minute session grants:
- POST /grant routes to the region holding the account's life, or the default.
- POST /logout makes every issued grant stale and expires current control.
- GET /healthz.

Schema v39 maps (issuer, subject) to stable accounts with strictly increasing
serials. The binary ships in the server image. The DEV manifests are in
deploy/kubernetes/grant-broker-dev and are not yet enabled in region/dev; that
waits for the image promotion and the out-of-band Secrets and Ingress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-10-15 10:20:27 -04:00
co-authored by Claude Opus 5.5
parent 817a830a8a
commit 68880ba3b4
29 changed files with 1967 additions and 18 deletions
+88
View File
@@ -0,0 +1,88 @@
-- ADR-0131: Keycloak identities map to stable game accounts, and the grant broker takes
-- strictly increasing grant serials for them. Logout raises the world's accepted serial
-- past every issued grant and expires the account's current control, so outstanding
-- grants are stale and a live session closes at its next renewal.
CREATE TABLE evolution.account_identities (
issuer text NOT NULL CHECK (octet_length(issuer) BETWEEN 1 AND 256),
subject text NOT NULL CHECK (octet_length(subject) BETWEEN 1 AND 255),
account_id uuid NOT NULL UNIQUE
CHECK (account_id <> '00000000-0000-0000-0000-000000000000'),
last_serial evolution.u64 NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (issuer, subject)
);
REVOKE ALL ON evolution.account_identities FROM PUBLIC;
-- The account for (issuer, subject), created on first use, its next grant serial (above
-- both the last one issued and the newest one the world accepted) and the region holding
-- its active life, if any.
CREATE FUNCTION evolution.issue_account_grant(p_world evolution.u64, p_issuer text,
p_subject text)
RETURNS TABLE(grant_account uuid, grant_serial evolution.u64, home_region evolution.u64)
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE
r evolution.account_identities%ROWTYPE;
accepted numeric;
next_serial numeric;
BEGIN
IF p_world IS NULL OR p_world = 0 OR p_issuer IS NULL OR p_subject IS NULL
OR octet_length(p_issuer) NOT BETWEEN 1 AND 256
OR octet_length(p_subject) NOT BETWEEN 1 AND 255 THEN
RAISE EXCEPTION 'invalid account identity' USING ERRCODE = '22023';
END IF;
INSERT INTO evolution.account_identities(issuer, subject, account_id)
VALUES (p_issuer, p_subject, gen_random_uuid())
ON CONFLICT (issuer, subject) DO NOTHING;
SELECT * INTO r FROM evolution.account_identities x
WHERE x.issuer = p_issuer AND x.subject = p_subject FOR UPDATE;
SELECT c.serial INTO accepted FROM evolution.account_controls c
WHERE c.world_id = p_world AND c.account_id = r.account_id;
next_serial := greatest(r.last_serial, coalesce(accepted, 0)) + 1;
IF next_serial > 18446744073709551615 THEN
RAISE EXCEPTION 'grant serials exhausted' USING ERRCODE = 'EVL05';
END IF;
UPDATE evolution.account_identities x SET last_serial = next_serial
WHERE x.issuer = p_issuer AND x.subject = p_subject;
grant_account := r.account_id;
grant_serial := next_serial;
SELECT l.region_id INTO home_region FROM evolution.life_reservations l
WHERE l.world_id = p_world AND l.account_id = r.account_id AND l.rejection = 0
AND l.ended_commit IS NULL;
RETURN NEXT;
END $$;
-- Logout: every grant issued so far becomes stale and the current control expires now.
-- False when the identity never had an account (nothing to revoke).
CREATE FUNCTION evolution.revoke_account_grants(p_world evolution.u64, p_issuer text,
p_subject text)
RETURNS boolean
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$
DECLARE r evolution.account_identities%ROWTYPE;
BEGIN
IF p_world IS NULL OR p_world = 0 OR p_issuer IS NULL OR p_subject IS NULL THEN
RAISE EXCEPTION 'invalid account identity' USING ERRCODE = '22023';
END IF;
SELECT * INTO r FROM evolution.account_identities x
WHERE x.issuer = p_issuer AND x.subject = p_subject FOR UPDATE;
IF NOT FOUND THEN
RETURN false;
END IF;
INSERT INTO evolution.account_controls AS a (world_id, account_id, serial, generation)
VALUES (p_world, r.account_id, greatest(r.last_serial, 1), 1)
ON CONFLICT (world_id, account_id) DO UPDATE
SET serial = greatest(a.serial, EXCLUDED.serial),
expires_at = CASE WHEN a.region_id IS NULL THEN NULL
ELSE least(a.expires_at, clock_timestamp()) END;
RETURN true;
END $$;
REVOKE ALL ON FUNCTION
evolution.issue_account_grant(evolution.u64, text, text),
evolution.revoke_account_grants(evolution.u64, text, text)
FROM PUBLIC;
GRANT EXECUTE ON FUNCTION
evolution.issue_account_grant(evolution.u64, text, text),
evolution.revoke_account_grants(evolution.u64, text, text)
TO :"runtime_role";
INSERT INTO evolution.schema_migrations(version, sha256) VALUES (39, :'migration_sha_39');
+26 -4
View File
@@ -114,6 +114,7 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
SELECT :'history_v35' || ',36:' || :'migration_sha_36' AS history_v36 \gset
SELECT :'history_v36' || ',37:' || :'migration_sha_37' AS history_v37 \gset
SELECT :'history_v37' || ',38:' || :'migration_sha_38' AS history_v38 \gset
SELECT :'history_v38' || ',39:' || :'migration_sha_39' AS history_v39 \gset
SELECT :'history' = :'history_v22' AS at_v22,
:'history' = :'history_v23' AS at_v23,
:'history' = :'history_v24' AS at_v24,
@@ -130,8 +131,10 @@ SELECT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname='evolution')
:'history' = :'history_v35' AS at_v35,
:'history' = :'history_v36' AS at_v36,
:'history' = :'history_v37' AS at_v37,
:'history' = :'history_v38' AS at_v38 \gset
\if :at_v38
:'history' = :'history_v38' AS at_v38,
:'history' = :'history_v39' AS at_v39 \gset
\if :at_v39
\elif :at_v38
\elif :at_v37
\elif :at_v36
\elif :at_v35
@@ -501,7 +504,8 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
coalesce(max(version), 0) = 35 AS at_v35,
coalesce(max(version), 0) = 36 AS at_v36,
coalesce(max(version), 0) = 37 AS at_v37,
coalesce(max(version), 0) = 38 AS at_v38
coalesce(max(version), 0) = 38 AS at_v38,
coalesce(max(version), 0) = 39 AS at_v39
FROM evolution.schema_migrations \gset
\if :at_v24
\ir 0025_cellular_v2_bundle.sql
@@ -518,6 +522,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v25
\ir 0026_cellular_profile_migration.sql
\ir 0027_cellular_v3_migration.sql
@@ -532,6 +537,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v26
\ir 0027_cellular_v3_migration.sql
\ir 0028_cellular_v3_trace_bounds.sql
@@ -545,6 +551,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v27
\ir 0028_cellular_v3_trace_bounds.sql
\ir 0029_cellular_mutation_reservations.sql
@@ -557,6 +564,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v28
\ir 0029_cellular_mutation_reservations.sql
\ir 0030_cellular_birth_publication_binding.sql
@@ -568,6 +576,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v29
\ir 0030_cellular_birth_publication_binding.sql
\ir 0031_fix_cellular_birth_publication_record.sql
@@ -578,6 +587,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v30
\ir 0031_fix_cellular_birth_publication_record.sql
\ir 0032_fix_cellular_v3_manifest_normalization.sql
@@ -587,6 +597,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v31
\ir 0032_fix_cellular_v3_manifest_normalization.sql
\ir 0033_fix_cellular_action_tick_offset.sql
@@ -595,6 +606,7 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v32
\ir 0033_fix_cellular_action_tick_offset.sql
\ir 0034_cellular_v4_engulf_bundle.sql
@@ -602,29 +614,37 @@ SELECT coalesce(max(version), 0) = 24 AS at_v24,
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v33
\ir 0034_cellular_v4_engulf_bundle.sql
\ir 0035_cellular_mutation_publication.sql
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v34
\ir 0035_cellular_mutation_publication.sql
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v35
\ir 0036_cellular_v5_budding_bundle.sql
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v36
\ir 0037_cellular_life_endings.sql
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v37
\ir 0038_cellular_v6_presence_bundle.sql
\ir 0039_account_identities.sql
\elif :at_v38
\ir 0039_account_identities.sql
\elif :at_v39
\else
\echo 'Unknown or changed migration history after v38'
\echo 'Unknown or changed migration history after v39'
DO $$ BEGIN RAISE EXCEPTION 'unknown or changed migration history'; END $$;
\endif
-- Effective-privilege audit on every run, including no-ops (ADR-0060). Attributes
@@ -679,6 +699,8 @@ BEGIN
('evolution.resolve_cellular_mutation_commit(evolution.u64,evolution.u64,uuid,bytea,bytea,bytea,bytea,bytea,bytea)'::regprocedure::oid),
('evolution.load_cellular_mutation_publications(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
('evolution.end_cellular_lives(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,bytea)'::regprocedure::oid),
('evolution.issue_account_grant(evolution.u64,text,text)'::regprocedure::oid),
('evolution.revoke_account_grants(evolution.u64,text,text)'::regprocedure::oid),
('evolution.load_cellular_publication_actions(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
('evolution.load_cellular_publication_births(evolution.u64,evolution.u64,uuid,evolution.u64,uuid)'::regprocedure::oid),
('evolution.publish_cellular_profile_migration(bytea)'::regprocedure::oid),