gunk-dev/the-valley

main.go

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