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:
+5
-2
@@ -312,8 +312,11 @@ if(EVO_BUILD_POSTGRES)
|
||||
target_link_libraries(evo-grant-broker PRIVATE evo_grant_broker)
|
||||
evo_owned_target(evo-grant-broker)
|
||||
# ADR-0182 read-only admin API and portal host (schema v61).
|
||||
add_library(evo_admin_api STATIC src/server/admin/admin_api.cpp)
|
||||
target_link_libraries(evo_admin_api PUBLIC evo_grant_broker)
|
||||
# Section 15: its HTTPS client to Gitea and Argo CD (OpenSSL libssl) and the region switch.
|
||||
find_package(OpenSSL 3 REQUIRED COMPONENTS Crypto SSL)
|
||||
add_library(evo_admin_api STATIC src/server/admin/admin_api.cpp
|
||||
src/server/admin/https_client.cpp src/server/admin/region_switch.cpp)
|
||||
target_link_libraries(evo_admin_api PUBLIC evo_grant_broker PRIVATE OpenSSL::SSL OpenSSL::Crypto)
|
||||
evo_owned_target(evo_admin_api)
|
||||
add_executable(evo-admin-api apps/evo-admin-api/main.cpp)
|
||||
target_link_libraries(evo-admin-api PRIVATE evo_admin_api)
|
||||
|
||||
@@ -3,13 +3,15 @@
|
||||
// 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]
|
||||
// [--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. No token is printed.
|
||||
// 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"
|
||||
|
||||
@@ -38,7 +40,7 @@ void on_stop(int) {
|
||||
|
||||
struct Options {
|
||||
std::string listen, jwks, oidc_issuer, audience, client, role, assets, baseline_tuning,
|
||||
region_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;
|
||||
@@ -79,6 +81,8 @@ std::optional<Options> parse(int argc, char** argv) {
|
||||
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")
|
||||
@@ -212,6 +216,34 @@ int main(int argc, char** argv) {
|
||||
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);
|
||||
|
||||
@@ -609,6 +609,7 @@ const tuningMap = (text) => new Map(parseTuning(text).flatMap((g) => g.settings.
|
||||
// ---------- Region list ----------
|
||||
async function regionsPage() {
|
||||
const data = await api('/api/regions');
|
||||
const opener = h('div', { class: 'card', style: { marginTop: '18px' } }, h('p', { class: 'hint' }, 'Loading…'));
|
||||
show(
|
||||
h('div', { class: 'head' }, h('div', {}, h('h1', {}, 'Regions'), h('div', { class: 'subtitle' }, `World ${data.world}`)),
|
||||
h('div', { class: 'tools' }, h('button', { onclick: () => route() }, 'Refresh'))),
|
||||
@@ -623,7 +624,121 @@ async function regionsPage() {
|
||||
{ key: 'build', label: 'Build', render: (r) => h('span', { class: 'mono', title: r.build || '' }, r.build ? r.build.slice(0, 12) : '—') },
|
||||
{ key: 'window_open', label: 'Sessions', render: windowBadge, sort: (r) => (r.window_open ? r.window_expires : 0) },
|
||||
{ key: 'viewable', label: '', render: (r) => (r.viewable ? h('a', { href: `#/regions/${r.region}` }, 'Open view →') : h('span', { class: 'muted' }, 'no view')) },
|
||||
], data.regions, { sortKey: 'region', onRow: (r) => { if (r.viewable) location.hash = `#/regions/${r.region}`; } }));
|
||||
], data.regions, { sortKey: 'region', onRow: (r) => { if (r.viewable) location.hash = `#/regions/${r.region}`; } }),
|
||||
opener);
|
||||
openRegionCard(opener);
|
||||
}
|
||||
|
||||
// ADR-0182 section 15: open a fresh region in place of the one DEV serves (one Git commit,
|
||||
// then a full Argo sync). The progress survives this tab reloading while the portal restarts.
|
||||
function argoBadges(argo) {
|
||||
if (!argo || argo.error) return h('span', { class: 'badge warn' }, `Argo unavailable${argo && argo.error ? `: ${argo.error}` : ''}`);
|
||||
const tone = (v, good) => (v === good ? 'alive' : v ? 'warn' : '');
|
||||
return h('span', {},
|
||||
h('span', { class: `badge ${tone(argo.phase, 'Succeeded')}`, title: argo.message || '' }, argo.phase || 'no operation'), ' ',
|
||||
h('span', { class: `badge ${tone(argo.sync, 'Synced')}` }, argo.sync || 'sync ?'), ' ',
|
||||
h('span', { class: `badge ${tone(argo.health, 'Healthy')}` }, argo.health || 'health ?'));
|
||||
}
|
||||
async function openRegionCard(card) {
|
||||
const progressKey = 'deploy-progress';
|
||||
const saved = (() => { try { return JSON.parse(sessionStorage.getItem(progressKey) || 'null'); } catch { return null; } })();
|
||||
if (saved && Date.now() - saved.at < 20 * 60 * 1000) return followOpening(card, saved, progressKey);
|
||||
let status;
|
||||
try { status = await api('/api/deploy'); } catch (e) { fill(card, h('h2', {}, 'Open a new region'), h('p', { class: 'error' }, `Unavailable: ${e.code || e.message}`)); return; }
|
||||
if (!status.configured) {
|
||||
fill(card, h('h2', {}, 'Open a new region'),
|
||||
h('p', { class: 'hint' }, 'Not configured: the admin API needs Secret aevolution-admin (Gitea and Argo CD settings and tokens) mounted at /run/deploy.'));
|
||||
return;
|
||||
}
|
||||
const problem = status.git && status.git.error ? `Git: ${status.git.error}` : status.layout_error ? `The region files changed shape (${status.layout_error}); edit them by hand.`
|
||||
: status.world_mismatch ? 'Git serves another world than this portal.' : !status.next ? 'The next region number is unavailable.' : '';
|
||||
const running = status.argo && status.argo.phase === 'Running';
|
||||
const reason = reasonInput();
|
||||
const confirmBox = h('input', { type: 'text', placeholder: status.next ? `Type ${status.next.region} to confirm` : '', class: 'count', style: { width: '170px' } });
|
||||
const note = h('div', { class: 'note' });
|
||||
const go = h('button', { class: 'primary', disabled: true }, status.next ? `Open region ${status.next.region}` : 'Open region');
|
||||
const ready = () => { go.disabled = !!problem || running || !status.next || confirmBox.value.trim() !== status.next.region; };
|
||||
confirmBox.addEventListener('input', ready);
|
||||
go.addEventListener('click', async () => {
|
||||
go.disabled = true;
|
||||
try {
|
||||
const q = new URLSearchParams({ region: status.next.region });
|
||||
if (reason.value.trim()) q.set('reason', reason.value.trim());
|
||||
const r = await api(`/api/deploy/region?${q}`, true, 'POST');
|
||||
const progress = { region: r.region, from: r.from, commit: r.commit, synced: r.sync_started, at: Date.now() };
|
||||
try { sessionStorage.setItem(progressKey, JSON.stringify(progress)); } catch { /* private mode */ }
|
||||
followOpening(card, progress, progressKey);
|
||||
} catch (e) {
|
||||
note.className = 'note error';
|
||||
const d = e.detail || {};
|
||||
note.textContent = {
|
||||
sync_running: 'An Argo sync is already running; try again when it finishes.',
|
||||
region_changed: `The next region is now ${d.expected}: reload.`,
|
||||
layout_changed: `The region files changed shape (${d.detail}).`,
|
||||
commit_refused: `Gitea refused the commit (HTTP ${d.status}); someone may have changed these files. Reload.`,
|
||||
gitea_unavailable: `Gitea is unreachable (${d.detail}).`,
|
||||
argo_unavailable: `Argo CD is unreachable (${d.detail}).`,
|
||||
}[e.code] || `Could not open the region: ${e.code || e.message}`;
|
||||
ready();
|
||||
}
|
||||
});
|
||||
const c = status.current || {}, n = status.next || {};
|
||||
const last = n.wildlife_count ? String(BigInt(n.wildlife_first) + BigInt(n.wildlife_count) - 1n) : '';
|
||||
fill(card,
|
||||
h('div', { class: 'card-head' }, h('h2', {}, 'Open a new region'), argoBadges(status.argo)),
|
||||
h('p', { class: 'hint' }, `Opens a fresh region in world ${c.world || '?'} and serves it in place of region ${c.region || '?'}. `,
|
||||
'The portal commits the change to Git (region values, grant broker and portal) and runs a full Argo sync: the provision hook opens the region first, then the region host, broker and this portal restart. ',
|
||||
'The current region stays in the stores but is no longer served.'),
|
||||
h('dl', { class: 'facts' },
|
||||
h('dt', {}, 'Serving now'), h('dd', {}, c.region ? `region ${c.region}` : '—'),
|
||||
h('dt', {}, 'Opens'), h('dd', {}, n.region ? h('strong', {}, `region ${n.region}`) : '—'),
|
||||
h('dt', {}, 'Seeded wildlife'), h('dd', { class: 'mono' }, n.wildlife_count ? `${n.wildlife_count} cells, ids ${n.wildlife_first}–${last}` : 'none'),
|
||||
h('dt', {}, 'Rules'), h('dd', {}, 'the Git baseline tuning files; adopt a revision afterwards to change them'),
|
||||
h('dt', {}, 'Repository'), h('dd', { class: 'mono' }, `${status.repository} @ ${status.branch}${status.git && status.git.head ? ` (${status.git.head.slice(0, 8)})` : ''}`),
|
||||
h('dt', {}, 'Application'), h('dd', { class: 'mono' }, status.application)),
|
||||
problem ? h('div', { class: 'callout', style: { marginTop: '12px' } }, problem) : null,
|
||||
running ? h('div', { class: 'callout', style: { marginTop: '12px' } }, 'An Argo sync is running; wait for it to finish.') : null,
|
||||
h('div', { class: 'form-row' }, reason, confirmBox, go), note);
|
||||
ready();
|
||||
}
|
||||
async function followOpening(card, progress, progressKey) {
|
||||
const steps = h('div', { class: 'list' });
|
||||
const step = (done, text, sub) => h('div', { class: 'list-item' },
|
||||
h('span', { class: `badge ${done === true ? 'alive' : done === false ? 'warn' : ''}` }, done === true ? 'done' : done === false ? 'waiting' : '…'),
|
||||
h('div', { class: 'grow' }, text, sub ? h('div', { class: 'sub' }, sub) : null));
|
||||
const finish = h('div', { class: 'form-row' });
|
||||
fill(card, h('div', { class: 'card-head' }, h('h2', {}, `Opening region ${progress.region}`), h('span', { class: 'muted small' }, `from region ${progress.from}`)), steps, finish);
|
||||
let stopped = false;
|
||||
const previous = stopPage;
|
||||
stopPage = () => { stopped = true; previous(); };
|
||||
const clear = () => { try { sessionStorage.removeItem(progressKey); } catch { /* */ } };
|
||||
for (;;) {
|
||||
if (stopped) return;
|
||||
let argo = null, regions = null, restarting = false;
|
||||
try { argo = (await api('/api/deploy?argo=1')).argo; } catch { restarting = true; }
|
||||
try { regions = await api('/api/regions'); } catch { restarting = true; }
|
||||
const target = regions && regions.regions.find((r) => String(r.region) === String(progress.region));
|
||||
const synced = argo && !argo.error && argo.revision === progress.commit && argo.phase === 'Succeeded';
|
||||
const live = target && target.lease_held && target.viewable;
|
||||
fill(steps,
|
||||
step(true, `Committed to Git`, h('span', { class: 'mono' }, progress.commit.slice(0, 12))),
|
||||
step(progress.synced ? (synced ? true : false) : false, progress.synced ? 'Full Argo sync of that commit' : 'Argo did not accept the sync request: start a full sync of the commit by hand',
|
||||
argo && !argo.error ? `${argo.phase || 'no operation'} · ${argo.sync} · ${argo.health}${argo.message ? ` · ${argo.message}` : ''}` : restarting ? 'the admin portal is restarting with the new region…' : ''),
|
||||
step(live ? true : false, `Region ${progress.region} provisioned and running`, target ? (target.lease_held ? `lease held · tick ${fmtInt(target.tick)}` : 'provisioned, waiting for the region host') : 'not provisioned yet'));
|
||||
if (live) {
|
||||
clear();
|
||||
fill(finish,
|
||||
h('a', { href: `#/regions/${progress.region}` }, h('button', { class: 'primary' }, `Open region ${progress.region} →`)),
|
||||
h('span', { class: 'hint', style: { margin: 0 } }, 'Players can join once a session window is open (region page, Access tab).'));
|
||||
return;
|
||||
}
|
||||
if (Date.now() - progress.at > 20 * 60 * 1000) {
|
||||
clear();
|
||||
fill(finish, h('div', { class: 'callout' }, 'Still not live after 20 minutes: check the Argo Application and the provision job.'));
|
||||
return;
|
||||
}
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||
}
|
||||
}
|
||||
|
||||
function windowBadge(r) {
|
||||
|
||||
@@ -48,7 +48,8 @@ spec:
|
||||
--role aevolution-admin --assets /usr/local/share/evo-admin-portal \
|
||||
--pg-service /run/postgres/pg_service.conf --pg-passfile /run/postgres/pgpass \
|
||||
--baseline-tuning /etc/evo/tuning/cellular-revamp.tuning \
|
||||
--region-tuning /etc/evo/tuning/cellular.tuning "${regions[@]}"
|
||||
--region-tuning /etc/evo/tuning/cellular.tuning \
|
||||
--deploy-config /run/deploy "${regions[@]}"
|
||||
env:
|
||||
- {name: ADMIN_WORLD, value: "100"}
|
||||
# Space-separated REGION=HOST:PORT: each region's operations address for /admin/view.
|
||||
@@ -81,7 +82,23 @@ spec:
|
||||
# The revamp tuning file the regions opened with: the portal's baseline until the
|
||||
# first tuning revision (ADR-0182 section 13); the region tuning file, shown read only.
|
||||
- {name: tuning, mountPath: /etc/evo/tuning, readOnly: true}
|
||||
# ADR-0182 section 15: the Gitea and Argo CD settings and tokens that let the portal
|
||||
# open a region, one file per key; without the Secret that feature stays off.
|
||||
- {name: deploy, mountPath: /run/deploy, readOnly: true}
|
||||
volumes:
|
||||
- name: deploy
|
||||
secret:
|
||||
secretName: aevolution-admin
|
||||
optional: true
|
||||
defaultMode: 0440
|
||||
items:
|
||||
- {key: GITEA_BASE_URL, path: GITEA_BASE_URL}
|
||||
- {key: GITEA_REPOSITORY, path: GITEA_REPOSITORY}
|
||||
- {key: GITEA_TOKEN, path: GITEA_TOKEN}
|
||||
- {key: ARGOCD_SERVER, path: ARGOCD_SERVER}
|
||||
- {key: ARGOCD_APPLICATION, path: ARGOCD_APPLICATION}
|
||||
- {key: ARGOCD_TOKEN, path: ARGOCD_TOKEN}
|
||||
- {key: ARGOCD_TLS_VERIFY, path: ARGOCD_TLS_VERIFY}
|
||||
- name: tuning
|
||||
configMap:
|
||||
name: cellular-tuning
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# Admin portal traffic on top of the namespace default-deny. Admins reach it only at
|
||||
# https://srs.aeternalisgames.com on 443, where ingress-nginx terminates TLS; port 8080 is
|
||||
# admitted from the ingress-nginx pods and nothing else. Egress: PostgreSQL on pg-forge-01
|
||||
# and the region's operations port (9100) for /admin/view.
|
||||
# admitted from the ingress-nginx pods and nothing else. Egress: PostgreSQL on pg-forge-01,
|
||||
# the region's operations port (9100) for /admin/view, and for opening a region (ADR-0182
|
||||
# section 15, approved 2026-10-28) Gitea through the node's ingress (git.aeternalisgames.com,
|
||||
# 172.10.1.160:443) and the argocd-server pods (8080, behind the Service's 443).
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
@@ -35,6 +37,20 @@ spec:
|
||||
app.kubernetes.io/name: evo-region
|
||||
ports:
|
||||
- {protocol: TCP, port: 9100}
|
||||
- to:
|
||||
- ipBlock:
|
||||
cidr: 172.10.1.160/32
|
||||
ports:
|
||||
- {protocol: TCP, port: 443}
|
||||
- to:
|
||||
- namespaceSelector:
|
||||
matchLabels:
|
||||
kubernetes.io/metadata.name: argocd
|
||||
podSelector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/name: argocd-server
|
||||
ports:
|
||||
- {protocol: TCP, port: 8080}
|
||||
---
|
||||
# The region's operations port also admits the admin portal (alongside Prometheus,
|
||||
# evo-region-scrape). Only 9100: never the session port.
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
# Project status and next steps
|
||||
|
||||
## Opening regions from the portal - 2026-10-28
|
||||
|
||||
The Regions page can open a fresh region in place of the one DEV serves: the admin API
|
||||
commits the region switch to `main` as aevolution-bot (through Secret `aevolution-admin`) and
|
||||
runs a full Argo sync, and the page follows the sync until the new region is live. See
|
||||
[ADR-0182](adr/0182-admin-portal-and-world-tuning.md) section 15.
|
||||
|
||||
## Guided tuning editor - 2026-10-28
|
||||
|
||||
The Tuning page is a guided editor: pick a section, edit a setting's value (units and plain
|
||||
|
||||
@@ -319,6 +319,38 @@ deposits.
|
||||
Not changed: a region with no living cell does not tick, so it neither recycles nor drips; an
|
||||
empty region needs a spawn or a player before the cycle restarts.
|
||||
|
||||
### 15. Opening a region from the portal (delivered 2026-10-28, DEV)
|
||||
|
||||
The portal opens a fresh region in place of the one DEV serves, as was done by hand for
|
||||
regions 14, 15 and 16. Concurrent regions and new worlds remain later work (per-region
|
||||
Deployments, routes and ports; a multi-world admin API and broker).
|
||||
|
||||
- **Decisions (2026-10-28).** Replace the served region; commit straight to `main` and run a
|
||||
full Argo sync at once (CI runs on the commit afterwards); the admin portal pod may reach
|
||||
Gitea and argocd-server; the in-cluster Argo hop may skip TLS verification
|
||||
(`ARGOCD_TLS_VERIFY=false`).
|
||||
- **Credentials.** Secret `aevolution-admin` (created by the operator: Gitea base URL,
|
||||
repository and repository-scoped token for `aevolution-bot`; Argo CD server, Application and
|
||||
token) is mounted read-only as files at `/run/deploy` (`--deploy-config`), never as
|
||||
environment; optional, so the portal runs without it and the feature stays off. Tokens travel
|
||||
only in request headers and are never logged.
|
||||
- **The change.** Four files, read at one commit of `main` with their blob shas
|
||||
(`region_switch_paths`): the region overlay's `region-values.yaml` (REGION_ID for the
|
||||
Deployment and the provision Job, REGION_LIFE_ROUTES, the opening's `--commit` and
|
||||
`--playtest-wildlife-first-id`, and a one-line note above the opening), its kustomization
|
||||
header, and the region named in the grant broker's routes and the portal's `ADMIN_REGIONS`.
|
||||
Nothing else changes; a file whose expected lines are missing, repeated or inconsistent is
|
||||
refused. The next region is one above every region with a lease in the world; its seeded
|
||||
wildlife range follows the previous one by `playtest.starting_wildlife_cells`; its genesis
|
||||
commit is 16 random bytes. One Gitea multi-file commit (the shas guard concurrent edits),
|
||||
then `POST /api/v1/applications/<app>/sync` for exactly that commit with prune: the
|
||||
provision hook opens the region before the region host restarts on it.
|
||||
- **Guards.** The request names the region the portal showed (409 if another is next now);
|
||||
a running Argo operation refuses (409); the portal asks the admin to type the number. The
|
||||
earlier region stays in the stores, unserved; a new region's session window starts closed.
|
||||
- **Network.** The portal's NetworkPolicy adds egress to 172.10.1.160:443 (Gitea through the
|
||||
node's ingress) and to the argocd-server pods on 8080.
|
||||
|
||||
## Delivery order
|
||||
|
||||
1. Read-only portal: login, accounts (from existing data), regions, region view (schema 61,
|
||||
|
||||
@@ -30,8 +30,10 @@ class JsonValue {
|
||||
[[nodiscard]] const JsonValue* member(std::string_view name) const;
|
||||
[[nodiscard]] const std::vector<JsonValue>* array() const;
|
||||
|
||||
// Whole-document parse; nullopt for anything malformed or out of bounds.
|
||||
[[nodiscard]] static std::optional<JsonValue> parse(std::string_view text);
|
||||
// Whole-document parse; nullopt for anything malformed or out of bounds. `limit` bounds
|
||||
// the text: access tokens and keys keep the default; a service's answer may allow more.
|
||||
[[nodiscard]] static std::optional<JsonValue> parse(std::string_view text,
|
||||
std::size_t limit = max_json_bytes);
|
||||
|
||||
private:
|
||||
friend class JsonParser;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "evo/persistence/postgres.hpp"
|
||||
#include "evo/server/admin/https_client.hpp"
|
||||
#include "evo/server/broker/access_token.hpp"
|
||||
#include "evo/server/ops_http.hpp"
|
||||
|
||||
@@ -55,6 +56,13 @@
|
||||
// `key = value` lines, applied to the baseline file or to
|
||||
// revision REV (which must be the newest: 409 stale_base),
|
||||
// keeping its comments; then as above
|
||||
// GET /api/deploy[?argo=1] ADR-0182 section 15: whether opening a region is
|
||||
// configured, the region Git main serves, the next region
|
||||
// and its seeded wildlife range, and the Argo Application's
|
||||
// sync (argo=1: the Argo status only)
|
||||
// POST /api/deploy/region?region=N[&reason=R] opens region N (the next one) in place of
|
||||
// the served region: one Gitea commit to the region,
|
||||
// broker and portal files, then a full Argo sync
|
||||
namespace evo::server::admin {
|
||||
struct AdminAsset {
|
||||
std::string path; // "/index.html", ...; "/" serves "/index.html".
|
||||
@@ -73,6 +81,18 @@ struct AdminConfig {
|
||||
std::chrono::milliseconds db_timeout{3000};
|
||||
std::chrono::milliseconds view_cache{750}; // A fetched view answers repeats this long.
|
||||
};
|
||||
// ADR-0182 section 15: where the admin API commits a region switch and what it syncs. The
|
||||
// tokens come from mounted Secret files and are only ever sent in request headers.
|
||||
struct AdminDeployConfig {
|
||||
std::string gitea_base; // https://git.example[:port][/prefix]
|
||||
std::string gitea_repository; // owner/name
|
||||
std::string gitea_token;
|
||||
std::string branch{"main"};
|
||||
std::string argo_server; // host:port, https assumed
|
||||
std::string argo_application;
|
||||
std::string argo_token;
|
||||
bool argo_verify{true};
|
||||
};
|
||||
using AdminConnect = std::function<std::unique_ptr<persistence::Postgres>()>;
|
||||
// The region's /admin/view body, or nullopt when it cannot be reached or is not ready.
|
||||
using AdminRegionView = std::function<std::optional<std::string>(std::uint64_t region)>;
|
||||
@@ -84,6 +104,8 @@ class AdminApi {
|
||||
// `now_seconds` is wall time (token expiry); `now` paces the view cache.
|
||||
[[nodiscard]] OpsResponse handle(const ApiRequest& request, std::uint64_t now_seconds,
|
||||
std::chrono::steady_clock::time_point now);
|
||||
// Turns on /api/deploy with the given services and transport (tests pass a fake).
|
||||
void enable_deploy(AdminDeployConfig config, HttpsTransport transport);
|
||||
|
||||
private:
|
||||
[[nodiscard]] OpsResponse api(std::string_view method, std::string_view path,
|
||||
@@ -109,6 +131,10 @@ class AdminApi {
|
||||
[[nodiscard]] OpsResponse tuning_text(std::uint64_t revision);
|
||||
[[nodiscard]] OpsResponse set_tuning(std::string_view query, std::string_view text,
|
||||
const broker::AccessTokenClaims& admin);
|
||||
[[nodiscard]] OpsResponse deploy_status(std::string_view query);
|
||||
[[nodiscard]] OpsResponse open_region(std::string_view query,
|
||||
const broker::AccessTokenClaims& admin);
|
||||
[[nodiscard]] std::optional<std::uint64_t> next_region(std::uint64_t served);
|
||||
[[nodiscard]] std::optional<persistence::PgRows> query(const std::string& sql,
|
||||
std::vector<std::string> parameters);
|
||||
|
||||
@@ -117,6 +143,8 @@ class AdminApi {
|
||||
AdminConnect connect_;
|
||||
AdminRegionView view_;
|
||||
std::vector<AdminAsset> assets_;
|
||||
std::optional<AdminDeployConfig> deploy_;
|
||||
HttpsTransport https_;
|
||||
std::unique_ptr<persistence::Postgres> db_;
|
||||
struct CachedView {
|
||||
std::chrono::steady_clock::time_point at;
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
// ADR-0182 section 15: a bounded, blocking HTTPS/1.1 client for the admin API's two outbound
|
||||
// services (Gitea and Argo CD). One request per connection (Connection: close); the system
|
||||
// trust store and host name check unless `verify` is false. Credentials travel only in the
|
||||
// request headers and are never logged.
|
||||
namespace evo::server::admin {
|
||||
struct HttpsRequest {
|
||||
std::string host;
|
||||
std::uint16_t port{443};
|
||||
std::string method{"GET"};
|
||||
std::string target{"/"}; // Path and query, already percent-encoded.
|
||||
std::vector<std::pair<std::string, std::string>> headers;
|
||||
std::string body;
|
||||
bool verify{true};
|
||||
std::chrono::milliseconds timeout{10'000};
|
||||
std::size_t limit{std::size_t{4} << 20U}; // Largest body accepted.
|
||||
};
|
||||
struct HttpsResponse {
|
||||
int status{};
|
||||
std::string body;
|
||||
};
|
||||
// The transport the admin API uses; tests substitute a fake.
|
||||
using HttpsTransport = std::function<std::optional<HttpsResponse>(const HttpsRequest&)>;
|
||||
|
||||
// Sends `request` over TLS: the answer, or nullopt on any connection, TLS, timeout, size or
|
||||
// framing failure.
|
||||
[[nodiscard]] std::optional<HttpsResponse> https_request(const HttpsRequest& request);
|
||||
// An HTTP/1.1 response read to the end of the connection: the status and the body (by
|
||||
// Content-Length, chunked, or to the end), or nullopt when malformed or over `limit`.
|
||||
[[nodiscard]] std::optional<HttpsResponse> parse_http_response(std::string_view raw,
|
||||
std::size_t limit);
|
||||
// "https://host[:port][/...]" split into host, port and the path prefix (no trailing slash).
|
||||
struct HttpsBase {
|
||||
std::string host;
|
||||
std::uint16_t port{443};
|
||||
std::string prefix;
|
||||
};
|
||||
[[nodiscard]] std::optional<HttpsBase> parse_https_base(std::string_view url);
|
||||
} // namespace evo::server::admin
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
// ADR-0182 section 15: the admin portal opens a fresh region in place of the one DEV serves.
|
||||
// The change is four Git files edited in place: the region overlay's values (world, region,
|
||||
// life routes, the opening's genesis commit and seeded wildlife range, both for the region
|
||||
// Deployment and its provision Job), its kustomization header, and the region named by the
|
||||
// grant broker's routes and the admin portal. Anything else is left exactly as it is; a file
|
||||
// whose layout differs from the expected lines is refused rather than guessed at.
|
||||
namespace evo::server::admin {
|
||||
inline constexpr std::array<std::string_view, 4> region_switch_paths{
|
||||
"deploy/kubernetes/region/dev/region-values.yaml",
|
||||
"deploy/kubernetes/region/dev/kustomization.yaml",
|
||||
"deploy/kubernetes/grant-broker-dev/deployment.yaml",
|
||||
"deploy/kubernetes/admin-portal-dev/deployment.yaml",
|
||||
};
|
||||
// The four files' text, in region_switch_paths order.
|
||||
using RegionSwitchFiles = std::array<std::string, 4>;
|
||||
// What the files say DEV serves: the world and region, the opening's genesis commit (32
|
||||
// lowercase hex digits) and the first seeded wildlife identity.
|
||||
struct RegionLayout {
|
||||
std::uint64_t world{}, region{};
|
||||
std::string commit;
|
||||
std::uint64_t wildlife_first{};
|
||||
auto operator<=>(const RegionLayout&) const = default;
|
||||
};
|
||||
// The served layout, or the name of the line that is missing, repeated or inconsistent.
|
||||
[[nodiscard]] std::expected<RegionLayout, std::string>
|
||||
read_region_layout(const RegionSwitchFiles& files);
|
||||
// The files serving `to` (same world) instead, with `note` (one line, no line breaks) recorded
|
||||
// as a comment above the opening; or why the files or `to` cannot be switched.
|
||||
[[nodiscard]] std::expected<RegionSwitchFiles, std::string>
|
||||
switch_region(const RegionSwitchFiles& files, const RegionLayout& to, std::string_view note);
|
||||
} // namespace evo::server::admin
|
||||
@@ -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
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "evo/persistence/account_identity_store.hpp"
|
||||
#include "evo/persistence/region_lease_store.hpp"
|
||||
#include "evo/server/admin/admin_api.hpp"
|
||||
#include "evo/server/admin/region_switch.hpp"
|
||||
#include "evo/server/broker/grant_broker.hpp"
|
||||
#include "evo/server/broker/json.hpp"
|
||||
|
||||
@@ -13,6 +14,7 @@
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
@@ -779,6 +781,245 @@ void admin_api_tests(const Realm& realm) {
|
||||
CHECK(send("/api/tuning?check=1&base=" + revision, "").status == 409);
|
||||
}
|
||||
|
||||
// ADR-0182 section 15: opening a region. The HTTPS reader and the four-file switch first,
|
||||
// over the repository's own files, then the endpoints against fake Gitea and Argo CD.
|
||||
{
|
||||
using server::admin::HttpsRequest;
|
||||
using server::admin::HttpsResponse;
|
||||
using server::admin::parse_http_response;
|
||||
using server::admin::region_switch_paths;
|
||||
const auto sized = parse_http_response(
|
||||
"HTTP/1.1 201 Created\r\nContent-Length: 5\r\nX: y\r\n\r\nhello", 100);
|
||||
CHECK(sized && sized->status == 201 && sized->body == "hello");
|
||||
const auto chunked =
|
||||
parse_http_response("HTTP/1.1 200 OK\r\nTransfer-Encoding: "
|
||||
"chunked\r\n\r\n4\r\nwiki\r\n5;x=1\r\npedia\r\n0\r\n\r\n",
|
||||
100);
|
||||
CHECK(chunked && chunked->status == 200 && chunked->body == "wikipedia");
|
||||
CHECK(!parse_http_response("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n4\r\nwi",
|
||||
100));
|
||||
CHECK(!parse_http_response("HTTP/1.1 200 OK\r\nContent-Length: 9\r\n\r\nshort", 100));
|
||||
CHECK(!parse_http_response("HTTP/1.1 200 OK\r\n\r\nlong enough", 4));
|
||||
CHECK(!parse_http_response("SSH-2.0 hello\r\n\r\n", 100));
|
||||
const auto with_prefix = server::admin::parse_https_base("https://git.example:8443/gitea/");
|
||||
CHECK(with_prefix && with_prefix->host == "git.example" && with_prefix->port == 8443 &&
|
||||
with_prefix->prefix == "/gitea");
|
||||
const auto bare =
|
||||
server::admin::parse_https_base("argocd-server.argocd.svc.cluster.local:443");
|
||||
CHECK(bare && bare->host == "argocd-server.argocd.svc.cluster.local" && bare->port == 443 &&
|
||||
bare->prefix.empty());
|
||||
CHECK(!server::admin::parse_https_base("http://plain.example"));
|
||||
|
||||
server::admin::RegionSwitchFiles files;
|
||||
for (std::size_t i = 0; i < files.size(); ++i) {
|
||||
std::ifstream in{env("EVO_SOURCE_ROOT") + "/" + std::string(region_switch_paths.at(i)),
|
||||
std::ios::binary};
|
||||
files.at(i) = std::string{std::istreambuf_iterator<char>{in}, {}};
|
||||
}
|
||||
const auto layout = server::admin::read_region_layout(files);
|
||||
CHECK(layout && layout->region != 0 && layout->wildlife_first != 0);
|
||||
auto to = *layout;
|
||||
to.region += 1;
|
||||
to.wildlife_first += 100;
|
||||
to.commit = std::string(32, 'e');
|
||||
const auto switched =
|
||||
server::admin::switch_region(files, to, "region opened_region by a test");
|
||||
CHECK(switched && server::admin::read_region_layout(*switched) == to);
|
||||
// Only the named lines change (and the note above the opening is added).
|
||||
const auto lines = [](const std::string& text) {
|
||||
std::vector<std::string> out;
|
||||
for (std::size_t at = 0; at < text.size();) {
|
||||
const auto end = std::min(text.find('\n', at), text.size());
|
||||
const auto line = text.substr(at, end - at);
|
||||
if (line.find("# Admin portal (ADR-0182 section 15)") == std::string::npos)
|
||||
out.push_back(line);
|
||||
at = end + 1;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
for (std::size_t i = 0; switched && i < files.size(); ++i) {
|
||||
const auto before = lines(files.at(i));
|
||||
const auto after = lines(switched->at(i));
|
||||
CHECK(before.size() == after.size());
|
||||
for (std::size_t l = 0; l < std::min(before.size(), after.size()); ++l)
|
||||
if (before[l] != after[l])
|
||||
CHECK(std::ranges::any_of(
|
||||
std::array<std::string_view, 8>{"REGION_ID", "REGION_LIFE_ROUTES",
|
||||
"--commit", "--playtest-wildlife-first-id",
|
||||
"Region host", "BROKER_DEFAULT_REGION",
|
||||
"BROKER_ROUTES", "ADMIN_REGIONS"},
|
||||
[&](std::string_view key) {
|
||||
return after[l].find(key) != std::string::npos;
|
||||
}));
|
||||
}
|
||||
CHECK(switched &&
|
||||
switched->at(0).find("# Admin portal (ADR-0182 section 15): region opened_region "
|
||||
"by a test\n - {name: REGION_OPENING") !=
|
||||
std::string::npos);
|
||||
CHECK(server::admin::switch_region(*switched, to, "again").error() == "target");
|
||||
CHECK(server::admin::switch_region(files, to, "two\nlines").error() == "note");
|
||||
auto broken = files;
|
||||
broken.at(0).replace(broken.at(0).find("{name: REGION_ID"), 4, "{nom");
|
||||
CHECK(server::admin::read_region_layout(broken).error() == "REGION_ID");
|
||||
|
||||
// The endpoints against fake services serving the repository files for world 9831.
|
||||
for (auto& text : files)
|
||||
for (const auto& from : {std::string{"REGION_WORLD, value: \"100\""},
|
||||
std::string{"evolution-dev: world 100,"}})
|
||||
for (auto at = text.find(from); at != std::string::npos; at = text.find(from, at))
|
||||
text.replace(at, from.size(),
|
||||
from == "evolution-dev: world 100,"
|
||||
? std::string{"evolution-dev: world 9831,"}
|
||||
: std::string{"REGION_WORLD, value: \"9831\""});
|
||||
const auto head = std::string(40, 'a');
|
||||
const auto made_commit = std::string(40, 'c');
|
||||
std::vector<HttpsRequest> seen;
|
||||
std::string argo_phase = "Succeeded";
|
||||
int commit_status = 201;
|
||||
const server::admin::HttpsTransport fake =
|
||||
[&](const HttpsRequest& request) -> std::optional<HttpsResponse> {
|
||||
seen.push_back(request);
|
||||
const auto& t = request.target;
|
||||
if (request.host == "argo.test") {
|
||||
if (request.method == "POST")
|
||||
return HttpsResponse{200, "{}"};
|
||||
return HttpsResponse{200,
|
||||
"{\"items\":[{\"metadata\":{\"name\":\"aevolution-dev\"},"
|
||||
"\"status\":{\"sync\":{\"status\":\"Synced\",\"revision\":\"" +
|
||||
head +
|
||||
"\"},\"health\":{\"status\":\"Healthy\"},"
|
||||
"\"operationState\":{\"phase\":\"" +
|
||||
argo_phase + "\",\"message\":\"done\"}}}]}"};
|
||||
}
|
||||
if (t == "/api/v1/repos/dingo/AEvolution/branches/main")
|
||||
return HttpsResponse{200,
|
||||
"{\"name\":\"main\",\"commit\":{\"id\":\"" + head + "\"}}"};
|
||||
if (request.method == "POST" && t == "/api/v1/repos/dingo/AEvolution/contents")
|
||||
return HttpsResponse{commit_status,
|
||||
"{\"commit\":{\"sha\":\"" + made_commit + "\"}}"};
|
||||
for (std::size_t i = 0; i < region_switch_paths.size(); ++i) {
|
||||
const std::string path(region_switch_paths.at(i));
|
||||
std::string raw = "/api/v1/repos/dingo/AEvolution/raw/";
|
||||
raw.append(path).append("?ref=").append(head);
|
||||
if (t == raw)
|
||||
return HttpsResponse{200, files.at(i)};
|
||||
}
|
||||
constexpr std::string_view listing = "/api/v1/repos/dingo/AEvolution/contents/";
|
||||
if (t.starts_with(listing)) {
|
||||
const auto dir = t.substr(listing.size(), t.find('?') - listing.size());
|
||||
std::string out = "[";
|
||||
for (std::size_t i = 0; i < region_switch_paths.size(); ++i) {
|
||||
const std::string path(region_switch_paths.at(i));
|
||||
if (path.substr(0, path.rfind('/')) == dir)
|
||||
out += std::string(out.size() == 1 ? "" : ",") + "{\"name\":\"" +
|
||||
path.substr(path.rfind('/') + 1) + "\",\"sha\":\"" +
|
||||
std::string(40, static_cast<char>('1' + i)) + "\"}";
|
||||
}
|
||||
return HttpsResponse{200, out + "]"};
|
||||
}
|
||||
return HttpsResponse{404, "{}"};
|
||||
};
|
||||
server::admin::AdminConfig config;
|
||||
config.world = 9831;
|
||||
config.view_regions = {1};
|
||||
std::ifstream revamp{env("EVO_SOURCE_ROOT") +
|
||||
"/deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning",
|
||||
std::ios::binary};
|
||||
config.baseline_tuning = std::string{std::istreambuf_iterator<char>{revamp}, {}};
|
||||
AdminApi deploying{config,
|
||||
*TokenVerifier::from_jwks(realm.jwks("sig-1"), admin_rules),
|
||||
[] { return connect_as(env("EVO_PG_ADMIN_USER")); },
|
||||
[](std::uint64_t) { return std::optional<std::string>{}; },
|
||||
{}};
|
||||
const auto ask = [&](std::string_view method, std::string_view target) {
|
||||
return deploying.handle({method, target, admin}, now, t0);
|
||||
};
|
||||
CHECK(ask("GET", "/api/deploy").body == "{\"configured\":false}\n");
|
||||
CHECK(ask("POST", "/api/deploy/region?region=17").status == 503);
|
||||
deploying.enable_deploy({.gitea_base = "https://git.test",
|
||||
.gitea_repository = "dingo/AEvolution",
|
||||
.gitea_token = "gitea-secret",
|
||||
.branch = "main",
|
||||
.argo_server = "argo.test:443",
|
||||
.argo_application = "aevolution-dev",
|
||||
.argo_token = "argo-secret",
|
||||
.argo_verify = false},
|
||||
fake);
|
||||
const auto status = must(JsonValue::parse(ask("GET", "/api/deploy").body));
|
||||
const auto served = std::to_string(layout->region);
|
||||
const auto next = std::string(must(status.member("next")->member("region")->string()));
|
||||
CHECK(status.member("configured")->boolean() == true &&
|
||||
status.member("current")->member("region")->string() == served &&
|
||||
status.member("current")->member("world")->string() == "9831" &&
|
||||
status.member("next")->member("wildlife_first")->string() ==
|
||||
std::to_string(layout->wildlife_first + 100) &&
|
||||
status.member("argo")->member("phase")->string() == "Succeeded" &&
|
||||
status.member("git")->member("head")->string() == head &&
|
||||
std::stoull(next) > layout->region);
|
||||
CHECK(must(JsonValue::parse(ask("GET", "/api/deploy?argo=1").body)).member("current") ==
|
||||
nullptr);
|
||||
// Credentials only in headers; Argo's in-cluster hop may skip verification, Gitea's not.
|
||||
for (const auto& request : seen)
|
||||
CHECK(request.target.find("secret") == std::string::npos &&
|
||||
std::ranges::count(request.headers,
|
||||
std::pair<std::string, std::string>{
|
||||
"Authorization", request.host == "argo.test"
|
||||
? "Bearer argo-secret"
|
||||
: "token gitea-secret"}) == 1 &&
|
||||
request.verify == (request.host != "argo.test"));
|
||||
// Refusals: a stale region number, a sync already running, a refused commit.
|
||||
CHECK(ask("POST", "/api/deploy/region?region=" + std::to_string(std::stoull(next) + 1))
|
||||
.status == 409);
|
||||
argo_phase = "Running";
|
||||
CHECK(ask("POST", "/api/deploy/region?region=" + next).status == 409);
|
||||
argo_phase = "Succeeded";
|
||||
commit_status = 409;
|
||||
CHECK(ask("POST", "/api/deploy/region?region=" + next).status == 409);
|
||||
commit_status = 201;
|
||||
seen.clear();
|
||||
const auto opened_region =
|
||||
ask("POST", "/api/deploy/region?region=" + next + "&reason=fresh+start");
|
||||
const auto result = must(JsonValue::parse(opened_region.body));
|
||||
CHECK(opened_region.status == 200 && result.member("region")->string() == next &&
|
||||
result.member("from")->string() == served &&
|
||||
result.member("commit")->string() == made_commit &&
|
||||
result.member("sync_started")->boolean() == true);
|
||||
const auto commit = std::ranges::find_if(
|
||||
seen, [](const HttpsRequest& r) { return r.method == "POST" && r.host == "git.test"; });
|
||||
const auto sync = std::ranges::find_if(seen, [](const HttpsRequest& r) {
|
||||
return r.method == "POST" && r.host == "argo.test";
|
||||
});
|
||||
CHECK(commit != seen.end() && sync != seen.end() &&
|
||||
sync->target == "/api/v1/applications/aevolution-dev/sync" &&
|
||||
sync->body == "{\"revision\":\"" + made_commit + "\",\"prune\":true}");
|
||||
if (commit != seen.end()) {
|
||||
const auto body = must(JsonValue::parse(commit->body, 1U << 20U));
|
||||
CHECK(body.member("branch")->string() == "main" &&
|
||||
std::string(must(body.member("message")->string())).find("open region " + next) !=
|
||||
std::string::npos &&
|
||||
std::string(must(body.member("message")->string())).find("ops@example.test") !=
|
||||
std::string::npos &&
|
||||
body.member("files")->array()->size() == 4);
|
||||
const auto& first_file = body.member("files")->array()->at(0);
|
||||
CHECK(first_file.member("path")->string() == region_switch_paths.at(0) &&
|
||||
first_file.member("sha")->string() == std::string(40, '1') &&
|
||||
first_file.member("operation")->string() == "update");
|
||||
const auto encoded = std::string(must(first_file.member("content")->string()));
|
||||
std::string decoded(encoded.size(), '\0');
|
||||
const auto length =
|
||||
EVP_DecodeBlock(reinterpret_cast<unsigned char*>(decoded.data()),
|
||||
reinterpret_cast<const unsigned char*>(encoded.data()),
|
||||
static_cast<int>(encoded.size()));
|
||||
CHECK(length > 0);
|
||||
decoded.resize(static_cast<std::size_t>(length) -
|
||||
static_cast<std::size_t>(std::ranges::count(encoded, '=')));
|
||||
CHECK(decoded.find("{name: REGION_ID, value: \"" + next + "\"}") != std::string::npos &&
|
||||
decoded.find("--playtest-wildlife-first-id " +
|
||||
std::to_string(layout->wildlife_first + 100)) != std::string::npos &&
|
||||
decoded.find("in place of " + served + "; seeded wildlife") != std::string::npos);
|
||||
}
|
||||
}
|
||||
|
||||
// Accounts (schema 64): control claims recorded alice's sessions; a ban refuses grants
|
||||
// and is listed with its reason; lifting it restores them; an admin logout succeeds.
|
||||
CHECK(alice->member("sessions")->integer().value_or(0) >= 1 &&
|
||||
|
||||
Reference in New Issue
Block a user