Captain / Documentation / Part 4. Effects and safety

Host policies

Contents ↓

A program says what it wants to do. The host, the software that runs it, decides what it may do. captain run is one host; the platform's other tools that run Captain programs are others, each with its own rules (Part 8).

The decision has to rest with the host, because the artifact cannot make it. exec is one effect: a program that runs uname and one that runs rm both perform exec program:args:, and captain inspect classes both as mutating. A claim that a program only looks, and changes nothing, can only be enforced by something the program has no influence over. So the host consults its policy before every effect is dispatched, and a refused effect fails without anything having been attempted.

What captain run admits

Without options, captain run admits every effect it implements, with two exceptions: it writes no files, and it starts no processes.

Effect Default --allow-write DIR --allow-exec --read-only
reads and observations on the local machine admitted admitted admitted admitted
native file writes (createFileText, copyFile, removeTree, ...) refused admitted beneath DIR refused refused
exec, observeProcess refused refused admitted listed programs only
shell refused refused admitted refused
http admitted admitted admitted refused
other effects on another machine (SSH) admitted admitted admitted refused

The options combine: --allow-write DIR --allow-exec admits both. Under --read-only, --allow-write and --allow-exec change nothing.

Writes go where the host says

--allow-write DIR admits the runtime's own file writes beneath DIR, and only there. It may be given more than once. A path is admitted if the directory it names, with every link in it resolved, lies beneath one of the named directories, so a link inside DIR that points out of it does not help. Without the option, no native write is admitted at all, whatever else the program is allowed.

The write effects add rules of their own: replacing a file only if it still has the digest the program read, refusing to follow links, refusing to replace what exists (Files).

--allow-write governs the effects the runtime implements itself. It does not reach inside a program started with exec or shell: once the runtime has started touch or cp, that program writes wherever the operating system lets it. That is why starting one needs consent of its own.

Starting processes

exec, shell and observeProcess start a program that the Captain program names, on this machine or, inside an on host: block, on another one. The runtime cannot see what that program does. So captain run starts none unless it is given --allow-exec, and it decides before anything runs: it follows every call, block and effect body the entry can reach, and if any of them can start a process it refuses the entry with CAP-RUN-EXEC, naming the effects. Nothing has run, so no run log is written. The answer covers every path, not only the one a run would take: an entry that runs a command only when something fails still needs the option. captain plan says so too, on a line after the plan's hash.

Some effects start a program that the runtime chooses and the Captain program does not: http over HTTPS uses the host's curl, effects on another machine travel over ssh, and the Gump effects call the host's gump. They do not count, because their effect says exactly what they do. Nor does observeProcessLiveness, which starts nothing.

--read-only is the other way to run a program that starts processes. Its policy judges every command before it starts, and admits only reporting programs, so it needs no --allow-exec.

A run that must not change anything needs --read-only. A run that may change things through the runtime's own writes, but must not run commands, needs neither option, and gets that by default.

Read-only

--read-only installs an observation policy. It admits effects by name, and refuses everything it does not name, whoever wrote the program:

  • every local read and observation: readFileText, listDirectory, the digests, readFileOctets, observePath, the byte streams, observeProcessLiveness, and observe serverAt: (which performs no I/O);
  • exec and observeProcess for a short list of reporting programs, each with a rule for its arguments;
  • nothing else: no writes, even with --allow-write; no shell, whose command line it could not judge; no http; no effect on another machine.

The programs it admits, and how:

Rule Programs
any arguments: programs with no mode that changes anything getent, uname, findmnt, id, getconf, lscpu, lsblk, df, free, uptime
named verbs only, and the verb must be the first argument systemctl: cat, is-active, is-enabled, is-failed, list-sockets, list-timers, list-unit-files, list-units, show, status; resolvectl: query, statistics, status
named flags only; patterns and paths are free ss (its listing and filtering flags, not -K, -D or -N); rg (its listed search and output flags, not --pre or --search-zip)

Some choices in that table are worth knowing:

  • systemctl is refused -H/--host and -M/--machine even with an admitted verb, because they make it act on another machine. An option is refused under any abbreviation and inside a cluster of short options.
  • A verb must come first because an option placed before it may take the next argument as its value: only systemctl's own parser could say what systemctl -p show restart ssh runs.
  • hostname is absent because it renames the machine when given an argument (uname -n reports the name), and journalctl because some of its options rotate and delete logs.

An admitted program gets almost nothing from the environment captain run was started in: only PATH, so that a bare program name is resolved as the operator configured it, and LC_ALL=C, so that its output parses the same on every machine. Many programs read behaviour from their environment, and a policy that judged the arguments but passed on the environment could be bypassed through it.

Seeing it

desk.capt
module Example::Desk.

;; Observation: the kernel's name, from a program the read-only policy admits.
export kernel -> Text [
  r := exec program: "uname"#path args: #( "-s" ) timeout: 5s.
  ret ((r stdout) trimmed).
].

;; A native write: the runtime itself creates the file.
export note text: Text at: Path -> Text [
  w := createFileText path: at content: text.
  ret Text from: (w size).
].

