Files
dingoandClaude Opus 5.5 68880ba3b4
Foundation / checks (checks) (push) Successful in 42m13s
Foundation / checks (deploy) (push) Successful in 4m5s
Foundation / checks (ecs) (push) Successful in 2m25s
Foundation / checks (network) (push) Successful in 19m18s
Foundation / checks (postgres) (push) Successful in 44m19s
Foundation / checks (tsan) (push) Successful in 9m48s
Foundation / checks (client) (push) Failing after 12m6s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped
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 <noreply@anthropic.com>
2026-10-15 10:20:27 -04:00

180 lines
7.0 KiB
C++

// 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 <array>
#include <charconv>
#include <chrono>
#include <csignal>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <optional>
#include <string>
#include <string_view>
#include <thread>
#include <vector>
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<std::uint64_t> 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<server::broker::BrokerRoute> 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<Options> 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::uint64_t>(std::chrono::duration_cast<std::chrono::seconds>(
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<std::uint8_t> scalar{std::istreambuf_iterator<char>{key_file},
std::istreambuf_iterator<char>{}};
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<persistence::Postgres> {
auto db = persistence::Postgres::connect(pg, 3s);
return db ? std::move(*db) : nullptr;
},
[](std::uint64_t& id) {
std::array<std::uint8_t, sizeof id> 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<unsigned>((*listening)->port()),
static_cast<unsigned long long>(options->broker.world),
static_cast<unsigned long long>(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;
}