feat(admin): open a fresh region from the portal (ADR-0182 section 15)
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
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
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>
This commit is contained in:
@@ -256,8 +256,8 @@ class JsonParser {
|
||||
std::size_t at_{};
|
||||
};
|
||||
|
||||
std::optional<JsonValue> JsonValue::parse(std::string_view text) {
|
||||
if (text.size() > max_json_bytes)
|
||||
std::optional<JsonValue> JsonValue::parse(std::string_view text, std::size_t limit) {
|
||||
if (text.size() > limit)
|
||||
return std::nullopt;
|
||||
return JsonParser{text}.document();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "evo/server/admin/admin_api.hpp"
|
||||
#include "evo/serialization/json.hpp"
|
||||
#include "evo/server/admin/region_switch.hpp"
|
||||
|
||||
#include "evo/biology/cellular_revamp_tuning.hpp"
|
||||
#include "evo/persistence/sha256.hpp"
|
||||
@@ -9,10 +11,14 @@
|
||||
#include <array>
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <cstdio>
|
||||
#include <expected>
|
||||
#include <initializer_list>
|
||||
#include <netdb.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <poll.h>
|
||||
#include <span>
|
||||
#include <sys/random.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -288,7 +294,7 @@ OpsResponse AdminApi::handle(const ApiRequest& request, std::uint64_t now_second
|
||||
// only tuning takes a request body.
|
||||
if (request.method != "GET" &&
|
||||
!(request.method == "POST" &&
|
||||
(path == "/api/tuning" ||
|
||||
(path == "/api/tuning" || path == "/api/deploy/region" ||
|
||||
(path.starts_with("/api/regions/") &&
|
||||
(path.ends_with("/session-window") || path.ends_with("/commands") ||
|
||||
path.ends_with("/schedules"))) ||
|
||||
@@ -350,6 +356,10 @@ OpsResponse AdminApi::api(std::string_view method, std::string_view path, std::s
|
||||
}
|
||||
if (path == "/api/regions")
|
||||
return regions();
|
||||
if (path == "/api/deploy")
|
||||
return method == "GET" ? deploy_status(query) : error(405, "method_not_allowed");
|
||||
if (path == "/api/deploy/region")
|
||||
return method == "POST" ? open_region(query, admin) : error(405, "method_not_allowed");
|
||||
if (path == "/api/tuning")
|
||||
return method == "POST" ? set_tuning(query, body, admin) : tuning();
|
||||
constexpr std::string_view tuning_prefix = "/api/tuning/";
|
||||
@@ -682,8 +692,305 @@ std::expected<std::string, OpsResponse> apply_tuning_changes(std::string_view ba
|
||||
}
|
||||
return text;
|
||||
}
|
||||
// ---------- ADR-0182 section 15: opening a region through Gitea and Argo CD ----------
|
||||
std::string base64(std::string_view bytes) {
|
||||
std::string out(4 * ((bytes.size() + 2) / 3) + 1, '\0');
|
||||
const auto written = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(out.data()),
|
||||
reinterpret_cast<const unsigned char*>(bytes.data()),
|
||||
static_cast<int>(bytes.size()));
|
||||
out.resize(static_cast<std::size_t>(written));
|
||||
return out;
|
||||
}
|
||||
// One call to a service: its host, its path prefix plus `target`, the credential header.
|
||||
std::optional<HttpsResponse> call(const HttpsTransport& transport, const HttpsBase& base,
|
||||
bool verify, std::string method, const std::string& target,
|
||||
std::string authorization, std::string body = {},
|
||||
std::size_t limit = std::size_t{4} << 20U) {
|
||||
HttpsRequest request;
|
||||
request.host = base.host;
|
||||
request.port = base.port;
|
||||
request.method = std::move(method);
|
||||
request.target = base.prefix + target;
|
||||
request.verify = verify;
|
||||
request.limit = limit;
|
||||
// The serve loop is single-threaded: a stalled service must not outlast the liveness probe.
|
||||
request.timeout = std::chrono::seconds{8};
|
||||
request.headers = {{"Authorization", std::move(authorization)}, {"Accept", "application/json"}};
|
||||
if (!body.empty())
|
||||
request.headers.emplace_back("Content-Type", "application/json");
|
||||
request.body = std::move(body);
|
||||
return transport(request);
|
||||
}
|
||||
const serialization::JsonValue* walk(const serialization::JsonValue* value,
|
||||
std::initializer_list<std::string_view> names) {
|
||||
for (const auto name : names)
|
||||
value = value ? value->member(name) : nullptr;
|
||||
return value;
|
||||
}
|
||||
std::string text_of(const serialization::JsonValue* value) {
|
||||
const auto text = value ? value->string() : std::nullopt;
|
||||
return text ? std::string(*text) : std::string{};
|
||||
}
|
||||
bool hex_text(std::string_view text, std::size_t size) {
|
||||
return text.size() == size && std::ranges::all_of(text, [](char c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
|
||||
});
|
||||
}
|
||||
// Main's head and the four switch files at that commit, with each file's blob sha.
|
||||
struct GitSnapshot {
|
||||
std::string head;
|
||||
RegionSwitchFiles files;
|
||||
std::array<std::string, 4> shas;
|
||||
};
|
||||
std::expected<GitSnapshot, std::string> git_snapshot(const AdminDeployConfig& config,
|
||||
const HttpsTransport& transport) {
|
||||
const auto base = parse_https_base(config.gitea_base);
|
||||
if (!base)
|
||||
return std::unexpected{"gitea_base"};
|
||||
const auto auth = "token " + config.gitea_token;
|
||||
const auto repo = "/api/v1/repos/" + config.gitea_repository;
|
||||
const auto branch =
|
||||
call(transport, *base, true, "GET", repo + "/branches/" + config.branch, auth);
|
||||
const auto branch_json = branch && branch->status == 200
|
||||
? serialization::JsonValue::parse(branch->body, 1U << 20U)
|
||||
: std::nullopt;
|
||||
GitSnapshot snapshot;
|
||||
snapshot.head = text_of(walk(branch_json ? &*branch_json : nullptr, {"commit", "id"}));
|
||||
if (!hex_text(snapshot.head, 40))
|
||||
return std::unexpected{"gitea_branch"};
|
||||
std::map<std::string, std::optional<serialization::JsonValue>> listings;
|
||||
for (std::size_t i = 0; i < region_switch_paths.size(); ++i) {
|
||||
const std::string path(region_switch_paths.at(i));
|
||||
std::string raw_target = repo;
|
||||
raw_target.append("/raw/").append(path).append("?ref=").append(snapshot.head);
|
||||
const auto raw = call(transport, *base, true, "GET", raw_target, auth);
|
||||
if (!raw || raw->status != 200)
|
||||
return std::unexpected{"gitea_raw " + path};
|
||||
snapshot.files.at(i) = raw->body;
|
||||
const auto slash = path.rfind('/');
|
||||
const auto dir = path.substr(0, slash);
|
||||
if (!listings.contains(dir)) {
|
||||
std::string list_target = repo;
|
||||
list_target.append("/contents/").append(dir).append("?ref=").append(snapshot.head);
|
||||
const auto listed = call(transport, *base, true, "GET", list_target, auth);
|
||||
listings[dir] = listed && listed->status == 200
|
||||
? serialization::JsonValue::parse(listed->body, 1U << 20U)
|
||||
: std::nullopt;
|
||||
}
|
||||
const auto& listing = listings[dir];
|
||||
const auto* entries = listing ? listing->array() : nullptr;
|
||||
for (const auto& entry : entries ? *entries : std::vector<serialization::JsonValue>{})
|
||||
if (text_of(entry.member("name")) == path.substr(slash + 1))
|
||||
snapshot.shas.at(i) = text_of(entry.member("sha"));
|
||||
if (!hex_text(snapshot.shas.at(i), 40))
|
||||
return std::unexpected{"gitea_contents " + dir};
|
||||
}
|
||||
return snapshot;
|
||||
}
|
||||
// The Argo Application's last operation and state.
|
||||
struct ArgoStatus {
|
||||
std::string phase, message, sync, revision, health;
|
||||
};
|
||||
std::expected<ArgoStatus, std::string> argo_status(const AdminDeployConfig& config,
|
||||
const HttpsTransport& transport) {
|
||||
const auto base = parse_https_base(config.argo_server);
|
||||
if (!base)
|
||||
return std::unexpected{"argo_server"};
|
||||
constexpr std::size_t limit = std::size_t{16} << 20U;
|
||||
// Only the fields shown; an Argo CD without field selection answers in full.
|
||||
const auto answer =
|
||||
call(transport, *base, config.argo_verify, "GET",
|
||||
"/api/v1/applications?name=" + config.argo_application +
|
||||
"&fields=items.metadata.name,items.status.sync,items.status.health,"
|
||||
"items.status.operationState.phase,items.status.operationState.message",
|
||||
"Bearer " + config.argo_token, {}, limit);
|
||||
if (!answer || answer->status != 200)
|
||||
return std::unexpected{answer ? "argo_status " + std::to_string(answer->status)
|
||||
: std::string{"argo_unreachable"}};
|
||||
const auto parsed = serialization::JsonValue::parse(answer->body, limit);
|
||||
const auto* items = parsed ? walk(&*parsed, {"items"}) : nullptr;
|
||||
for (const auto& item :
|
||||
items && items->array() ? *items->array() : std::vector<serialization::JsonValue>{}) {
|
||||
if (text_of(walk(&item, {"metadata", "name"})) != config.argo_application)
|
||||
continue;
|
||||
return ArgoStatus{text_of(walk(&item, {"status", "operationState", "phase"})),
|
||||
text_of(walk(&item, {"status", "operationState", "message"})),
|
||||
text_of(walk(&item, {"status", "sync", "status"})),
|
||||
text_of(walk(&item, {"status", "sync", "revision"})),
|
||||
text_of(walk(&item, {"status", "health", "status"}))};
|
||||
}
|
||||
return std::unexpected{"argo_application"};
|
||||
}
|
||||
std::string argo_json(const std::expected<ArgoStatus, std::string>& argo) {
|
||||
if (!argo)
|
||||
return "{\"error\":" + json_string(argo.error()) + "}";
|
||||
return "{\"phase\":" + json_string(argo->phase) + ",\"message\":" + json_string(argo->message) +
|
||||
",\"sync\":" + json_string(argo->sync) + ",\"revision\":" + json_string(argo->revision) +
|
||||
",\"health\":" + json_string(argo->health) + "}";
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void AdminApi::enable_deploy(AdminDeployConfig config, HttpsTransport transport) {
|
||||
deploy_ = std::move(config);
|
||||
https_ = std::move(transport);
|
||||
}
|
||||
|
||||
std::optional<std::uint64_t> AdminApi::next_region(std::uint64_t served) {
|
||||
const auto rows = query("SELECT max(region_id)::text FROM evolution.admin_regions_v1($1)",
|
||||
{std::to_string(config_.world)});
|
||||
if (!rows)
|
||||
return std::nullopt;
|
||||
const auto highest =
|
||||
rows->empty() ? std::uint64_t{0} : number(at(rows->front(), 0).value_or("0")).value_or(0);
|
||||
return std::max(highest, served) + 1;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// Seeded wildlife per opening (playtest.starting_wildlife_cells of the baseline file).
|
||||
std::uint64_t seeded_wildlife(const std::string& baseline) {
|
||||
const auto parsed = biology::parse_cellular_revamp_tuning(baseline);
|
||||
return parsed ? static_cast<std::uint64_t>(parsed->playtest_starting_wildlife_cells) : 0;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
OpsResponse AdminApi::deploy_status(std::string_view query_text) {
|
||||
if (!deploy_ || !https_)
|
||||
return json(200, "{\"configured\":false}\n");
|
||||
const auto argo = argo_status(*deploy_, https_);
|
||||
std::string out =
|
||||
"{\"configured\":true,\"repository\":" + json_string(deploy_->gitea_repository) +
|
||||
",\"branch\":" + json_string(deploy_->branch) +
|
||||
",\"application\":" + json_string(deploy_->argo_application) +
|
||||
",\"argo\":" + argo_json(argo);
|
||||
if (query_value(query_text, "argo", 1) == "1")
|
||||
return json(200, out + "}\n");
|
||||
const auto snapshot = git_snapshot(*deploy_, https_);
|
||||
if (!snapshot)
|
||||
return json(200, out + ",\"git\":{\"error\":" + json_string(snapshot.error()) + "}}\n");
|
||||
out += ",\"git\":{\"head\":" + json_string(snapshot->head) + "}";
|
||||
const auto layout = read_region_layout(snapshot->files);
|
||||
if (!layout)
|
||||
return json(200, out + ",\"layout_error\":" + json_string(layout.error()) + "}\n");
|
||||
const auto count = seeded_wildlife(config_.baseline_tuning);
|
||||
const auto next = next_region(layout->region);
|
||||
out += ",\"current\":{\"world\":\"" + std::to_string(layout->world) + "\",\"region\":\"" +
|
||||
std::to_string(layout->region) + "\",\"commit\":" + json_string(layout->commit) +
|
||||
",\"wildlife_first\":\"" + std::to_string(layout->wildlife_first) + "\"}";
|
||||
if (next)
|
||||
out += ",\"next\":{\"region\":\"" + std::to_string(*next) + "\",\"wildlife_first\":\"" +
|
||||
std::to_string(layout->wildlife_first + count) +
|
||||
"\",\"wildlife_count\":" + std::to_string(count) + "}";
|
||||
return json(200,
|
||||
out + (layout->world == config_.world ? "" : ",\"world_mismatch\":true") + "}\n");
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::open_region(std::string_view query_text,
|
||||
const broker::AccessTokenClaims& admin) {
|
||||
if (!deploy_ || !https_)
|
||||
return error(503, "deploy_not_configured");
|
||||
const auto wanted = number(query_value(query_text, "region", 20).value_or(""));
|
||||
const auto reason = query_value(query_text, "reason");
|
||||
if (!wanted || *wanted == 0 ||
|
||||
(!reason && query_text.find("reason=") != std::string_view::npos))
|
||||
return error(400, "bad_request");
|
||||
const auto argo = argo_status(*deploy_, https_);
|
||||
if (!argo)
|
||||
return json(503, "{\"error\":\"argo_unavailable\",\"detail\":" + json_string(argo.error()) +
|
||||
"}\n");
|
||||
if (argo->phase == "Running")
|
||||
return error(409, "sync_running");
|
||||
const auto snapshot = git_snapshot(*deploy_, https_);
|
||||
if (!snapshot)
|
||||
return json(503, "{\"error\":\"gitea_unavailable\",\"detail\":" +
|
||||
json_string(snapshot.error()) + "}\n");
|
||||
const auto layout = read_region_layout(snapshot->files);
|
||||
if (!layout)
|
||||
return json(409, "{\"error\":\"layout_changed\",\"detail\":" + json_string(layout.error()) +
|
||||
"}\n");
|
||||
if (layout->world != config_.world)
|
||||
return error(409, "world_mismatch");
|
||||
const auto next = next_region(layout->region);
|
||||
if (!next)
|
||||
return error(503, "unavailable");
|
||||
if (*wanted != *next)
|
||||
return json(409, "{\"error\":\"region_changed\",\"expected\":\"" + std::to_string(*next) +
|
||||
"\"}\n");
|
||||
std::array<unsigned char, 16> random{};
|
||||
if (::getrandom(random.data(), random.size(), 0) != static_cast<ssize_t>(random.size()))
|
||||
return error(503, "unavailable");
|
||||
const auto count = seeded_wildlife(config_.baseline_tuning);
|
||||
const RegionLayout to{layout->world, *next, hex(random), layout->wildlife_first + count};
|
||||
const auto last = to.wildlife_first + (count == 0 ? 0 : count - 1);
|
||||
const auto author = author_of(admin);
|
||||
const auto today = std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now());
|
||||
const std::chrono::year_month_day date{today};
|
||||
const auto day = std::to_string(static_cast<int>(date.year())) + "-" +
|
||||
(static_cast<unsigned>(date.month()) < 10 ? "0" : "") +
|
||||
std::to_string(static_cast<unsigned>(date.month())) + "-" +
|
||||
(static_cast<unsigned>(date.day()) < 10 ? "0" : "") +
|
||||
std::to_string(static_cast<unsigned>(date.day()));
|
||||
const auto range = count == 0 ? std::string{"no seeded wildlife"}
|
||||
: "seeded wildlife " + std::to_string(to.wildlife_first) + "-" +
|
||||
std::to_string(last);
|
||||
const auto switched =
|
||||
switch_region(snapshot->files, to,
|
||||
"region " + std::to_string(to.region) + " opened " + day + " in place of " +
|
||||
std::to_string(layout->region) + "; " + range + ".");
|
||||
if (!switched)
|
||||
return json(409, "{\"error\":\"layout_changed\",\"detail\":" +
|
||||
json_string(switched.error()) + "}\n");
|
||||
const auto message = "deploy(dev): open region " + std::to_string(to.region) +
|
||||
" from the admin portal\n\nWorld " + std::to_string(to.world) +
|
||||
" serves region " + std::to_string(to.region) + " instead of " +
|
||||
std::to_string(layout->region) + " (ADR-0182 section 15): " + range +
|
||||
", genesis commit " + to.commit + ". Earlier regions stay in the stores." +
|
||||
"\n\nRequested by " + author +
|
||||
(reason && !reason->empty() ? ": " + *reason : std::string{}) + "\n";
|
||||
std::string files;
|
||||
for (std::size_t i = 0; i < region_switch_paths.size(); ++i) {
|
||||
if (switched->at(i) == snapshot->files.at(i))
|
||||
continue;
|
||||
files += std::string(files.empty() ? "" : ",") +
|
||||
"{\"operation\":\"update\",\"path\":" + json_string(region_switch_paths.at(i)) +
|
||||
",\"content\":\"" + base64(switched->at(i)) + "\",\"sha\":\"" +
|
||||
snapshot->shas.at(i) + "\"}";
|
||||
}
|
||||
const auto gitea = parse_https_base(deploy_->gitea_base);
|
||||
const auto argo_base = parse_https_base(deploy_->argo_server);
|
||||
if (!gitea || !argo_base)
|
||||
return error(503, "deploy_not_configured");
|
||||
const auto committed = call(
|
||||
https_, *gitea, true, "POST", "/api/v1/repos/" + deploy_->gitea_repository + "/contents",
|
||||
"token " + deploy_->gitea_token,
|
||||
"{\"branch\":" + json_string(deploy_->branch) + ",\"message\":" + json_string(message) +
|
||||
",\"files\":[" + files + "]}");
|
||||
if (!committed || (committed->status != 200 && committed->status != 201))
|
||||
return json(committed && committed->status < 500 ? 409 : 503,
|
||||
"{\"error\":\"commit_refused\",\"status\":" +
|
||||
std::to_string(committed ? committed->status : 0) + "}\n");
|
||||
const auto commit_json = serialization::JsonValue::parse(committed->body, 1U << 20U);
|
||||
const auto commit = text_of(walk(commit_json ? &*commit_json : nullptr, {"commit", "sha"}));
|
||||
if (!hex_text(commit, 40))
|
||||
return error(503, "commit_unreadable");
|
||||
std::printf("admin deploy region=%llu from=%llu commit=%s by %s\n",
|
||||
static_cast<unsigned long long>(to.region),
|
||||
static_cast<unsigned long long>(layout->region), commit.c_str(), author.c_str());
|
||||
std::fflush(stdout);
|
||||
// A full sync of exactly that commit: the provision hook opens the region first.
|
||||
const auto synced =
|
||||
call(https_, *argo_base, deploy_->argo_verify, "POST",
|
||||
"/api/v1/applications/" + deploy_->argo_application + "/sync",
|
||||
"Bearer " + deploy_->argo_token, "{\"revision\":\"" + commit + "\",\"prune\":true}");
|
||||
const bool started = synced && synced->status == 200;
|
||||
return json(200,
|
||||
"{\"region\":\"" + std::to_string(to.region) + "\",\"from\":\"" +
|
||||
std::to_string(layout->region) + "\",\"commit\":\"" + commit +
|
||||
"\",\"sync_started\":" + (started ? "true" : "false") +
|
||||
(started ? std::string{}
|
||||
: ",\"sync_status\":" + std::to_string(synced ? synced->status : 0)) +
|
||||
"}\n");
|
||||
}
|
||||
|
||||
OpsResponse AdminApi::commands(std::uint64_t region) {
|
||||
const auto rows =
|
||||
query("SELECT command_id, kind, cell_count, target, energy, carbon, nitrogen, phosphorus, "
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
#include "evo/server/admin/https_client.hpp"
|
||||
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/x509v3.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <charconv>
|
||||
#include <memory>
|
||||
#include <netdb.h>
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace evo::server::admin {
|
||||
namespace {
|
||||
using Clock = std::chrono::steady_clock;
|
||||
|
||||
struct Socket {
|
||||
int fd{-1};
|
||||
Socket(const Socket&) = delete;
|
||||
Socket& operator=(const Socket&) = delete;
|
||||
explicit Socket(int value) : fd{value} {}
|
||||
~Socket() {
|
||||
if (fd >= 0)
|
||||
::close(fd);
|
||||
}
|
||||
};
|
||||
|
||||
bool wait(int fd, short events, Clock::time_point deadline) {
|
||||
for (;;) {
|
||||
const auto left =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(deadline - Clock::now());
|
||||
if (left.count() <= 0)
|
||||
return false;
|
||||
pollfd entry{fd, events, 0};
|
||||
const int ready = ::poll(&entry, 1, static_cast<int>(left.count()));
|
||||
if (ready > 0)
|
||||
return true;
|
||||
if (ready < 0 && errno != EINTR)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int connect_to(const std::string& host, std::uint16_t port, Clock::time_point deadline) {
|
||||
addrinfo hints{};
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
addrinfo* found = nullptr;
|
||||
const auto service = std::to_string(port);
|
||||
if (::getaddrinfo(host.c_str(), service.c_str(), &hints, &found) != 0 || found == nullptr)
|
||||
return -1;
|
||||
const std::unique_ptr<addrinfo, decltype(&::freeaddrinfo)> results{found, ::freeaddrinfo};
|
||||
for (const addrinfo* candidate = found; candidate != nullptr; candidate = candidate->ai_next) {
|
||||
const int fd =
|
||||
::socket(candidate->ai_family, candidate->ai_socktype | SOCK_NONBLOCK | SOCK_CLOEXEC,
|
||||
candidate->ai_protocol);
|
||||
if (fd < 0)
|
||||
continue;
|
||||
Socket socket{fd};
|
||||
if (::connect(fd, candidate->ai_addr, candidate->ai_addrlen) != 0) {
|
||||
int failure = 0;
|
||||
socklen_t length = sizeof failure;
|
||||
if (errno != EINPROGRESS || !wait(fd, POLLOUT, deadline) ||
|
||||
::getsockopt(fd, SOL_SOCKET, SO_ERROR, &failure, &length) != 0 || failure != 0)
|
||||
continue;
|
||||
}
|
||||
socket.fd = -1; // Handed to the caller.
|
||||
return fd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Waits for the TLS operation that just returned `result` to become possible again.
|
||||
bool retry(SSL* ssl, int result, Clock::time_point deadline) {
|
||||
const int fd = SSL_get_fd(ssl);
|
||||
switch (SSL_get_error(ssl, result)) {
|
||||
case SSL_ERROR_WANT_READ:
|
||||
return wait(fd, POLLIN, deadline);
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
return wait(fd, POLLOUT, deadline);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string lower(std::string_view text) {
|
||||
std::string out(text);
|
||||
for (auto& ch : out)
|
||||
ch = static_cast<char>(ch >= 'A' && ch <= 'Z' ? ch - 'A' + 'a' : ch);
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::optional<HttpsResponse> parse_http_response(std::string_view raw, std::size_t limit) {
|
||||
const auto head_end = raw.find("\r\n\r\n");
|
||||
if (head_end == std::string_view::npos || !raw.starts_with("HTTP/1."))
|
||||
return std::nullopt;
|
||||
const auto line_end = raw.find("\r\n");
|
||||
const auto space = raw.find(' ');
|
||||
if (space == std::string_view::npos || space + 4 > line_end)
|
||||
return std::nullopt;
|
||||
HttpsResponse response;
|
||||
const auto code = raw.substr(space + 1, 3);
|
||||
if (std::from_chars(code.data(), code.data() + code.size(), response.status).ec !=
|
||||
std::errc{} ||
|
||||
response.status < 100 || response.status > 599)
|
||||
return std::nullopt;
|
||||
std::optional<std::size_t> length;
|
||||
bool chunked = false;
|
||||
for (auto at = line_end + 2; at < head_end;) {
|
||||
const auto end = raw.find("\r\n", at);
|
||||
const auto header = raw.substr(at, end - at);
|
||||
at = end + 2;
|
||||
const auto colon = header.find(':');
|
||||
if (colon == std::string_view::npos)
|
||||
continue;
|
||||
const auto name = lower(header.substr(0, colon));
|
||||
auto value = header.substr(colon + 1);
|
||||
while (!value.empty() && value.front() == ' ')
|
||||
value.remove_prefix(1);
|
||||
if (name == "content-length") {
|
||||
std::size_t bytes{};
|
||||
if (std::from_chars(value.data(), value.data() + value.size(), bytes).ec != std::errc{})
|
||||
return std::nullopt;
|
||||
length = bytes;
|
||||
} else if (name == "transfer-encoding") {
|
||||
chunked = lower(value).find("chunked") != std::string::npos;
|
||||
}
|
||||
}
|
||||
auto body = raw.substr(head_end + 4);
|
||||
if (chunked) {
|
||||
std::string out;
|
||||
for (;;) {
|
||||
const auto end = body.find("\r\n");
|
||||
if (end == std::string_view::npos)
|
||||
return std::nullopt;
|
||||
std::size_t size{};
|
||||
const auto digits = body.substr(0, std::min(end, body.find(';')));
|
||||
if (std::from_chars(digits.data(), digits.data() + digits.size(), size, 16).ec !=
|
||||
std::errc{})
|
||||
return std::nullopt;
|
||||
body.remove_prefix(end + 2);
|
||||
if (size == 0)
|
||||
break;
|
||||
if (body.size() < size + 2 || body.substr(size, 2) != "\r\n" ||
|
||||
out.size() + size > limit)
|
||||
return std::nullopt;
|
||||
out.append(body.substr(0, size));
|
||||
body.remove_prefix(size + 2);
|
||||
}
|
||||
response.body = std::move(out);
|
||||
} else {
|
||||
if (length && *length > body.size())
|
||||
return std::nullopt;
|
||||
response.body = std::string(length ? body.substr(0, *length) : body);
|
||||
}
|
||||
if (response.body.size() > limit)
|
||||
return std::nullopt;
|
||||
return response;
|
||||
}
|
||||
|
||||
std::optional<HttpsBase> parse_https_base(std::string_view url) {
|
||||
if (url.starts_with("https://"))
|
||||
url.remove_prefix(8);
|
||||
else if (url.find("://") != std::string_view::npos)
|
||||
return std::nullopt;
|
||||
const auto slash = url.find('/');
|
||||
auto authority = url.substr(0, slash);
|
||||
auto prefix = slash == std::string_view::npos ? std::string_view{} : url.substr(slash);
|
||||
while (!prefix.empty() && prefix.back() == '/')
|
||||
prefix.remove_suffix(1);
|
||||
HttpsBase base;
|
||||
const auto colon = authority.rfind(':');
|
||||
if (colon != std::string_view::npos) {
|
||||
const auto port = authority.substr(colon + 1);
|
||||
if (std::from_chars(port.data(), port.data() + port.size(), base.port).ec != std::errc{} ||
|
||||
base.port == 0)
|
||||
return std::nullopt;
|
||||
authority = authority.substr(0, colon);
|
||||
}
|
||||
if (authority.empty())
|
||||
return std::nullopt;
|
||||
base.host = std::string(authority);
|
||||
base.prefix = std::string(prefix);
|
||||
return base;
|
||||
}
|
||||
|
||||
std::optional<HttpsResponse> https_request(const HttpsRequest& request) {
|
||||
const auto deadline = Clock::now() + request.timeout;
|
||||
const int fd = connect_to(request.host, request.port, deadline);
|
||||
if (fd < 0)
|
||||
return std::nullopt;
|
||||
const Socket socket{fd};
|
||||
const std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> context{
|
||||
SSL_CTX_new(TLS_client_method()), SSL_CTX_free};
|
||||
if (!context || SSL_CTX_set_min_proto_version(context.get(), TLS1_2_VERSION) != 1)
|
||||
return std::nullopt;
|
||||
if (request.verify) {
|
||||
if (SSL_CTX_set_default_verify_paths(context.get()) != 1)
|
||||
return std::nullopt;
|
||||
SSL_CTX_set_verify(context.get(), SSL_VERIFY_PEER, nullptr);
|
||||
} else {
|
||||
SSL_CTX_set_verify(context.get(), SSL_VERIFY_NONE, nullptr);
|
||||
}
|
||||
const std::unique_ptr<SSL, decltype(&SSL_free)> ssl{SSL_new(context.get()), SSL_free};
|
||||
if (!ssl || SSL_set_fd(ssl.get(), fd) != 1 ||
|
||||
SSL_set_tlsext_host_name(ssl.get(), request.host.c_str()) != 1 ||
|
||||
(request.verify && SSL_set1_host(ssl.get(), request.host.c_str()) != 1))
|
||||
return std::nullopt;
|
||||
for (;;) {
|
||||
const int done = SSL_connect(ssl.get());
|
||||
if (done == 1)
|
||||
break;
|
||||
if (!retry(ssl.get(), done, deadline))
|
||||
return std::nullopt;
|
||||
}
|
||||
std::string message =
|
||||
request.method + " " + request.target + " HTTP/1.1\r\nHost: " + request.host +
|
||||
"\r\nConnection: close\r\nContent-Length: " + std::to_string(request.body.size()) + "\r\n";
|
||||
for (const auto& [name, value] : request.headers)
|
||||
message.append(name).append(": ").append(value).append("\r\n");
|
||||
message += "\r\n" + request.body;
|
||||
for (std::size_t sent = 0; sent < message.size();) {
|
||||
std::size_t wrote = 0;
|
||||
const int done =
|
||||
SSL_write_ex(ssl.get(), message.data() + sent, message.size() - sent, &wrote);
|
||||
if (done == 1)
|
||||
sent += wrote;
|
||||
else if (!retry(ssl.get(), done, deadline))
|
||||
return std::nullopt;
|
||||
}
|
||||
constexpr std::size_t header_room = 64U << 10U;
|
||||
std::string raw;
|
||||
for (;;) {
|
||||
char chunk[16384];
|
||||
std::size_t got = 0;
|
||||
const int done = SSL_read_ex(ssl.get(), chunk, sizeof chunk, &got);
|
||||
if (done == 1) {
|
||||
raw.append(chunk, got);
|
||||
if (raw.size() > request.limit + header_room)
|
||||
return std::nullopt;
|
||||
continue;
|
||||
}
|
||||
const int error = SSL_get_error(ssl.get(), done);
|
||||
if (error == SSL_ERROR_ZERO_RETURN ||
|
||||
(error == SSL_ERROR_SYSCALL && ERR_peek_error() == 0) ||
|
||||
(error == SSL_ERROR_SSL &&
|
||||
ERR_GET_REASON(ERR_peek_error()) == SSL_R_UNEXPECTED_EOF_WHILE_READING))
|
||||
break; // The server closed the connection: the answer is complete.
|
||||
if (!retry(ssl.get(), done, deadline))
|
||||
return std::nullopt;
|
||||
}
|
||||
ERR_clear_error();
|
||||
return parse_http_response(raw, request.limit);
|
||||
}
|
||||
} // namespace evo::server::admin
|
||||
@@ -0,0 +1,157 @@
|
||||
#include "evo/server/admin/region_switch.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
namespace evo::server::admin {
|
||||
namespace {
|
||||
bool any_value(std::string_view /*value*/) {
|
||||
return true;
|
||||
}
|
||||
bool hex32(std::string_view value) {
|
||||
return value.size() == 32 && std::ranges::all_of(value, [](char c) {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
|
||||
});
|
||||
}
|
||||
// Every value following `prefix`, up to (not including) the first character in `stops`, that
|
||||
// `wanted` accepts (a comment can mention a flag without a value).
|
||||
std::vector<std::pair<std::size_t, std::string_view>>
|
||||
values_after(std::string_view text, std::string_view prefix, std::string_view stops,
|
||||
bool (*wanted)(std::string_view) = any_value) {
|
||||
std::vector<std::pair<std::size_t, std::string_view>> found;
|
||||
for (auto at = text.find(prefix); at != std::string_view::npos;
|
||||
at = text.find(prefix, at + 1)) {
|
||||
const auto first = at + prefix.size();
|
||||
const auto end = std::min(text.find_first_of(stops, first), text.size());
|
||||
if (wanted(text.substr(first, end - first)))
|
||||
found.emplace_back(first, text.substr(first, end - first));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
std::optional<std::uint64_t> number(std::string_view text) {
|
||||
std::uint64_t value{};
|
||||
if (text.empty() ||
|
||||
std::from_chars(text.data(), text.data() + text.size(), value).ec != std::errc{})
|
||||
return std::nullopt;
|
||||
return value;
|
||||
}
|
||||
// The one value after `prefix` (all of `count` occurrences equal), or nullopt.
|
||||
std::optional<std::string_view> single(std::string_view text, std::string_view prefix,
|
||||
std::string_view stops, std::size_t count = 1,
|
||||
bool (*wanted)(std::string_view) = any_value) {
|
||||
const auto found = values_after(text, prefix, stops, wanted);
|
||||
if (found.size() != count ||
|
||||
!std::ranges::all_of(found, [&](const auto& v) { return v.second == found[0].second; }))
|
||||
return std::nullopt;
|
||||
return found[0].second;
|
||||
}
|
||||
// `text` with every value after `prefix` replaced by `value` (back to front, so positions hold).
|
||||
std::string replace_after(std::string text, std::string_view prefix, std::string_view stops,
|
||||
std::string_view value, bool (*wanted)(std::string_view) = any_value) {
|
||||
auto found = values_after(text, prefix, stops, wanted);
|
||||
std::ranges::reverse(found);
|
||||
for (const auto& [at, old] : found)
|
||||
text.replace(at, old.size(), value);
|
||||
return text;
|
||||
}
|
||||
|
||||
constexpr std::string_view world_key = "{name: REGION_WORLD, value: \"";
|
||||
constexpr std::string_view region_key = "{name: REGION_ID, value: \"";
|
||||
constexpr std::string_view routes_key = "{name: REGION_LIFE_ROUTES, value: \"";
|
||||
constexpr std::string_view commit_key = "--commit ";
|
||||
constexpr std::string_view wildlife_key = "--playtest-wildlife-first-id ";
|
||||
constexpr std::string_view header_key = "# Region host for evolution-dev: world ";
|
||||
constexpr std::string_view broker_region_key = "{name: BROKER_DEFAULT_REGION, value: \"";
|
||||
constexpr std::string_view broker_routes_key = "{name: BROKER_ROUTES, value: \"";
|
||||
constexpr std::string_view portal_regions_key = "{name: ADMIN_REGIONS, value: \"";
|
||||
constexpr std::string_view opening_key = " - {name: REGION_OPENING, value: ";
|
||||
constexpr std::string_view note_prefix = " # Admin portal (ADR-0182 section 15): ";
|
||||
} // namespace
|
||||
|
||||
std::expected<RegionLayout, std::string> read_region_layout(const RegionSwitchFiles& files) {
|
||||
const auto& [values, kustomization, broker, portal] = files;
|
||||
RegionLayout layout;
|
||||
// The Deployment and the provision Job each name the world and region.
|
||||
const auto world = single(values, world_key, "\"", 2);
|
||||
const auto region = single(values, region_key, "\"", 2);
|
||||
const auto world_id = world ? number(*world) : std::nullopt;
|
||||
const auto region_id = region ? number(*region) : std::nullopt;
|
||||
if (!world_id)
|
||||
return std::unexpected{"REGION_WORLD"};
|
||||
if (!region_id)
|
||||
return std::unexpected{"REGION_ID"};
|
||||
layout.world = *world_id;
|
||||
layout.region = *region_id;
|
||||
// Canonical numbers only, so every other file can be matched by the same text.
|
||||
const auto id = std::to_string(layout.region);
|
||||
if (*region != id || *world != std::to_string(layout.world))
|
||||
return std::unexpected{"REGION_ID"};
|
||||
const auto routes = single(values, routes_key, "=");
|
||||
if (!routes || *routes != id)
|
||||
return std::unexpected{"REGION_LIFE_ROUTES"};
|
||||
const auto commit = single(values, commit_key, " \n", 1, hex32);
|
||||
if (!commit)
|
||||
return std::unexpected{"--commit"};
|
||||
layout.commit = std::string(*commit);
|
||||
const auto wildlife = single(values, wildlife_key, " \n");
|
||||
const auto wildlife_first = wildlife ? number(*wildlife) : std::nullopt;
|
||||
if (!wildlife_first)
|
||||
return std::unexpected{"--playtest-wildlife-first-id"};
|
||||
layout.wildlife_first = *wildlife_first;
|
||||
if (values.find(opening_key) == std::string::npos ||
|
||||
values.find(opening_key) != values.rfind(opening_key))
|
||||
return std::unexpected{"REGION_OPENING"};
|
||||
const auto header = single(kustomization, header_key, "\n");
|
||||
if (!header || !header->starts_with(std::to_string(layout.world) + ", region " + id + ","))
|
||||
return std::unexpected{"kustomization header"};
|
||||
const auto broker_region = single(broker, broker_region_key, "\"");
|
||||
if (!broker_region || *broker_region != id)
|
||||
return std::unexpected{"BROKER_DEFAULT_REGION"};
|
||||
const auto broker_routes = single(broker, broker_routes_key, "=");
|
||||
if (!broker_routes || *broker_routes != id)
|
||||
return std::unexpected{"BROKER_ROUTES"};
|
||||
const auto portal_regions = single(portal, portal_regions_key, "=");
|
||||
if (!portal_regions || *portal_regions != id)
|
||||
return std::unexpected{"ADMIN_REGIONS"};
|
||||
return layout;
|
||||
}
|
||||
|
||||
std::expected<RegionSwitchFiles, std::string>
|
||||
switch_region(const RegionSwitchFiles& files, const RegionLayout& to, std::string_view note) {
|
||||
const auto from = read_region_layout(files);
|
||||
if (!from)
|
||||
return std::unexpected{from.error()};
|
||||
if (to.world != from->world || to.region == 0 || to.region == from->region ||
|
||||
to.wildlife_first == 0 || !hex32(to.commit))
|
||||
return std::unexpected{"target"};
|
||||
if (note.find_first_of("\r\n") != std::string_view::npos)
|
||||
return std::unexpected{"note"};
|
||||
auto [values, kustomization, broker, portal] = files;
|
||||
const auto region = std::to_string(to.region);
|
||||
values = replace_after(std::move(values), region_key, "\"", region);
|
||||
values = replace_after(std::move(values), routes_key, "=", region);
|
||||
values = replace_after(std::move(values), commit_key, " \n", to.commit, hex32);
|
||||
values =
|
||||
replace_after(std::move(values), wildlife_key, " \n", std::to_string(to.wildlife_first));
|
||||
// One note above the opening, replacing the previous portal note.
|
||||
const auto line = std::string(note_prefix) + std::string(note) + "\n";
|
||||
if (const auto old = values.find(note_prefix); old != std::string::npos)
|
||||
values.erase(old, values.find('\n', old) - old + 1);
|
||||
values.insert(values.find(opening_key), line);
|
||||
const auto header_at = kustomization.find(header_key) + header_key.size();
|
||||
const auto old_region = ", region " + std::to_string(from->region) + ",";
|
||||
kustomization.replace(kustomization.find(old_region, header_at), old_region.size(),
|
||||
", region " + region + ",");
|
||||
broker = replace_after(std::move(broker), broker_region_key, "\"", region);
|
||||
broker = replace_after(std::move(broker), broker_routes_key, "=", region);
|
||||
portal = replace_after(std::move(portal), portal_regions_key, "=", region);
|
||||
RegionSwitchFiles result{std::move(values), std::move(kustomization), std::move(broker),
|
||||
std::move(portal)};
|
||||
const auto check = read_region_layout(result);
|
||||
if (!check || *check != to)
|
||||
return std::unexpected{"result"};
|
||||
return result;
|
||||
}
|
||||
} // namespace evo::server::admin
|
||||
Reference in New Issue
Block a user