gunk-dev/the-valley

signers.go

source8b3dd027213fmain's published tip history
8.4 KiB
1package main
2
3import (
4 "bytes"
5 "flag"
6 "fmt"
7 "os"
8 "path/filepath"
9 "strings"
10 "syscall"
11
12 "the-valley/note"
13)
14
15// signersRef is where the host publishes what a client verifies this
16// integrator's transfer statements with: a tree holding the known-signers
17// file the integrator reads, as signersKeys, and the name it signs under,
18// as signersName, one line. When the integrator is given a signer projects
19// file, the tree holds that file too, as signersProjects, so a client can
20// tell which signers' evidence counts in which projects.
21//
22// Only the host's own machinery writes it, never a push. The push hook
23// refuses every push to a refs/the-valley/* ref it does not name. On the
24// host, this publisher writes it. The integrator can write it too: the
25// instance repository is group-writable, and the integrator is in the
26// group because it lands code there. A client already trusts the
27// integrator with what lands, so the publication is not kept from it.
28//
29// signersLock is the file in the instance repository that a run holds a
30// lock on, from reading the signers file to moving the ref.
31const (
32 signersRef = "refs/the-valley/signers"
33 signersKeys = "known-signers"
34 signersName = "integrator-name"
35 signersProjects = "signer-projects"
36 signersLock = "valley-signers.lock"
37)
38
39const signersUsage = `usage: integrator publish-signers --instance-repo DIR --known-signers F [--name NAME] [--signer-projects F] [--wake DIR]
40
41Publishes, in the instance repository, the two values a client verifies
42this host's transfer statements with: the known-signers file a controller
43reads, and the name it signs under. Both go into one tree at
44refs/the-valley/signers, so a client reads both from one ref or neither.
45Given --signer-projects, the tree holds that file too, as signer-projects,
46so a client can tell which signers' evidence counts in which projects.
47
48 --instance-repo DIR the bare repository clients read the host's
49 publications from
50 --known-signers F the file a controller is given as --known-signers
51 --name NAME the name a controller signs under, as it is given
52 to watch (default: this host's, as watch defaults)
53 --signer-projects F the file a controller is given as --signer-projects;
54 without it, the publication holds none
55 --wake DIR the host check worker's wake directory: a run that
56 moves the ref creates the empty file DIR/due, so
57 the worker reads the new signers; empty marks nothing
58
59A publication that would not change is not written. One that would is
60written with a compare-and-swap on the ref. Two runs at once take turns:
61each holds a lock in the instance repository from reading the file to
62moving the ref, so a run that read an older file cannot publish it over
63a newer one.
64
65The file must read as verifier keys, the name must be one a verifier key
66can carry, and a signer projects file must read as watch reads one, so
67it may not name NAME. A file that holds no key under the name, or no key
68at all, is published all the same, because it is what the controller
69uses. So when every signing key is revoked, the publication drops the
70old keys too, and clients stop verifying with them. The run says on
71stderr that no client can verify a statement with the file. An empty
72signer projects file restricts nobody, as no file does, and is not
73published.
74`
75
76// publishSigners writes the publication, and says what it did on stdout.
77func publishSigners(args []string) error {
78 fs := flag.NewFlagSet("publish-signers", flag.ExitOnError)
79 fs.Usage = func() { fmt.Fprint(os.Stderr, signersUsage) }
80 repo := fs.String("instance-repo", "", "the instance repository")
81 known := fs.String("known-signers", "", "the file a controller is given as --known-signers")
82 name := fs.String("name", defaultSignerName(), "the name a controller signs under")
83 projects := fs.String("signer-projects", "", "the file a controller is given as --signer-projects")
84 wake := fs.String("wake", "", "the check worker's wake directory, marked due when the publication moves")
85 if err := fs.Parse(args); err != nil {
86 return err
87 }
88 if fs.NArg() > 0 {
89 return fmt.Errorf("publish-signers takes no argument, and was given %q", fs.Arg(0))
90 }
91 for _, missing := range []struct{ flag, value string }{
92 {"--instance-repo", *repo},
93 {"--known-signers", *known},
94 } {
95 if missing.value == "" {
96 return fmt.Errorf("%s is required", missing.flag)
97 }
98 }
99 unlock, err := lockSigners(*repo)
100 if err != nil {
101 return err
102 }
103 defer unlock()
104 keys, err := os.ReadFile(*known)
105 if err != nil {
106 return fmt.Errorf("reading the known-signers file: %w", err)
107 }
108 _, restrictions, err := readSignerProjects(*projects, *name)
109 if err != nil {
110 return err
111 }
112 tree, err := signersTree(*repo, keys, *name, restrictions)
113 if err != nil {
114 return err
115 }
116 // The ref is read by its exact name. With no publication yet, old is
117 // "", whatever branch is named like the ref.
118 old, _, err := resolveRef(*repo, signersRef)
119 if err != nil {
120 return err
121 }
122 if old == tree {
123 fmt.Printf("%s already publishes %s and the signers in %s\n", signersRef, *name, *known)
124 return nil
125 }
126 // The value the ref is expected to hold: the one just read, or none at
127 // all. An empty old value makes update-ref require that the ref does
128 // not exist. The lock keeps other runs out, and the compare-and-swap
129 // keeps this run from writing over anything else on the host that
130 // moved the ref since.
131 if _, err := git(*repo, "update-ref", "-m", "publish-signers", signersRef, tree, old); err != nil {
132 return err
133 }
134 fmt.Printf("%s now publishes %s and the signers in %s\n", signersRef, *name, *known)
135 // The check worker reads this publication for coverage, so a moved
136 // one marks it due. A run that published nothing new marks nothing.
137 // The ref has moved either way, so a mark that fails is said and is
138 // not this run's failure.
139 if err := markDue(*wake); err != nil {
140 fmt.Printf("the check worker was not marked due: %v\n", err)
141 } else if *wake != "" {
142 fmt.Println("the check worker is due")
143 }
144 return nil
145}
146
147// signersTree writes the publication's objects into repo and returns its
148// tree. The tree holds a signer projects file when projects is not empty.
149func signersTree(repo string, keys []byte, name string, projects []byte) (string, error) {
150 if err := note.CheckName(name); err != nil {
151 return "", fmt.Errorf("--name: %w", err)
152 }
153 // A file with no key at all reads, and is published: it is what the
154 // registry compiles to once every signing key is revoked.
155 parsed, err := note.ParseVerifierKeys(keys)
156 if err != nil {
157 return "", fmt.Errorf("the known-signers file does not read: %w", err)
158 }
159 signs := false
160 for _, k := range parsed {
161 signs = signs || k.Name == name
162 }
163 if !signs {
164 fmt.Fprintf(os.Stderr, "integrator: the known-signers file holds no key under %s, the name the controller signs under, so no client can verify its transfer statements with it\n", name)
165 }
166 keysBlob, err := hashObject(repo, keys)
167 if err != nil {
168 return "", err
169 }
170 nameBlob, err := hashObject(repo, []byte(name+"\n"))
171 if err != nil {
172 return "", err
173 }
174 entries := fmt.Sprintf("100644 blob %s\t%s\n100644 blob %s\t%s\n", nameBlob, signersName, keysBlob, signersKeys)
175 if len(projects) > 0 {
176 projectsBlob, err := hashObject(repo, projects)
177 if err != nil {
178 return "", err
179 }
180 entries += fmt.Sprintf("100644 blob %s\t%s\n", projectsBlob, signersProjects)
181 }
182 out, err := gitInput(repo, []byte(entries), "mktree")
183 if err != nil {
184 return "", err
185 }
186 return strings.TrimSpace(string(out)), nil
187}
188
189// lockSigners takes the publication's lock and returns what lets it go. A
190// run that asks while another holds it waits its turn. The lock is
191// flock's, on signersLock, so the kernel lets it go when the run ends
192// however it ends, and a run that dies leaves nothing to clear.
193func lockSigners(repo string) (func(), error) {
194 path := filepath.Join(repo, signersLock)
195 f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0o644)
196 if err != nil {
197 return nil, fmt.Errorf("could not open the publication's lock: %w", err)
198 }
199 if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
200 f.Close()
201 return nil, fmt.Errorf("could not lock %s: %w", path, err)
202 }
203 return func() { f.Close() }, nil // close lets the flock go
204}
205
206func hashObject(repo string, body []byte) (string, error) {
207 out, err := gitInput(repo, body, "hash-object", "-w", "--stdin")
208 return string(bytes.TrimSpace(out)), err
209}