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
+49 -1
View File
@@ -40,6 +40,21 @@ async function getEnvironment(pool, environmentId) {
return rows[0] || null;
}
// Phase 4.1 — the Gateway's read path (GET /environments/by-token/:token) needs to know HOW
// to reach the Lab, not just its Docker-internal address: is this lab_host 'direct' (Gateway
// reaches env.internal_ip itself) or 'router' (Gateway must go through that lab_host's Lab
// Host Router instead)? Joining lab_hosts here keeps that decision entirely data-driven —
// the Gateway never hardcodes or guesses a host's routing mode.
async function getEnvironmentByTokenWithRouting(pool, token) {
const { rows } = await pool.query(
`SELECT e.*, h.routing_mode AS lab_host_routing_mode, h.router_endpoint AS lab_host_router_endpoint
FROM environments e JOIN lab_hosts h ON h.lab_host_id = e.lab_host_id
WHERE e.routing_token = $1`,
[token]
);
return rows[0] || null;
}
async function listEnvironments(pool, studentId) {
const { rows } = await pool.query(
'SELECT * FROM environments WHERE student_id = $1 ORDER BY created_at DESC',
@@ -90,6 +105,23 @@ async function provisionEnvironment({ pool, studentId, resourcePolicy, imageRef,
throw Object.assign(new Error(`student '${studentId}' already has a non-terminal environment (${existing.environment_id})`), { code: 'ALREADY_EXISTS', existing });
}
// Admission control (Phase 4.1 §26) — checked BEFORE touching Docker at all, so a full host
// fails cleanly and fast (503, no partial network/container left behind to clean up) instead
// of failing mid-provision with a generic 500 once Docker itself runs out of room. No-op
// when maxConcurrentLabs isn't configured (see resourcePolicyFor's comment on why that's the
// default).
if (resourcePolicy.maxConcurrentLabs) {
const { rows } = await pool.query(
`SELECT count(*)::int AS n FROM environments WHERE status NOT IN ('destroyed', 'error')`
);
if (rows[0].n >= resourcePolicy.maxConcurrentLabs) {
throw Object.assign(
new Error('Lab capacity is temporarily full. Try again shortly.'),
{ code: 'CAPACITY_EXCEEDED', current: rows[0].n, max: resourcePolicy.maxConcurrentLabs }
);
}
}
const labHost = await selectLabHost(pool);
const docker = clientFor(labHost);
const routingToken = generateRoutingToken();
@@ -145,6 +177,15 @@ async function provisionEnvironment({ pool, studentId, resourcePolicy, imageRef,
const container = await docker.createContainer({
name: containerName,
Image: imageRef,
// Phase 4.1: labeled by routing token (not environment_id alone) so the Lab Host Router
// — which runs on this same Docker host and has no Postgres access — can resolve
// "which local container does this opaque token address" purely from local `docker ps`,
// with no cross-VM call back to the Provisioner needed at request time. See
// docs/PHASE4_1_PREPRODUCTION_HARDENING.md §1, platform/lab-router/src/router.js.
Labels: {
'rootnode.routing-token': routingToken,
'rootnode.environment-id': env.environment_id,
},
HostConfig: {
Privileged: true, // required for k3s — see docs/LAB_ARCHITECTURE.md §7, not avoidable
NetworkMode: networkName,
@@ -321,6 +362,13 @@ async function resetEnvironment({ pool, environmentId, resourcePolicy, egressCon
const container = await docker.createContainer({
name: containerName,
Image: env.image_ref,
// Phase 4.1 — same routing-token label as provisionEnvironment; a reset keeps the same
// environment_id/routing_token (same DB row) so this must relabel the RECREATED container
// the same way, or the Lab Host Router would resolve stale/no container for this token.
Labels: {
'rootnode.routing-token': env.routing_token,
'rootnode.environment-id': environmentId,
},
HostConfig: {
Privileged: true,
NetworkMode: networkName,
@@ -453,7 +501,7 @@ async function reconcileOnStartup(pool) {
}
module.exports = {
generateRoutingToken, emitEvent, getEnvironment, listEnvironments, getActiveEnvironmentForStudent,
generateRoutingToken, emitEvent, getEnvironment, getEnvironmentByTokenWithRouting, listEnvironments, getActiveEnvironmentForStudent,
provisionEnvironment, destroyEnvironment, resetEnvironment,
suspendEnvironment, resumeEnvironment, pollHealth, shortId, reconcileOnStartup,
};