;; A write by another program, which the runtime cannot see into.
export touch at: Path -> Text [
  exec program: "touch"#path args: #( (Text from: at) ) timeout: 5s.
  ret "touched".
].

export greet -> Text [
  r := shell command: "echo hello" timeout: 5s.
  ret ((r stdout) trimmed).
].
Terminal
$ captain build desk.capt -o desk.capb
built desk.capb ([..])
$ mkdir notes
$ captain run desk.capb kernel
error[CAP-RUN-EXEC]: entry `kernel` can start a process (`exec program:args:`); give --allow-exec to let it run commands, or --read-only to run only the reporting programs the observation policy lists
$ captain run desk.capb kernel --allow-exec
run log: kernel.runlog
[..]
$ captain run desk.capb kernel --read-only
run log: kernel.runlog
[..]
$ captain run desk.capb touch at=notes/a.txt --allow-exec
run log: touch.runlog
"touched"
$ ls notes
a.txt
$ captain run desk.capb touch at=notes/a.txt --read-only
run log: touch.runlog
error[CAP-RUN-EFFECT]: effect `exec program:args:` failed: the read-only observation policy does not admit program `touch`
$ captain run desk.capb greet --read-only
run log: greet.runlog
error[CAP-RUN-EFFECT]: effect `shell command:` failed: the read-only observation policy refuses effect `shell command:`
$ captain run desk.capb note text=hi at=notes/b.txt
run log: note.runlog
error[CAP-RUN-EFFECT]: effect `createFileText path:content:` failed: createFileText: this host admits no local writes; it must name the directories it allows
$ captain run desk.capb note text=hi at=notes/b.txt --allow-write notes
run log: note.runlog
"2"
$ captain run desk.capb note text=hi at=c.txt --allow-write notes
run log: note.runlog
error[CAP-RUN-EFFECT]: effect `createFileText path:content:` failed: createFileText: `c.txt` is not beneath a directory this host admits writes to
$ captain run desk.capb note text=hi at=notes/d.txt --allow-write notes --read-only
run log: note.runlog
error[CAP-RUN-EFFECT]: effect `createFileText path:content:` failed: the read-only observation policy refuses effect `createFileText path:content:`

touch wrote notes/a.txt without --allow-write: once a process has started, the runtime cannot limit it. The note entry runs no command, so it needs no --allow-exec.

A refusal by policy is an effect that failed, recorded in the run log like any other failure, so a replay reproduces it. It is never an unknown outcome: nothing was attempted, so there is nothing to reconcile.

Policies and plans are separate. A plan does not mention the policy, and an approved plan can still be refused by the host that runs it (Plans and approval).

Other hosts

The observation policy is part of the runtime library, not of the command line, and other hosts build their own from it: admitting more programs, or admitting local writes beneath a directory they name, while still refusing everything they do not name. Mate, for example, lets a program read anywhere its user may and write only beneath the directory it started in (Mate).

--allow-exec is captain run's, and so are its other options. The other hosts decide for themselves whether a program may start a process: Mate runs only the commands its policies name, and Forge and Bosun start none outside their own tasks.

SSH

Effects inside an on host: server using: #Ssh do: [ ... ] block travel over the system OpenSSH client (Frontiers). It always runs in batch mode, so it never prompts, and with strict host-key checking, so it never accepts a key it has not been told to trust. These options shape the connection:

Option Effect
--ssh-user USER log in as USER
--ssh-known-hosts PATH use this known-hosts file instead of the operator's usual ones; checking stays strict
--ssh-identity PATH use exactly this private key, and no other
--ssh-port PORT connect to this port

Two commands pin a server's host key before a program connects to it:

Terminal
$ captain ssh-inspect 203.0.113.10
256 SHA256:... 203.0.113.10 (ED25519)
$ captain ssh-accept 203.0.113.10 --fingerprint SHA256:... --known-hosts deploy_known_hosts
accepted SHA256:... for 203.0.113.10:22 in deploy_known_hosts
$ captain run deploy.capb hostnames first=203.0.113.10 second=203.0.113.11 \
    --allow-exec --ssh-user deploy --ssh-known-hosts deploy_known_hosts

ssh-inspect shows the fingerprints of the keys a server offers. ssh-accept adds to the known-hosts file only a key whose fingerprint you give it, and refuses if the server offers no such key. Check the fingerprint against one you obtained some other way, such as the server's console: the point of the step is that a person decides which key to trust. Both take --port N.

The other run options

Option Effect
--allow-exec let the program start processes (Starting processes); recorded in the run log as an allow exec line
--plan FILE.approved run only if the invocation plans to the approved hash, and mint its grants (Plans and approval)
--log FILE write the run log here instead of <entry>.runlog
--arg-file NAME=PATH take the Text argument NAME from a file, for text too long or awkward for the command line
--secret... supply secrets (Secrets, receipts and grants)
--rewrite-host A=B send http requests for host A (or A:port) to B instead, over plain HTTP

--rewrite-host exists for testing a wrapper against a local mock of a service. It applies to http only, and it turns an HTTPS request into a plain one. The run log records each rewrite, as a rewrite A B line, so a log says when its http outcomes came from a mock, and captain replay says so too (Run logs). Never use it for a real operation.