gunk-dev/the-valley

main.go

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