gunk-dev/the-valley

signers.go

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