gunk-dev/the-valley

signers.go

source695d65eb4898main's published tip history
8.4 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. The host writes it, and no push may.
13//
14// The instance repository is the one --all reads the project list from
15// (list.go). Nothing is read until a snapshot verifies its first
16// statement, so a command that verifies none reads no more than it did
17// before. A value a flag or the environment gives is used whatever the
18// host publishes. Before the two values are used they are checked against
19// each other: the file must read as verifier keys and hold a key under the
20// name. When it does not, the command says why, naming where each value
21// came from, and verifies no statement.
22
23package main
24
25import (
26 "errors"
27 "fmt"
28 "io"
29 "os"
30 "strings"
31
32 "the-valley/note"
33)
34
35const (
36 signersRef = "refs/the-valley/signers"
37 signersKeys = "known-signers"
38 signersName = "integrator-name"
39)
40
41// signerVerbs are the verbs that read how far a version has landed, and
42// so take --known-signers and --integrator-name.
43var signerVerbs = map[string]bool{
44 "status": true, "changes": true, "pending": true, "rebase": true,
45 "stack": true, "brings": true, "plan": true,
46}
47
48// setting is one of the two values, and where it came from: a flag, a
49// variable, or, when published, the ref and the repository it is in.
50type setting struct {
51 value, from string
52 published bool
53}
54
55// named is the setting as a message names it, as the kind of value it is.
56// A published file is named by where it was published alone, since the
57// file read is a temporary copy.
58func (s setting) named(kind string) string {
59 switch {
60 case s.published && kind == "the known-signers file":
61 return kind + " published at " + s.from
62 case s.published:
63 return fmt.Sprintf("%s %s published at %s", kind, s.value, s.from)
64 }
65 return fmt.Sprintf("%s %s (from %s)", kind, s.value, s.from)
66}
67
68// signerSource finds the two values for one run of a command.
69type signerSource struct {
70 g git
71 stderr io.Writer
72 keys, name setting // what the flags gave
73
74 resolved bool
75 found, signer string // what resolve answered
76 temp string // the published file, written out for attest
77}
78
79// verification is the current run's source, which run sets up.
80var verification = &signerSource{}
81
82// newVerifier is what a snapshot checks a transfer statement with: the
83// values the current run's source finds, once one is needed.
84func newVerifier() verifier {
85 return verifier{Attest: "attest", Resolve: verification.resolve}
86}
87
88// takeSignerFlags removes --known-signers and --integrator-name from args,
89// in either the --flag value or the --flag=value form, and returns what
90// is left and what they gave.
91func takeSignerFlags(args []string) (rest []string, keys, name setting, err error) {
92 for i := 0; i < len(args); i++ {
93 flag, value, inline := strings.Cut(args[i], "=")
94 var into *setting
95 switch flag {
96 case "--known-signers":
97 into = &keys
98 case "--integrator-name":
99 into = &name
100 default:
101 rest = append(rest, args[i])
102 continue
103 }
104 if !inline && i+1 < len(args) {
105 i++
106 value = args[i]
107 }
108 if value == "" {
109 return nil, keys, name, failure(flag + " needs a value")
110 }
111 *into = setting{value: value, from: flag}
112 }
113 return rest, keys, name, nil
114}
115
116// resolve is the keys file and the name, or two empty strings when a
117// statement cannot be verified. It reads the publication only for a value
118// neither a flag nor the environment gives, and answers the same on every
119// call.
120func (s *signerSource) resolve() (keys, signer string) {
121 if s.resolved {
122 return s.found, s.signer
123 }
124 s.resolved = true
125 k, n := s.keys, s.name
126 if k.value == "" {
127 k = setting{value: os.Getenv("VALLEY_KNOWN_SIGNERS"), from: "VALLEY_KNOWN_SIGNERS"}
128 }
129 if n.value == "" {
130 n = setting{value: os.Getenv("VALLEY_INTEGRATOR_SIGNER"), from: "VALLEY_INTEGRATOR_SIGNER"}
131 }
132 if k.value == "" || n.value == "" {
133 p, err := readSigners(s.g)
134 if err != nil {
135 fmt.Fprintf(s.stderr, "valley: %v\n", err)
136 }
137 if p != nil && k.value == "" {
138 if path, err := s.writeKeys(p.keys); err != nil {
139 fmt.Fprintf(s.stderr, "valley: could not write out the known-signers file published at %s: %v\n", p.where, err)
140 } else {
141 k = setting{value: path, from: p.where, published: true}
142 }
143 }
144 if p != nil && n.value == "" {
145 n = setting{value: p.name, from: p.where, published: true}
146 }
147 }
148 if k.value == "" || n.value == "" {
149 return "", ""
150 }
151 if err := agree(k, n); err != nil {
152 fmt.Fprintf(s.stderr, "valley: %v, so no transfer statement is verified\n", err)
153 return "", ""
154 }
155 s.found, s.signer = k.value, n.value
156 return s.found, s.signer
157}
158
159// agree checks the two values against each other: the name must be one a
160// verifier key can carry, and the file must read as verifier keys and hold
161// one under that name.
162func agree(keys, name setting) error {
163 file, signer := keys.named("the known-signers file"), name.named("the integrator name")
164 if err := note.CheckName(name.value); err != nil {
165 return fmt.Errorf("%s is no signer's name: %v", signer, err)
166 }
167 data, err := os.ReadFile(keys.value)
168 if err != nil {
169 return fmt.Errorf("%s cannot be read: %v", file, err)
170 }
171 parsed, err := note.ParseVerifierKeys(data)
172 if err != nil {
173 return fmt.Errorf("%s does not read as verifier keys: %v", file, err)
174 }
175 for _, v := range parsed {
176 if v.Name == name.value {
177 return nil
178 }
179 }
180 return fmt.Errorf("%s holds no key under %s", file, signer)
181}
182
183// writeKeys writes the published file where attest can read it, for done
184// to remove.
185func (s *signerSource) writeKeys(data []byte) (string, error) {
186 f, err := os.CreateTemp("", "valley-known-signers-*")
187 if err != nil {
188 return "", err
189 }
190 _, err = f.Write(data)
191 if cerr := f.Close(); err == nil {
192 err = cerr
193 }
194 if err != nil {
195 os.Remove(f.Name())
196 return "", err
197 }
198 s.temp = f.Name()
199 return s.temp, nil
200}
201
202// done removes what resolve wrote.
203func (s *signerSource) done() {
204 if s.temp != "" {
205 os.Remove(s.temp)
206 s.temp = ""
207 }
208}
209
210// publishedSigners is the serving host's publication, and where it was
211// read.
212type publishedSigners struct {
213 keys []byte
214 name string
215 where string
216}
217
218// readSigners reads the serving host's publication. It is nil, with no
219// error, when there is none: origin's main names no instance repository,
220// or the instance publishes nothing at refs/the-valley/signers. A
221// publication that cannot be read, or does not read as one, is an error.
222func readSigners(g git) (*publishedSigners, error) {
223 instance, err := instanceOf(g)
224 var none noInstance
225 if errors.As(err, &none) {
226 return nil, nil
227 }
228 if err != nil {
229 return nil, fmt.Errorf("could not find the instance repository to read %s from: %v", signersRef, err)
230 }
231 g = g.restricted()
232 oid, err := remoteRef(g, instance, signersRef)
233 if err != nil {
234 return nil, fmt.Errorf("could not read %s in the instance repository %s", signersRef, instance)
235 }
236 if oid == "" {
237 return nil, nil
238 }
239 if err := fetchFrom(g, instance, signersRef, oid); err != nil {
240 return nil, err
241 }
242 p := &publishedSigners{where: signersRef + " in " + instance}
243 listing, err := g.output(true, "ls-tree", "-z", oid)
244 if err != nil {
245 return nil, fmt.Errorf("%s is not a tree", p.where)
246 }
247 blobs := map[string]string{}
248 for _, entry := range strings.Split(listing, "\x00") {
249 meta, file, _ := strings.Cut(entry, "\t")
250 if f := strings.Fields(meta); len(f) == 3 && f[1] == "blob" {
251 blobs[file] = f[2]
252 }
253 }
254 if blobs[signersKeys] == "" || blobs[signersName] == "" {
255 return nil, fmt.Errorf("%s holds no %s or no %s", p.where, signersKeys, signersName)
256 }
257 keys, err := g.output(true, "cat-file", "blob", blobs[signersKeys])
258 if err != nil {
259 return nil, fmt.Errorf("could not read %s in %s", signersKeys, p.where)
260 }
261 p.keys = []byte(keys + "\n")
262 if p.name, err = g.output(true, "cat-file", "blob", blobs[signersName]); err != nil {
263 return nil, fmt.Errorf("could not read %s in %s", signersName, p.where)
264 }
265 if strings.Contains(p.name, "\n") {
266 return nil, fmt.Errorf("%s in %s is more than one line", signersName, p.where)
267 }
268 return p, nil
269}