gunk-dev/the-valley

signers.go

source695d65eb4898main's published tip history
4.6 KiB
1package main
2
3import (
4 "bytes"
5 "errors"
6 "flag"
7 "fmt"
8 "os"
9 "strings"
10
11 "the-valley/note"
12)
13
14// signersRef is where the host publishes what a client verifies this
15// integrator's transfer statements with: a tree holding the known-signers
16// file the integrator reads, as signersKeys, and the name it signs under,
17// as signersName, one line. No push may write it, because the push hook
18// refuses every refs/the-valley/* ref it does not name, so the host is its
19// only writer.
20const (
21 signersRef = "refs/the-valley/signers"
22 signersKeys = "known-signers"
23 signersName = "integrator-name"
24)
25
26const signersUsage = `usage: integrator publish-signers --instance-repo DIR --known-signers F [--name NAME]
27
28Publishes, in the instance repository, the two values a client verifies
29this host's transfer statements with: the known-signers file a controller
30reads, and the name it signs under. Both go into one tree at
31refs/the-valley/signers, so a client reads both from one ref or neither.
32
33 --instance-repo DIR the bare repository clients read the host's
34 publications from
35 --known-signers F the file a controller is given as --known-signers
36 --name NAME the name a controller signs under, as it is given
37 to watch (default: this host's, as watch defaults)
38
39A publication that would not change is not written. One that would is
40written with a compare-and-swap on the ref, so two runs at once cannot
41interleave. The file must read as verifier keys, and the name must be one
42a verifier key can carry. A file that holds no key under the name is
43published all the same, because it is what the controller uses, and the
44run says on stderr that no client can verify a statement with it.
45`
46
47// publishSigners writes the publication, and says what it did on stdout.
48func publishSigners(args []string) error {
49 fs := flag.NewFlagSet("publish-signers", flag.ExitOnError)
50 fs.Usage = func() { fmt.Fprint(os.Stderr, signersUsage) }
51 repo := fs.String("instance-repo", "", "the instance repository")
52 known := fs.String("known-signers", "", "the file a controller is given as --known-signers")
53 name := fs.String("name", defaultSignerName(), "the name a controller signs under")
54 if err := fs.Parse(args); err != nil {
55 return err
56 }
57 if fs.NArg() > 0 {
58 return fmt.Errorf("publish-signers takes no argument, and was given %q", fs.Arg(0))
59 }
60 for _, missing := range []struct{ flag, value string }{
61 {"--instance-repo", *repo},
62 {"--known-signers", *known},
63 } {
64 if missing.value == "" {
65 return fmt.Errorf("%s is required", missing.flag)
66 }
67 }
68 keys, err := os.ReadFile(*known)
69 if err != nil {
70 return fmt.Errorf("reading the known-signers file: %w", err)
71 }
72 tree, err := signersTree(*repo, keys, *name)
73 if err != nil {
74 return err
75 }
76 old, _ := gitLine(*repo, "rev-parse", "--verify", "--quiet", signersRef)
77 if old == tree {
78 fmt.Printf("%s already publishes %s and the signers in %s\n", signersRef, *name, *known)
79 return nil
80 }
81 // The value the ref is expected to hold: the one just read, or none at
82 // all. An empty old value makes update-ref require that the ref does
83 // not exist.
84 if _, err := git(*repo, "update-ref", "-m", "publish-signers", signersRef, tree, old); err != nil {
85 return err
86 }
87 fmt.Printf("%s now publishes %s and the signers in %s\n", signersRef, *name, *known)
88 return nil
89}
90
91// signersTree writes the publication's objects into repo and returns its
92// tree.
93func signersTree(repo string, keys []byte, name string) (string, error) {
94 if err := note.CheckName(name); err != nil {
95 return "", fmt.Errorf("--name: %w", err)
96 }
97 parsed, err := note.ParseVerifierKeys(keys)
98 if err != nil {
99 return "", fmt.Errorf("the known-signers file does not read: %w", err)
100 }
101 if len(parsed) == 0 {
102 return "", errors.New("the known-signers file holds no verifier key")
103 }
104 signs := false
105 for _, k := range parsed {
106 signs = signs || k.Name == name
107 }
108 if !signs {
109 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)
110 }
111 keysBlob, err := hashObject(repo, keys)
112 if err != nil {
113 return "", err
114 }
115 nameBlob, err := hashObject(repo, []byte(name+"\n"))
116 if err != nil {
117 return "", err
118 }
119 entries := fmt.Sprintf("100644 blob %s\t%s\n100644 blob %s\t%s\n", nameBlob, signersName, keysBlob, signersKeys)
120 out, err := gitInput(repo, []byte(entries), "mktree")
121 if err != nil {
122 return "", err
123 }
124 return strings.TrimSpace(string(out)), nil
125}
126
127func hashObject(repo string, body []byte) (string, error) {
128 out, err := gitInput(repo, body, "hash-object", "-w", "--stdin")
129 return string(bytes.TrimSpace(out)), err
130}