feat(routing): Lab Host Router closes the Gateway-to-Lab cross-VM gap

Adds platform/lab-router/ — a narrow service that runs on the Lab Host,
authenticates the Gateway via mTLS, and resolves an opaque routing token to
a local container purely from Docker label state (no Postgres access, no
callback to the Provisioner). Replaces the Gateway's env.internal_ip
assumption (only valid when Gateway and Lab share a Docker daemon) with a
data-driven direct/router decision per lab_host
(lab_hosts.routing_mode/router_endpoint, migration 004), resolved in one
place (platform/gateway/src/lab-endpoint.js's labTarget()) that every
proxy/fetch call site in server.js now goes through.

Extends the existing Docker TLS CA (infra/scripts/docker-tls/generate-docker-tls.sh)
with a second mTLS leaf pair (router-server/gateway-client) for this new
data-plane connection, deployed by the docker-tls Ansible role into a
separate directory from the control-plane pair. Firewall
(rn-lab-host-01.rules) replaces the old speculative wide port-range
allowance with one narrow rule: platform_ip -> the Router's port only.

dev-local stays routing_mode: direct — zero behavior change for local dev,
confirmed by the full existing regression suite passing unmodified.
This commit is contained in:
2026-08-24 20:14:39 -05:00
parent bf1e1b5c76
commit eae8e48d43
27 changed files with 1431 additions and 68 deletions
+25 -4
View File
@@ -40,11 +40,18 @@ function egressConfig() {
function resourcePolicyFor() {
const policy = getResourcePolicy().default;
// Phase 4.1 §26 — optional; null/unset (the default) means no cap is enforced, matching the
// Phase 4 rule against fabricating a concurrency number before it's measured. Set this from
// infra/scripts/capacity-model.js's real output once the host exists (see
// infra/environments/production/capacity.yml.example's hard-admission-control field) — see
// docs/PHASE4_1_PREPRODUCTION_HARDENING.md §26.
const admission = getResourcePolicy().admission_control || {};
return {
cpuLimit: policy.cpu_limit,
memLimitMb: policy.mem_limit_mb,
pidsLimit: policy.pids_limit,
storageLimitMb: policy.storage_limit_mb,
maxConcurrentLabs: admission.max_concurrent_labs || null,
};
}
@@ -81,7 +88,10 @@ function isValidUUID(v) {
function asyncRoute(fn) {
return (req, res) => fn(req, res).catch((e) => {
console.error(`[${req.requestId}]`, e);
const status = e.code === 'NOT_FOUND' ? 404 : e.code === 'ALREADY_EXISTS' ? 409 : 500;
// CAPACITY_EXCEEDED (Phase 4.1 §26) is 503, not 500 — the host being full is an expected,
// recoverable operational state, not an application error. See lifecycle.js's admission
// check, which runs BEFORE any Docker/network resources are touched.
const status = e.code === 'NOT_FOUND' ? 404 : e.code === 'ALREADY_EXISTS' ? 409 : e.code === 'CAPACITY_EXCEEDED' ? 503 : 500;
res.status(status).json({ error: e.message, code: e.code || 'INTERNAL', request_id: req.requestId });
});
}
@@ -98,6 +108,14 @@ app.get('/health', asyncRoute(async (req, res) => {
res.status(ok ? 200 : 503).json({ ok, db: dbOk, lab_hosts: hostChecks });
}));
// GET /capacity — Phase 4.1 §26. Internal-only, same shared-secret auth as everything else
// here — lets the Platform API's instructor dashboard show real capacity state instead of
// instructors only discovering a full host when a student's launch fails.
app.get('/capacity', asyncRoute(async (req, res) => {
const { rows } = await getPool().query(`SELECT count(*)::int AS n FROM environments WHERE status NOT IN ('destroyed', 'error')`);
res.json({ current: rows[0].n, max: resourcePolicyFor().maxConcurrentLabs });
}));
// POST /environments — { student_id }
app.post('/environments', asyncRoute(async (req, res) => {
const { student_id } = req.body || {};
@@ -130,9 +148,12 @@ app.get('/environments', asyncRoute(async (req, res) => {
// owning student_id) before proxying. Internal-only, same shared-secret auth as every other
// route here — the Gateway is the only caller, browsers never reach this directly.
app.get('/environments/by-token/:token', asyncRoute(async (req, res) => {
const { rows } = await getPool().query('SELECT * FROM environments WHERE routing_token = $1', [req.params.token]);
if (!rows.length) return res.status(404).json({ error: 'not found', request_id: req.requestId });
res.json(redactRow(rows[0]));
// Phase 4.1: also joins lab_host_routing_mode/lab_host_router_endpoint so the Gateway can
// decide direct-vs-router without knowing anything about Docker topology itself — see
// lifecycle.getEnvironmentByTokenWithRouting and docs/PHASE4_1_PREPRODUCTION_HARDENING.md §1.
const env = await lifecycle.getEnvironmentByTokenWithRouting(getPool(), req.params.token);
if (!env) return res.status(404).json({ error: 'not found', request_id: req.requestId });
res.json(redactRow(env));
}));
// GET /environments/:id/token — Phase 2A addition. Every other read path deliberately redacts