Repository navigation
Description
Problem
On each KVM host one moca-egress proxy serves every template worker's guests and writes one audit
event per connection or request (remote-worker/internal/egress/audit.go, Event: time, vm_id,
sid, host, dial_addr, method, path, status, bytes, credential by name, decision,
drop counts) as a JSON line into the host's journal. For a sandbox, sid is its workspace key, which is
the sandbox id. So the facts are recorded per sandbox, but only on the host: a sandbox's owner, or a
benchmark grader who must void a run for a download its rules forbid, cannot ask Moca what a sandbox
reached, and nothing ties those events to the sandbox's provenance.
Proposal
- Collection, on the host (
host-agent.sh, in its--wanted-onlyruns, about every 15 s): read the
proxy's journal after a cursor and post the events unchanged, in batches of up to 1000, at most 10
per run, within a 120 s budget. It never acts on the proxy and is off unlessmoca-egress.service
is active. Only this host's sandboxes' events leave the host: the wanted answer listsegressSids;
other workloads' events (Moca's P6 sessions on the same proxy) stay, counted only. A failed post
keeps the batch and cursor; a batch refused as malformed is kept on the host inegress.rejected/
and the cursor moves past it; a cursor the journal no longer holds restarts from this boot, skipping
entries already sent by a position mark. All files 0600 under$SBX_SRV. POST /v1/hosts/{host}/egress {events, cursor, oversized?}(host bearer), idempotent by (host,
cursor) →{accepted, refused, capped, invalid, dropped, cursor}. An event is stored only when its
sidis a sandbox on a worker of a build this host holds, ended at most 600 s ago; an event naming
a field the proxy does not write is refused (its JSON is the wire format); an event over 8 KiB is
sent asoversized(itssidonly).- Store (
src/egress-log.ts, Redis beside the sandbox records): a stream per sandbox (one
canonical line per event, capped at 10,000 per sandbox and 200,000 across all logs, the excess counted
as dropped), drop counts, and a per-host cursor; kept as long as the
sandbox record (its end plusSH_SANDBOX_RETENTION_SECONDS). The per-sandbox index drops entries
whose sandbox record has expired. - Read (the sandbox's owner, or an admin; else
404 sandbox_not_found):
GET /v1/sandboxes/{id}/egress?after=<next>&limit=N→{events, next, summary};summaryis
byHost(count, decisions, bytes per destination),total,dropped,droppedBy(proxy,cap,
budget,invalid),complete(dropped == 0),sealed,sha256.
?format=ndjsonreturns the whole log (application/x-ndjson). - Digest and provenance: the log's digest is the sha256 of its NDJSON (each event's canonical JSON
and a newline, in arrival order). The sandbox'sterminatedrecord (Record template provenance in a hash chain, verifiable offline #730) gainsegress: {events, sha256, dropped, droppedBy, complete}. Late events are accepted for 600 s after the end; the janitor's
sweep then seals the log and, if it changed since the end record, appends onesandboxrecord with
state: egressnaming the final log.deploy/sbx/template-onboarding/verify-provenance.py EXPORT --egress <sandbox>=<file>checks a
saved copy offline against the newest record naming it. - Drops are never silent: a proxy
audit_droppedreport is charged to every sandbox on that host's
workers within 60 s before it, so a log may read incomplete when it lost nothing, never complete when
it did. - mocactl (the client part, filed here):
mocactl sbx egress [ID] [--json | --ndjson | --verify]
(per host, and whether the log is complete;--ndjsonprints the log whose sha256 the end record
names;--verifychecks its sha256 and count against that record, exit 0 only when both match).
What the PR will contain
Split in two (about 2,820 lines in all, with tests):
- PR-29a, the control plane (about 2,030):
src/egress-log.ts,src/egress-handlers.ts,
src/host-handlers.ts(the route,egressSids),src/sandbox-handlers.ts(the end record's digest,
the seal),src/routes.ts,src/handlers.ts,src/main.ts;test/sandbox-egress.test.ts,
test/egress-redis.test.ts(Redis), the egress cases oftest/provenance.test.ts; a design
docs/specs/…-sandbox-egress-records-design.md,docs/api/openapi.yaml(a minor API version bump), CHANGELOG. - PR-29b, the host agent, the verifier and the client (about 790):
deploy/sbx/host-agent.sh's
egress step and its cases intests/host-agent.test.sh; new cases inremote-worker/internal/egress/audit_test.go
(RedactPathand the event fields as the wire format);verify-provenance.py --egress; mocactl
sbx egresswithtest/sbx-egress.test.ts. Depends on PR-29a.
Depends on
#730 (provenance), #741 (host routes and the agent), #742 (the wanted state). Related: #623 (guest
egress).
How to verify
pnpm -C packages/control-plane test -- sandbox-egress egress-redis provenance(Redis)bash deploy/sbx/tests/host-agent.test.shgo test ./remote-worker/internal/egress/...pnpm -C packages/mocactl test -- sbx-egress- On a KVM host with
moca-egressactive and an egress template: a sandbox'scurlto an allowed host
and to a denied one both appear inmocactl sbx egress <id>within a wanted run or two; after
sbx rm,mocactl sbx egress <id> --verifyexits 0 andverify-provenance.py --egresspasses on a
saved copy.
Limits / out of scope
- A secret a guest puts in a URL path segment is logged as sent. Paths are logged without query,
fragment or userinfo, and an injected credential appears by name only, but the proxy does not redact
path segments; this PR leaves the proxy unchanged and documents it. - Shipping events has been exercised by tests, not yet live with events: on deployments run so far
the proxy was off, so the logs were empty, complete, and digested as the sha256 of no bytes. - After a control-plane rollout, the sweep may seal older sandboxes' empty logs once (append-only,
0 events); expected, no guard proposed. - Moca Pi sessions' egress, request bodies and packet capture are out of scope; what the proxy allows is
the host operator's allowlist. - Deploy the control plane first: an older one answers 404 on the route, and the agent keeps its batch
and retries.
Assisted-By: Claude Code
Activity
fix(control-plane): egress budgets count bytes, kept as a running total
fix(control-plane): the egress sweep seals a log after its record, no…
docs(control-plane): egress host scoping rests on the host's inventory
fix(control-plane): the egress log's summary comes with its first pag…
feat(control-plane): stream the egress log's NDJSON export
fix(control-plane): an audit_dropped report's count is bounded
test(control-plane): the memory egress store pages by stream-id order
docs(specs): the egress design names overBudget and the retention set…
fix(sbx): the host agent reads the egress journal with --all
fix(sbx): the host agent reads egressSids again after the journal read
fix(sbx): the host agent checks its egress cursor before reading afte…
test(sbx): the egress test headers say active, and are numbered after…
fix(mocactl): sbx egress --ndjson and --verify use the log's bytes, r…
fix(mocactl): sbx egress prints only numbers as numbers, and stops on…
docs(sbx): save an egress log for the offline check once it is sealed…
fix(sbx): verify-provenance.py --egress checks the record's events an…
fix(mocactl): sbx egress takes the summary from the first page, where…
docs(control-plane): the egress log keeps an event's fields and value…
docs(sbx): the egress step keeps each event's fields and values, not …
docs(reference): change-policy names the control plane's API 1.10.0
feat(control-plane): per-sandbox egress records through the API
feat(sbx): the host agent ships the egress proxy's events to the cont…
feat(sbx): verify-provenance.py --egress and mocactl sbx egress
docs(sbx): the template onboarding guide, its step list and the folde…
docs(sbx): template governance, the process and how its rules are owned