Files
dingoandClaude Opus 5.5 9b854e6960
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m31s
Foundation / postgres-tools (push) Successful in 27s
Foundation / server-image (push) Successful in 4m33s
Foundation / image (push) Successful in 5m39s
feat(admin): open a fresh region from the portal (ADR-0182 section 15)
The Regions page opens the next region in place of the one DEV serves: the
admin API reads the region overlay, its kustomization, the grant broker and the
portal deployment from main at one commit, rewrites only the region id, routes,
genesis commit and seeded wildlife range (refusing files whose layout differs),
commits them in one Gitea multi-file commit as aevolution-bot, then starts a full
Argo sync of exactly that commit. The page follows the sync until the region is
live, across the portal's own restart.

Credentials come from Secret aevolution-admin mounted read-only as files
(--deploy-config); without it the feature stays off. A small OpenSSL HTTPS
client talks to Gitea (verified) and argocd-server (verification per
ARGOCD_TLS_VERIFY). The portal's NetworkPolicy gains egress to the node's
ingress for Gitea and to the argocd-server pods, as approved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:51:36 -04:00

262 lines
12 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]
// [--region-tuning F] [--deploy-config DIR]
//
// --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; --region-tuning the region tuning file they start with, shown
// read only. --deploy-config is the mounted Secret holding the Gitea and Argo CD settings and
// tokens (one file per key) that let the portal open a region (ADR-0182 section 15); without
// it, or with a key missing, that feature stays off. 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,
region_tuning, deploy_config;
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 == "--region-tuning")
o.region_tuning = value;
else if (flag == "--deploy-config")
o.deploy_config = 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);
}
if (!options->region_tuning.empty()) {
auto region = read_file(options->region_tuning);
if (!region) {
std::puts("error region-tuning");
return 2;
}
config.region_tuning = std::move(*region);
}
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)};
// ADR-0182 section 15: the portal opens regions only when every deploy key is present.
if (!options->deploy_config.empty()) {
const auto key = [&](const char* name) {
auto text = read_file(options->deploy_config + "/" + name).value_or("");
while (!text.empty() &&
(text.back() == '\n' || text.back() == '\r' || text.back() == ' '))
text.pop_back();
return text;
};
server::admin::AdminDeployConfig deploy{.gitea_base = key("GITEA_BASE_URL"),
.gitea_repository = key("GITEA_REPOSITORY"),
.gitea_token = key("GITEA_TOKEN"),
.branch = "main",
.argo_server = key("ARGOCD_SERVER"),
.argo_application = key("ARGOCD_APPLICATION"),
.argo_token = key("ARGOCD_TOKEN"),
.argo_verify = key("ARGOCD_TLS_VERIFY") != "false"};
if (deploy.gitea_base.empty() || deploy.gitea_repository.empty() ||
deploy.gitea_token.empty() || deploy.argo_server.empty() ||
deploy.argo_application.empty() || deploy.argo_token.empty()) {
std::puts("deploy disabled (incomplete configuration)");
} else {
std::printf("deploy enabled repository %s application %s argo-verify %s\n",
deploy.gitea_repository.c_str(), deploy.argo_application.c_str(),
deploy.argo_verify ? "yes" : "no");
api.enable_deploy(std::move(deploy), server::admin::https_request);
}
}
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;
}