gunk-dev/the-valley

main.go

source301e803586a4main's published tip history
5.7 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
53verify flags:
54 --note FILE the signed note, as stored
55 --known-keys FILE verifier keys the reader accepts, one per line
56 --signer NAME signer whose signature must be present, repeatable
57 --schema FILE attestation schema to vet against
58 --repo DIR repository to re-check the recorded digests against
59 --rev REV revision in it (default: HEAD)
60
61digest flags:
62 --repo DIR repository (default: the git toplevel here)
63 --rev REV revision (default: HEAD)
64
65inputs flags:
66 --repo DIR repository (default: the git toplevel here)
67 --rev REV revision (default: HEAD)
68 --check NAME[=ATTR] flake check to digest, repeatable; defaults to every
69 check the flake defines for this system
70 --system SYSTEM flake system (default: this machine's)
71
72inputs evaluates each check over the revision's tree and prints its name,
73the digest of its input closure, and the digest of its derivation. It
74builds nothing: the integrator compares closure digests to decide whether
75a contributor's evidence transfers to a moved base, and check latency has
76no business on that path.
77
78sign and key flags:
79 --key PATH ed25519 private key
80 --name NAME the name that key signs under (default: this host's)
81
82render takes one json file, or stdin, and writes the statement text. Given
83statement text instead, it writes that text back โ€” the form is a fixed
84point, so anything render changes was not in it. sign takes statement text
85and writes a note, or takes a note and adds a signature line to it. The
86conformance vectors in attest/conformance are rendered and verified this
87way.
88`
89
90func main() {
91 if len(os.Args) < 2 {
92 fmt.Fprint(os.Stderr, usage)
93 os.Exit(2)
94 }
95 var err error
96 switch os.Args[1] {
97 case "run":
98 err = cmdRun(os.Args[2:])
99 case "verify":
100 err = cmdVerify(os.Args[2:])
101 case "digest":
102 err = cmdDigest(os.Args[2:])
103 case "inputs":
104 err = cmdInputs(os.Args[2:])
105 case "render":
106 err = cmdRender(os.Args[2:])
107 case "sign":
108 err = cmdSign(os.Args[2:])
109 case "key":
110 err = cmdKey(os.Args[2:])
111 case "help", "-h", "--help":
112 fmt.Print(usage)
113 return
114 default:
115 fmt.Fprintf(os.Stderr, "attest: unknown command %q\n\n%s", os.Args[1], usage)
116 os.Exit(2)
117 }
118 if err != nil {
119 fmt.Fprintf(os.Stderr, "attest: %v\n", err)
120 os.Exit(1)
121 }
122}
123
124// repeatable collects a flag given more than once.
125type repeatable []string
126
127func (r *repeatable) String() string { return fmt.Sprint(*r) }
128func (r *repeatable) Set(v string) error {
129 *r = append(*r, v)
130 return nil
131}
132
133func newFlagSet(name string) *flag.FlagSet {
134 fs := flag.NewFlagSet(name, flag.ExitOnError)
135 fs.Usage = func() { fmt.Fprint(os.Stderr, usage) }
136 return fs
137}
138
139func cmdDigest(args []string) error {
140 fs := newFlagSet("digest")
141 repo := fs.String("repo", "", "repository")
142 rev := fs.String("rev", "HEAD", "revision")
143 if err := fs.Parse(args); err != nil {
144 return err
145 }
146 root, err := repoRoot(*repo)
147 if err != nil {
148 return err
149 }
150 entries, err := gitTreeEntries(root, *rev)
151 if err != nil {
152 return err
153 }
154 fmt.Printf("%s:%s\n", treeScheme, treeDigest(entries))
155 return nil
156}
157
158func repoRoot(dir string) (string, error) {
159 if dir == "" {
160 dir = "."
161 }
162 return gitLine(dir, "rev-parse", "--show-toplevel")
163}