Files
loki/scripts/test/private-builders.ts
CatoandClaude 01d0c4fc65 feat(cloud): sealed builds — rootless podman and gVisor as readings, switched on per tenant by the operator (#1348)
Track 4.2 of docs/master-plan-2026-10.md, built; switched on by a reading.

- SandboxExecutor gains a podman mode (buildPodmanRunArgs): rootless as the
  tenant (--userns=keep-id), the tenant's home bound at the same path so
  sign-ins, trust and transcripts stay where the runner looks, the project's
  real path as workdir, network on, capabilities dropped,
  --cgroups=disabled so the deploy's drain still counts the processes, and
  gVisor's runsc as the OCI runtime when the box has it.
- scripts/hetzner/agent-image/Containerfile: the image (shell, git, gh,
  node, Claude Code, Codex). install-sealed-builds.sh, run by every deploy:
  rootless podman, runsc best-effort, the image built into a shared
  read-only store, and a reading in /etc/loki/sealed-builds.
- Nothing switches on by itself: new-tenant.sh writes the unit drop-in
  (LOKI_EXECUTOR=sandbox, LOKI_SANDBOX_RUNTIME=podman, runsc when read) only
  when the reading says ready AND /etc/loki/sealed-builds.on exists. The
  roadmap milestone stays unticked until a tenant builds in a container.
- Tests: sandbox-executor pins the sealed shape; private-builders pins the
  two-reading switch and that no tenant ever joins the docker group.


Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-20 13:20:21 +02:00

133 lines
6.1 KiB
TypeScript

/**
* One private builder per account (docs/architecture/private-builders.md):
* the flag grants cloud building and nothing of the fleet's; the tenant's
* root, user and unit instance derive from one short id, in TypeScript and
* in the box scripts alike; the unit is sealed. Pure: reads files.
* Run: npx tsx scripts/test/private-builders.ts
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import {
decideQueuedExecution,
isBoxRootedDir,
isCloudBuilderAllowed,
type ExecutionAccess,
} from "../../src/lib/execution-access";
import {
TENANT_DEV_ROOT_RE,
tenantDevRoot,
tenantShortId,
} from "../../src/lib/agent-execution/box-workspace-path";
const read = (p: string) => readFileSync(p, "utf8");
const uid = "3f2a9c11-6b4d-4e8a-9c1f-0a1b2c3d4e5f";
// ─── The flag is the switch, and only for building ─────────────────────────
assert.equal(isCloudBuilderAllowed({ isDefault: false, privateBuilder: false }), false);
assert.equal(isCloudBuilderAllowed({ isDefault: false, privateBuilder: true }), true);
assert.equal(isCloudBuilderAllowed({ isDefault: true, privateBuilder: false }), true);
assert.equal(isCloudBuilderAllowed(null), false);
const access = (allowed: boolean): ExecutionAccess => ({
userId: uid,
cloudBuilderAllowed: allowed,
presence: { cloud: true, local: false, any: true },
});
const tenant = decideQueuedExecution(access(true), { defaultChannel: "cloud" });
assert.ok(tenant.ok && tenant.channel === "cloud", "a private builder routes to the cloud");
const ea = read("src/lib/execution-access.ts");
assert.match(ea, /return !!user\?\.isDefault;\n\}/, "the fleet predicate is the founder alone");
assert.doesNotMatch(ea, /process\.env\.LOKI_CLOUD_BUILDER_USER_IDS/, "no environment allowlist");
for (const f of [
"src/lib/kickoff/steps.ts",
"src/app/api/projects/[id]/domain/route.ts",
"src/lib/workspace-access.ts",
])
assert.match(
read(f),
/isFleetInfrastructureAllowed/,
`${f}: the fleet's powers stay the fleet's`,
);
for (const f of ["src/app/api/control/route.ts", "src/app/api/builder/presence/route.ts"])
assert.doesNotMatch(
read(f),
/cloudBuilderAllowed: !isRuntimeAvailable\(\)\s*\? await isFleetInfrastructureAllowed/,
`${f}: the UI asks about building, not the fleet`,
);
// ─── One short id, everywhere ───────────────────────────────────────────────
assert.equal(tenantShortId(uid), "3f2a9c11");
assert.equal(tenantDevRoot(uid), "/home/lk-3f2a9c11/dev");
assert.equal(
isBoxRootedDir("/home/lk-3f2a9c11/dev/petvity"),
true,
"a tenant's clone is on the box",
);
assert.equal(TENANT_DEV_ROOT_RE.test("/home/lk-3f2a9c11/devil"), false);
const newTenant = read("scripts/hetzner/new-tenant.sh");
assert.match(newTenant, /SHORT="\$\{USER_ID:0:8\}"/, "the script derives the same short id");
assert.match(newTenant, /TENANT="lk-\$\{SHORT\}"/);
assert.match(newTenant, /UNIT="loki-box-runner@\$\{SHORT\}\.service"/);
assert.match(newTenant, /--user "\$USER_ID"/, "a token minted for that user, not the owner");
assert.match(newTenant, /PATH only; no secrets here/, "a clean shell for bash -lic");
// ─── The unit is sealed ─────────────────────────────────────────────────────
const unit = read("scripts/hetzner/loki-box-runner@.service");
for (const line of [
"User=lk-%i",
"ProtectSystem=strict",
"ProtectHome=tmpfs",
"BindPaths=/home/lk-%i",
"NoNewPrivileges=true",
"MemoryMax=3G",
"OOMPolicy=continue",
"Environment=LOKI_AGENT_ENV=allowlist",
"Environment=LOKI_BOX_DEV_ROOT=/home/lk-%i/dev",
"EnvironmentFile=-/etc/loki/runners/%i.env",
])
assert.match(unit, new RegExp(`^${line.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`, "m"), line);
assert.doesNotMatch(unit, /DATABASE_URL|GITHUB_TOKEN|\/opt\/loki\/runner\/\.env$/m, "no secrets");
assert.match(
unit,
/InaccessiblePaths=.*-\/opt\/loki\/app .*-\/opt\/loki\/runner\/\.env/,
"the app and the runner env are invisible",
);
// ─── The box reconciles the flag; the deploy carries the pieces ─────────────
const reconcile = read("scripts/hetzner/reconcile-tenants.sh");
assert.match(
reconcile,
/private_builder = true and is_default = false/,
"the founder stays on the legacy unit",
);
assert.match(reconcile, /new-tenant\.sh/);
const deploy = read("scripts/deploy-hetzner.sh");
for (const f of [
"new-tenant.sh",
"reconcile-tenants.sh",
"loki-box-runner@.service",
"box-agent-claude.md",
])
assert.ok(deploy.includes(`/scripts/hetzner/${f}`), `the deploy ships ${f} to the runner dir`);
assert.match(deploy, /install-tenant-reconcile\.sh/, "the deploy installs the timer");
assert.match(deploy, /loki-box-runner@\*/, "the deploy restarts every tenant unit");
const drain = read("scripts/hetzner/drain-and-restart-runner.sh");
assert.match(drain, /UNIT="\$\{1:-loki-box-runner\}"/, "the drain takes the unit");
assert.match(
drain,
/systemctl show -p ControlGroup/,
"and finds its cgroup wherever systemd put it",
);
// ─── Sealed builds: built, switched on only by a reading ────────────────────
assert.match(newTenant, /\/etc\/loki\/sealed-builds\.on/, "the operator's one file");
assert.match(newTenant, /grep -qx ready \/etc\/loki\/sealed-builds/, "and the box's reading");
assert.match(newTenant, /LOKI_SANDBOX_RUNTIME=podman/);
assert.match(unit, /^NoNewPrivileges=true$/m, "still sealed as a unit");
assert.doesNotMatch(unit, /LOKI_EXECUTOR/, "the template never seals by itself");
const installer = read("scripts/hetzner/install-sealed-builds.sh");
assert.doesNotMatch(installer, /usermod .*docker/, "no tenant in the docker group: that is root");
assert.match(installer, /additionalimagestores/, "one image, read-only to every tenant");
assert.match(deploy, /install-sealed-builds\.sh/, "the deploy takes the reading");
console.log("✓ private builders: the flag, one short id, a sealed unit, reconciled on the box");