gunk-dev/the-valley

main.go

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