gunk-dev/the-valley

signers.go

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