gunk-dev/the-valley

signers.go

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