gunk-dev/the-valley

main.go

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