sidecar
sidecar
¶
Sidecar config + per-container path resolution for the supervisor.
The parent supervisor and every service child read the same per-container
sidecar JSON — written at container-creation time by
write_sidecar and pinned by the
terok.sandbox.sidecar annotation — so the config schema
(SidecarConfig) and
the socket/log layout it resolves to
(SupervisorPaths)
live here, imported by both the parent
(main) and the child runners
(children). Keeping them in their
own module (rather than on main) is what lets a child import the
config vocabulary without importing the parent's process-supervision
logic.
The write side lives here too, beside the reader whose rules it has to
satisfy: store_sidecar
is the one writer. The edits a sidecar takes after creation are
refresh_clearance
— its two clearance fields, rewritten from current config before a restart —
and the desktop wiring kept by
record_wayland and
update_wayland;
remove_sidecar is the
one removal. They take turns under one lock per sidecar directory, so
none of them lands between another's check and its act.
SERVICE_NAMES = ('verdict', 'clearance', 'gate', 'vault', 'signer', 'wayland')
module-attribute
¶
OPTIONAL_SERVICES = frozenset({'wayland'})
module-attribute
¶
SidecarConfig(broker_context, container_name, ipc_mode, db_path, runtime_dir, routes_path=None, vault_systemd_creds_file=None, credentials_use_desktop_keyring=False, credentials_passphrase_command=None, scope_id=None, project_id='', task_id='', tcp_port=None, ssh_signer_port=None, gate_port=None, gate_base_path=None, gate_token=None, dossier_path=None, allow_debugger=False, clearance='prompt', clearance_verdict_ttl=None, _resolved_passphrase=None, wayland=WaylandSession())
dataclass
¶
Per-container config the supervisor reads from the sidecar JSON.
Written at container-creation time by
write_sidecar (terok-executor
routes through the same writer) and read back by the OCI hook on
every podman start. Keyed by container name
(<state>/sidecar/<name>.json); the terok.sandbox.sidecar
annotation pins the absolute path. The file persists across
stop/start cycles — its ports and tokens must keep matching the
container's immutable env — and is removed at real teardown by
remove_container_state.
Restarts refresh clearance and clearance_verdict_ttl from
current config, and the selected Wayland session endpoint without
changing the saved mount policy.
broker_context
instance-attribute
¶
container_name
instance-attribute
¶
ipc_mode
instance-attribute
¶
db_path
instance-attribute
¶
runtime_dir
instance-attribute
¶
/run/user/<host_uid>/terok/sandbox — pinned by the launch path
because the supervisor cannot re-derive it from inside crun's
rootless user namespace (its os.getuid() is 0 there, which
misroutes generic resolvers to the root-only /run/terok).
routes_path = None
class-attribute
instance-attribute
¶
Vault routing table captured by the launch process.
None derives routes.json beside db_path for sidecars made
outside the canonical writer.
vault_systemd_creds_file = None
class-attribute
instance-attribute
¶
Machine-bound passphrase credential captured by the launch process.
None derives vault.passphrase.cred beside db_path.
credentials_use_desktop_keyring = False
class-attribute
instance-attribute
¶
Whether the pre-confinement passphrase walk may consult the desktop keyring.
credentials_passphrase_command = None
class-attribute
instance-attribute
¶
Optional helper the child executes before installing Landlock.
scope_id = None
class-attribute
instance-attribute
¶
project_id = ''
class-attribute
instance-attribute
¶
task_id = ''
class-attribute
instance-attribute
¶
tcp_port = None
class-attribute
instance-attribute
¶
Per-container TCP port for the vault proxy in TCP mode.
None in socket mode (the path is derived from the
container ID, not carried here).
ssh_signer_port = None
class-attribute
instance-attribute
¶
Per-container TCP port for the SSH signer in TCP mode.
None in socket mode.
gate_port = None
class-attribute
instance-attribute
¶
Per-container TCP port for the git gate in TCP mode.
None in socket mode.
gate_base_path = None
class-attribute
instance-attribute
¶
Directory holding the shared per-project bare mirrors
(<gate_base_path>/<project_id>.git). None when the gate is
not wired for this container.
gate_token = None
class-attribute
instance-attribute
¶
The single token the gate validates. Travels only via the
sidecar; None when the gate is not wired.
dossier_path = None
class-attribute
instance-attribute
¶
allow_debugger = False
class-attribute
instance-attribute
¶
Debug mode — the children leave themselves ptrace-able instead of
clearing the dumpable flag, so a debugger can attach. False (fully
hardened) unless the launch path opted the task in.
clearance = 'prompt'
class-attribute
instance-attribute
¶
Who is asked about a connection the shield refused (services.clearance).
prompt runs the verdict and clearance children and puts each
refusal to the operator; mute runs them with the hub muting this
container from its start; off runs neither (see
wired_services).
The shield refuses either way. prompt also stands in for a
sidecar written before the setting existed and for a value that
cannot be read: a bad value costs the mute, never the supervisor.
Recorded at creation and refreshed by a restart, so a changed setting reaches the supervisor at the task's next start; a running supervisor keeps the mode it started with.
clearance_verdict_ttl = None
class-attribute
instance-attribute
¶
How long an unanswered prompt stays pending (clearance.verdict_ttl).
The operator's value as the launcher read it, for the clearance child
to parse and hand to its hub: the child is confined before the hub
starts, so the hub cannot read config.yml there. None leaves
the hub on its default. It also stands in for a sidecar written before
the field existed and for a value that is not text: a bad value costs
the setting, never the supervisor.
Recorded at creation and refreshed by a restart, like clearance.
wayland = field(default_factory=WaylandSession)
class-attribute
instance-attribute
¶
Optional desktop wiring, refreshed from the invoking session on every start.
SupervisorPaths(container_id, container_runtime_dir, vault_socket, ssh_signer_socket, gate_socket, clearance_socket, events_socket, verdict_socket, control_socket, log_path)
dataclass
¶
Resolved per-container socket / log / pid locations.
Computed once at supervisor startup from the container ID and the runtime/state dirs the sidecar config doesn't carry directly.
container_id
instance-attribute
¶
container_runtime_dir
instance-attribute
¶
Per-container directory holding isolated service subdirectories.
Keyed on container_name (which the launch path knows before
podman run so it can pre-create the dir and bind-mount it as
/run/terok/ inside the container). Different containers get
different host dirs.
vault_socket
instance-attribute
¶
ssh_signer_socket
instance-attribute
¶
gate_socket
instance-attribute
¶
Per-container git-gate Unix socket in its service-only directory.
Used only in socket mode; in TCP mode the gate binds a loopback port instead.
clearance_socket
instance-attribute
¶
events_socket
instance-attribute
¶
Per-container ingester socket the shield reader and shield
up/down push raw line-JSON to. Distinct from
clearance_socket (the varlink subscriber socket operator UIs
glob): the reader speaks line-JSON, not varlink, so the produce and
subscribe roles need separate sockets.
verdict_socket
instance-attribute
¶
control_socket
instance-attribute
¶
log_path
instance-attribute
¶
wayland_socket
property
¶
The proxy's listener, independent of the other services' transport.
for_container(container_id, container_name, sidecar_path, runtime_dir)
classmethod
¶
Build the per-container path bundle.
Both anchors come from the launch path — neither is re-resolved inside the supervisor:
- runtime_dir (
/run/user/<host_uid>/terok/sandbox) for per-container sockets; carried in the sidecar because the rootless user namespace makes genericis_root-based resolvers (terok_util.namespace_runtime_dir) misroute to/run/terok. - sidecar_path's grandparent for the persistent log file;
honours whatever
paths.rootresolved to when the launch path wrote the sidecar.
Sockets carry the 12-char short container ID (podman's
display convention) rather than the full UUID — AF_UNIX's
sun_path is 108 bytes including the null terminator, and
<terok-runtime>/clearance/<64-char-uuid>/hub.sock lands at
or past that limit. Twelve characters of hex give 48 bits of
entropy, well past the no-collisions-within-one-host bar.
Logs keep the full UUID because they live on the filesystem
with no AF_UNIX limit and the full UUID is easier to grep.
Clearance / verdict / control sockets live at the
cross-package <terok>/ runtime root (parent of the
sandbox-namespaced runtime_dir) because they're owned by
terok-clearance semantically and consumed by every package
that subscribes (terok-shield's NFLOG reader, terok-clearance
TUI, …). Sandbox-specific sockets (vault, ssh-agent) live in
a per-container runtime_dir/run/<container_name>/ directory
the launch path bind-mounts at /run/terok/ inside the
container. Each service gets a child directory so its Landlock
write grant cannot replace a sibling's socket.
Source code in src/terok_sandbox/supervisor/sidecar.py
wired_services(cfg)
¶
The services cfg wires, in launch order.
Three parts of the set are conditional:
- the gate — a sidecar without a mirror path and a token has no repo to serve;
- the clearance arm, verdict and clearance together — left out when
services.clearanceisoff. The shield still refuses every blocked connection, and its audit log — kept whileshield.auditis on — is then the only record of the refused destinations; nobody is asked about them, and no verdict path is left that could widen the firewall. (mutekeeps both: the hub runs, it just starts with the container muted.) - Wayland — included only when the task requested a proxy and its host binary and desktop endpoint were resolved for this start.
The parent launches no child for a service left out, and the doctor's
children check expects none — nor any of OPTIONAL_SERVICES, whose
loss their own checks report. Vault and signer run for every container.
Source code in src/terok_sandbox/supervisor/sidecar.py
load_sidecar(sidecar_path)
¶
Read and parse the sidecar JSON at sidecar_path.
The OCI hook pinned this exact path via the
terok.sandbox.sidecar annotation, so the supervisor never
guesses — it opens the named file directly. Returns None on
any I/O / schema failure; run_supervisor surfaces that as
exit-code 2. The per-field validation lives in helpers that raise
_BadSidecar on the first bad value, so this reader stays a flat
read → validate → build.
Source code in src/terok_sandbox/supervisor/sidecar.py
store_sidecar(target, payload, *, replacing=None)
¶
Create the sidecar at target, or atomically replace it, with payload.
The one writer behind every producer of a sidecar —
write_sidecar at creation and
the edits this module makes before a restart — so the file's rules are
kept in one place:
- 0600 from the first byte. The payload can carry a live gate
token. It goes to a temp file
mkstempcreates0600beside target —O_EXCLunder a random name, so nothing planted there is followed — andfchmodpins the mode against a restrictive umask. The rename then drops whatever mode an older writer left. - Atomic. The temp file is fsync'd, then renamed over target.
The OCI hook and the supervisor re-read the file on every
podman start; they see the old sidecar or the new one, never a torn one, and a write that fails midway leaves the old file as it was. - Never through a symlink. Whatever sits at target must be a
regular file. A symlink planted there (pointing at, say,
~/.ssh/authorized_keys) is refused rather than replaced. - One change at a time. The check of what sits at target and the
rename over it run under the sidecar directory's lock, which every
change holds from its check through its act — creation, the clearance
edit and
remove_sidecaralike — so no other change lands in between. Readers take no lock: the rename hands them a whole file either way. - An edit stays an edit. replacing — the
fstatof the file a caller read, and still holds open so its inode number cannot be reused — makes the rename land only while target is still that file, unchanged since: the same inode, with the size and modification time it had. An edit never recreates a sidecar removed since it was read, nor overwrites one a relaunch has written since, and a rewrite in place by a writer that takes no lock shows in the size or the modification time.
Raises:
| Type | Description |
|---|---|
OSError
|
target is not a regular file, is no longer the file replacing describes, the directory's lock file cannot be opened, or the write failed. target is left as it was, and no temp file is left behind. |
Source code in src/terok_sandbox/supervisor/sidecar.py
refresh_clearance(sidecar_path, clearance, verdict_ttl)
¶
Rewrite an existing sidecar's two clearance fields, and nothing else.
services.clearance and clearance.verdict_ttl reach a supervisor
only through the sidecar, and every podman start re-reads it; a
restart that should honour the current settings rewrites these two
fields first. The
orchestrator-facing verb is
Sandbox.clearance_refresh.
Every other field — the ports and tokens that must keep matching the
container's immutable env, the paths, the task identity — is written
back exactly as parsed: same keys, same order, same values. A sidecar
written before a setting existed gains its key.
Strict where the reader is lenient. The reader takes any stored value
and falls back to prompt, because a bad value must never cost the
supervisor; here an unknown clearance is refused before anything is
touched, so nothing outside the vocabulary is ever written. So is a
verdict_ttl that is neither text nor None. Whether its text is a
duration is for the hub's parser to say, in the clearance child. The file
is trusted only the way the OCI hook trusts it — a regular file this
user owns, never reached through a symlink — and replaced under the
rules of store_sidecar,
the writer creation uses.
The whole edit — read, check, rename — is one change: it holds the sidecar directory's lock throughout, so a teardown or a relaunch of the same container takes its turn before the edit or after it, never in the middle. A sidecar removed first is refused as missing; a relaunch's fresh sidecar, written first, is the one edited. A missing sidecar is refused, never created: the container has no supervisor to configure, and writing one would invent wiring it was not launched with.
Raises:
| Type | Description |
|---|---|
ValueError
|
clearance is not |
FileNotFoundError
|
No sidecar exists at sidecar_path. |
OSError
|
The sidecar is a symlink, not a regular file, or owned by another user; it changed while being rewritten; the directory's lock file cannot be opened; or the open or the write failed. The sidecar is left as it was in every case. |
Source code in src/terok_sandbox/supervisor/sidecar.py
record_wayland(sidecar_path, session)
¶
Record desktop wiring only after its creation-time mounts were prepared.
Source code in src/terok_sandbox/supervisor/sidecar.py
update_wayland(sidecar_path, update)
¶
Replace the saved desktop wiring with update(saved) as one locked edit.
Storage only: what the new wiring is, and whether it may differ from the old, is update's decision. Nothing is written when it raises, or when the result requests no desktop.
Source code in src/terok_sandbox/supervisor/sidecar.py
remove_sidecar(sidecar_path)
¶
Remove the sidecar at sidecar_path, if there is one, once no other change is under way.
The removal beside
store_sidecar,
called by
remove_container_state
at real teardown. The unlink holds the sidecar directory's lock: an
edit already under way finishes first and its file goes, and an edit
that comes later finds no sidecar and is refused. None can land its
rename after the unlink and bring the sidecar back — gate token and all
— for a container that is gone.
Idempotent: a missing sidecar is a no-op, and so is a missing sidecar directory.
Raises:
| Type | Description |
|---|---|
OSError
|
The directory's lock file cannot be opened, or the unlink failed. |