From 68880ba3b44f078bffad1bae7a312598b59fe807 Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 15 Oct 2026 10:20:27 -0400 Subject: [PATCH] ADR-0131: Keycloak grant broker and schema v39 account identities 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 --- CMakeLists.txt | 15 +- apps/evo-grant-broker/main.cpp | 179 +++++++++ deploy/docker/Dockerfile | 4 +- .../grant-broker-dev/deployment.yaml | 95 +++++ .../grant-broker-dev/kustomization.yaml | 14 + .../grant-broker-dev/networkpolicy.yaml | 32 ++ .../kubernetes/grant-broker-dev/service.yaml | 16 + .../grant-broker-dev/serviceaccount.yaml | 10 + .../region/dev-session-route/listener.yaml | 2 +- .../0131-keycloak-login-and-grant-broker.md | 101 ++++++ docs/adr/README.md | 1 + docs/engineering/VALIDATION.md | 31 ++ .../persistence/account_identity_store.hpp | 33 ++ include/evo/server/broker/access_token.hpp | 66 ++++ include/evo/server/broker/grant_broker.hpp | 54 +++ include/evo/server/broker/json.hpp | 44 +++ include/evo/server/ops_http.hpp | 11 + migrations/0039_account_identities.sql | 88 +++++ migrations/apply.sql | 30 +- src/persistence/region_lease_store.cpp | 63 +++- src/server/broker/access_token.cpp | 216 +++++++++++ src/server/broker/grant_broker.cpp | 150 ++++++++ src/server/broker/json.cpp | 301 ++++++++++++++++ src/server/ops_http.cpp | 70 +++- tests/grant_broker_tests.cpp | 341 ++++++++++++++++++ tests/postgres_tests.cpp | 3 + tests/region_migration_tests.cpp | 2 +- tools/ci/postgres-check.sh | 10 +- tools/migrate-postgres.sh | 3 + 29 files changed, 1967 insertions(+), 18 deletions(-) create mode 100644 apps/evo-grant-broker/main.cpp create mode 100644 deploy/kubernetes/grant-broker-dev/deployment.yaml create mode 100644 deploy/kubernetes/grant-broker-dev/kustomization.yaml create mode 100644 deploy/kubernetes/grant-broker-dev/networkpolicy.yaml create mode 100644 deploy/kubernetes/grant-broker-dev/service.yaml create mode 100644 deploy/kubernetes/grant-broker-dev/serviceaccount.yaml create mode 100644 docs/adr/0131-keycloak-login-and-grant-broker.md create mode 100644 include/evo/persistence/account_identity_store.hpp create mode 100644 include/evo/server/broker/access_token.hpp create mode 100644 include/evo/server/broker/grant_broker.hpp create mode 100644 include/evo/server/broker/json.hpp create mode 100644 migrations/0039_account_identities.sql create mode 100644 src/server/broker/access_token.cpp create mode 100644 src/server/broker/grant_broker.cpp create mode 100644 src/server/broker/json.cpp create mode 100644 tests/grant_broker_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 252a46c..eef81f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -276,6 +276,15 @@ if(EVO_BUILD_POSTGRES) src/server/session/postgres_action_ledger.cpp src/server/session/postgres_handoff_ledger.cpp) target_link_libraries(evo_control_ledger_postgres PUBLIC evo::postgres evo_network_protocol) evo_owned_target(evo_control_ledger_postgres) + # ADR-0131 Keycloak grant broker (schema v39). + add_library(evo_grant_broker STATIC src/server/broker/json.cpp + src/server/broker/access_token.cpp src/server/broker/grant_broker.cpp) + target_link_libraries(evo_grant_broker PUBLIC evo::region_host evo_network_tls + PRIVATE OpenSSL::Crypto) + evo_owned_target(evo_grant_broker) + add_executable(evo-grant-broker apps/evo-grant-broker/main.cpp) + target_link_libraries(evo-grant-broker PRIVATE evo_grant_broker) + evo_owned_target(evo-grant-broker) endif() # M7.9 clone and history audit tool (ADR-0067). add_executable(evo-world-clone apps/evo-world-clone/main.cpp) @@ -297,8 +306,10 @@ if(EVO_BUILD_POSTGRES) target_link_libraries(evo-postgres-tests PRIVATE evo::region_host Threads::Threads) if(EVO_BUILD_NETWORK) # M9.7: real evo-server sessions over TLS (ADR-0083). - target_sources(evo-postgres-tests PRIVATE tests/region_session_tests.cpp) - target_link_libraries(evo-postgres-tests PRIVATE evo_network_session evo_control_ledger_postgres) + target_sources(evo-postgres-tests PRIVATE tests/region_session_tests.cpp + tests/grant_broker_tests.cpp) + target_link_libraries(evo-postgres-tests PRIVATE evo_network_session evo_control_ledger_postgres + evo_grant_broker OpenSSL::Crypto) target_compile_definitions(evo-postgres-tests PRIVATE EVO_POSTGRES_SESSIONS=1) endif() if(EVO_BUILD_QUIC) diff --git a/apps/evo-grant-broker/main.cpp b/apps/evo-grant-broker/main.cpp new file mode 100644 index 0000000..ad225c5 --- /dev/null +++ b/apps/evo-grant-broker/main.cpp @@ -0,0 +1,179 @@ +// ADR-0131 grant broker: verifies Keycloak access tokens and issues short-lived session +// grants (POST /grant), revokes an account's grants (POST /logout), GET /healthz. +// +// evo-grant-broker --listen ADDRESS:PORT --world N --frame N --default-region N +// --route REGION=HOST:PORT=TLS_NAME [--route ...] --issuer-key FILE +// --jwks JSON --oidc-issuer URL --audience NAME --client NAME +// --pg-passfile F --pg-service F [--grant-lifetime 60..3600] +// +// The issuer key file is the raw 32-byte P-256 scalar (the dev issuer.key format). No +// token, grant or key is ever printed. +#include "evo/persistence/postgres.hpp" +#include "evo/server/broker/grant_broker.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { +using namespace evo; +using namespace std::chrono_literals; + +volatile std::sig_atomic_t stop_requested = 0; +void on_stop(int) { + stop_requested = 1; +} + +struct Options { + std::string listen, issuer_key, jwks, oidc_issuer, audience, client; + server::broker::BrokerConfig broker; + persistence::PgOptions pg; +}; + +std::optional number(std::string_view text) { + std::uint64_t value{}; + const auto [end, error] = std::from_chars(text.data(), text.data() + text.size(), value); + if (error != std::errc{} || end != text.data() + text.size()) + return std::nullopt; + return value; +} + +std::optional route(std::string_view text) { + const auto first = text.find('='); + const auto second = first == std::string_view::npos ? first : text.find('=', first + 1); + if (second == std::string_view::npos) + return std::nullopt; + const auto region = number(text.substr(0, first)); + const auto endpoint = text.substr(first + 1, second - first - 1); + const auto name = text.substr(second + 1); + if (!region || *region == 0 || endpoint.empty() || endpoint.size() > 64 || name.empty() || + name.size() > 64) + return std::nullopt; + return server::broker::BrokerRoute{*region, std::string(endpoint), std::string(name)}; +} + +std::optional parse(int argc, char** argv) { + Options o; + std::string pg_service, pg_passfile; + for (int i = 1; i + 1 < argc; i += 2) { + const std::string_view flag = argv[i]; + const std::string_view value = argv[i + 1]; + const auto n = number(value); + if (flag == "--listen") + o.listen = value; + else if (flag == "--world" && n && *n > 0) + o.broker.world = *n; + else if (flag == "--frame" && n && *n > 0) + o.broker.frame = *n; + else if (flag == "--default-region" && n && *n > 0) + o.broker.default_region = *n; + else if (flag == "--route") { + const auto parsed = route(value); + if (!parsed) + return std::nullopt; + o.broker.routes.push_back(*parsed); + } else if (flag == "--issuer-key") + o.issuer_key = value; + else if (flag == "--jwks") + o.jwks = value; + else if (flag == "--oidc-issuer") + o.oidc_issuer = value; + else if (flag == "--audience") + o.audience = value; + else if (flag == "--client") + o.client = value; + else if (flag == "--pg-service") + pg_service = value; + else if (flag == "--pg-passfile") + pg_passfile = value; + else if (flag == "--grant-lifetime" && n && *n >= 60 && *n <= 3600) + o.broker.grant_lifetime_seconds = *n; + else + return std::nullopt; + } + if (argc % 2 != 1 || o.listen.empty() || o.broker.world == 0 || o.broker.frame == 0 || + o.broker.default_region == 0 || o.broker.routes.empty() || o.issuer_key.empty() || + o.jwks.empty() || o.oidc_issuer.empty() || o.audience.empty() || o.client.empty() || + pg_service.empty() || pg_passfile.empty()) + return std::nullopt; + auto service = persistence::read_pg_service(pg_service, "evolution"); + if (!service) + return std::nullopt; + o.pg = *service; + o.pg.password_file = pg_passfile; + return o; +} + +std::uint64_t wall_seconds() { + return static_cast(std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()) + .count()); +} +} // namespace + +int main(int argc, char** argv) { + const auto options = parse(argc, argv); + if (!options) { + std::fputs("usage: evo-grant-broker (see apps/evo-grant-broker/main.cpp)\n", stderr); + return 2; + } + std::signal(SIGTERM, on_stop); + std::signal(SIGINT, on_stop); + std::signal(SIGPIPE, SIG_IGN); + + std::ifstream key_file{options->issuer_key, std::ios::binary}; + const std::vector scalar{std::istreambuf_iterator{key_file}, + std::istreambuf_iterator{}}; + auto key = network::GrantSigningKey::import(network::DeploymentEnvironment::dev, 1, scalar); + if (!key) { + std::puts("error issuer-key"); + return 2; + } + auto verifier = server::broker::TokenVerifier::from_jwks( + options->jwks, {options->oidc_issuer, options->audience, options->client, 30}); + if (!verifier) { + std::puts("error jwks"); + return 2; + } + auto listening = server::OpsServer::listen(options->listen); + if (!listening) { + std::puts("error listen"); + return 2; + } + const auto pg = options->pg; + server::broker::GrantBroker broker{options->broker, std::move(*verifier), std::move(*key), + [pg]() -> std::unique_ptr { + auto db = persistence::Postgres::connect(pg, 3s); + return db ? std::move(*db) : nullptr; + }, + [](std::uint64_t& id) { + std::array bytes{}; + if (!network::random_bytes(bytes)) + return false; + std::memcpy(&id, bytes.data(), sizeof id); + return true; + }}; + std::printf("grant broker listening %u world %llu region %llu\n", + static_cast((*listening)->port()), + static_cast(options->broker.world), + static_cast(options->broker.default_region)); + std::fflush(stdout); + while (stop_requested == 0) { + (*listening)->serve_api([&](const server::ApiRequest& request) { + return broker.handle(request, wall_seconds()); + }); + std::this_thread::sleep_for(5ms); + } + std::puts("grant broker stopped"); + return 0; +} diff --git a/deploy/docker/Dockerfile b/deploy/docker/Dockerfile index 903f20d..d69aceb 100644 --- a/deploy/docker/Dockerfile +++ b/deploy/docker/Dockerfile @@ -139,7 +139,7 @@ RUN cmake --preset release -B build/postgres -DCMAKE_CXX_COMPILER=g++-14 \ -DCMAKE_C_COMPILER=gcc-14 -DEVO_BUILD_NETWORK=ON \ -DEVO_BUILD_QUIC=ON -DEVO_BUILD_POSTGRES=ON \ -DPostgreSQL_ROOT=/source/.cache/postgres/install \ - && cmake --build build/postgres --target evo-server evo-session-probe evo-world-clone --parallel "${PG_BUILD_JOBS}" + && cmake --build build/postgres --target evo-server evo-session-probe evo-world-clone evo-grant-broker --parallel "${PG_BUILD_JOBS}" FROM postgres-build AS postgres-test-build ARG PG_BUILD_JOBS=6 @@ -187,6 +187,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends libstdc++ COPY --from=postgres-build /source/build/postgres/evo-server /usr/local/bin/evo-server # M8.3 operations tool: check-store qualification, clone and history audit. COPY --from=postgres-build /source/build/postgres/evo-world-clone /usr/local/bin/evo-world-clone +# ADR-0131 grant broker (Keycloak tokens to session grants); its own Deployment. +COPY --from=postgres-build /source/build/postgres/evo-grant-broker /usr/local/bin/evo-grant-broker COPY --from=postgres-build /source/.cache/postgres/install/lib/libpq.so* /opt/postgres/lib/ COPY --from=postgres-build /source/build/postgres/msquic/bin/Release/libmsquic.so* /opt/msquic/lib/ COPY --from=postgres-build /source/.cache/postgres/postgresql-18.6/COPYRIGHT /usr/share/doc/evo-postgres/COPYRIGHT diff --git a/deploy/kubernetes/grant-broker-dev/deployment.yaml b/deploy/kubernetes/grant-broker-dev/deployment.yaml new file mode 100644 index 0000000..db5c8d5 --- /dev/null +++ b/deploy/kubernetes/grant-broker-dev/deployment.yaml @@ -0,0 +1,95 @@ +# ADR-0131 grant broker. One replica: serials are ordered by the database, but the +# per-account rate limit lives in the process. The realm's public signing key is +# pinned below (BROKER_JWKS); a Keycloak key rotation needs a Git update here. +apiVersion: apps/v1 +kind: Deployment +metadata: + name: evo-grant-broker + annotations: + argocd.argoproj.io/sync-wave: "2" # After migration (schema v39). + labels: + app.kubernetes.io/name: evo-grant-broker + app.kubernetes.io/component: grant-broker +spec: + replicas: 1 + revisionHistoryLimit: 3 + progressDeadlineSeconds: 180 + selector: + matchLabels: + app.kubernetes.io/name: evo-grant-broker + template: + metadata: + labels: + app.kubernetes.io/name: evo-grant-broker + app.kubernetes.io/component: grant-broker + app.kubernetes.io/part-of: aevolution + spec: + serviceAccountName: aevolution-grant-broker + automountServiceAccountToken: false + terminationGracePeriodSeconds: 10 + securityContext: + runAsNonRoot: true + runAsUser: 10001 + runAsGroup: 10001 + fsGroup: 10001 + seccompProfile: {type: RuntimeDefault} + containers: + - name: evo-grant-broker + image: aevolution-server + command: ["/bin/bash", "-c"] + args: + - | + set -eu + routes=() + for route in $BROKER_ROUTES; do routes+=(--route "$route"); done + exec /usr/local/bin/evo-grant-broker --listen 0.0.0.0:8080 --world "$BROKER_WORLD" --frame "$BROKER_FRAME" --default-region "$BROKER_DEFAULT_REGION" "${routes[@]}" --issuer-key /run/grant-issuer/issuer.key --jwks "$BROKER_JWKS" --oidc-issuer "$BROKER_OIDC_ISSUER" --audience aevolution-grants --client aevolution-game --grant-lifetime "$BROKER_GRANT_LIFETIME" --pg-service /run/postgres/pg_service.conf --pg-passfile /run/postgres/pgpass + env: + - {name: BROKER_WORLD, value: "100"} + - {name: BROKER_FRAME, value: "9"} + # New accounts start here; an account with a live life goes to its region. + - {name: BROKER_DEFAULT_REGION, value: "4"} + # Space-separated REGION=HOST:PORT=TLS_NAME, every region a grant may name. + - {name: BROKER_ROUTES, value: "4=void.aeternalisgames.com:7400=void.aeternalisgames.com"} + # Must not exceed the region's REGION_M10_MAX_GRANT_LIFETIME. + - {name: BROKER_GRANT_LIFETIME, value: "600"} + - {name: BROKER_OIDC_ISSUER, value: "https://auth.aeternalisgames.com/realms/aeternalis"} + # Public RS256 signing key(s) of realm aeternalis, from its JWKS endpoint. + - name: BROKER_JWKS + value: '{"keys":[{"alg":"RS256","e":"AQAB","kid":"NGQG4QzQuBCIHVEomUPy-OoWOTVQIuyf3RXqeNbP8JI","kty":"RSA","n":"vvOpu1xxdyH_mB8LkJG6cwc6UVmmAj6mwQi1oEwrpJDLerBI2OZr2kpgMAUJ3-hMIkz5tRtSjHieMFteVrpDDQaCViIPCIvJTL3T7ujTGFNReSE6ho7h0DB_kybFeEWVpLqm6SSXWFYls5TKo6_t8Hw4G3njiRW9pX_NxyaBHqLkrCi63EyIGHOkdgUczg2d4pM4JTt-q25bcFSlYz8bhD3WEORQmbp-bU7aPPluCqCEAZRrbzf8Iy7svNemP5daDdlWQ3LaNy-ixC2HRjiPZdqK5BgAE800BigI-l1dkyJ4X172TLIPVPLW1WKlvdm3kHOpBXwACYiy8boSNDW7gQ","use":"sig"}]}' + ports: + - {name: http, containerPort: 8080, protocol: TCP} + readinessProbe: + httpGet: {path: /healthz, port: http} + periodSeconds: 5 + timeoutSeconds: 1 + livenessProbe: + httpGet: {path: /healthz, port: http} + initialDelaySeconds: 5 + periodSeconds: 10 + timeoutSeconds: 2 + failureThreshold: 3 + resources: + requests: {cpu: 10m, memory: 16Mi} + limits: {memory: 64Mi} + securityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + capabilities: {drop: [ALL]} + volumeMounts: + - {name: connection, mountPath: /run/postgres, readOnly: true} + - {name: issuer, mountPath: /run/grant-issuer, readOnly: true} + volumes: + - name: connection + secret: + secretName: aevolution-grant-broker-pg + defaultMode: 0440 + items: + - {key: pg_service.conf, path: pg_service.conf} + - {key: pgpass, path: pgpass} + - {key: ca.crt, path: ca.crt} + - name: issuer + secret: + secretName: aevolution-grant-issuer + defaultMode: 0440 + items: + - {key: issuer.key, path: issuer.key} diff --git a/deploy/kubernetes/grant-broker-dev/kustomization.yaml b/deploy/kubernetes/grant-broker-dev/kustomization.yaml new file mode 100644 index 0000000..c6e7e03 --- /dev/null +++ b/deploy/kubernetes/grant-broker-dev/kustomization.yaml @@ -0,0 +1,14 @@ +# ADR-0131 grant broker for evolution-dev ONLY: verifies Keycloak access tokens +# (realm aeternalis) and issues 10-minute session grants for world 100 frame 9. +# Public route: https://grants.aeternalisgames.com -> Ingress (created out of band: +# the aevolution-dev Argo project admits no Ingress) -> Service evo-grant-broker:8080. +# Out-of-band Secrets in evolution-dev: aevolution-grant-issuer (issuer.key, the raw +# 32-byte dev issuer scalar) and aevolution-grant-broker-pg (pg_service.conf, pgpass, +# ca.crt for a login in the runtime group). +apiVersion: kustomize.config.k8s.io/v1alpha1 +kind: Component +resources: + - serviceaccount.yaml + - deployment.yaml + - service.yaml + - networkpolicy.yaml diff --git a/deploy/kubernetes/grant-broker-dev/networkpolicy.yaml b/deploy/kubernetes/grant-broker-dev/networkpolicy.yaml new file mode 100644 index 0000000..f78b43b --- /dev/null +++ b/deploy/kubernetes/grant-broker-dev/networkpolicy.yaml @@ -0,0 +1,32 @@ +# Broker traffic on top of the namespace default-deny: HTTP 8080 from the ingress +# controller only (RKE2's bundled ingress-nginx in kube-system; it may run on the host +# network, so the node subnet is admitted too), and PostgreSQL on pg-forge-01 only. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: evo-grant-broker + annotations: + argocd.argoproj.io/sync-wave: "-1" +spec: + podSelector: + matchLabels: + app.kubernetes.io/name: evo-grant-broker + policyTypes: [Ingress, Egress] + ingress: + - from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + app.kubernetes.io/name: rke2-ingress-nginx + - ipBlock: + cidr: 172.10.1.0/24 + ports: + - {protocol: TCP, port: 8080} + egress: + - to: + - ipBlock: + cidr: 172.10.1.127/32 + ports: + - {protocol: TCP, port: 5432} diff --git a/deploy/kubernetes/grant-broker-dev/service.yaml b/deploy/kubernetes/grant-broker-dev/service.yaml new file mode 100644 index 0000000..64ca17a --- /dev/null +++ b/deploy/kubernetes/grant-broker-dev/service.yaml @@ -0,0 +1,16 @@ +# Cluster-internal; the Ingress for grants.aeternalisgames.com terminates TLS and +# forwards here. +apiVersion: v1 +kind: Service +metadata: + name: evo-grant-broker + annotations: + argocd.argoproj.io/sync-wave: "2" + labels: + app.kubernetes.io/name: evo-grant-broker +spec: + type: ClusterIP + selector: + app.kubernetes.io/name: evo-grant-broker + ports: + - {name: http, port: 8080, targetPort: http, protocol: TCP} diff --git a/deploy/kubernetes/grant-broker-dev/serviceaccount.yaml b/deploy/kubernetes/grant-broker-dev/serviceaccount.yaml new file mode 100644 index 0000000..c9fa43a --- /dev/null +++ b/deploy/kubernetes/grant-broker-dev/serviceaccount.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: aevolution-grant-broker + annotations: + argocd.argoproj.io/sync-wave: "-1" +automountServiceAccountToken: false +# Create this pull-only Secret separately in evolution-dev; never commit credentials. +imagePullSecrets: + - name: harbor-pull diff --git a/deploy/kubernetes/region/dev-session-route/listener.yaml b/deploy/kubernetes/region/dev-session-route/listener.yaml index e9ffea4..765d1a3 100644 --- a/deploy/kubernetes/region/dev-session-route/listener.yaml +++ b/deploy/kubernetes/region/dev-session-route/listener.yaml @@ -12,7 +12,7 @@ spec: env: - {name: REGION_SESSION, value: "dev"} - {name: REGION_SESSION_LISTEN, value: "0.0.0.0:7400"} - - {name: REGION_SESSION_WINDOW, value: "dev-20261015T114156-9412:1792078916"} + - {name: REGION_SESSION_WINDOW, value: "dev-20261015T142027-62240:1792084827"} - {name: REGION_M10_MAX_GRANT_LIFETIME, value: "14400"} ports: # QUIC (ADR-0090): UDP only; nothing listens on TCP 7400. diff --git a/docs/adr/0131-keycloak-login-and-grant-broker.md b/docs/adr/0131-keycloak-login-and-grant-broker.md new file mode 100644 index 0000000..3f48b1f --- /dev/null +++ b/docs/adr/0131-keycloak-login-and-grant-broker.md @@ -0,0 +1,101 @@ +# 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 `: 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":"","account":"","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). +- Its NetworkPolicy admits TCP 8080 from the ingress controller and the node subnet, + and egress 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. diff --git a/docs/adr/README.md b/docs/adr/README.md index 209e3e0..a38dff3 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -31,6 +31,7 @@ - [ADR-0128: Cellular offline freeze, linger and resume](0128-cellular-offline-freeze-and-resume.md) - M11.7: a frozen step intent (set for owned lives without a session) freezes biology; owner format 6 / rules 5 track presence: lingering for 200 ticks (10 s, trial) perceivable and vulnerable, then suspended out of the world without aging; an unfrozen intent resumes in place or at the nearest clear spot within three extents; bundle v6, schema v38. - [ADR-0129: Cellular readable play signals](0129-cellular-readable-play-signals.md) - M11.8 server half: v10 diagnostics report engulf and bud results in two reserved bytes, and v10 type 27 tells the owner its life ended and why (starved or engulfed); presentation stays with the maintainer. - [ADR-0130: Cellular playtest telemetry boundary](0130-cellular-playtest-telemetry-boundary.md) - M11.9: playtest telemetry is an offline, read-only `evo-world-clone summary` of a region head with account-free aggregates only (population, death causes, lifespans, buds, mutations, variety); per-player questions stay in consented notes. +- [ADR-0131: Keycloak login and the grant broker](0131-keycloak-login-and-grant-broker.md) - players sign in to Keycloak realm `aeternalis` (PKCE, public client `aevolution-game`); `evo-grant-broker` verifies the RS256 access token, maps it to a game account (schema v39), issues 10-minute grants with increasing serials, and revokes on logout by raising the accepted serial and expiring control. - [ADR-0058: Region durability contract](0058-region-durability-contract.md) - accepted M7.1 full checkpoint batches and explicit state closure; storage implementation follows. diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index 6a2c027..fa0f3f9 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -7541,3 +7541,34 @@ authentication (Windows credential manager rejected; WSL had no configured credential). A subsequent Windows retry successfully pushed through `1519287` to `origin/main`. No credentials were changed or printed by this task. Remote CI has not yet been checked for this client commit; the verified ZIP is local. + +## 2026-10-15 — ADR-0131 grant broker and schema v39 (CI pending) + +`evo-grant-broker` verifies Keycloak RS256 access tokens offline against the realm's +pinned JWKS and issues 10-minute session grants. Schema v39 maps (issuer, subject) to +stable accounts and takes strictly increasing serials. Logout raises the accepted +serial and expires current control. `OpsServer::serve_api` adds bodiless GET/POST +with an Authorization header. + +Tests (`grant_broker_tests.cpp`, PostgreSQL suite): +- the bounded JSON reader: escapes, surrogates, depth 16, size and integer bounds; +- the token verifier with an OpenSSL-generated realm key: audience, authorized + party, type, issuer, expiry and leeway, nbf, unverified email, missing subject, + unknown kid, `none`/HS256, spliced and foreign signatures, oversize; +- the broker against real PostgreSQL. No database call happens before a token + verifies. Grants verify with the issuer's public key; serials 1, 2, then 3 after + logout. Rate limit 429. Logout fences the live control's renewal, makes serial 2 + stale and lets serial 3 claim. Logout without control still raises the serial. + Unknown identities are not revoked, an offline database gives 503, and the runtime + role cannot read identities directly. + +Executed locally: WSL g++-14 build of the broker and `evo-postgres-tests`, and the +PostgreSQL C++ suite (schema v39). g++-14 `-Werror -Wshadow` and clang-tidy-19 on the +broker, ops HTTP, store and test sources. clang-format 19. `deploy-policy-check.py` +(0 failures, including the rendered `region/dev` with the broker component), +selftest and ops helpers. + +Limits: no live Keycloak token was exercised yet. DEV deployment waits for: +- the image promotion; +- the out-of-band Secrets `aevolution-grant-issuer` and `aevolution-grant-broker-pg`; +- the Ingress for `grants.aeternalisgames.com`. diff --git a/include/evo/persistence/account_identity_store.hpp b/include/evo/persistence/account_identity_store.hpp new file mode 100644 index 0000000..972aae1 --- /dev/null +++ b/include/evo/persistence/account_identity_store.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "evo/persistence/postgres.hpp" +#include "evo/persistence/region_lease.hpp" + +#include +#include +#include +#include +#include + +// Schema v39, ADR-0131: Keycloak identities to game accounts, and grant serials. +namespace evo::persistence { +enum class AccountIdentityError : std::uint8_t { + invalid, // Arguments refused. + store, // Other failure; nothing changed. + unknown // May have executed: reconnect before relying on the serial. +}; +struct IssuedGrantSerial { + Uuid account{}; + std::uint64_t serial{}; + std::optional home_region; // Region holding the active life, if any. + friend bool operator==(const IssuedGrantSerial&, const IssuedGrantSerial&) = default; +}; +// The account for (issuer, subject), created on first use, with its next grant serial. +[[nodiscard]] std::expected +issue_account_grant(Postgres& db, std::uint64_t world, std::string_view issuer, + std::string_view subject, std::chrono::milliseconds timeout); +// Logout: every grant issued so far becomes stale and current control expires. False +// when the identity never had an account. +[[nodiscard]] std::expected +revoke_account_grants(Postgres& db, std::uint64_t world, std::string_view issuer, + std::string_view subject, std::chrono::milliseconds timeout); +} // namespace evo::persistence diff --git a/include/evo/server/broker/access_token.hpp b/include/evo/server/broker/access_token.hpp new file mode 100644 index 0000000..d3d303b --- /dev/null +++ b/include/evo/server/broker/access_token.hpp @@ -0,0 +1,66 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +// ADR-0131: verifies Keycloak access tokens (RS256 JWTs) against the realm's public +// keys, offline. Only what the broker needs is checked and returned. +namespace evo::server::broker { +struct AccessTokenRules { + std::string issuer; // The realm's issuer URL, exact. + std::string audience; // Must appear in `aud` (string or array). + std::string authorized_party; // `azp`: the game's public client. + std::uint64_t leeway_seconds{30}; +}; +struct AccessTokenClaims { + std::string issuer, subject; + std::uint64_t expires_at{}; +}; +enum class TokenError : std::uint8_t { + malformed, + unsupported_algorithm, + unknown_key, + bad_signature, + wrong_issuer, + wrong_audience, + wrong_party, + wrong_type, + expired, + not_yet_valid, + unverified_email, + missing_subject +}; +inline constexpr std::size_t max_access_token_bytes = 6144; + +class TokenVerifier { + public: + // RSA signing keys (`use` sig or absent, `alg` RS256 or absent, at least 2048 bits) + // from a JWKS document; other keys are ignored. Fails when none is usable. + [[nodiscard]] static std::expected from_jwks(std::string_view jwks, + AccessTokenRules rules); + [[nodiscard]] std::expected + verify(std::string_view token, std::uint64_t now_seconds) const; + [[nodiscard]] std::size_t keys() const noexcept { + return keys_.size(); + } + + private: + struct Key { + std::string kid; + std::shared_ptr public_key; // EVP_PKEY, freed by its deleter. + }; + TokenVerifier(std::vector keys, AccessTokenRules rules) + : keys_{std::move(keys)}, rules_{std::move(rules)} {} + std::vector keys_; + AccessTokenRules rules_; +}; + +[[nodiscard]] std::optional> base64url_decode(std::string_view text); +[[nodiscard]] std::string base64_encode(std::span bytes); +} // namespace evo::server::broker diff --git a/include/evo/server/broker/grant_broker.hpp b/include/evo/server/broker/grant_broker.hpp new file mode 100644 index 0000000..8c6d3be --- /dev/null +++ b/include/evo/server/broker/grant_broker.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include "evo/network/session_grant.hpp" +#include "evo/persistence/postgres.hpp" +#include "evo/server/broker/access_token.hpp" +#include "evo/server/ops_http.hpp" + +#include +#include +#include +#include +#include +#include +#include + +// ADR-0131: turns a verified Keycloak access token into a short-lived session grant +// for the region holding the account's life, and revokes an account's grants on logout. +namespace evo::server::broker { +struct BrokerRoute { + std::uint64_t region{}; + std::string endpoint, server_name; // "host:port" and the region's TLS name. +}; +struct BrokerConfig { + std::uint64_t world{}, frame{}, default_region{}; + std::vector routes; // Every region a grant may name. + std::uint64_t grant_lifetime_seconds{600}; + std::chrono::seconds min_interval{2}; // Per account, between grants. + std::chrono::milliseconds db_timeout{3000}; +}; +using BrokerConnect = std::function()>; +// Supplies fresh 64-bit grant ids (random); false when randomness is unavailable. +using GrantIdSource = std::function; + +class GrantBroker { + public: + GrantBroker(BrokerConfig config, TokenVerifier verifier, network::GrantSigningKey key, + BrokerConnect connect, GrantIdSource ids); + // GET /healthz, POST /grant and POST /logout. `now_seconds` is wall time. + [[nodiscard]] OpsResponse handle(const ApiRequest& request, std::uint64_t now_seconds); + + private: + [[nodiscard]] OpsResponse issue(const AccessTokenClaims& claims, std::uint64_t now_seconds); + [[nodiscard]] OpsResponse logout(const AccessTokenClaims& claims); + persistence::Postgres* database(); + + BrokerConfig config_; + TokenVerifier verifier_; + network::GrantSigningKey key_; + BrokerConnect connect_; + GrantIdSource ids_; + std::unique_ptr db_; + std::map last_grant_; // Subject to wall seconds; bounded. +}; +} // namespace evo::server::broker diff --git a/include/evo/server/broker/json.hpp b/include/evo/server/broker/json.hpp new file mode 100644 index 0000000..ce837ae --- /dev/null +++ b/include/evo/server/broker/json.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +// ADR-0131: a small, bounded JSON reader for access-token claims and the realm's JWKS. +// It accepts RFC 8259 JSON within the bounds below and nothing else (no comments, no +// trailing commas, no duplicate-key resolution beyond "first wins"). +namespace evo::server::broker { +inline constexpr std::size_t max_json_bytes = std::size_t{16} * 1024; +inline constexpr std::size_t max_json_depth = 16; + +class JsonValue { + public: + enum class Kind : std::uint8_t { null, boolean, number, string, array, object }; + [[nodiscard]] Kind kind() const noexcept { + return kind_; + } + // Typed views; nullopt or nullptr when the value has another kind. + [[nodiscard]] std::optional boolean() const; + [[nodiscard]] std::optional string() const; + // Integers only (no fraction or exponent), within 0..2^63-1. + [[nodiscard]] std::optional integer() const; + [[nodiscard]] const JsonValue* member(std::string_view name) const; + [[nodiscard]] const std::vector* array() const; + + // Whole-document parse; nullopt for anything malformed or out of bounds. + [[nodiscard]] static std::optional parse(std::string_view text); + + private: + friend class JsonParser; + Kind kind_{Kind::null}; + bool boolean_{}; + std::string text_; // String value, or the number's literal. + std::vector items_; + std::vector> members_; +}; +} // namespace evo::server::broker diff --git a/include/evo/server/ops_http.hpp b/include/evo/server/ops_http.hpp index f69a26c..d9f5b76 100644 --- a/include/evo/server/ops_http.hpp +++ b/include/evo/server/ops_http.hpp @@ -22,6 +22,12 @@ struct OpsResponse { }; // Receives the request path (no query handling: probes use exact paths). using OpsHandler = std::function; +// ADR-0131 API mode (the grant broker): GET or POST without a body, the path and the +// Authorization header value (empty when absent). Views are valid during the call only. +struct ApiRequest { + std::string_view method, path, authorization; +}; +using ApiHandler = std::function; enum class OpsError : std::uint8_t { address, socket }; class OpsServer { @@ -31,6 +37,7 @@ class OpsServer { // pressure cannot hold the host loop; the rest wait in the listen backlog. static constexpr std::size_t max_accepts_per_serve = 4; static constexpr std::size_t max_request_bytes = 4096; + static constexpr std::size_t max_api_request_bytes = 8192; // Room for an access token. static constexpr std::size_t max_response_bytes = 16384; static constexpr std::chrono::milliseconds connection_deadline{1000}; @@ -49,6 +56,8 @@ class OpsServer { // expires connections without waiting. Connections beyond the bound are closed // at once; slow or oversized ones are answered or dropped without delaying others. void serve(const OpsHandler& handler); + // The same bounded loop for API requests (max_api_request_bytes). + void serve_api(const ApiHandler& handler); [[nodiscard]] std::uint64_t refused() const noexcept { return refused_; } @@ -65,6 +74,8 @@ class OpsServer { }; OpsServer(int fd, std::uint16_t port) : fd_(fd), port_(port) {} void respond(Connection& c, const OpsHandler& handler); + void respond_api(Connection& c, const ApiHandler& handler); + template void serve_with(std::size_t request_limit, const Answer& answer); int fd_; std::uint16_t port_; std::vector connections_; diff --git a/migrations/0039_account_identities.sql b/migrations/0039_account_identities.sql new file mode 100644 index 0000000..da9be5b --- /dev/null +++ b/migrations/0039_account_identities.sql @@ -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'); diff --git a/migrations/apply.sql b/migrations/apply.sql index 245fdb1..d78dc5c 100644 --- a/migrations/apply.sql +++ b/migrations/apply.sql @@ -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), diff --git a/src/persistence/region_lease_store.cpp b/src/persistence/region_lease_store.cpp index e5ea964..d7b9299 100644 --- a/src/persistence/region_lease_store.cpp +++ b/src/persistence/region_lease_store.cpp @@ -1,5 +1,6 @@ #include "evo/persistence/region_lease_store.hpp" #include "evo/persistence/account_control_store.hpp" +#include "evo/persistence/account_identity_store.hpp" #include "evo/persistence/life_reservation_store.hpp" #include "evo/persistence/probe_store.hpp" #include @@ -1441,4 +1442,64 @@ load_pending_cellular_mutations(Postgres& db, RegionKey key, const LeaseGrant& l } return out; } -} // namespace evo::persistence +namespace { +AccountIdentityError identity_failure(const PgFailure& f) { + const std::string_view state(f.sqlstate.data(), 5); + if (f.kind == PgError::unknown) + return AccountIdentityError::unknown; + if (f.kind == PgError::config || f.kind == PgError::bounds || state == "22023") + return AccountIdentityError::invalid; + return AccountIdentityError::store; +} +} // namespace + +std::expected +issue_account_grant(Postgres& db, std::uint64_t world, std::string_view issuer, + std::string_view subject, milliseconds timeout) { + if (world == 0 || issuer.empty() || issuer.size() > 256 || subject.empty() || + subject.size() > 255) + return std::unexpected{AccountIdentityError::invalid}; + const std::array p{std::to_string(world), std::string(issuer), + std::string(subject)}; + auto rows = db.query("SELECT grant_account::text, grant_serial::text, home_region::text " + "FROM evolution.issue_account_grant($1::evolution.u64, $2, $3)", + p, timeout); + if (!rows) + return std::unexpected{identity_failure(rows.error())}; + if (rows->size() != 1 || (*rows)[0].size() != 3) + return std::unexpected{AccountIdentityError::unknown}; + const auto& row = (*rows)[0]; + const auto account = parse_uuid(row[0]); + const auto serial = parse_integer(row[1]); + if (!account || *account == Uuid{} || !serial || *serial == 0) + return std::unexpected{AccountIdentityError::unknown}; + IssuedGrantSerial issued{*account, *serial, std::nullopt}; + if (row[2]) { + const auto region = parse_integer(row[2]); + if (!region || *region == 0) + return std::unexpected{AccountIdentityError::unknown}; + issued.home_region = region; + } + return issued; +} + +std::expected revoke_account_grants(Postgres& db, std::uint64_t world, + std::string_view issuer, + std::string_view subject, + milliseconds timeout) { + if (world == 0 || issuer.empty() || subject.empty()) + return std::unexpected{AccountIdentityError::invalid}; + const std::array p{std::to_string(world), std::string(issuer), + std::string(subject)}; + auto rows = db.query("SELECT evolution.revoke_account_grants($1::evolution.u64, $2, $3)::text", + p, timeout); + if (!rows) + return std::unexpected{identity_failure(rows.error())}; + if (rows->size() != 1 || (*rows)[0].size() != 1) + return std::unexpected{AccountIdentityError::unknown}; + const auto& revoked = (*rows)[0][0]; + if (!revoked) + return std::unexpected{AccountIdentityError::unknown}; + return *revoked == "true"; +} +} // namespace evo::persistence \ No newline at end of file diff --git a/src/server/broker/access_token.cpp b/src/server/broker/access_token.cpp new file mode 100644 index 0000000..eaca08a --- /dev/null +++ b/src/server/broker/access_token.cpp @@ -0,0 +1,216 @@ +#include "evo/server/broker/access_token.hpp" + +#include "evo/server/broker/json.hpp" + +#include +#include +#include +#include + +#include +#include + +namespace evo::server::broker { +namespace { +constexpr std::string_view alphabet = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +std::shared_ptr rsa_public_key(std::span n, + std::span e) { + std::unique_ptr modulus{ + BN_bin2bn(n.data(), static_cast(n.size()), nullptr), BN_free}; + std::unique_ptr exponent{ + BN_bin2bn(e.data(), static_cast(e.size()), nullptr), BN_free}; + std::unique_ptr build{OSSL_PARAM_BLD_new(), + OSSL_PARAM_BLD_free}; + if (!modulus || !exponent || !build || + OSSL_PARAM_BLD_push_BN(build.get(), OSSL_PKEY_PARAM_RSA_N, modulus.get()) != 1 || + OSSL_PARAM_BLD_push_BN(build.get(), OSSL_PKEY_PARAM_RSA_E, exponent.get()) != 1) + return nullptr; + std::unique_ptr params{ + OSSL_PARAM_BLD_to_param(build.get()), OSSL_PARAM_free}; + std::unique_ptr context{ + EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr), EVP_PKEY_CTX_free}; + EVP_PKEY* key = nullptr; + if (!params || !context || EVP_PKEY_fromdata_init(context.get()) != 1 || + EVP_PKEY_fromdata(context.get(), &key, EVP_PKEY_PUBLIC_KEY, params.get()) != 1 || + key == nullptr) + return nullptr; + std::shared_ptr owned{key, [](void* p) { EVP_PKEY_free(static_cast(p)); }}; + if (EVP_PKEY_get_bits(key) < 2048) + return nullptr; + return owned; +} + +bool verify_rs256(void* key, std::string_view signed_part, std::span sig) { + std::unique_ptr context{EVP_MD_CTX_new(), + EVP_MD_CTX_free}; + return context && + EVP_DigestVerifyInit(context.get(), nullptr, EVP_sha256(), nullptr, + static_cast(key)) == 1 && + EVP_DigestVerify(context.get(), sig.data(), sig.size(), + reinterpret_cast(signed_part.data()), + signed_part.size()) == 1; +} + +std::optional json_segment(std::string_view segment) { + const auto bytes = base64url_decode(segment); + if (!bytes) + return std::nullopt; + return JsonValue::parse( + std::string_view{reinterpret_cast(bytes->data()), bytes->size()}); +} +} // namespace + +std::optional> base64url_decode(std::string_view text) { + if (text.size() % 4 == 1) + return std::nullopt; + std::vector out; + out.reserve(text.size() * 3 / 4); + std::uint32_t buffer = 0; + int bits = 0; + for (const char ch : text) { + std::uint32_t value{}; + if (ch >= 'A' && ch <= 'Z') + value = static_cast(ch - 'A'); + else if (ch >= 'a' && ch <= 'z') + value = static_cast(ch - 'a' + 26); + else if (ch >= '0' && ch <= '9') + value = static_cast(ch - '0' + 52); + else if (ch == '-') + value = 62; + else if (ch == '_') + value = 63; + else + return std::nullopt; // No padding or other characters in JWT segments. + buffer = (buffer << 6U) | value; + bits += 6; + if (bits >= 8) { + bits -= 8; + out.push_back( + static_cast((buffer >> static_cast(bits)) & 0xFFU)); + } + } + // Leftover bits must be zero (canonical encoding). + if (bits > 0 && (buffer & ((1U << static_cast(bits)) - 1U)) != 0) + return std::nullopt; + return out; +} + +std::string base64_encode(std::span bytes) { + std::string out; + out.reserve((bytes.size() + 2) / 3 * 4); + for (std::size_t i = 0; i < bytes.size(); i += 3) { + const std::uint32_t chunk = + (std::uint32_t{bytes[i]} << 16U) | + (i + 1 < bytes.size() ? std::uint32_t{bytes[i + 1]} << 8U : 0U) | + (i + 2 < bytes.size() ? std::uint32_t{bytes[i + 2]} : 0U); + out.push_back(alphabet[(chunk >> 18U) & 63U]); + out.push_back(alphabet[(chunk >> 12U) & 63U]); + out.push_back(i + 1 < bytes.size() ? alphabet[(chunk >> 6U) & 63U] : '='); + out.push_back(i + 2 < bytes.size() ? alphabet[chunk & 63U] : '='); + } + return out; +} + +std::expected TokenVerifier::from_jwks(std::string_view jwks, + AccessTokenRules rules) { + const auto document = JsonValue::parse(jwks); + const auto* keys = document ? document->member("keys") : nullptr; + if (!keys || !keys->array() || rules.issuer.empty() || rules.audience.empty() || + rules.authorized_party.empty()) + return std::unexpected{TokenError::malformed}; + std::vector usable; + for (const auto& entry : *keys->array()) { + const auto field = [&](std::string_view name) -> std::optional { + const auto* value = entry.member(name); + return value ? value->string() : std::nullopt; + }; + const auto kty = field("kty"); + const auto use = field("use"); + const auto alg = field("alg"); + const auto kid = field("kid"); + const auto n = field("n"); + const auto e = field("e"); + if (kty != "RSA" || !kid || kid->empty() || !n || !e || (use && *use != "sig") || + (alg && *alg != "RS256")) + continue; + const auto modulus = base64url_decode(*n); + const auto exponent = base64url_decode(*e); + if (!modulus || !exponent) + continue; + if (auto key = rsa_public_key(*modulus, *exponent)) + usable.push_back({std::string(*kid), std::move(key)}); + } + if (usable.empty()) + return std::unexpected{TokenError::unknown_key}; + return TokenVerifier{std::move(usable), std::move(rules)}; +} + +std::expected +TokenVerifier::verify(std::string_view token, std::uint64_t now_seconds) const { + if (token.empty() || token.size() > max_access_token_bytes) + return std::unexpected{TokenError::malformed}; + const auto first = token.find('.'); + const auto second = first == std::string_view::npos ? first : token.find('.', first + 1); + if (second == std::string_view::npos || token.find('.', second + 1) != std::string_view::npos) + return std::unexpected{TokenError::malformed}; + const auto header = json_segment(token.substr(0, first)); + const auto payload = json_segment(token.substr(first + 1, second - first - 1)); + const auto signature = base64url_decode(token.substr(second + 1)); + if (!header || !payload || !signature || header->kind() != JsonValue::Kind::object || + payload->kind() != JsonValue::Kind::object) + return std::unexpected{TokenError::malformed}; + + const auto* alg = header->member("alg"); + const auto* kid = header->member("kid"); + if (!alg || alg->string() != "RS256" || header->member("crit") != nullptr) + return std::unexpected{TokenError::unsupported_algorithm}; + const auto key_id = kid ? kid->string() : std::nullopt; + const auto key = std::ranges::find_if( + keys_, [&](const Key& candidate) { return key_id && candidate.kid == *key_id; }); + if (key == keys_.end()) + return std::unexpected{TokenError::unknown_key}; + if (!verify_rs256(key->public_key.get(), token.substr(0, second), *signature)) + return std::unexpected{TokenError::bad_signature}; + + // Only claims under a verified signature are read from here on. + const auto text = [&](std::string_view name) -> std::optional { + const auto* value = payload->member(name); + return value ? value->string() : std::nullopt; + }; + const auto number = [&](std::string_view name) -> std::optional { + const auto* value = payload->member(name); + return value ? value->integer() : std::nullopt; + }; + if (text("iss") != rules_.issuer) + return std::unexpected{TokenError::wrong_issuer}; + const auto* aud = payload->member("aud"); + bool audience = aud && aud->string() == rules_.audience; + if (aud && aud->array()) + audience = std::ranges::any_of(*aud->array(), [&](const JsonValue& entry) { + return entry.string() == rules_.audience; + }); + if (!audience) + return std::unexpected{TokenError::wrong_audience}; + if (text("azp") != rules_.authorized_party) + return std::unexpected{TokenError::wrong_party}; + if (text("typ") != "Bearer") // Keycloak access tokens; ID tokens say "ID". + return std::unexpected{TokenError::wrong_type}; + const auto expires = number("exp"); + if (!expires || now_seconds >= *expires + rules_.leeway_seconds) + return std::unexpected{TokenError::expired}; + if (const auto* nbf = payload->member("nbf")) { + const auto not_before = nbf->integer(); + if (!not_before || *not_before > now_seconds + rules_.leeway_seconds) + return std::unexpected{TokenError::not_yet_valid}; + } + const auto* verified = payload->member("email_verified"); + if (!verified || verified->boolean() != true) + return std::unexpected{TokenError::unverified_email}; + const auto subject = text("sub"); + if (!subject || subject->empty() || subject->size() > 255) + return std::unexpected{TokenError::missing_subject}; + return AccessTokenClaims{rules_.issuer, std::string(*subject), *expires}; +} +} // namespace evo::server::broker diff --git a/src/server/broker/grant_broker.cpp b/src/server/broker/grant_broker.cpp new file mode 100644 index 0000000..1055561 --- /dev/null +++ b/src/server/broker/grant_broker.cpp @@ -0,0 +1,150 @@ +#include "evo/server/broker/grant_broker.hpp" + +#include "evo/persistence/account_identity_store.hpp" + +#include +#include + +namespace evo::server::broker { +namespace { +constexpr std::size_t max_tracked_subjects = 4096; + +OpsResponse json(int status, std::string body) { + return {status, std::move(body), "application/json"}; +} +OpsResponse error(int status, std::string_view code) { + return json(status, "{\"error\":\"" + std::string(code) + "\"}\n"); +} +const char* token_error(TokenError e) { + switch (e) { + case TokenError::expired: + return "token_expired"; + case TokenError::unverified_email: + return "email_not_verified"; + case TokenError::wrong_audience: + case TokenError::wrong_party: + case TokenError::wrong_issuer: + case TokenError::wrong_type: + return "token_not_for_this_service"; + default: + return "invalid_token"; + } +} +std::string uuid_text(const persistence::Uuid& id) { + constexpr std::string_view digits = "0123456789abcdef"; + std::string out; + for (std::size_t i = 0; i < id.size(); ++i) { + if (i == 4 || i == 6 || i == 8 || i == 10) + out.push_back('-'); + out.push_back(digits[id[i] >> 4U]); + out.push_back(digits[id[i] & 0x0FU]); + } + return out; +} +// JSON string content for operator-configured names (endpoint, server name). +std::string escaped(std::string_view text) { + std::string out; + for (const char ch : text) { + if (ch == '"' || ch == '\\') + out.push_back('\\'); + if (static_cast(ch) >= 0x20) + out.push_back(ch); + } + return out; +} +} // namespace + +GrantBroker::GrantBroker(BrokerConfig config, TokenVerifier verifier, network::GrantSigningKey key, + BrokerConnect connect, GrantIdSource ids) + : config_{std::move(config)}, verifier_{std::move(verifier)}, key_{std::move(key)}, + connect_{std::move(connect)}, ids_{std::move(ids)} {} + +persistence::Postgres* GrantBroker::database() { + if (!db_ && connect_) + db_ = connect_(); + return db_.get(); +} + +OpsResponse GrantBroker::handle(const ApiRequest& request, std::uint64_t now_seconds) { + if (request.path == "/healthz") + return request.method == "GET" ? OpsResponse{200, "ok\n"} + : OpsResponse{405, "method not allowed\n"}; + if (request.path != "/grant" && request.path != "/logout") + return {404, "not found\n"}; + if (request.method != "POST") + return {405, "method not allowed\n"}; + auto authorization = request.authorization; + constexpr std::string_view scheme = "bearer "; + if (authorization.size() <= scheme.size() || + !std::ranges::equal(authorization.substr(0, scheme.size()), scheme, + [](char a, char b) { return (a | 0x20) == b; })) + return error(401, "missing_token"); + authorization.remove_prefix(scheme.size()); + const auto claims = verifier_.verify(authorization, now_seconds); + if (!claims) + return error(401, token_error(claims.error())); + return request.path == "/grant" ? issue(*claims, now_seconds) : logout(*claims); +} + +OpsResponse GrantBroker::issue(const AccessTokenClaims& claims, std::uint64_t now_seconds) { + const auto previous = last_grant_.find(claims.subject); + if (previous != last_grant_.end() && + now_seconds < previous->second + static_cast(config_.min_interval.count())) + return error(429, "too_many_requests"); + auto* db = database(); + if (db == nullptr) + return error(503, "unavailable"); + const auto issued = persistence::issue_account_grant(*db, config_.world, claims.issuer, + claims.subject, config_.db_timeout); + if (!issued) { + if (issued.error() != persistence::AccountIdentityError::invalid) + db_.reset(); + return error(503, "unavailable"); + } + const auto region = issued->home_region.value_or(config_.default_region); + const auto route = std::ranges::find(config_.routes, region, &BrokerRoute::region); + if (route == config_.routes.end()) + return error(503, "region_unavailable"); + + network::GrantClaims grant; + if (!ids_ || !ids_(grant.grant_id) || grant.grant_id == 0) + return error(503, "unavailable"); + grant.account.bytes = issued->account; + grant.serial = issued->serial; + grant.world = {config_.world, config_.frame}; + grant.region = region; + grant.issued_at = now_seconds; + grant.expires_at = now_seconds + config_.grant_lifetime_seconds; + const auto signed_grant = key_.sign(grant); + if (!signed_grant) + return error(503, "unavailable"); + + if (last_grant_.size() >= max_tracked_subjects && previous == last_grant_.end()) + std::erase_if(last_grant_, [&](const auto& entry) { + return entry.second + static_cast(config_.min_interval.count()) <= + now_seconds; + }); + if (last_grant_.size() < max_tracked_subjects) + last_grant_[claims.subject] = now_seconds; + return json(200, "{\"grant\":\"" + base64_encode(*signed_grant) + "\",\"account\":\"" + + uuid_text(issued->account) + "\",\"region\":" + std::to_string(region) + + ",\"endpoint\":\"" + escaped(route->endpoint) + "\",\"server_name\":\"" + + escaped(route->server_name) + + "\",\"expires_at\":" + std::to_string(grant.expires_at) + "}\n"); +} + +OpsResponse GrantBroker::logout(const AccessTokenClaims& claims) { + auto* db = database(); + if (db == nullptr) + return error(503, "unavailable"); + const auto revoked = persistence::revoke_account_grants(*db, config_.world, claims.issuer, + claims.subject, config_.db_timeout); + if (!revoked) { + if (revoked.error() != persistence::AccountIdentityError::invalid) + db_.reset(); + return error(503, "unavailable"); + } + last_grant_.erase(claims.subject); + return json(200, std::string("{\"revoked\":") + (*revoked ? "true" : "false") + "}\n"); +} +} // namespace evo::server::broker diff --git a/src/server/broker/json.cpp b/src/server/broker/json.cpp new file mode 100644 index 0000000..dfb2420 --- /dev/null +++ b/src/server/broker/json.cpp @@ -0,0 +1,301 @@ +#include "evo/server/broker/json.hpp" + +#include +#include + +namespace evo::server::broker { +namespace { +void append_utf8(std::string& out, std::uint32_t code) { + if (code < 0x80) { + out.push_back(static_cast(code)); + } else if (code < 0x800) { + out.push_back(static_cast(0xC0 | (code >> 6U))); + out.push_back(static_cast(0x80 | (code & 0x3FU))); + } else if (code < 0x10000) { + out.push_back(static_cast(0xE0 | (code >> 12U))); + out.push_back(static_cast(0x80 | ((code >> 6U) & 0x3FU))); + out.push_back(static_cast(0x80 | (code & 0x3FU))); + } else { + out.push_back(static_cast(0xF0 | (code >> 18U))); + out.push_back(static_cast(0x80 | ((code >> 12U) & 0x3FU))); + out.push_back(static_cast(0x80 | ((code >> 6U) & 0x3FU))); + out.push_back(static_cast(0x80 | (code & 0x3FU))); + } +} +} // namespace + +class JsonParser { + public: + explicit JsonParser(std::string_view text) : text_{text} {} + std::optional document() { + auto value = parse_value(0); + skip_space(); + if (!value || at_ != text_.size()) + return std::nullopt; + return value; + } + + private: + void skip_space() { + while (at_ < text_.size() && (text_[at_] == ' ' || text_[at_] == '\t' || + text_[at_] == '\n' || text_[at_] == '\r')) + ++at_; + } + bool literal(std::string_view word) { + if (text_.substr(at_, word.size()) != word) + return false; + at_ += word.size(); + return true; + } + std::optional hex4() { + if (at_ + 4 > text_.size()) + return std::nullopt; + std::uint32_t value{}; + for (int i = 0; i < 4; ++i) { + const char ch = text_[at_++]; + value <<= 4U; + if (ch >= '0' && ch <= '9') + value |= static_cast(ch - '0'); + else if (ch >= 'a' && ch <= 'f') + value |= static_cast(ch - 'a' + 10); + else if (ch >= 'A' && ch <= 'F') + value |= static_cast(ch - 'A' + 10); + else + return std::nullopt; + } + return value; + } + std::optional parse_string() { + if (at_ >= text_.size() || text_[at_] != '"') + return std::nullopt; + ++at_; + std::string out; + while (at_ < text_.size()) { + const auto ch = static_cast(text_[at_++]); + if (ch == '"') + return out; + if (ch < 0x20) + return std::nullopt; + if (ch != '\\') { + out.push_back(static_cast(ch)); + continue; + } + if (at_ >= text_.size()) + return std::nullopt; + switch (text_[at_++]) { + case '"': + out.push_back('"'); + break; + case '\\': + out.push_back('\\'); + break; + case '/': + out.push_back('/'); + break; + case 'b': + out.push_back('\b'); + break; + case 'f': + out.push_back('\f'); + break; + case 'n': + out.push_back('\n'); + break; + case 'r': + out.push_back('\r'); + break; + case 't': + out.push_back('\t'); + break; + case 'u': { + auto code = hex4(); + if (!code || (*code >= 0xDC00 && *code <= 0xDFFF)) + return std::nullopt; + if (*code >= 0xD800 && *code <= 0xDBFF) { + if (!literal("\\u")) + return std::nullopt; + const auto low = hex4(); + if (!low || *low < 0xDC00 || *low > 0xDFFF) + return std::nullopt; + code = 0x10000 + ((*code - 0xD800) << 10U) + (*low - 0xDC00); + } + append_utf8(out, *code); + break; + } + default: + return std::nullopt; + } + } + return std::nullopt; + } + std::optional parse_number() { + const auto start = at_; + if (at_ < text_.size() && text_[at_] == '-') + ++at_; + if (at_ >= text_.size()) + return std::nullopt; + if (text_[at_] == '0') { + ++at_; + } else if (text_[at_] >= '1' && text_[at_] <= '9') { + while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') + ++at_; + } else { + return std::nullopt; + } + if (at_ < text_.size() && text_[at_] == '.') { + ++at_; + const auto digits = at_; + while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') + ++at_; + if (at_ == digits) + return std::nullopt; + } + if (at_ < text_.size() && (text_[at_] == 'e' || text_[at_] == 'E')) { + ++at_; + if (at_ < text_.size() && (text_[at_] == '+' || text_[at_] == '-')) + ++at_; + const auto digits = at_; + while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') + ++at_; + if (at_ == digits) + return std::nullopt; + } + JsonValue value; + value.kind_ = JsonValue::Kind::number; + value.text_ = std::string(text_.substr(start, at_ - start)); + return value; + } + // `depth` counts the containers enclosing this value. + std::optional parse_value(std::size_t depth) { + skip_space(); + if (at_ >= text_.size()) + return std::nullopt; + if ((text_[at_] == '{' || text_[at_] == '[') && depth >= max_json_depth) + return std::nullopt; + JsonValue value; + const char ch = text_[at_]; + if (ch == '{') { + ++at_; + value.kind_ = JsonValue::Kind::object; + skip_space(); + if (at_ < text_.size() && text_[at_] == '}') { + ++at_; + return value; + } + for (;;) { + skip_space(); + auto name = parse_string(); + skip_space(); + if (!name || at_ >= text_.size() || text_[at_] != ':') + return std::nullopt; + ++at_; + auto member = parse_value(depth + 1); + if (!member) + return std::nullopt; + value.members_.emplace_back(std::move(*name), std::move(*member)); + skip_space(); + if (at_ >= text_.size()) + return std::nullopt; + if (text_[at_] == ',') { + ++at_; + continue; + } + if (text_[at_++] != '}') + return std::nullopt; + return value; + } + } + if (ch == '[') { + ++at_; + value.kind_ = JsonValue::Kind::array; + skip_space(); + if (at_ < text_.size() && text_[at_] == ']') { + ++at_; + return value; + } + for (;;) { + auto item = parse_value(depth + 1); + if (!item) + return std::nullopt; + value.items_.push_back(std::move(*item)); + skip_space(); + if (at_ >= text_.size()) + return std::nullopt; + if (text_[at_] == ',') { + ++at_; + continue; + } + if (text_[at_++] != ']') + return std::nullopt; + return value; + } + } + if (ch == '"') { + auto text = parse_string(); + if (!text) + return std::nullopt; + value.kind_ = JsonValue::Kind::string; + value.text_ = std::move(*text); + return value; + } + if (literal("true")) { + value.kind_ = JsonValue::Kind::boolean; + value.boolean_ = true; + return value; + } + if (literal("false")) { + value.kind_ = JsonValue::Kind::boolean; + return value; + } + if (literal("null")) + return value; + return parse_number(); + } + + std::string_view text_; + std::size_t at_{}; +}; + +std::optional JsonValue::parse(std::string_view text) { + if (text.size() > max_json_bytes) + return std::nullopt; + return JsonParser{text}.document(); +} + +std::optional JsonValue::boolean() const { + if (kind_ != Kind::boolean) + return std::nullopt; + return boolean_; +} + +std::optional JsonValue::string() const { + if (kind_ != Kind::string) + return std::nullopt; + return std::string_view{text_}; +} + +std::optional JsonValue::integer() const { + if (kind_ != Kind::number || text_.empty() || text_.front() == '-' || + text_.find_first_of(".eE") != std::string::npos) + return std::nullopt; + std::uint64_t value{}; + const auto [end, error] = std::from_chars(text_.data(), text_.data() + text_.size(), value); + if (error != std::errc{} || end != text_.data() + text_.size() || + value > static_cast(std::numeric_limits::max())) + return std::nullopt; + return value; +} + +const JsonValue* JsonValue::member(std::string_view name) const { + if (kind_ != Kind::object) + return nullptr; + for (const auto& [key, value] : members_) + if (key == name) + return &value; + return nullptr; +} + +const std::vector* JsonValue::array() const { + return kind_ == Kind::array ? &items_ : nullptr; +} +} // namespace evo::server::broker diff --git a/src/server/ops_http.cpp b/src/server/ops_http.cpp index d2b84ca..1f18f57 100644 --- a/src/server/ops_http.cpp +++ b/src/server/ops_http.cpp @@ -15,8 +15,12 @@ const char* reason(int status) { return "OK"; case 400: return "Bad Request"; + case 401: + return "Unauthorized"; case 404: return "Not Found"; + case 429: + return "Too Many Requests"; case 405: return "Method Not Allowed"; case 431: @@ -33,7 +37,9 @@ std::string encode(const OpsResponse& r) { "\r\nContent-Length: " + std::to_string(r.body.size()) + "\r\nCache-Control: no-store\r\nConnection: close\r\n"; if (r.status == 405) - out += "Allow: GET\r\n"; + out += "Allow: GET, POST\r\n"; + if (r.status == 401) + out += "WWW-Authenticate: Bearer\r\n"; return out + "\r\n" + r.body; } bool printable(std::string_view s) { @@ -122,7 +128,65 @@ void OpsServer::respond(Connection& c, const OpsHandler& handler) { c.out = encode(response); } +void OpsServer::respond_api(Connection& c, const ApiHandler& handler) { + OpsResponse response; + const std::string_view request = c.in; + const auto line_end = request.find("\r\n"); + const auto line = request.substr(0, line_end); + const auto first = line.find(' '); + const auto second = first == std::string_view::npos ? first : line.find(' ', first + 1); + if (second == std::string_view::npos || line.substr(second + 1).rfind("HTTP/1.", 0) != 0) { + c.out = encode({400, "bad request\n"}); + return; + } + const auto method = line.substr(0, first); + const auto path = line.substr(first + 1, second - first - 1); + std::string_view authorization; + bool has_body = false; + // Header lines up to the blank line; names are case-insensitive. + auto at = line_end + 2; + while (at < request.size()) { + const auto end = request.find("\r\n", at); + if (end == std::string_view::npos || end == at) + break; + const auto header = request.substr(at, end - at); + const auto colon = header.find(':'); + if (colon != std::string_view::npos) { + std::string name(header.substr(0, colon)); + for (auto& ch : name) + ch = static_cast(ch >= 'A' && ch <= 'Z' ? ch - 'A' + 'a' : ch); + auto value = header.substr(colon + 1); + while (!value.empty() && value.front() == ' ') + value.remove_prefix(1); + if (name == "authorization") + authorization = value; + else if ((name == "content-length" && value != "0") || name == "transfer-encoding") + has_body = true; + } + at = end + 2; + } + if (method != "GET" && method != "POST") + response = {405, "method not allowed\n"}; + else if (path.empty() || path.front() != '/' || path.size() > 256 || !printable(path) || + has_body) + response = {400, "bad request\n"}; + else + response = handler({method, path, authorization}); + if (response.body.size() > max_response_bytes) + response = {500, "response too large\n"}; + c.out = encode(response); +} + void OpsServer::serve(const OpsHandler& handler) { + serve_with(max_request_bytes, [&](Connection& c) { respond(c, handler); }); +} + +void OpsServer::serve_api(const ApiHandler& handler) { + serve_with(max_api_request_bytes, [&](Connection& c) { respond_api(c, handler); }); +} + +template +void OpsServer::serve_with(std::size_t request_limit, const Answer& answer) { const auto accepted_at = Clock::now(); for (std::size_t attempt = 0; attempt < max_accepts_per_serve; ++attempt) { const int fd = ::accept4(fd_, nullptr, nullptr, SOCK_NONBLOCK | SOCK_CLOEXEC); @@ -148,10 +212,10 @@ void OpsServer::serve(const OpsHandler& handler) { done = true; } else if (n > 0) { c.in.append(chunk, static_cast(n)); - if (c.in.size() > max_request_bytes) + if (c.in.size() > request_limit) c.out = encode({431, "request too large\n"}); else if (c.in.find("\r\n\r\n") != std::string::npos) - respond(c, handler); + answer(c); } } if (!done && !c.out.empty()) { diff --git a/tests/grant_broker_tests.cpp b/tests/grant_broker_tests.cpp new file mode 100644 index 0000000..b45858f --- /dev/null +++ b/tests/grant_broker_tests.cpp @@ -0,0 +1,341 @@ +// ADR-0131 grant broker: bounded JSON, offline RS256 access-token checks, and real +// PostgreSQL schema v39 identities, serials and logout revocation. Isolated world 9831. +#include "evo/persistence/account_control_store.hpp" +#include "evo/persistence/account_identity_store.hpp" +#include "evo/persistence/region_lease_store.hpp" +#include "evo/server/broker/grant_broker.hpp" +#include "evo/server/broker/json.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +void run_grant_broker_tests(); + +namespace { +using namespace evo; +using namespace evo::server::broker; +using namespace std::chrono_literals; +void check(bool value, std::source_location at = std::source_location::current()) { + if (!value) + throw std::runtime_error("grant broker test line " + std::to_string(at.line())); +} +#define CHECK(...) check(static_cast(__VA_ARGS__)) +template +const T& must(const std::optional& value, + std::source_location at = std::source_location::current()) { + if (!value) + throw std::runtime_error("grant broker test line " + std::to_string(at.line())); + return *value; +} +std::string env(const char* name) { + const auto* value = std::getenv(name); + if (!value) + throw std::runtime_error("missing PostgreSQL test environment"); + return value; +} +std::unique_ptr connect_as(const std::string& user) { + auto db = persistence::Postgres::connect( + {env("PGHOST"), {}, env("PGPORT"), env("PGDATABASE"), user, {}, {}}, 5s); + CHECK(db); + return std::move(*db); +} + +std::string base64url(std::span bytes) { + auto text = base64_encode(bytes); + for (auto& ch : text) + ch = ch == '+' ? '-' : ch == '/' ? '_' : ch; + while (!text.empty() && text.back() == '=') + text.pop_back(); + return text; +} +std::string base64url(std::string_view text) { + return base64url(std::span{reinterpret_cast(text.data()), text.size()}); +} + +// A test realm: one RSA key, its JWKS, and RS256 token signing. +class Realm { + public: + Realm() : key_{EVP_PKEY_Q_keygen(nullptr, nullptr, "RSA", std::size_t{2048}), EVP_PKEY_free} { + CHECK(key_); + } + [[nodiscard]] std::string jwks(std::string_view kid) const { + return R"({"keys":[{"kid":"enc","kty":"RSA","use":"enc","alg":"RSA-OAEP","n":")" + + component(OSSL_PKEY_PARAM_RSA_N) + R"(","e":"AQAB"},{"kid":")" + std::string(kid) + + R"(","kty":"RSA","use":"sig","alg":"RS256","n":")" + + component(OSSL_PKEY_PARAM_RSA_N) + R"(","e":")" + component(OSSL_PKEY_PARAM_RSA_E) + + R"("}]})"; + } + [[nodiscard]] std::string token(std::string_view header, std::string_view payload) const { + const auto input = base64url(header) + "." + base64url(payload); + std::unique_ptr context{EVP_MD_CTX_new(), + EVP_MD_CTX_free}; + std::size_t size = 0; + CHECK(context && + EVP_DigestSignInit(context.get(), nullptr, EVP_sha256(), nullptr, key_.get()) == 1 && + EVP_DigestSign(context.get(), nullptr, &size, + reinterpret_cast(input.data()), + input.size()) == 1); + std::vector signature(size); + CHECK(EVP_DigestSign(context.get(), signature.data(), &size, + reinterpret_cast(input.data()), + input.size()) == 1); + signature.resize(size); + return input + "." + base64url(signature); + } + + private: + [[nodiscard]] std::string component(const char* name) const { + BIGNUM* value = nullptr; + CHECK(EVP_PKEY_get_bn_param(key_.get(), name, &value) == 1); + std::vector bytes(static_cast(BN_num_bytes(value))); + BN_bn2bin(value, bytes.data()); + BN_free(value); + return base64url(bytes); + } + std::unique_ptr key_; +}; + +constexpr std::string_view issuer = "https://auth.example.test/realms/aeternalis"; +constexpr std::uint64_t now = 1'800'000'000; +const std::string header = R"({"alg":"RS256","typ":"JWT","kid":"sig-1"})"; +std::string claims(std::string_view subject, std::string_view extra = {}, + std::string_view aud = R"(["account","aevolution-grants"])", + std::string_view azp = "aevolution-game", std::string_view typ = "Bearer", + std::uint64_t exp = now + 300, std::string_view verified = "true") { + return R"({"exp":)" + std::to_string(exp) + R"(,"iat":)" + std::to_string(now - 10) + + R"(,"iss":")" + std::string(issuer) + R"(","aud":)" + std::string(aud) + R"(,"sub":")" + + std::string(subject) + R"(","typ":")" + std::string(typ) + R"(","azp":")" + + std::string(azp) + R"(","email_verified":)" + std::string(verified) + + std::string(extra) + "}"; +} +AccessTokenRules rules() { + return {std::string(issuer), "aevolution-grants", "aevolution-game", 30}; +} + +void json_tests() { + const auto parsed = JsonValue::parse("{\"a\":[1,true,null,\"x\\u00e9\\ud83d\\ude00\\n\"]," + "\"b\":{\"c\":9223372036854775807},\"a\":2}"); + const auto& doc = must(parsed); + CHECK(doc.member("a")->array()->size() == 4); + CHECK(doc.member("a")->array()->at(1).boolean() == true); + CHECK(doc.member("a")->array()->at(3).string() == "x\xC3\xA9\xF0\x9F\x98\x80\n"); + CHECK(doc.member("b")->member("c")->integer() == 9223372036854775807ULL); + CHECK(doc.member("missing") == nullptr); + for (const std::string_view bad : {"", "{", "[1,]", R"({"a":1,})", "01", "1.5e", R"("\ud83d")", + "[1] x", "{'a':1}", "\"\x01\"", "nul", "-", R"({"a" 1})"}) + CHECK(!JsonValue::parse(bad)); + CHECK(!must(JsonValue::parse("9223372036854775808")).integer()); + CHECK(!must(JsonValue::parse("1.5")).integer()); + CHECK(!must(JsonValue::parse("-1")).integer()); + CHECK(JsonValue::parse(std::string(16, '[') + std::string(16, ']'))); + CHECK(!JsonValue::parse(std::string(17, '[') + std::string(17, ']'))); + CHECK(!JsonValue::parse("\"" + std::string(max_json_bytes, 'a') + "\"")); +} + +void token_tests(const Realm& realm) { + CHECK(base64url_decode("AQAB") == std::vector{1, 0, 1}); + CHECK(!base64url_decode("AQAB=")); + CHECK(!base64url_decode("AQ+B")); + CHECK(!base64url_decode("AR")); // Non-canonical trailing bits. + + CHECK(!TokenVerifier::from_jwks(R"({"keys":[]})", rules())); + CHECK(!TokenVerifier::from_jwks("not json", rules())); + const auto verifier = TokenVerifier::from_jwks(realm.jwks("sig-1"), rules()); + CHECK(verifier && verifier->keys() == 1); // The enc key is ignored. + + const auto good = verifier->verify(realm.token(header, claims("user-a")), now); + CHECK(good && good->subject == "user-a" && good->issuer == issuer && + good->expires_at == now + 300); + CHECK( + verifier->verify(realm.token(header, claims("user-a", {}, R"("aevolution-grants")")), now)); + const auto failure = [&](const std::string& token, TokenError expected, + std::uint64_t at = now) { + const auto result = verifier->verify(token, at); + return !result && result.error() == expected; + }; + CHECK(failure(realm.token(header, claims("u", {}, R"(["account"])")), + TokenError::wrong_audience)); + CHECK(failure(realm.token(header, claims("u", {}, R"("aevolution-grants")", "other")), + TokenError::wrong_party)); + CHECK(failure( + realm.token(header, claims("u", {}, R"("aevolution-grants")", "aevolution-game", "ID")), + TokenError::wrong_type)); + CHECK(failure(realm.token(header, claims("u")), TokenError::expired, now + 330)); + CHECK(verifier->verify(realm.token(header, claims("u")), now + 329)); // Leeway. + CHECK(failure(realm.token(header, claims("u", R"(,"nbf":1800000100)")), + TokenError::not_yet_valid)); + CHECK(failure(realm.token(header, claims("u", {}, R"("aevolution-grants")", "aevolution-game", + "Bearer", now + 300, "false")), + TokenError::unverified_email)); + CHECK(failure(realm.token(header, claims("")), TokenError::missing_subject)); + auto wrong_issuer = claims("u"); + wrong_issuer.replace(wrong_issuer.find("aeternalis"), 10, "other-realm"); + CHECK(failure(realm.token(header, wrong_issuer), TokenError::wrong_issuer)); + CHECK(failure(realm.token(R"({"alg":"RS256","kid":"sig-2"})", claims("u")), + TokenError::unknown_key)); + CHECK(failure(realm.token(R"({"alg":"none","kid":"sig-1"})", claims("u")), + TokenError::unsupported_algorithm)); + CHECK(failure(realm.token(R"({"alg":"HS256","kid":"sig-1"})", claims("u")), + TokenError::unsupported_algorithm)); + // A signature over different claims, and a token signed by another realm's key. + auto spliced = realm.token(header, claims("user-a")); + const auto other = realm.token(header, claims("user-b")); + spliced = spliced.substr(0, spliced.rfind('.')) + other.substr(other.rfind('.')); + CHECK(failure(spliced, TokenError::bad_signature)); + const Realm stranger; + CHECK(failure(stranger.token(header, claims("user-a")), TokenError::bad_signature)); + CHECK(failure("a.b", TokenError::malformed)); + CHECK(failure(std::string(max_access_token_bytes + 1, 'a'), TokenError::malformed)); +} + +struct Grant { + network::GrantClaims claims; + std::string account; + std::uint64_t region{}; +}; +Grant decode(const server::OpsResponse& response, const network::GrantVerifier& verifier) { + CHECK(response.status == 200 && response.content_type == "application/json"); + const auto parsed = JsonValue::parse(response.body); + const auto& body = must(parsed); + CHECK(body.member("grant") && body.member("account") && body.member("region")); + CHECK(body.member("endpoint")->string() == "void.example.test:7400"); + CHECK(body.member("server_name")->string() == "void.example.test"); + const auto decoded = base64url_decode([&] { + auto text = std::string(must(body.member("grant")->string())); + for (auto& ch : text) + ch = ch == '+' ? '-' : ch == '/' ? '_' : ch; + while (!text.empty() && text.back() == '=') + text.pop_back(); + return text; + }()); + const auto verified = verifier.verify(must(decoded)); + CHECK(verified); + const auto& claims = verified->claims(); + CHECK(body.member("expires_at")->integer() == claims.expires_at); + return {claims, std::string(must(body.member("account")->string())), + must(body.member("region")->integer())}; +} + +void broker_tests(const Realm& realm) { + auto admin = connect_as(env("PGUSER")); + CHECK(admin->query("INSERT INTO evolution.retained_builds(build_identity) VALUES " + "(decode(repeat('d7',32),'hex')) ON CONFLICT DO NOTHING", + {}, 5s)); + CHECK(admin->query("INSERT INTO evolution.region_leases(world_id,region_id,frame_id) " + "VALUES (9831,1,7)", + {}, 5s)); + const auto runtime = env("EVO_PG_RUNTIME_USER"); + auto key = network::GrantSigningKey::generate(network::DeploymentEnvironment::dev, 1); + CHECK(key); + auto public_key = key->public_key(); + auto grants = network::GrantVerifier::create(network::DeploymentEnvironment::dev); + CHECK(public_key && grants && grants->add_key(1, *public_key)); + std::uint64_t next_id = 41; + BrokerConfig config{9831, 7, 1, {{1, "void.example.test:7400", "void.example.test"}}}; + int connects = 0; + GrantBroker broker{config, *TokenVerifier::from_jwks(realm.jwks("sig-1"), rules()), + std::move(*key), + [&]() -> std::unique_ptr { + ++connects; + return connect_as(runtime); + }, + [&](std::uint64_t& id) { + id = next_id++; + return true; + }}; + const auto alice = "Bearer " + realm.token(header, claims("alice-sub")); + const auto bob = "bearer " + realm.token(header, claims("bob-sub")); + const auto post = [&](std::string_view path, const std::string& authorization, + std::uint64_t at) { + return broker.handle({"POST", path, authorization}, at); + }; + + CHECK(broker.handle({"GET", "/healthz", {}}, now).status == 200); + CHECK(broker.handle({"GET", "/grant", alice}, now).status == 405); + CHECK(broker.handle({"POST", "/other", alice}, now).status == 404); + CHECK(post("/grant", "", now).status == 401); + CHECK(post("/grant", "Basic abc", now).status == 401); + const auto forged = post("/grant", "Bearer " + Realm{}.token(header, claims("alice-sub")), now); + CHECK(forged.status == 401 && forged.body.find("invalid_token") != std::string::npos); + CHECK(connects == 0); // Nothing reaches the database before a token verifies. + + const auto first = decode(post("/grant", alice, now), *grants); + CHECK(first.claims.serial == 1 && first.claims.grant_id == 41 && first.region == 1); + CHECK(first.claims.world.world == 9831 && first.claims.world.frame == 7); + CHECK(first.claims.region == 1 && first.claims.issued_at == now && + first.claims.expires_at == now + 600); + CHECK(first.account.size() == 36 && first.account[8] == '-'); + CHECK(post("/grant", alice, now + 1).status == 429); + const auto second = decode(post("/grant", alice, now + 2), *grants); + CHECK(second.claims.serial == 2 && second.account == first.account); + CHECK(second.claims.account.bytes == first.claims.account.bytes); + const auto other = decode(post("/grant", bob, now + 2), *grants); + CHECK(other.account != first.account && other.claims.serial == 1); + + // The region accepts the newest grant; logout makes it, and every older one, stale. + persistence::Sha256Digest build{}; + build.fill(0xD7); + auto region = connect_as(runtime); + const persistence::RegionKey home{9831, 1}; + const auto lease = persistence::acquire_region_lease(*region, home, build, 20000ms, 5s); + CHECK(lease); + const auto& account = second.claims.account.bytes; + const auto claim = + persistence::claim_account_control(*region, home, *lease, account, 2, 7, 20000ms, 5s); + CHECK(claim); + CHECK(persistence::renew_account_control(*region, home, *lease, account, claim->generation, + 20000ms, 5s)); + const auto logout = post("/logout", alice, now + 3); + CHECK(logout.status == 200 && logout.body == "{\"revoked\":true}\n"); + const auto renewed = persistence::renew_account_control(*region, home, *lease, account, + claim->generation, 20000ms, 5s); + CHECK(!renewed && renewed.error().kind == persistence::ControlStoreError::fenced); + + // The next login gets a serial above everything revoked and takes control again. + const auto third = decode(post("/grant", alice, now + 3), *grants); + CHECK(third.claims.serial == 3 && third.account == first.account); + const auto stale = + persistence::claim_account_control(*region, home, *lease, account, 2, 8, 20000ms, 5s); + CHECK(!stale && stale.error().kind == persistence::ControlStoreError::stale_grant); + CHECK(persistence::claim_account_control(*region, home, *lease, account, 3, 8, 20000ms, 5s)); + + // Bob never held control: logout still raises the accepted serial for him. + CHECK(post("/logout", bob, now + 4).body == "{\"revoked\":true}\n"); + CHECK(!persistence::claim_account_control(*region, home, *lease, other.claims.account.bytes, 1, + 9, 20000ms, 5s)); + const auto unknown = post("/logout", "Bearer " + realm.token(header, claims("nobody")), now); + CHECK(unknown.status == 200 && unknown.body == "{\"revoked\":false}\n"); + + // Store-level argument checks, and a database that cannot be reached. + CHECK(!persistence::issue_account_grant(*region, 0, issuer, "x", 5s)); + CHECK(!persistence::issue_account_grant(*region, 9831, issuer, "", 5s)); + CHECK(!persistence::issue_account_grant(*region, 9831, issuer, std::string(256, 's'), 5s)); + GrantBroker offline{ + config, *TokenVerifier::from_jwks(realm.jwks("sig-1"), rules()), + std::move(*network::GrantSigningKey::generate(network::DeploymentEnvironment::dev, 1)), + [] { return std::unique_ptr{}; }, + [](std::uint64_t& id) { + id = 1; + return true; + }}; + CHECK(offline.handle({"POST", "/grant", alice}, now).status == 503); + + // Runtime role cannot read or write identities except through the functions. + CHECK(!region->query("SELECT count(*) FROM evolution.account_identities", {}, 5s)); +} +} // namespace + +void run_grant_broker_tests() { + json_tests(); + const Realm realm; + token_tests(realm); + broker_tests(realm); +} diff --git a/tests/postgres_tests.cpp b/tests/postgres_tests.cpp index c739877..d61f9a2 100644 --- a/tests/postgres_tests.cpp +++ b/tests/postgres_tests.cpp @@ -16,6 +16,7 @@ void run_region_host_tests(); void run_region_server_process_tests(); #ifdef EVO_POSTGRES_SESSIONS void run_region_session_tests(); +void run_grant_broker_tests(); #endif #ifdef EVO_POSTGRES_M10 void run_region_m10_session_tests(); @@ -151,6 +152,8 @@ int main(int argc, char** argv) { #ifdef EVO_POSTGRES_SESSIONS run_region_session_tests(); std::puts("region dev session integration passed"); + run_grant_broker_tests(); + std::puts("grant broker integration passed"); #endif #ifdef EVO_POSTGRES_M10 run_region_m10_session_tests(); diff --git a/tests/region_migration_tests.cpp b/tests/region_migration_tests.cpp index 8fde30a..ec4afd1 100644 --- a/tests/region_migration_tests.cpp +++ b/tests/region_migration_tests.cpp @@ -212,7 +212,7 @@ void upgrade_across_migration_and_barrier() { " EVO_PG_RUNTIME_ROLE=evo_runtime_test bash \"$EVO_SOURCE_ROOT/" "tools/migrate-postgres.sh\" > /dev/null") == 0); CHECK(history(*owner_db) == "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24," - "25,26,27,28,29,30,31,32,33,34,35,36,37,38"); + "25,26,27,28,29,30,31,32,33,34,35,36,37,38,39"); CHECK(scalar(*owner_db, "SELECT (c.condeferrable AND c.condeferred AND " "c.confrelid = 'evolution.region_commits'::regclass)::text " "FROM pg_catalog.pg_constraint c WHERE c.conrelid = " diff --git a/tools/ci/postgres-check.sh b/tools/ci/postgres-check.sh index 32b2457..fb4d814 100644 --- a/tools/ci/postgres-check.sh +++ b/tools/ci/postgres-check.sh @@ -122,7 +122,7 @@ PGDATABASE=upgrade_path psql -X -v ON_ERROR_STOP=1 -1 -v "migration_sha=$v1_sha" -f "$root/migrations/0001_region_bootstrap.sql" > /dev/null test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',') FROM evolution.schema_migrations")" = 1 PGDATABASE=upgrade_path bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38 +test "$(PGDATABASE=upgrade_path psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 # v2 heads have no payload binding: v2->v3 fails closed and leaves history at v2. v2_sha="$(sha256sum "$root/migrations/0002_region_fencing.sql")" v2_sha="${v2_sha%% *}" @@ -143,7 +143,7 @@ PGDATABASE=v3_commits psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '01000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32), 'hex'))" > /dev/null PGDATABASE=v3_commits bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 38:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38 +test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) || ':' || string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 39:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 test "$(PGDATABASE=v3_commits psql -XAtc "SELECT count(*) FROM evolution.region_commits")" = 1 # Review P1: a v4 database holding a published (possibly terminal) checkpoint has # no durable decisions for it. The v5 upgrade must refuse before changing anything. @@ -197,7 +197,7 @@ PGDATABASE=v6_populated psql -X -v ON_ERROR_STOP=1 \ -c "INSERT INTO evolution.retained_builds VALUES (decode(repeat('b1', 32), 'hex'))" \ -c "SELECT evolution.provision_region_checkpoint(decode('4145564f52434d31' || '02000000' || '01000000' || '0100000000000000' || '0100000000000000' || '0900000000000000' || repeat('11', 16) || repeat('00', 8) || repeat('00', 16) || repeat('00', 32) || repeat('00', 16) || repeat('00', 16) || repeat('00', 8) || '50c3000000000000' || '020000000200000001000000010000000100000001000000010000000100000001000000' || repeat('b1', 32) || '00000000' || '00000000' || '01000000' || '6400000000000000' || repeat('a0', 32) || repeat('00', 80), 'hex'))" > /dev/null PGDATABASE=v6_populated bash "$root/tools/migrate-postgres.sh" -test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38 +test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(version::text, ',' ORDER BY version) FROM evolution.schema_migrations")" = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT count(*) FROM evolution.region_commits c, LATERAL evolution.parse_checkpoint_manifest(c.manifest) m WHERE m.commit_sequence = 0 AND m.predecessor_commit IS NULL")" = 1 test "$(PGDATABASE=v6_populated psql -XAtc "SELECT string_agg(conname, ',') FROM pg_constraint WHERE conrelid = 'evolution.region_heads'::regclass AND contype = 'c' AND pg_get_constraintdef(oid) LIKE '%end_tick%'")" = region_heads_published_tick phase permission-audit @@ -250,9 +250,9 @@ expect_audit_failure 'REVOKE EXECUTE ON FUNCTION evolution.claim_account_control bash "$root/tools/migrate-postgres.sh" # Restored configuration passes the audit again. phase checksum-drift # Unknown future versions and checksum drift must not be repaired silently. -psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=39 WHERE version=38' +psql -X -v ON_ERROR_STOP=1 -c 'UPDATE evolution.schema_migrations SET version=40 WHERE version=39' if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi -psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=38 WHERE version=39" +psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET version=39 WHERE version=40" psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=14" if bash "$root/tools/migrate-postgres.sh"; then exit 1; fi psql -X -v ON_ERROR_STOP=1 -c "UPDATE evolution.schema_migrations SET sha256=repeat('0',64) WHERE version=13" diff --git a/tools/migrate-postgres.sh b/tools/migrate-postgres.sh index ba0d258..7c4a25b 100644 --- a/tools/migrate-postgres.sh +++ b/tools/migrate-postgres.sh @@ -88,6 +88,8 @@ digest_37="$(sha256sum "$root/migrations/0037_cellular_life_endings.sql")" digest_37="${digest_37%% *}" digest_38="$(sha256sum "$root/migrations/0038_cellular_v6_presence_bundle.sql")" digest_38="${digest_38%% *}" +digest_39="$(sha256sum "$root/migrations/0039_account_identities.sql")" +digest_39="${digest_39%% *}" psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_2=$digest_2" --set="migration_sha_3=$digest_3" \ --set="migration_sha_4=$digest_4" --set="migration_sha_5=$digest_5" \ @@ -119,5 +121,6 @@ psql -X --no-password --set=ON_ERROR_STOP=1 --set="migration_sha=$digest" \ --set="migration_sha_36=$digest_36" \ --set="migration_sha_37=$digest_37" \ --set="migration_sha_38=$digest_38" \ + --set="migration_sha_39=$digest_39" \ --set="runtime_role=$runtime_role" \ --file="$root/migrations/apply.sql"