gunk-dev/the-valley

signers.go

sourced8c2e5434360main's published tip history
8.5 KiB
1// What a transfer statement is verified with, and where each value comes
2// from.
3//
4// A transfer statement is verified with two values: the known-signers file
5// of verifier keys, and the name the integrator signs under. Each is taken
6// from the first of these that gives it:
7//
8// 1. a flag: --known-signers <file> or --integrator-name <name>;
9// 2. the environment: VALLEY_KNOWN_SIGNERS or VALLEY_INTEGRATOR_SIGNER;
10// 3. the serving host's publication: a tree at refs/the-valley/signers in
11// its instance repository, holding the file as known-signers and the
12// name as integrator-name. Only the host's machinery writes it, never
13// a push: its publisher does, and its integrator, which lands code in
14// that repository, can.
15//
16// The instance repository is the one --all reads the project list from
17// (list.go). Nothing is read until a snapshot verifies its first
18// statement, so a command that verifies none reads no more than it did
19// before. A value a flag or the environment gives is used whatever the
20// host publishes. Before the two values are used they are checked against
21// each other: the file must read as verifier keys and hold a key under the
22// name. When it does not, the command says why, naming where each value
23// came from, and verifies no statement.
24
25package main
26
27import (
28 "errors"
29 "fmt"
30 "io"
31 "os"
32 "strings"
33
34 "the-valley/note"
35)
36
37const (
38 signersRef = "refs/the-valley/signers"
39 signersKeys = "known-signers"
40 signersName = "integrator-name"
41)
42
43// signerVerbs are the verbs that read how far a version has landed, and
44// so take --known-signers and --integrator-name.
45var signerVerbs = map[string]bool{
46 "status": true, "changes": true, "pending": true, "rebase": true,
47 "stack": true, "brings": true, "plan": true,
48}
49
50// setting is one of the two values, and where it came from: a flag, a
51// variable, or, when published, the ref and the repository it is in.
52type setting struct {
53 value, from string
54 published bool
55}
56
57// named is the setting as a message names it, as the kind of value it is.
58// A published file is named by where it was published alone, since the
59// file read is a temporary copy.
60func (s setting) named(kind string) string {
61 switch {
62 case s.published && kind == "the known-signers file":
63 return kind + " published at " + s.from
64 case s.published:
65 return fmt.Sprintf("%s %s published at %s", kind, s.value, s.from)
66 }
67 return fmt.Sprintf("%s %s (from %s)", kind, s.value, s.from)
68}
69
70// signerSource finds the two values for one run of a command.
71type signerSource struct {
72 g git
73 stderr io.Writer
74 keys, name setting // what the flags gave
75
76 resolved bool
77 found, signer string // what resolve answered
78 temp string // the published file, written out for attest
79}
80
81// verification is the current run's source, which run sets up.
82var verification = &signerSource{}
83
84// newVerifier is what a snapshot checks a transfer statement with: the
85// values the current run's source finds, once one is needed.
86func newVerifier() verifier {
87 return verifier{Attest: "attest", Resolve: verification.resolve}
88}
89
90// takeSignerFlags removes --known-signers and --integrator-name from args,
91// in either the --flag value or the --flag=value form, and returns what
92// is left and what they gave.
93func takeSignerFlags(args []string) (rest []string, keys, name setting, err error) {
94 for i := 0; i < len(args); i++ {
95 flag, value, inline := strings.Cut(args[i], "=")
96 var into *setting
97 switch flag {
98 case "--known-signers":
99 into = &keys
100 case "--integrator-name":
101 into = &name
102 default:
103 rest = append(rest, args[i])
104 continue
105 }
106 if !inline && i+1 < len(args) {
107 i++
108 value = args[i]
109 }
110 if value == "" {
111 return nil, keys, name, failure(flag + " needs a value")
112 }
113 *into = setting{value: value, from: flag}
114 }
115 return rest, keys, name, nil
116}
117
118// resolve is the keys file and the name, or two empty strings when a
119// statement cannot be verified. It reads the publication only for a value
120// neither a flag nor the environment gives, and answers the same on every
121// call.
122func (s *signerSource) resolve() (keys, signer string) {
123 if s.resolved {
124 return s.found, s.signer
125 }
126 s.resolved = true
127 k, n := s.keys, s.name
128 if k.value == "" {
129 k = setting{value: os.Getenv("VALLEY_KNOWN_SIGNERS"), from: "VALLEY_KNOWN_SIGNERS"}
130 }
131 if n.value == "" {
132 n = setting{value: os.Getenv("VALLEY_INTEGRATOR_SIGNER"), from: "VALLEY_INTEGRATOR_SIGNER"}
133 }
134 if k.value == "" || n.value == "" {
135 p, err := readSigners(s.g)
136 if err != nil {
137 fmt.Fprintf(s.stderr, "valley: %v\n", err)
138 }
139 if p != nil && k.value == "" {
140 if path, err := s.writeKeys(p.keys); err != nil {
141 fmt.Fprintf(s.stderr, "valley: could not write out the known-signers file published at %s: %v\n", p.where, err)
142 } else {
143 k = setting{value: path, from: p.where, published: true}
144 }
145 }
146 if p != nil && n.value == "" {
147 n = setting{value: p.name, from: p.where, published: true}
148 }
149 }
150 if k.value == "" || n.value == "" {
151 return "", ""
152 }
153 if err := agree(k, n); err != nil {
154 fmt.Fprintf(s.stderr, "valley: %v, so no transfer statement is verified\n", err)
155 return "", ""
156 }
157 s.found, s.signer = k.value, n.value
158 return s.found, s.signer
159}
160
161// agree checks the two values against each other: the name must be one a
162// verifier key can carry, and the file must read as verifier keys and hold
163// one under that name.
164func agree(keys, name setting) error {
165 file, signer := keys.named("the known-signers file"), name.named("the integrator name")
166 if err := note.CheckName(name.value); err != nil {
167 return fmt.Errorf("%s is no signer's name: %v", signer, err)
168 }
169 data, err := os.ReadFile(keys.value)
170 if err != nil {
171 return fmt.Errorf("%s cannot be read: %v", file, err)
172 }
173 parsed, err := note.ParseVerifierKeys(data)
174 if err != nil {
175 return fmt.Errorf("%s does not read as verifier keys: %v", file, err)
176 }
177 for _, v := range parsed {
178 if v.Name == name.value {
179 return nil
180 }
181 }
182 return fmt.Errorf("%s holds no key under %s", file, signer)
183}
184
185// writeKeys writes the published file where attest can read it, for done
186// to remove.
187func (s *signerSource) writeKeys(data []byte) (string, error) {
188 f, err := os.CreateTemp("", "valley-known-signers-*")
189 if err != nil {
190 return "", err
191 }
192 _, err = f.Write(data)
193 if cerr := f.Close(); err == nil {
194 err = cerr
195 }
196 if err != nil {
197 os.Remove(f.Name())
198 return "", err
199 }
200 s.temp = f.Name()
201 return s.temp, nil
202}
203
204// done removes what resolve wrote.
205func (s *signerSource) done() {
206 if s.temp != "" {
207 os.Remove(s.temp)
208 s.temp = ""
209 }
210}
211
212// publishedSigners is the serving host's publication, and where it was
213// read.
214type publishedSigners struct {
215 keys []byte
216 name string
217 where string
218}
219
220// readSigners reads the serving host's publication. It is nil, with no
221// error, when there is none: origin's main names no instance repository,
222// or the instance publishes nothing at refs/the-valley/signers. A
223// publication that cannot be read, or does not read as one, is an error.
224func readSigners(g git) (*publishedSigners, error) {
225 instance, err := instanceOf(g)
226 var none noInstance
227 if errors.As(err, &none) {
228 return nil, nil
229 }
230 if err != nil {
231 return nil, fmt.Errorf("could not find the instance repository to read %s from: %v", signersRef, err)
232 }
233 g = g.restricted()
234 oid, err := remoteRef(g, instance, signersRef)
235 if err != nil {
236 return nil, fmt.Errorf("could not read %s in the instance repository %s", signersRef, instance)
237 }
238 if oid == "" {
239 return nil, nil
240 }
241 if err := fetchFrom(g, instance, signersRef, oid); err != nil {
242 return nil, err
243 }
244 p := &publishedSigners{where: signersRef + " in " + instance}
245 listing, err := g.output(true, "ls-tree", "-z", oid)
246 if err != nil {
247 return nil, fmt.Errorf("%s is not a tree", p.where)
248 }
249 blobs := map[string]string{}
250 for _, entry := range strings.Split(listing, "\x00") {
251 meta, file, _ := strings.Cut(entry, "\t")
252 if f := strings.Fields(meta); len(f) == 3 && f[1] == "blob" {
253 blobs[file] = f[2]
254 }
255 }
256 if blobs[signersKeys] == "" || blobs[signersName] == "" {
257 return nil, fmt.Errorf("%s holds no %s or no %s", p.where, signersKeys, signersName)
258 }
259 keys, err := g.output(true, "cat-file", "blob", blobs[signersKeys])
260 if err != nil {
261 return nil, fmt.Errorf("could not read %s in %s", signersKeys, p.where)
262 }
263 p.keys = []byte(keys + "\n")
264 if p.name, err = g.output(true, "cat-file", "blob", blobs[signersName]); err != nil {
265 return nil, fmt.Errorf("could not read %s in %s", signersName, p.where)
266 }
267 if strings.Contains(p.name, "\n") {
268 return nil, fmt.Errorf("%s in %s is more than one line", signersName, p.where)
269 }
270 return p, nil
271}