gunk-dev/the-valley

main.go

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