gunk-dev/the-valley

signers.go

sourced8c2e5434360main's published tip history
6.3 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.
19//
20// Only the host's own machinery writes it, never a push. The push hook
21// refuses every push to a refs/the-valley/* ref it does not name. On the
22// host, this publisher writes it. The integrator can write it too: the
23// instance repository is group-writable, and the integrator is in the
24// group because it lands code there. A client already trusts the
25// integrator with what lands, so the publication is not kept from it.
26//
27// signersLock is the file in the instance repository that a run holds a
28// lock on, from reading the signers file to moving the ref.
29const (
30 signersRef = "refs/the-valley/signers"
31 signersKeys = "known-signers"
32 signersName = "integrator-name"
33 signersLock = "valley-signers.lock"
34)
35
36const signersUsage = `usage: integrator publish-signers --instance-repo DIR --known-signers F [--name NAME]
37
38Publishes, in the instance repository, the two values a client verifies
39this host's transfer statements with: the known-signers file a controller
40reads, and the name it signs under. Both go into one tree at
41refs/the-valley/signers, so a client reads both from one ref or neither.
42
43 --instance-repo DIR the bare repository clients read the host's
44 publications from
45 --known-signers F the file a controller is given as --known-signers
46 --name NAME the name a controller signs under, as it is given
47 to watch (default: this host's, as watch defaults)
48
49A publication that would not change is not written. One that would is
50written with a compare-and-swap on the ref. Two runs at once take turns:
51each holds a lock in the instance repository from reading the file to
52moving the ref, so a run that read an older file cannot publish it over
53a newer one.
54
55The file must read as verifier keys, and the name must be one a verifier
56key can carry. A file that holds no key under the name, or no key at all,
57is published all the same, because it is what the controller uses. So
58when every signing key is revoked, the publication drops the old keys
59too, and clients stop verifying with them. The run says on stderr that
60no client can verify a statement with the file.
61`
62
63// publishSigners writes the publication, and says what it did on stdout.
64func publishSigners(args []string) error {
65 fs := flag.NewFlagSet("publish-signers", flag.ExitOnError)
66 fs.Usage = func() { fmt.Fprint(os.Stderr, signersUsage) }
67 repo := fs.String("instance-repo", "", "the instance repository")
68 known := fs.String("known-signers", "", "the file a controller is given as --known-signers")
69 name := fs.String("name", defaultSignerName(), "the name a controller signs under")
70 if err := fs.Parse(args); err != nil {
71 return err
72 }
73 if fs.NArg() > 0 {
74 return fmt.Errorf("publish-signers takes no argument, and was given %q", fs.Arg(0))
75 }
76 for _, missing := range []struct{ flag, value string }{
77 {"--instance-repo", *repo},
78 {"--known-signers", *known},
79 } {
80 if missing.value == "" {
81 return fmt.Errorf("%s is required", missing.flag)
82 }
83 }
84 unlock, err := lockSigners(*repo)
85 if err != nil {
86 return err
87 }
88 defer unlock()
89 keys, err := os.ReadFile(*known)
90 if err != nil {
91 return fmt.Errorf("reading the known-signers file: %w", err)
92 }
93 tree, err := signersTree(*repo, keys, *name)
94 if err != nil {
95 return err
96 }
97 old, _ := gitLine(*repo, "rev-parse", "--verify", "--quiet", signersRef)
98 if old == tree {
99 fmt.Printf("%s already publishes %s and the signers in %s\n", signersRef, *name, *known)
100 return nil
101 }
102 // The value the ref is expected to hold: the one just read, or none at
103 // all. An empty old value makes update-ref require that the ref does
104 // not exist. The lock keeps other runs out, and the compare-and-swap
105 // keeps this run from writing over anything else on the host that
106 // moved the ref since.
107 if _, err := git(*repo, "update-ref", "-m", "publish-signers", signersRef, tree, old); err != nil {
108 return err
109 }
110 fmt.Printf("%s now publishes %s and the signers in %s\n", signersRef, *name, *known)
111 return nil
112}
113
114// signersTree writes the publication's objects into repo and returns its
115// tree.
116func signersTree(repo string, keys []byte, name string) (string, error) {
117 if err := note.CheckName(name); err != nil {
118 return "", fmt.Errorf("--name: %w", err)
119 }
120 // A file with no key at all reads, and is published: it is what the
121 // registry compiles to once every signing key is revoked.
122 parsed, err := note.ParseVerifierKeys(keys)
123 if err != nil {
124 return "", fmt.Errorf("the known-signers file does not read: %w", err)
125 }
126 signs := false
127 for _, k := range parsed {
128 signs = signs || k.Name == name
129 }
130 if !signs {
131 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)
132 }
133 keysBlob, err := hashObject(repo, keys)
134 if err != nil {
135 return "", err
136 }
137 nameBlob, err := hashObject(repo, []byte(name+"\n"))
138 if err != nil {
139 return "", err
140 }
141 entries := fmt.Sprintf("100644 blob %s\t%s\n100644 blob %s\t%s\n", nameBlob, signersName, keysBlob, signersKeys)
142 out, err := gitInput(repo, []byte(entries), "mktree")
143 if err != nil {
144 return "", err
145 }
146 return strings.TrimSpace(string(out)), nil
147}
148
149// lockSigners takes the publication's lock and returns what lets it go. A
150// run that asks while another holds it waits its turn. The lock is
151// flock's, on signersLock, so the kernel lets it go when the run ends
152// however it ends, and a run that dies leaves nothing to clear.
153func lockSigners(repo string) (func(), error) {
154 path := filepath.Join(repo, signersLock)
155 f, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0o644)
156 if err != nil {
157 return nil, fmt.Errorf("could not open the publication's lock: %w", err)
158 }
159 if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
160 f.Close()
161 return nil, fmt.Errorf("could not lock %s: %w", path, err)
162 }
163 return func() { f.Close() }, nil // close lets the flock go
164}
165
166func hashObject(repo string, body []byte) (string, error) {
167 out, err := gitInput(repo, body, "hash-object", "-w", "--stdin")
168 return string(bytes.TrimSpace(out)), err
169}