gunk-dev/the-valley

main.go

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