gunk-dev/the-valley

signers.go

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