gunk-dev/the-valley

main.go

source58e68be0d151main's published tip history
10.3 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
129 --known-keys FILE verifier keys the reader accepts, one per line
130 --signer NAME signer whose signature must be present, repeatable
131 --schema FILE attestation schema to vet against
132 --repo DIR repository to re-check the recorded digests against
133 --rev REV revision in it (default: HEAD)
134
135digest flags:
136 --repo DIR repository (default: the git toplevel here)
137 --rev REV revision (default: HEAD)
138
139inputs flags:
140 --repo DIR repository (default: the git toplevel here)
141 --rev REV revision (default: HEAD)
142 --check NAME[=ATTR] flake check to digest, repeatable; defaults to every
143 check the flake defines for this system
144 --system SYSTEM flake system (default: this machine's)
145
146inputs evaluates each check over the revision's tree and prints its name,
147the digest of its input closure, and the digest of its derivation. It
148builds nothing: the integrator compares closure digests to decide whether
149a contributor's evidence transfers to a moved base, and check latency has
150no business on that path. Several checks are evaluated together, in one
151nix evaluation where nix allows it, and printed in the order given. A
152check that does not evaluate ends the command with its error, after the
153lines of the checks before it.
154
155sign and key flags:
156 --key PATH ed25519 private key
157 --name NAME the name that key signs under (default: this host's)
158
159render takes one json file, or stdin, and writes the statement text. Given
160statement text instead, it writes that text back โ€” the form is a fixed
161point, so anything render changes was not in it. sign takes statement text
162and writes a note, or takes a note and adds a signature line to it. The
163conformance vectors in attest/conformance are rendered and verified this
164way.
165`
166
167func main() {
168 if len(os.Args) < 2 {
169 fmt.Fprint(os.Stderr, usage)
170 os.Exit(2)
171 }
172 var err error
173 switch os.Args[1] {
174 case "run":
175 err = cmdRun(os.Args[2:])
176 case "verify":
177 err = cmdVerify(os.Args[2:])
178 case "digest":
179 err = cmdDigest(os.Args[2:])
180 case "inputs":
181 err = cmdInputs(os.Args[2:])
182 case "render":
183 err = cmdRender(os.Args[2:])
184 case "sign":
185 err = cmdSign(os.Args[2:])
186 case "key":
187 err = cmdKey(os.Args[2:])
188 case "help", "-h", "--help":
189 fmt.Print(usage)
190 return
191 default:
192 fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage)
193 os.Exit(2)
194 }
195 if err != nil {
196 fmt.Fprintf(os.Stderr, "attest: %v\n", err)
197 os.Exit(1)
198 }
199}
200
201// repeatable collects a flag given more than once.
202type repeatable []string
203
204func (r *repeatable) String() string { return fmt.Sprint(*r) }
205func (r *repeatable) Set(v string) error {
206 *r = append(*r, v)
207 return nil
208}
209
210func newFlagSet(name string) *flag.FlagSet {
211 fs := flag.NewFlagSet(name, flag.ExitOnError)
212 fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
213 return fs
214}
215
216func cmdDigest(args []string) error {
217 fs := newFlagSet("digest")
218 repo := fs.String("repo", "", "repository")
219 rev := fs.String("rev", "HEAD", "revision")
220 if err := fs.Parse(args); err != nil {
221 return err
222 }
223 root, err := repoRoot(*repo)
224 if err != nil {
225 return err
226 }
227 entries, err := gitTreeEntries(root, *rev)
228 if err != nil {
229 return err
230 }
231 fmt.Printf("%s:%s\n", statement.TreeScheme, statement.TreeDigest(entries))
232 return nil
233}
234
235func repoRoot(dir string) (string, error) {
236 if dir == "" {
237 dir = "."
238 }
239 return gitLine(dir, "rev-parse", "--show-toplevel")
240}