gunk-dev/the-valley

signers.go

sourcebc00697b19b5main's published tip history
12.2 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//
25// When the publication is read, its signer projects file is read too, if
26// it holds one (note/signerprojects.go). A signer that file names, a
27// worker or another controller, is never this host's integrator, so while
28// it names the integrator name no statement is verified
29// (rebase/snapshot.go). A publication whose signer projects file is not a
30// file, or does not read, is not used at all.
31
32package main
33
34import (
35 "errors"
36 "fmt"
37 "io"
38 "os"
39 "strings"
40
41 "the-valley/note"
42 "the-valley/rebase"
43)
44
45const (
46 signersRef = "refs/the-valley/signers"
47 signersKeys = "known-signers"
48 signersName = "integrator-name"
49 signersProjects = "signer-projects"
50)
51
52// signerVerbs are the verbs that read how far a version has landed, and
53// so take --known-signers and --integrator-name.
54var signerVerbs = map[string]bool{
55 "status": true, "changes": true, "pending": true, "rebase": true,
56 "stack": true, "brings": true, "plan": true,
57}
58
59// setting is one of the two values, and where it came from: a flag, a
60// variable, or, when published, the ref and the repository it is in.
61type setting struct {
62 value, from string
63 published bool
64}
65
66// named is the setting as a message names it, as the kind of value it is.
67// A published file is named by where it was published alone, since the
68// file read is a temporary copy.
69func (s setting) named(kind string) string {
70 switch {
71 case s.published && kind == "the known-signers file":
72 return kind + " published at " + s.from
73 case s.published:
74 return fmt.Sprintf("%s %s published at %s", kind, s.value, s.from)
75 }
76 return fmt.Sprintf("%s %s (from %s)", kind, s.value, s.from)
77}
78
79// signerSource finds the two values for one run of a command.
80type signerSource struct {
81 g git
82 stderr io.Writer
83 keys, name setting // what the flags gave
84
85 resolved bool
86 found, signer string // what resolve answered
87 projects note.SignerProjects // and the signer projects file
88 temp string // the published file, written out for the snapshot to read
89
90 published map[string]publicationRead // each repository's publication, by its directory, once read
91}
92
93// publicationRead is what readSigners answered.
94type publicationRead struct {
95 p *publishedSigners
96 err error
97}
98
99// publication is readSigners for g's repository, read once in a run:
100// transfer verification (resolve) reads it through here, and so does a
101// listing's evidence and main's walk (listEvidence), so a listing that
102// verifies transfer statements and judges evidence reads the host's
103// signers once for each repository directory it reads.
104func (s *signerSource) publication(g git) (*publishedSigners, error) {
105 if r, read := s.published[g.dir]; read {
106 return r.p, r.err
107 }
108 p, err := readSigners(g)
109 if s.published == nil {
110 s.published = map[string]publicationRead{}
111 }
112 s.published[g.dir] = publicationRead{p, err}
113 return p, err
114}
115
116// verification is the current run's source, which run sets up. A walk
117// gives it to each step's review (walk.go).
118var verification = &signerSource{}
119
120// newVerifier is what a snapshot checks a transfer statement with: the
121// values the current run's source finds, once one is needed.
122func newVerifier() verifier { return verification.verifier() }
123
124// verifier is what a snapshot checks a transfer statement with: the
125// values s finds, once one is needed, and the user's cache directory,
126// where what it verified is remembered between commands.
127func (s *signerSource) verifier() verifier {
128 return verifier{Resolve: s.resolve, Cache: rebase.CacheDir()}
129}
130
131// given reports whether a flag or the environment gives either value:
132// the operator has said what to trust, rather than a publication.
133func (s *signerSource) given() bool {
134 return s.keys.value != "" || s.name.value != "" ||
135 os.Getenv("VALLEY_KNOWN_SIGNERS") != "" || os.Getenv("VALLEY_INTEGRATOR_SIGNER") != ""
136}
137
138// takeSignerFlags removes --known-signers and --integrator-name from args,
139// in either the --flag value or the --flag=value form, and returns what
140// is left and what they gave.
141func takeSignerFlags(args []string) (rest []string, keys, name setting, err error) {
142 for i := 0; i < len(args); i++ {
143 flag, value, inline := strings.Cut(args[i], "=")
144 var into *setting
145 switch flag {
146 case "--known-signers":
147 into = &keys
148 case "--integrator-name":
149 into = &name
150 default:
151 rest = append(rest, args[i])
152 continue
153 }
154 if !inline && i+1 < len(args) {
155 i++
156 value = args[i]
157 }
158 if value == "" {
159 return nil, keys, name, failure(flag + " needs a value")
160 }
161 *into = setting{value: value, from: flag}
162 }
163 return rest, keys, name, nil
164}
165
166// resolve is the keys file, the name and the published signer projects
167// file, or two empty strings when a statement cannot be verified. It reads
168// the publication only for a value neither a flag nor the environment
169// gives, and answers the same on every call.
170func (s *signerSource) resolve() (keys, signer string, projects map[string]map[string]bool) {
171 if s.resolved {
172 return s.found, s.signer, s.projects
173 }
174 s.resolved = true
175 k, n := s.keys, s.name
176 if k.value == "" {
177 k = setting{value: os.Getenv("VALLEY_KNOWN_SIGNERS"), from: "VALLEY_KNOWN_SIGNERS"}
178 }
179 if n.value == "" {
180 n = setting{value: os.Getenv("VALLEY_INTEGRATOR_SIGNER"), from: "VALLEY_INTEGRATOR_SIGNER"}
181 }
182 if k.value == "" || n.value == "" {
183 p, err := s.publication(s.g)
184 if err != nil {
185 fmt.Fprintf(s.stderr, "valley: %v\n", err)
186 }
187 if p != nil && k.value == "" {
188 if path, err := s.writeKeys(p.keys); err != nil {
189 fmt.Fprintf(s.stderr, "valley: could not write out the known-signers file published at %s: %v\n", p.where, err)
190 } else {
191 k = setting{value: path, from: p.where, published: true}
192 }
193 }
194 if p != nil && n.value == "" {
195 n = setting{value: p.name, from: p.where, published: true}
196 }
197 if p != nil {
198 s.projects = p.projects
199 }
200 }
201 if k.value == "" || n.value == "" {
202 return "", "", nil
203 }
204 if err := agree(k, n); err != nil {
205 fmt.Fprintf(s.stderr, "valley: %v, so no transfer statement is verified\n", err)
206 return "", "", nil
207 }
208 if s.projects.Restricts(n.value) {
209 fmt.Fprintf(s.stderr, "valley: %s is restricted to projects by the published %s, so it is not this host's integrator, and no transfer statement is verified\n", n.named("the integrator name"), signersProjects)
210 }
211 s.found, s.signer = k.value, n.value
212 return s.found, s.signer, s.projects
213}
214
215// agree checks the two values against each other: the name must be one a
216// verifier key can carry, and the file must read as verifier keys and hold
217// one under that name.
218func agree(keys, name setting) error {
219 file, signer := keys.named("the known-signers file"), name.named("the integrator name")
220 if err := note.CheckName(name.value); err != nil {
221 return fmt.Errorf("%s is no signer's name: %v", signer, err)
222 }
223 data, err := os.ReadFile(keys.value)
224 if err != nil {
225 return fmt.Errorf("%s cannot be read: %v", file, err)
226 }
227 parsed, err := note.ParseVerifierKeys(data)
228 if err != nil {
229 return fmt.Errorf("%s does not read as verifier keys: %v", file, err)
230 }
231 for _, v := range parsed {
232 if v.Name == name.value {
233 return nil
234 }
235 }
236 return fmt.Errorf("%s holds no key under %s", file, signer)
237}
238
239// writeKeys writes the published file where the snapshot reads it, for done
240// to remove.
241func (s *signerSource) writeKeys(data []byte) (string, error) {
242 f, err := os.CreateTemp("", "valley-known-signers-*")
243 if err != nil {
244 return "", err
245 }
246 _, err = f.Write(data)
247 if cerr := f.Close(); err == nil {
248 err = cerr
249 }
250 if err != nil {
251 os.Remove(f.Name())
252 return "", err
253 }
254 s.temp = f.Name()
255 return s.temp, nil
256}
257
258// done removes what resolve wrote.
259func (s *signerSource) done() {
260 if s.temp != "" {
261 os.Remove(s.temp)
262 s.temp = ""
263 }
264}
265
266// publishedSigners is the serving host's publication, and where it was
267// read. Its other files are not read here: a signed approval reads the
268// ones it needs (approvalsign.go) from the tree's entries, as the tree
269// was fetched, so a file that is missing or does not read concerns no
270// other reader.
271type publishedSigners struct {
272 keys []byte
273 name string
274 projects note.SignerProjects
275 where string
276
277 instance string // the instance repository's URL
278 oid string // the publication's tree
279 kinds map[string]string // each entry's mode and type, as ls-tree lists them
280 blobs map[string]string // each blob entry's object ID
281}
282
283// readSigners reads the serving host's publication. It is nil, with no
284// error, when there is none: origin's main names no instance repository,
285// or the instance publishes nothing at refs/the-valley/signers. A
286// publication that cannot be read, or does not read as one, is an error.
287func readSigners(g git) (*publishedSigners, error) {
288 instance, err := instanceOf(g)
289 var none noInstance
290 if errors.As(err, &none) {
291 return nil, nil
292 }
293 if err != nil {
294 return nil, fmt.Errorf("could not find the instance repository to read %s from: %v", signersRef, err)
295 }
296 g = g.restricted()
297 oid, err := remoteRef(g, instance, signersRef)
298 if err != nil {
299 return nil, fmt.Errorf("could not read %s in the instance repository %s", signersRef, instance)
300 }
301 if oid == "" {
302 return nil, nil
303 }
304 if err := fetchFrom(g, instance, signersRef, oid); err != nil {
305 return nil, err
306 }
307 p := &publishedSigners{where: signersRef + " in " + instance, instance: instance, oid: oid}
308 listing, err := g.output(true, "ls-tree", "-z", oid)
309 if err != nil {
310 return nil, fmt.Errorf("%s is not a tree", p.where)
311 }
312 blobs, kinds := map[string]string{}, map[string]string{}
313 for _, entry := range strings.Split(listing, "\x00") {
314 meta, file, _ := strings.Cut(entry, "\t")
315 f := strings.Fields(meta)
316 if len(f) != 3 {
317 continue
318 }
319 kinds[file] = f[0] + " " + f[1]
320 if f[1] == "blob" {
321 blobs[file] = f[2]
322 }
323 }
324 p.kinds, p.blobs = kinds, blobs
325 if blobs[signersKeys] == "" || blobs[signersName] == "" {
326 return nil, fmt.Errorf("%s holds no %s or no %s", p.where, signersKeys, signersName)
327 }
328 // A signer projects entry that is there but is no file is refused,
329 // rather than read as absent, which would restrict nobody.
330 if kind, ok := kinds[signersProjects]; ok && kind != "100644 blob" {
331 return nil, fmt.Errorf("%s in %s is not a file", signersProjects, p.where)
332 }
333 keys, err := g.output(true, "cat-file", "blob", blobs[signersKeys])
334 if err != nil {
335 return nil, fmt.Errorf("could not read %s in %s", signersKeys, p.where)
336 }
337 p.keys = []byte(keys + "\n")
338 if p.name, err = g.output(true, "cat-file", "blob", blobs[signersName]); err != nil {
339 return nil, fmt.Errorf("could not read %s in %s", signersName, p.where)
340 }
341 if strings.Contains(p.name, "\n") {
342 return nil, fmt.Errorf("%s in %s is more than one line", signersName, p.where)
343 }
344 if blobs[signersProjects] != "" {
345 projects, err := g.output(true, "cat-file", "blob", blobs[signersProjects])
346 if err != nil {
347 return nil, fmt.Errorf("could not read %s in %s", signersProjects, p.where)
348 }
349 if p.projects, err = note.ParseSignerProjects([]byte(projects)); err != nil {
350 return nil, fmt.Errorf("%s in %s does not read: %v", signersProjects, p.where, err)
351 }
352 }
353 return p, nil
354}