gunk-dev/the-valley

signers.go

sourced03575e2a892main's published tip history
7.5 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 old, _ := gitLine(*repo, "rev-parse", "--verify", "--quiet", signersRef)
113 if old == tree {
114 fmt.Printf("%s already publishes %s and the signers in %s\n", signersRef, *name, *known)
115 return nil
116 }
117 // The value the ref is expected to hold: the one just read, or none at
118 // all. An empty old value makes update-ref require that the ref does
119 // not exist. The lock keeps other runs out, and the compare-and-swap
120 // keeps this run from writing over anything else on the host that
121 // moved the ref since.
122 if _, err := git(*repo, "update-ref", "-m", "publish-signers", signersRef, tree, old); err != nil {
123 return err
124 }
125 fmt.Printf("%s now publishes %s and the signers in %s\n", signersRef, *name, *known)
126 return nil
127}
128
129// signersTree writes the publication's objects into repo and returns its
130// tree. The tree holds a signer projects file when projects is not empty.
131func signersTree(repo string, keys []byte, name string, projects []byte) (string, error) {
132 if err := note.CheckName(name); err != nil {
133 return "", fmt.Errorf("--name: %w", err)
134 }
135 // A file with no key at all reads, and is published: it is what the
136 // registry compiles to once every signing key is revoked.
137 parsed, err := note.ParseVerifierKeys(keys)
138 if err != nil {
139 return "", fmt.Errorf("the known-signers file does not read: %w", err)
140 }
141 signs := false
142 for _, k := range parsed {
143 signs = signs || k.Name == name
144 }
145 if !signs {
146 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)
147 }
148 keysBlob, err := hashObject(repo, keys)
149 if err != nil {
150 return "", err
151 }
152 nameBlob, err := hashObject(repo, []byte(name+"\n"))
153 if err != nil {
154 return "", err
155 }
156 entries := fmt.Sprintf("100644 blob %s\t%s\n100644 blob %s\t%s\n", nameBlob, signersName, keysBlob, signersKeys)
157 if len(projects) > 0 {
158 projectsBlob, err := hashObject(repo, projects)
159 if err != nil {
160 return "", err
161 }
162 entries += fmt.Sprintf("100644 blob %s\t%s\n", projectsBlob, signersProjects)
163 }
164 out, err := gitInput(repo, []byte(entries), "mktree")
165 if err != nil {
166 return "", err
167 }
168 return strings.TrimSpace(string(out)), nil
169}
170
171// lockSigners takes the publication's lock and returns what lets it go. A
172// run that asks while another holds it waits its turn. The lock is
173// flock's, on signersLock, so the kernel lets it go when the run ends
174// however it ends, and a run that dies leaves nothing to clear.
175func lockSigners(repo string) (func(), error) {
176 path := filepath.Join(repo, signersLock)
177 f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0o644)
178 if err != nil {
179 return nil, fmt.Errorf("could not open the publication's lock: %w", err)
180 }
181 if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
182 f.Close()
183 return nil, fmt.Errorf("could not lock %s: %w", path, err)
184 }
185 return func() { f.Close() }, nil // close lets the flock go
186}
187
188func hashObject(repo string, body []byte) (string, error) {
189 out, err := gitInput(repo, body, "hash-object", "-w", "--stdin")
190 return string(bytes.TrimSpace(out)), err
191}