gunk-dev/the-valley

main.go

source68d12192bbd3main's published tip history
10.9 KiB
1// attest composes, signs, stores and verifies a valley attestation: a
2// host-signed statement that a check ran over a tree with a result
3// (dcr-0de694f). The statement's shape is schema/attestation.cue; this
4// program only composes documents that satisfy it.
5package main
6
7import (
8 "flag"
9 "fmt"
10 "os"
11
12 "the-valley/note/statement"
13)
14
15const usage = `usage: attest <command> [flags]
16
17 run run a repository's checks and store a signed attestation
18 verify check a note's signatures and re-check its digests
19 digest print the valley tree digest of a revision
20 inputs print a pure check's input-closure digest, building nothing
21 render write a json document as the statement text a signature covers
22 sign sign statement text, or add a signature to a note
23 key print the verifier key a reader is given for a signing key
24 help print this message
25
26A statement is signed as a note: the statement text, a blank line, and one
27signature line per signer, each an em-dash, the signer's name, and base64
28of a key hash and a raw Ed25519 signature over the text. Several signers
29over one statement are sibling lines under one text. A note is stored at a
30ref keyed by the subject digest:
31` + refPrefix + `/<tree digest>/<signer key hash>.
32The host signs; there is no unsigned mode.
33
34Every repository read — the tree digested, exported and verified — is made
35with git's replacement refs off, and with no GIT_ variable inherited but
36safe.directory entries and no user or system configuration read. A
37refs/replace/* in the repository cannot make a revision read as some other
38tree. A push to a remote keeps the person's own configuration.
39
40run flags:
41 --repo DIR repository to attest (default: the git toplevel here)
42 --rev REV revision whose tree is the subject (default: HEAD)
43 --check NAME[=ATTR] flake check to run, repeatable; defaults to every
44 check the flake defines for this system
45 --command NAME=CMD effectful check to run, repeatable
46 --environment NAME environment an effectful statement names (default "local")
47 --key PATH ed25519 private key the host signs with (required)
48 --name NAME the name that key signs under (default: this host's)
49 --provenance FILE json run provenance, carried in every statement
50 --schema FILE attestation schema to vet against
51 --system SYSTEM flake system (default: this machine's)
52 --push REMOTE publish: one atomic push of the branch and the refs,
53 adding this run's notes to a ref the remote holds
54 --branch REF branch pushed alongside (default: the current branch)
55 --ref-only with --push, publish the attestation ref alone
56 --timings also append one JSON line per check to the timing
57 log, $XDG_STATE_HOME/valley/timings.jsonl
58 (default ~/.local/state/valley/timings.jsonl), a
59 regular file only its owner can read
60 --change ID the change ID each timing names
61 --change-version N the change version each timing names
62 --cost NAME=CLASS the cost class the policy declares for a check, for
63 its timing; repeatable
64 --slots N how many nix builds run at once (default: one per
65 CPU this process may run on)
66 --gc-roots DIR root every output the flake checks build in DIR,
67 so nix's garbage collection keeps them
68 --eval-reservations DIR
69 admit the flake checks' evaluation by memory, among
70 the runs that share DIR (needs --eval-budget)
71 --eval-estimate N the peak memory the evaluation is expected to
72 need, in bytes (default: the whole budget)
73 --eval-budget N the memory, in bytes, that the evaluations
74 admitted through DIR share
75 --eval-peak FILE write the evaluation's peak memory, in bytes, and
76 how it ended, to FILE once it ends
77
78run evaluates every flake check in one nix evaluation, then builds each
79one's derivation in a nix build of its own, up to --slots of them at
80once, so the nix daemon builds them side by side. Each check's result is its
81own build's, and a check that fails keeps its own build's output as its
82log. A check that does not evaluate is an error for that check alone.
83The --command checks run after the flake checks, one at a time.
84
85With --eval-reservations, the evaluation first makes a reservation in
86DIR, a file named by attest's pid that holds its estimate, and waits
87until its estimate plus those of every reservation made before it fit
88the budget, or until none was made before it. It removes the reservation
89before the first build. So the evaluations of runs that share DIR never
90expect more memory together than the budget, an evaluation whose
91estimate exceeds the budget runs alone, and the builds still overlap. A
92check's time does not count the wait. attest and its nix evaluations hold
93a lock on the reservation until they end, and on Linux each nix
94evaluation is killed when attest dies. A reservation that no running
95process holds does not count, so a run killed during its evaluation
96leaves nothing that counts once its evaluation is gone. So does a
97reservation file that is not a valid reservation. A reservation that
98cannot be made, or reservations that cannot be read, end the run with an
99error before anything is evaluated.
100
101An evaluation's peak memory is the largest maximum resident set size of
102the nix evaluations it ran. Its outcome is ok when every one of them
103succeeded, killed when a signal ended one, and failed otherwise.
104--eval-peak writes both on one line, such as "734003200 ok".
105
106run prints one time line per check after it finishes: its name, its
107result, how long it took, and how it was satisfied. A flake check's time
108is its evaluation, which it shares with the others, and its own build.
109The flake checks build at the same time, so their times add up to more
110than the run took. "ran" means the
111command ran or nix built or fetched the output; "cached" means the output
112was already in the local nix store; "unknown" means the store was not
113asked, which only a run with --timings does, or did not answer within 5s.
114A timing is not evidence: nothing signs, stores or pushes it. Timing
115output that cannot be written within a second is dropped, and a timing
116log that cannot be written is a warning, never a failure.
117
118With --gc-roots, every output of every flake check that builds, or was
119already in the store, gets an indirect garbage-collector root in DIR: a
120symlink named by the output's store path basename, registered with the
121nix daemon. An output is rooted once however many trees use it. Each run
122that uses an output makes its symlink afresh, so the symlink's
123modification time is when the output was last used. Removing the symlink
124lets nix collect the output. A root that cannot be added is a warning,
125never a failure. Without --gc-roots, run roots nothing.
126
127verify flags:
128 --note FILE the signed note, as stored; repeatable with --verdicts
129 --verdicts print one verdict line for each note, and nothing else
130 --known-keys FILE verifier keys the reader accepts, one per line
131 --signer NAME signer whose signature must be present, repeatable
132 --schema FILE attestation schema to vet against
133 --repo DIR repository to re-check the recorded digests against
134 --rev REV revision in it (default: HEAD)
135
136verify --verdicts checks each note as it would check that note alone,
137reading the keys and the tree once and vetting the statements in one cue
138run, each alone again when that run fails. It prints nothing until every
139note is checked, then one line for each, in the order given, and a line
140that counts them. Every value is a Go string literal:
141
142 verdict 1 verified "worker/attestations+74a84b34"
143 verdict 2 refused "tree digest mismatch: …"
144 end 2
145
146It exits 1 when any note is refused.
147
148digest flags:
149 --repo DIR repository (default: the git toplevel here)
150 --rev REV revision (default: HEAD)
151
152inputs flags:
153 --repo DIR repository (default: the git toplevel here)
154 --rev REV revision (default: HEAD)
155 --check NAME[=ATTR] flake check to digest, repeatable; defaults to every
156 check the flake defines for this system
157 --system SYSTEM flake system (default: this machine's)
158
159inputs evaluates each check over the revision's tree and prints its name,
160the digest of its input closure, and the digest of its derivation. It
161builds nothing: the integrator compares closure digests to decide whether
162a contributor's evidence transfers to a moved base, and check latency has
163no business on that path. Several checks are evaluated together, in one
164nix evaluation where nix allows it, and printed in the order given. A
165check that does not evaluate ends the command with its error, after the
166lines of the checks before it.
167
168sign and key flags:
169 --key PATH ed25519 private key
170 --name NAME the name that key signs under (default: this host's)
171
172render takes one json file, or stdin, and writes the statement text. Given
173statement text instead, it writes that text back — the form is a fixed
174point, so anything render changes was not in it. sign takes statement text
175and writes a note, or takes a note and adds a signature line to it. The
176conformance vectors in attest/conformance are rendered and verified this
177way.
178`
179
180func main() {
181 if len(os.Args) < 2 {
182 fmt.Fprint(os.Stderr, usage)
183 os.Exit(2)
184 }
185 var err error
186 switch os.Args[1] {
187 case "run":
188 err = cmdRun(os.Args[2:])
189 case "verify":
190 err = cmdVerify(os.Args[2:])
191 case "digest":
192 err = cmdDigest(os.Args[2:])
193 case "inputs":
194 err = cmdInputs(os.Args[2:])
195 case "render":
196 err = cmdRender(os.Args[2:])
197 case "sign":
198 err = cmdSign(os.Args[2:])
199 case "key":
200 err = cmdKey(os.Args[2:])
201 case "help", "-h", "--help":
202 fmt.Print(usage)
203 return
204 default:
205 fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage)
206 os.Exit(2)
207 }
208 if err != nil {
209 fmt.Fprintf(os.Stderr, "attest: %v\n", err)
210 os.Exit(1)
211 }
212}
213
214// repeatable collects a flag given more than once.
215type repeatable []string
216
217func (r *repeatable) String() string { return fmt.Sprint(*r) }
218func (r *repeatable) Set(v string) error {
219 *r = append(*r, v)
220 return nil
221}
222
223func newFlagSet(name string) *flag.FlagSet {
224 fs := flag.NewFlagSet(name, flag.ExitOnError)
225 fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
226 return fs
227}
228
229func cmdDigest(args []string) error {
230 fs := newFlagSet("digest")
231 repo := fs.String("repo", "", "repository")
232 rev := fs.String("rev", "HEAD", "revision")
233 if err := fs.Parse(args); err != nil {
234 return err
235 }
236 root, err := repoRoot(*repo)
237 if err != nil {
238 return err
239 }
240 entries, err := gitTreeEntries(root, *rev)
241 if err != nil {
242 return err
243 }
244 fmt.Printf("%s:%s\n", statement.TreeScheme, statement.TreeDigest(entries))
245 return nil
246}
247
248func repoRoot(dir string) (string, error) {
249 if dir == "" {
250 dir = "."
251 }
252 return gitLine(dir, "rev-parse", "--show-toplevel")
253}