Foundation / checks (client) (push) Successful in 4s
Foundation / checks (checks) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m47s
Foundation / postgres-tools (push) Successful in 29s
Foundation / server-image (push) Successful in 4m42s
Foundation / image (push) Successful in 5m55s
ADR-0182 section 13. The admin portal's Tuning page edits the world's rules-13 tuning (the Git revamp file format); the admin API checks it, stores an append-only revision and queues an adopt_tuning admin command (kind 7: revision and rules digest) for every region of the world. - Lifecycle: adopt_tuning replaces the region's rules after births when a resolver names the rules for that revision and digest; the safety revision cannot change live. At most one adoption per batch. - Host: fetches the revision text, rebuilds and digests the rules before appending the command; publication and recovery name the rules from the successor state, checked against the recorded digest. - Schema 66: tuning_revisions, admin_set_tuning_v1, load_tuning_revision_v1 (runtime), admin_tuning_revisions_v1, admin_tuning_text_v1. - OpsServer: optional bounded request bodies (Content-Length, 413). - Region view: rules_sha256, so the portal shows each region's revision. - Portal: Tuning page; baseline from the mounted cellular-revamp.tuning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
217 lines
8.9 KiB
C++
217 lines
8.9 KiB
C++
// ADR-0182 read-only admin API and portal host for one world (DEV).
|
|
//
|
|
// evo-admin-api --listen ADDRESS:PORT --world N --jwks JSON --oidc-issuer URL
|
|
// --audience NAME --client NAME --role NAME --assets DIR
|
|
// --pg-passfile F --pg-service F [--region REGION=HOST:PORT ...] [--baseline-tuning F]
|
|
//
|
|
// --assets holds the portal's index.html, app.js and style.css. Each --region names a
|
|
// region's operations address, whose /admin/view the portal may show; those regions adopt
|
|
// tuning revisions. --baseline-tuning is the revamp tuning file they opened with, shown
|
|
// until the first revision. No token is printed.
|
|
#include "evo/persistence/postgres.hpp"
|
|
#include "evo/server/admin/admin_api.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <charconv>
|
|
#include <chrono>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <map>
|
|
#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, jwks, oidc_issuer, audience, client, role, assets, baseline_tuning;
|
|
std::uint64_t world{};
|
|
std::map<std::uint64_t, std::string> regions; // Region to its operations address.
|
|
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<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.world = *n;
|
|
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 == "--role")
|
|
o.role = value;
|
|
else if (flag == "--assets")
|
|
o.assets = value;
|
|
else if (flag == "--baseline-tuning")
|
|
o.baseline_tuning = value;
|
|
else if (flag == "--pg-service")
|
|
pg_service = value;
|
|
else if (flag == "--pg-passfile")
|
|
pg_passfile = value;
|
|
else if (flag == "--region") {
|
|
const auto equals = value.find('=');
|
|
const auto region =
|
|
equals == std::string_view::npos ? std::nullopt : number(value.substr(0, equals));
|
|
const auto address =
|
|
equals == std::string_view::npos ? std::string_view{} : value.substr(equals + 1);
|
|
if (!region || *region == 0 || address.empty() || address.size() > 128 ||
|
|
!o.regions.emplace(*region, std::string(address)).second)
|
|
return std::nullopt;
|
|
} else
|
|
return std::nullopt;
|
|
}
|
|
// Names are embedded in the portal's JSON settings: printable ASCII without quotes.
|
|
const auto plain = [](std::string_view text) {
|
|
return !text.empty() && text.size() <= 256 && std::ranges::all_of(text, [](char ch) {
|
|
return ch > ' ' && ch <= '~' && ch != '"' && ch != '\\';
|
|
});
|
|
};
|
|
if (argc % 2 != 1 || o.listen.empty() || o.world == 0 || o.jwks.empty() ||
|
|
!plain(o.oidc_issuer) || !o.oidc_issuer.starts_with("https://") || !plain(o.audience) ||
|
|
!plain(o.client) || !plain(o.role) || o.assets.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::optional<std::string> read_file(const std::string& path) {
|
|
std::ifstream file{path, std::ios::binary};
|
|
if (!file)
|
|
return std::nullopt;
|
|
std::string body{std::istreambuf_iterator<char>{file}, std::istreambuf_iterator<char>{}};
|
|
if (body.empty() || body.size() > (std::size_t{1} << 20U))
|
|
return std::nullopt;
|
|
return body;
|
|
}
|
|
|
|
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-admin-api (see apps/evo-admin-api/main.cpp)\n", stderr);
|
|
return 2;
|
|
}
|
|
std::signal(SIGTERM, on_stop);
|
|
std::signal(SIGINT, on_stop);
|
|
std::signal(SIGPIPE, SIG_IGN);
|
|
|
|
auto verifier = server::broker::TokenVerifier::from_jwks(
|
|
options->jwks,
|
|
{options->oidc_issuer, options->audience, options->client, 30, options->role});
|
|
if (!verifier) {
|
|
std::puts("error jwks");
|
|
return 2;
|
|
}
|
|
std::vector<server::admin::AdminAsset> assets;
|
|
for (const auto& [name, type] : {std::pair{"index.html", "text/html; charset=utf-8"},
|
|
std::pair{"app.js", "text/javascript; charset=utf-8"},
|
|
std::pair{"style.css", "text/css; charset=utf-8"}}) {
|
|
auto body = read_file(options->assets + "/" + name);
|
|
if (!body) {
|
|
std::printf("error assets %s\n", name);
|
|
return 2;
|
|
}
|
|
assets.push_back({std::string("/") + name, type, std::move(*body)});
|
|
}
|
|
// The realm's origin: sign-in redirects there and the portal exchanges tokens with it.
|
|
const auto origin_end = options->oidc_issuer.find('/', std::string_view{"https://"}.size());
|
|
const auto origin = options->oidc_issuer.substr(0, origin_end);
|
|
server::admin::AdminConfig config;
|
|
config.world = options->world;
|
|
for (const auto& [region, address] : options->regions)
|
|
config.view_regions.push_back(region);
|
|
if (!options->baseline_tuning.empty()) {
|
|
auto baseline = read_file(options->baseline_tuning);
|
|
if (!baseline) {
|
|
std::puts("error baseline-tuning");
|
|
return 2;
|
|
}
|
|
config.baseline_tuning = std::move(*baseline);
|
|
}
|
|
config.portal_config_json = "{\"issuer\":\"" + options->oidc_issuer + "\",\"client_id\":\"" +
|
|
options->client + "\",\"world\":" + std::to_string(options->world) +
|
|
"}\n";
|
|
config.content_security_policy =
|
|
"default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data:; "
|
|
"connect-src 'self' " +
|
|
origin + "; form-action 'self' " + origin + "; frame-ancestors 'none'; base-uri 'none'";
|
|
|
|
auto listening = server::OpsServer::listen(options->listen,
|
|
server::OpsServer::max_configurable_response_bytes);
|
|
if (!listening) {
|
|
std::puts("error listen");
|
|
return 2;
|
|
}
|
|
const auto pg = options->pg;
|
|
const auto regions = options->regions;
|
|
server::admin::AdminApi api{std::move(config), std::move(*verifier),
|
|
[pg]() -> std::unique_ptr<persistence::Postgres> {
|
|
auto db = persistence::Postgres::connect(pg, 3s);
|
|
return db ? std::move(*db) : nullptr;
|
|
},
|
|
[regions](std::uint64_t region) -> std::optional<std::string> {
|
|
const auto found = regions.find(region);
|
|
if (found == regions.end())
|
|
return std::nullopt;
|
|
return server::admin::http_get(
|
|
found->second, "/admin/view", 1500ms,
|
|
server::OpsServer::max_configurable_response_bytes);
|
|
},
|
|
std::move(assets)};
|
|
std::printf("admin api listening %u world %llu\n", static_cast<unsigned>((*listening)->port()),
|
|
static_cast<unsigned long long>(options->world));
|
|
std::fflush(stdout);
|
|
while (stop_requested == 0) {
|
|
(*listening)
|
|
->serve_api(
|
|
[&](const server::ApiRequest& request) {
|
|
return api.handle(request, wall_seconds(), std::chrono::steady_clock::now());
|
|
},
|
|
server::OpsServer::max_api_body_bytes); // Tuning texts (ADR-0182 section 13).
|
|
std::this_thread::sleep_for(5ms);
|
|
}
|
|
std::puts("admin api stopped");
|
|
return 0;
|
|
}
|