gunk-dev/the-valley

signers.go

source2d226693a6c1main's published tip history
7.6 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]
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
56A publication that would not change is not written. One that would is
57written with a compare-and-swap on the ref. Two runs at once take turns:
58each holds a lock in the instance repository from reading the file to
59moving the ref, so a run that read an older file cannot publish it over
60a newer one.
61
62The file must read as verifier keys, the name must be one a verifier key
63can carry, and a signer projects file must read as watch reads one, so
64it may not name NAME. A file that holds no key under the name, or no key
65at all, is published all the same, because it is what the controller
66uses. So when every signing key is revoked, the publication drops the
67old keys too, and clients stop verifying with them. The run says on
68stderr that no client can verify a statement with the file. An empty
69signer projects file restricts nobody, as no file does, and is not
70published.
71`
72
73// publishSigners writes the publication, and says what it did on stdout.
74func publishSigners(args []string) error {
75 fs := flag.NewFlagSet("publish-signers", flag.ExitOnError)
76 fs.Usage = func() { fmt.Fprint(os.Stderr, signersUsage) }
77 repo := fs.String("instance-repo", "", "the instance repository")
78 known := fs.String("known-signers", "", "the file a controller is given as --known-signers")
79 name := fs.String("name", defaultSignerName(), "the name a controller signs under")
80 projects := fs.String("signer-projects", "", "the file a controller is given as --signer-projects")
81 if err := fs.Parse(args); err != nil {
82 return err
83 }
84 if fs.NArg() > 0 {
85 return fmt.Errorf("publish-signers takes no argument, and was given %q", fs.Arg(0))
86 }
87 for _, missing := range []struct{ flag, value string }{
88 {"--instance-repo", *repo},
89 {"--known-signers", *known},
90 } {
91 if missing.value == "" {
92 return fmt.Errorf("%s is required", missing.flag)
93 }
94 }
95 unlock, err := lockSigners(*repo)
96 if err != nil {
97 return err
98 }
99 defer unlock()
100 keys, err := os.ReadFile(*known)
101 if err != nil {
102 return fmt.Errorf("reading the known-signers file: %w", err)
103 }
104 _, restrictions, err := readSignerProjects(*projects, *name)
105 if err != nil {
106 return err
107 }
108 tree, err := signersTree(*repo, keys, *name, restrictions)
109 if err != nil {
110 return err
111 }
112 // The ref is read by its exact name. With no publication yet, old is
113 // "", whatever branch is named like the ref.
114 old, _, err := resolveRef(*repo, signersRef)
115 if err != nil {
116 return err
117 }
118 if old == tree {
119 fmt.Printf("%s already publishes %s and the signers in %s\n", signersRef, *name, *known)
120 return nil
121 }
122 // The value the ref is expected to hold: the one just read, or none at
123 // all. An empty old value makes update-ref require that the ref does
124 // not exist. The lock keeps other runs out, and the compare-and-swap
125 // keeps this run from writing over anything else on the host that
126 // moved the ref since.
127 if _, err := git(*repo, "update-ref", "-m", "publish-signers", signersRef, tree, old); err != nil {
128 return err
129 }
130 fmt.Printf("%s now publishes %s and the signers in %s\n", signersRef, *name, *known)
131 return nil
132}
133
134// signersTree writes the publication's objects into repo and returns its
135// tree. The tree holds a signer projects file when projects is not empty.
136func signersTree(repo string, keys []byte, name string, projects []byte) (string, error) {
137 if err := note.CheckName(name); err != nil {
138 return "", fmt.Errorf("--name: %w", err)
139 }
140 // A file with no key at all reads, and is published: it is what the
141 // registry compiles to once every signing key is revoked.
142 parsed, err := note.ParseVerifierKeys(keys)
143 if err != nil {
144 return "", fmt.Errorf("the known-signers file does not read: %w", err)
145 }
146 signs := false
147 for _, k := range parsed {
148 signs = signs || k.Name == name
149 }
150 if !signs {
151 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)
152 }
153 keysBlob, err := hashObject(repo, keys)
154 if err != nil {
155 return "", err
156 }
157 nameBlob, err := hashObject(repo, []byte(name+"\n"))
158 if err != nil {
159 return "", err
160 }
161 entries := fmt.Sprintf("100644 blob %s\t%s\n100644 blob %s\t%s\n", nameBlob, signersName, keysBlob, signersKeys)
162 if len(projects) > 0 {
163 projectsBlob, err := hashObject(repo, projects)
164 if err != nil {
165 return "", err
166 }
167 entries += fmt.Sprintf("100644 blob %s\t%s\n", projectsBlob, signersProjects)
168 }
169 out, err := gitInput(repo, []byte(entries), "mktree")
170 if err != nil {
171 return "", err
172 }
173 return strings.TrimSpace(string(out)), nil
174}
175
176// lockSigners takes the publication's lock and returns what lets it go. A
177// run that asks while another holds it waits its turn. The lock is
178// flock's, on signersLock, so the kernel lets it go when the run ends
179// however it ends, and a run that dies leaves nothing to clear.
180func lockSigners(repo string) (func(), error) {
181 path := filepath.Join(repo, signersLock)
182 f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0o644)
183 if err != nil {
184 return nil, fmt.Errorf("could not open the publication's lock: %w", err)
185 }
186 if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
187 f.Close()
188 return nil, fmt.Errorf("could not lock %s: %w", path, err)
189 }
190 return func() { f.Close() }, nil // close lets the flock go
191}
192
193func hashObject(repo string, body []byte) (string, error) {
194 out, err := gitInput(repo, body, "hash-object", "-w", "--stdin")
195 return string(bytes.TrimSpace(out)), err
196}