terok-sandbox¶
The hardened-Podman runtime that powers terok.
terok-sandbox launches per-task containers with a credential vault, a gated git server, and egress firewall.
What it provides¶
- Hardened container lifecycle — rootless Podman containers launched through a
Sandboxfacade, services are provided by a supervisor whose lifecycle is managed by the container's OCI hooks - Credential vault — long-lived secrets stay on the host, SQLCipher-encrypted at rest. The container receives short-lived phantom tokens that are exchanged for the real value at the moment of use, scoped per route, audited per request.
- Per-task git gate — a token-authenticated HTTP mirror of the upstream repository. Tasks clone and push only through the gate; the operator chooses whether the gate forwards to upstream automatically or only on human review.
- Shield install + drive — a thin adapter that installs the terok-shield OCI hooks at setup time and drives the firewall at runtime (allow / deny / up / down).
- Clearance composed in-supervisor — every container gets a
per-container supervisor, spawned by an OCI hook, that hosts the
hub, verdict server, and desktop notifier alongside the other
services; it exits when
podman waitreturns and the poststop hook reaps it. No lingering daemons between tasks;pgrep terokis empty when no containers are running.services.clearancedecides who is asked about a refused connection — the operator, nobody (muted), or no clearance arm at all; see Clearance prompts. - Setup as one call —
terok-sandbox setupbrings the whole stack up idempotently;terok-sandbox uninstallundoes it.
Per-container supervisor¶
The supervisor composes the vault proxy, git gate, SSH signer, clearance hub, verdict server, and desktop notifier per container, each service in its own hardened child process. Lifecycle, end-to-end:
terok-sandbox preparewrites a sidecar config to<state_root>/sidecar/<container-name>.json(state root defaults to~/.local/share/terok/sandbox) and emits the podman flags for the container (the/run/terokbind-mount, shield annotations, gate token, …) — including aterok.sandbox.sidecarannotation carrying the sidecar's absolute path.- The operator (or the calling orchestrator) runs
podman run. - The OCI
createRuntimehook installed byterok-sandbox setupreads the sidecar, thenPopens a stdlib-only wrapper that supervisesterok-sandbox supervisor <id> <sidecar-path>— the actual long-running asyncio loop. On non-zero exit the wrapper re-runs the supervisor with exponential backoff, up to five attempts. - The supervisor brings up a
VerdictServerand aClearanceHubwith its desktop notifier subscriber (unless the sidecar saysservices.clearance: off), the gitGateServer(when the sidecar wires a gate), and aVaultProxyand the SSH signer (insocketortcpmode per the sidecar); awaitspodman wait <id>; tears them down in reverse order on container exit. Services degrade independently — a single failed bring-up (say, a TCP port stolen while the container sat stopped) is logged and skipped, never fatal to the rest; the container itself starts regardless. - The OCI poststop hook SIGTERMs the wrapper PID (recorded under
<state_root>/pids/supervisor-<id>.pid, SIGKILL after a 2 s grace) and unlinks the PID file. The sidecar stays: OCI hooks fire per run-cycle, so a laterpodman startre-runs step 3 with the same wiring the container's immutable env was created with — restarts come back supervised. The one field a restart may rewrite first isclearance(see Clearance prompts); the ports and tokens never change. The sidecar is removed at real teardown (terok-sandbox cleanup, terok's task delete) or by the doctor's stray sweep once the container is gone.
Operators don't usually invoke terok-sandbox supervisor directly —
it's hidden from the main help and spawned by the hook chain — but
running it under a debugger after terok-sandbox prepare is a
supported way to step through a faulty composition without involving
podman.
Clearance prompts¶
When the shield refuses a connection, the clearance hub can put the
refusal to the operator — as a desktop prompt, or as a row in a
clearance UI such as terok's — and the verdict server applies the
answer. One setting in the services: section of config.yml
decides who is asked:
| Value | Supervisor runs | The shield | Who is asked |
|---|---|---|---|
prompt (default) |
verdict + clearance | refuses | the operator, live |
mute |
verdict + clearance, hub muted from the start | refuses | nobody, until the operator unmutes the task |
off |
neither | refuses | nobody, ever |
Whatever the value, the shield records each refused destination in the
container's audit log while shield.audit is on (the default) — one
line per destination per 30 seconds, however often it is retried.
Under mute and off that log is the only record a refusal leaves:
with shield.audit off as well, nothing records it at all.
mutesilences the question, not the firewall. Every blocked connection is still refused, and its destination still recorded in the audit log whileshield.auditis on. Muting drops the prompt and nothing else: nothing is allowed, nothing is auto-denied. The hub keeps running, so the operator can unmute the running task from a clearance client; a runtime mute or unmute lasts until the container stops, and every start re-applies the configured value.offleaves the clearance arm out. Refusals stay enforced, but nobody is asked and there is no hub to unmute: the supervisor starts no clearance hub and no verdict server, so no verdict path exists that could widen the firewall, and the task runs two fewer processes. The doctor's supervisor-children check expects neither. Grant what a task needs through its egress policy instead — withshield.auditon, the audit log shows what it reached for.- A change applies at the next start of the task — create,
recreate, or a terok restart. The value travels to the supervisor in
the per-container sidecar, which every
podman startre-reads. Creation writes it; a restart that should honour a changed setting rewrites the sidecar'sclearancefield from current config just before the start (Sandbox.clearance_refresh), the way it refreshes the shield's policy tiers, and writes every other field back as it was. terok's restart does this. A running task keeps the mode it started with, and a barepodman startreplays whatever the sidecar last recorded. Embedded callers setSandboxConfig(clearance=…); orchestrators with per-project settings pass the resolved project value the same way. - A bad value never costs the supervisor. An unrecognised value in
a sidecar is read as
prompt, with a warning in the supervisor log, and a sidecar written before the setting existed prompts as it always did. An invalid value inconfig.ymlwarns and reads asprompt, and costs that key alone:services.modebeside it keeps what the operator wrote, so a typo here cannot quietly undo atcpopt-out.
How long a prompt waits¶
A prompt that nobody answers expires. clearance.verdict_ttl sets
how long the hub keeps it answerable:
clearance:
verdict_ttl: 24h # seconds, or a count with s, m, h or d; never = until the container ends
The key belongs to terok-clearance: its hub owns the grammar, and the
default is 24 hours. The value reaches the hub the way
services.clearance does. The hub's process is confined before the
hub starts and cannot read config.yml, which can hold the vault
passphrase. So the launcher reads the key and records it in the
sidecar, and the clearance child hands it to the hub that it builds.
Creation writes it, Sandbox.clearance_refresh rewrites it before a
restart, and a running task keeps the lifetime it started with.
Embedded callers set SandboxConfig(clearance_verdict_ttl=…). A value
that is not a duration leaves the hub on its default, with a warning in
the supervisor log.
Where it sits in the stack¶
terok-sandbox is the boundary layer. Above it, single-task callers (terok-executor) and multi-task orchestrators (terok) treat the sandbox as a black-box "give me a hardened container." Below it, it composes terok-shield for egress filtering and terok-clearance for the operator-in-the-loop verdict path.
The split exists so that callers do not need to understand nftables, OCI hook wiring, vault sockets, or supervisor lifecycles to get a safe container.
Installation¶
For most users this dependency is pulled in transitively by
terok-executor or terok. Install it directly only when building
a custom orchestrator on top of the sandbox API.
Quick start¶
from pathlib import Path
from terok_sandbox import RunSpec, Sandbox, SandboxConfig
sandbox = Sandbox(SandboxConfig())
sandbox.run(
RunSpec(
container_name="task-001",
image="terok-l1-cli:ubuntu-24.04",
env={},
volumes=(),
command=(),
task_dir=Path("/var/lib/myapp/task-001"),
)
)
See the developer guide for the full lifecycle and integration patterns.
Strict runtime inspection¶
Use Container.inspect() and Image.inspect() before decisions that can discard
retained state. They return ContainerInspection and ImageInspection snapshots,
or None only when absence is confirmed. Timeouts, runtime failures and malformed
metadata raise RuntimeInspectionError; they do not become empty environments or
labels that could be mistaken for an obsolete protocol.
A container snapshot includes its ID, state, creation environment, reported bind/volume mounts and an image handle pinned to the observed immutable image ID. An image snapshot includes its ID and OCI labels. Mounts are Podman's reported bind/volume projection, not the full OCI mount topology (for example, tmpfs mounts are not included). A snapshot is an observation, not a lease: callers must still account for disappearance or replacement after inspection.
Existing handle properties remain best-effort display probes. Do not use their empty/default values as evidence for destructive recovery.
Emergency runtime inventory¶
ContainerRuntime.inventory(timeout=...) returns full container IDs, names,
runtime states and host-recorded annotations. It does not filter by task-name
prefix or read agent-controlled status files. Discovery errors and malformed or
incomplete records raise rather than appearing as an empty host.
ContainerRuntime.force_stop(container_id, timeout=...) targets a full ID and
sends SIGKILL directly, including to paused or stopping containers, without
unpausing them first. Command errors and timeouts propagate. An orchestrator
should select ownership using trusted annotations and impose an
overall containment deadline, rather than serially waiting for other cleanup.