Three of Captain's types are not ordinary data. Each one is a wrapper the program can hold and pass on but cannot make, open or fake:
| Type | What it is | Who makes it |
|---|---|---|
Secret[T] |
a handle to a credential | the program names it; the host supplies the bytes, inside the effect that uses them |
Receipt[T] |
evidence that an effect completed | the runtime, when an effect body succeeds |
Grant[T] |
authority to do something that needs approval | the runtime, from an approved plan |
The checker enforces all three before anything runs. These are exactly the properties a reviewer cannot check by reading: whether a token could end up in a log, whether a deletion really happened after the backup, whether a step was approved. So they are rules of the language, not conventions.
Secrets
token := Secret named: #ApiToken.Secret named: gives a Secret[Opaque]: a handle that carries the secret's
name and nothing else. The program never holds the bytes. They are looked up
by the host when an effect that takes a secret runs, and handed straight to
that effect's implementation: http ... bearer: token sends them as an
Authorization header. (For HTTPS the runtime uses the system curl, and
passes the header on its standard input, never on its command line.)
Where a secret may go
A secret handle may appear in exactly three places:
- bound to a name, as above;
- passed where a parameter is declared
Secret[...], such asbearer:; - captured by a frontier block, as a handle (Frontiers).
Everything else is refused. There is deliberately no way to reveal a secret, turn it into text, compare it or put it in a record. Rules that strict are easy to check, and a checker that is easy to trust is the point.
module Example::Compare.
export same -> bool [
a := Secret named: #Primary.
b := Secret named: #Backup.
ret a = b.
].module Example::Login.
export struct Login { user: Text, password: Secret[Opaque] }.module Example::Hand.
export token -> Secret[Opaque] [
ret Secret named: #ApiToken.
].A secret is not text, so it cannot become an argument to a program either:
module Example::Argv.
export call url: Text -> Text [
token := Secret named: #ApiToken.
r := exec program: "curl"#path args: #( "-H", token, url ) timeout: 10s.
ret (r stdout).
].A procedure may take a Secret[Opaque] parameter and pass it on to an
effect. That is how a wrapper accepts a credential from its caller.
What shows the name, and what never shows the bytes
The name of a secret is not secret, and Captain shows it wherever it helps a
reviewer. captain inspect lists the names a program uses, under
secrets. A plan marks each node that uses one:
module Example::Status.
;; The HTTP status a service answers with, asking with a bearer token.
export status of: Url -> i32 [
token := Secret named: #ApiToken.
r := http get: of bearer: token timeout: 5s.
ret (r status).
].$ captain plan status.capt status of=http://127.0.0.1:9/health
...
node 1: `http get:` on LocalMachine
get: "http://127.0.0.1:9/health"#url
bearer: Secret(#ApiToken)
timeout: 5s
secrets: #ApiToken (names only; bytes at transport)
bounds: timeout 5s
...The bytes appear nowhere the program can reach: not in a value, not in a plan, not in a diagnostic, not in the run log. A run log records each effect's request by a digest of its arguments, and a secret argument is digested by its name. So a run can be replayed without the secret.
What an effect returns is data, and the run log records it in full. If a service sends a credential back in a response body, that body is in the log like any other.
Giving a program its secrets
captain run takes secrets from the invocation, never from the program:
| Option | Where the value comes from |
|---|---|
--secret-file NAME=PATH |
a regular file with mode 0600 or stricter; trailing line breaks are removed, and an empty file is refused |
--secret-env NAME=ENV |
the environment variable ENV, which must not be empty |
--secret-fd-env NAME=ENV |
an inherited file descriptor whose number is in ENV: between 3 and 1024, at most 64 KiB of UTF-8; for a parent process that hands a secret over without writing it anywhere |
CAPTAIN_SECRET_NAME |
an environment variable with that exact name |
--secret NAME=VALUE |
the command line itself; other users of the machine may see it in the process list, so keep it for tests |
A secret the program names but the host does not supply fails the effect that needs it, when it runs:
$ captain build status.capt -o status.capb
built status.capb ([..])
$ captain run status.capb status of=http://127.0.0.1:9/health
run log: status.runlog
error[CAP-RUN-EFFECT]: effect `http get:` failed: secret #ApiToken is not provided (use --secret ApiToken=... or CAPTAIN_SECRET_ApiToken)
$ printf 'not-a-real-token\n' > token.txt
$ chmod 644 token.txt
$ captain run status.capb status of=http://127.0.0.1:9/health --secret-file ApiToken=token.txt
error: secret #ApiToken: token.txt is readable or writable by group/others; require mode 0600 or stricter
$ chmod 600 token.txt
$ captain run status.capb status of=http://127.0.0.1:9/health --secret-file ApiToken=token.txt
run log: status.runlog
[..]
$ grep -c not-a-real-token status.runlog
0The last run makes a request to port 9 of the local machine, where usually nothing listens, so it fails to connect; either way the token is not in the log.
The specification also gives each frontier a secret scope, so that a frontier with no access to a secret is refused before the bytes could travel. Scopes are written in frontier declarations but not yet enforced: today the host that runs the program supplies every secret.
Receipts
An effect that returns Receipt[T] gives back proof that it ran. The body
does not make the receipt; the runtime mints it when the body completes, and
a failed body mints nothing (Effects).
No expression can construct one:
module Example::Forge.
export struct Saved {}.
export fake -> Receipt[Saved] [
ret Receipt[Saved] {}.
].The use of a receipt is as a parameter. An effect that must come after another demands the earlier one's receipt, and the checker will not let a program call it without one. Ordering mistakes become type errors, found before anything runs.
module Example::Archive.
;; What the two receipts attest to.
export struct Saved {}.
export struct Cleared {}.
;; Keep a copy of a file's text.
effect save on: LocalMachine file: Path copy: Path -> Receipt[Saved] [
f := readFileText path: file maxBytes: 1MiB.
createFileText path: copy content: (f content).
].
;; Empty a file. It demands proof that a copy was saved, and approval.
effect clear on: LocalMachine file: Path savedBy: Receipt[Saved]
authorizedBy: Grant[Destroy] -> Receipt[Cleared] [
f := readFileText path: file maxBytes: 1MiB.
writeFileText path: file content: "" replacing: (f digest).
].
export archive file: Path copy: Path authorizedBy: Grant[Destroy]
-> Receipt[Cleared] [
saved := save file: file copy: copy.
ret clear file: file savedBy: saved authorizedBy: authorizedBy.
].Leaving out the receipt is refused:
module Example::Careless.
export struct Saved {}.
export struct Cleared {}.
effect clear on: LocalMachine file: Path savedBy: Receipt[Saved]
-> Receipt[Cleared] [
].
export wipe file: Path -> Receipt[Cleared] [
ret clear file: file.
].So is handing over a receipt of the wrong kind:
module Example::Mixup.
export struct Saved {}.
export struct Cleared {}.
effect save file: Path -> Receipt[Saved] [
].
effect clear file: Path savedBy: Receipt[Saved] -> Receipt[Cleared] [
].
export twice file: Path [
first := clear file: file savedBy: (save file: file).
clear file: file savedBy: first.
].Receipts are ordinary values otherwise: they can be stored, copied, returned
and captured by a frontier block. A receipt records the effect that minted
it, the frontier it ran on and an identity derived from the program, the
effect and its arguments. Running archive shows one (the run is in
Grants, below).
When a receipt is presented to an effect, the runtime checks that this
program minted it, for the effect and arguments it names: a receipt that a
host made up instead is refused with CAP-RUN-RECEIPT, and the effect does
not run.
Two parts of the design are not built yet. The runtime does not check that a receipt was issued for the same resource the effect acts on: the type gives the order, not the identity. And there is no single-use receipt: any receipt can be presented any number of times.
Grants
A Grant[T] is authority to perform something that needs approval. An
effect that must not run unapproved demands one as a parameter, like
clear above. Like a receipt, a grant cannot be constructed:
module Example::Self.
export forge -> i32 [
g := Grant[Destroy] {}.
ret 1.
].The only grants are minted by captain run --plan, for the Grant[...]
parameters of the entry point, from an approval of the plan
(Plans and approval). An entry point that takes
one is classed destructive by captain inspect, and cannot run any other
way:
$ captain build archive.capt -o archive.capb
built archive.capb ([..])
$ mkdir logs
$ printf 'started\nstopped\n' > logs/app.log
$ captain run archive.capb archive file=logs/app.log copy=logs/app.log.1
error: `archive file:copy:authorizedBy:` demands Grant[Destroy]: plan it, approve the plan, then run with --plan <approval file>
$ captain plan archive.capb archive file=logs/app.log copy=logs/app.log.1
...
authorities required (minted on approval, bound to this plan's hash):
Grant[Destroy]
...
$ captain approve archive.capp
approved plan sha256 [..]
minted Grant[Destroy] (bound to this plan)
approval file: archive.capp.approved
$ captain run archive.capb archive file=logs/app.log copy=logs/app.log.1 \
--plan archive.capp.approved --allow-write logs
run log: archive.runlog
Receipt[Cleared] { effect: clear file:savedBy:authorizedBy:, frontier: LocalMachine, identity: [..] }
$ cat logs/app.log.1
started
stoppedA grant cannot be given as text on the command line, and the virtual
machine checks again, before it dispatches anything, that an effect which
demands a grant holds one. Reaching such an effect without one fails with
CAP-RUN-GRANT.
Destruction needs more authority than creation
Destroy is the prelude's general grant subject; a module may use its own
type, as Grant[Posted] does in the previous chapter.
The rule the platform's wrappers follow is that effects which create do not
demand a grant, and effects which destroy do: a cloud wrapper's
destroyDroplet takes authorization: Grant[Destroy], its
createDroplet takes nothing. A program can then create freely, but it
cannot delete anything without a person having approved that exact plan.
The language does not know which effects destroy. It is the signature
written by the wrapper's author that makes an effect demand a grant, and a
reviewer of a wrapper library should check those signatures. The
specification also describes an authority clause that would mint some
grants automatically by policy and require approval for others; it belongs
to cluster declarations, which do not run yet.