gunk-dev/the-valley

signers.go

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