gunk-dev/the-valley

main.go

sourceb7b15bf706a0main's published tip history
8.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
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
65run evaluates every flake check in one nix evaluation, then builds each
66one's derivation in a nix build of its own, up to --slots of them at
67once, so the nix daemon builds them side by side. Each check's result is its
68own build's, and a check that fails keeps its own build's output as its
69log. A check that does not evaluate is an error for that check alone.
70The --command checks run after the flake checks, one at a time.
71
72run prints one time line per check after it finishes: its name, its
73result, how long it took, and how it was satisfied. A flake check's time
74is its evaluation, which it shares with the others, and its own build.
75The flake checks build at the same time, so their times add up to more
76than the run took. "ran" means the
77command ran or nix built or fetched the output; "cached" means the output
78was already in the local nix store; "unknown" means the store was not
79asked, which only a run with --timings does, or did not answer within 5s.
80A timing is not evidence: nothing signs, stores or pushes it. Timing
81output that cannot be written within a second is dropped, and a timing
82log that cannot be written is a warning, never a failure.
83
84With --gc-roots, every output of every flake check that builds, or was
85already in the store, gets an indirect garbage-collector root in DIR: a
86symlink named by the output's store path basename, registered with the
87nix daemon. An output is rooted once however many trees use it. Each run
88that uses an output makes its symlink afresh, so the symlink's
89modification time is when the output was last used. Removing the symlink
90lets nix collect the output. A root that cannot be added is a warning,
91never a failure. Without --gc-roots, run roots nothing.
92
93verify flags:
94 --note FILE the signed note, as stored
95 --known-keys FILE verifier keys the reader accepts, one per line
96 --signer NAME signer whose signature must be present, repeatable
97 --schema FILE attestation schema to vet against
98 --repo DIR repository to re-check the recorded digests against
99 --rev REV revision in it (default: HEAD)
100
101digest flags:
102 --repo DIR repository (default: the git toplevel here)
103 --rev REV revision (default: HEAD)
104
105inputs flags:
106 --repo DIR repository (default: the git toplevel here)
107 --rev REV revision (default: HEAD)
108 --check NAME[=ATTR] flake check to digest, repeatable; defaults to every
109 check the flake defines for this system
110 --system SYSTEM flake system (default: this machine's)
111
112inputs evaluates each check over the revision's tree and prints its name,
113the digest of its input closure, and the digest of its derivation. It
114builds nothing: the integrator compares closure digests to decide whether
115a contributor's evidence transfers to a moved base, and check latency has
116no business on that path. Several checks are evaluated together, in one
117nix evaluation where nix allows it, and printed in the order given. A
118check that does not evaluate ends the command with its error, after the
119lines of the checks before it.
120
121sign and key flags:
122 --key PATH ed25519 private key
123 --name NAME the name that key signs under (default: this host's)
124
125render takes one json file, or stdin, and writes the statement text. Given
126statement text instead, it writes that text back โ€” the form is a fixed
127point, so anything render changes was not in it. sign takes statement text
128and writes a note, or takes a note and adds a signature line to it. The
129conformance vectors in attest/conformance are rendered and verified this
130way.
131`
132
133func main() {
134 if len(os.Args) < 2 {
135 fmt.Fprint(os.Stderr, usage)
136 os.Exit(2)
137 }
138 var err error
139 switch os.Args[1] {
140 case "run":
141 err = cmdRun(os.Args[2:])
142 case "verify":
143 err = cmdVerify(os.Args[2:])
144 case "digest":
145 err = cmdDigest(os.Args[2:])
146 case "inputs":
147 err = cmdInputs(os.Args[2:])
148 case "render":
149 err = cmdRender(os.Args[2:])
150 case "sign":
151 err = cmdSign(os.Args[2:])
152 case "key":
153 err = cmdKey(os.Args[2:])
154 case "help", "-h", "--help":
155 fmt.Print(usage)
156 return
157 default:
158 fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage)
159 os.Exit(2)
160 }
161 if err != nil {
162 fmt.Fprintf(os.Stderr, "attest: %v\n", err)
163 os.Exit(1)
164 }
165}
166
167// repeatable collects a flag given more than once.
168type repeatable []string
169
170func (r *repeatable) String() string { return fmt.Sprint(*r) }
171func (r *repeatable) Set(v string) error {
172 *r = append(*r, v)
173 return nil
174}
175
176func newFlagSet(name string) *flag.FlagSet {
177 fs := flag.NewFlagSet(name, flag.ExitOnError)
178 fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
179 return fs
180}
181
182func cmdDigest(args []string) error {
183 fs := newFlagSet("digest")
184 repo := fs.String("repo", "", "repository")
185 rev := fs.String("rev", "HEAD", "revision")
186 if err := fs.Parse(args); err != nil {
187 return err
188 }
189 root, err := repoRoot(*repo)
190 if err != nil {
191 return err
192 }
193 entries, err := gitTreeEntries(root, *rev)
194 if err != nil {
195 return err
196 }
197 fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries))
198 return nil
199}
200
201func repoRoot(dir string) (string, error) {
202 if dir == "" {
203 dir = "."
204 }
205 return gitLine(dir, "rev-parse", "--show-toplevel")
206}