gunk-dev/the-valley

signers_test.go

sourcecaaa5bee5e11main's published tip history
12.0 KiB
1package main
2
3import (
4 "crypto/ed25519"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9
10 "the-valley/approval"
11 "the-valley/note"
12)
13
14// verifierLine is a verifier key line for who, from a key made of seed.
15func verifierLine(who string, seed byte) string {
16 priv := ed25519.NewKeyFromSeed([]byte(strings.Repeat(string(rune(seed)), ed25519.SeedSize)))
17 return note.NewVerifierKey(who, priv.Public().(ed25519.PublicKey)).String() + "\n"
18}
19
20// The publication carries both approval signers files byte for byte, and
21// the instance identity derived from the root commits of the instance
22// repository's main. A run with nothing new moves nothing. A file with no
23// key is published, and so is an empty file where none is named, so a
24// revoked or unconfigured authority replaces the keys published before.
25// A named file that cannot be read, or does not read as verifier keys,
26// publishes nothing, and no file stands in for another.
27func TestThePublicationSaysWhoMayApproveInWhichInstance(t *testing.T) {
28 s := newScratchRepo(t)
29 dir := t.TempDir()
30 const name = "valley-host/attestations"
31 key := verifierLine
32 write := func(file, content string) string {
33 path := filepath.Join(dir, file)
34 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
35 t.Fatal(err)
36 }
37 return path
38 }
39 known := write("known-signers", key(name, 1))
40 approvers := write("approval-signers", "# compiled\n"+key("operator", 2)+key("alice", 3))
41 hardware := write("approval-signers-hardware", "# compiled\n"+key("operator", 2))
42 publish := func(extra ...string) error {
43 t.Helper()
44 stdout, stderr := os.Stdout, os.Stderr
45 os.Stdout, os.Stderr = devNull(t), devNull(t)
46 defer func() { os.Stdout, os.Stderr = stdout, stderr }()
47 return publishSigners(append([]string{"--instance-repo", s.repo, "--known-signers", known, "--name", name}, extra...))
48 }
49 published := func() string {
50 oid, _, err := resolveRef(s.repo, signersRef)
51 if err != nil {
52 t.Fatal(err)
53 }
54 return oid
55 }
56 holds := func(file, want string) {
57 t.Helper()
58 got, err := git(s.repo, "cat-file", "blob", published()+":"+file)
59 if err != nil || got != want {
60 t.Errorf("the publication's %s holds %q (%v), want %q", file, got, err, want)
61 }
62 }
63 identity := func(roots ...string) string {
64 t.Helper()
65 id, err := approval.InstanceID("sha1", roots)
66 if err != nil {
67 t.Fatal(err)
68 }
69 return id
70 }
71 both := []string{"--approval-signers", approvers, "--approval-signers-hardware", hardware}
72
73 base := s.git("rev-parse", "refs/heads/main")
74 if err := publish(both...); err != nil {
75 t.Fatal(err)
76 }
77 if got := s.git("ls-tree", "--name-only", published()); got != "approval-signers\napproval-signers-hardware\ninstance-id\nintegrator-name\nknown-signers" {
78 t.Fatalf("the publication holds %q", got)
79 }
80 holds(signersApprovers, "# compiled\n"+key("operator", 2)+key("alice", 3))
81 holds(signersHardware, "# compiled\n"+key("operator", 2))
82 holds(signersInstance, identity(base)+"\n")
83
84 first := published()
85 if err := publish(both...); err != nil || published() != first {
86 t.Fatalf("publishing what is published moved the ref (%v)", err)
87 }
88
89 // The registry revokes every approval key: the files hold no key, and
90 // are published as they are.
91 write("approval-signers", "# compiled\n")
92 write("approval-signers-hardware", "# compiled\n")
93 if err := publish(both...); err != nil {
94 t.Fatal(err)
95 }
96 holds(signersApprovers, "# compiled\n")
97 holds(signersHardware, "# compiled\n")
98
99 // A host that configures no approval keys publishes empty files.
100 write("approval-signers", key("operator", 2))
101 if err := publish("--approval-signers", approvers); err != nil {
102 t.Fatal(err)
103 }
104 holds(signersApprovers, key("operator", 2))
105 holds(signersHardware, "")
106 if err := publish(); err != nil {
107 t.Fatal(err)
108 }
109 holds(signersApprovers, "")
110 holds(signersHardware, "")
111
112 // A named file that is missing or does not read publishes nothing.
113 last := published()
114 for what, args := range map[string][]string{
115 "a missing approval signers file": {"--approval-signers", filepath.Join(dir, "missing")},
116 "a missing hardware file": {"--approval-signers", approvers, "--approval-signers-hardware", filepath.Join(dir, "missing")},
117 "an approval signers file of prose": {"--approval-signers", write("prose", "not a verifier key\n")},
118 } {
119 if err := publish(args...); err == nil {
120 t.Errorf("%s was published", what)
121 }
122 if published() != last {
123 t.Errorf("%s moved the publication", what)
124 }
125 }
126
127 // The identity is the lineage's: a clone shares it, an unrelated root
128 // integrated into main changes it, and a shallow clone has none.
129 clone := filepath.Join(t.TempDir(), "clone.git")
130 s.git("clone", "--quiet", "--bare", s.repo, clone)
131 if id, err := instanceID(clone); err != nil || id != identity(base) {
132 t.Errorf("the clone's identity is %q (%v), want the original's", id, err)
133 }
134 other := s.commit("an unrelated root")
135 merge := s.commit("the merge", base, other)
136 s.git("update-ref", "refs/heads/main", merge)
137 if err := publish(); err != nil {
138 t.Fatal(err)
139 }
140 holds(signersInstance, identity(base, other)+"\n")
141 shallow := filepath.Join(t.TempDir(), "shallow.git")
142 s.git("clone", "--quiet", "--bare", "--depth", "1", "file://"+s.repo, shallow)
143 if _, err := instanceID(shallow); err == nil || !strings.Contains(err.Error(), "is shallow") {
144 t.Errorf("a shallow repository's identity: %v", err)
145 }
146
147 // An instance repository with no main yet publishes the rest.
148 empty := filepath.Join(t.TempDir(), "empty.git")
149 s.git("init", "--quiet", "--bare", empty)
150 s.repo = empty
151 if err := publish(); err != nil {
152 t.Fatal(err)
153 }
154 if got := s.git("ls-tree", "--name-only", published()); got != "approval-signers\napproval-signers-hardware\nintegrator-name\nknown-signers" {
155 t.Errorf("the publication of a repository with no main holds %q", got)
156 }
157}
158
159// The publication carries the approval signers files without the
160// authority stamp the compiler opens them with. A compilation that moves
161// only the stamp, to another registry commit or another day, leaves the
162// publication's tree as it was, and the ref does not move. A compilation
163// that changes a key moves it.
164func TestTheStampAloneMovesNoPublication(t *testing.T) {
165 s := newScratchRepo(t)
166 // Every write of the ref is logged, so a run that wrote the same tree
167 // again would show.
168 s.git("config", "core.logAllRefUpdates", "always")
169 dir := t.TempDir()
170 write := func(file, content string) string {
171 path := filepath.Join(dir, file)
172 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
173 t.Fatal(err)
174 }
175 return path
176 }
177 stamp := func(commit, day string) string {
178 return "# valley-approval-authority-v1 ref=refs/heads/main commit=" + strings.Repeat(commit, 40) + " day=" + day + "\n"
179 }
180 const name = "valley-host/attestations"
181 const header = "# Compiled from the identity registry — do not edit.\n"
182 keys := header + verifierLine("operator", 2)
183 args := []string{"--instance-repo", s.repo, "--name", name,
184 "--known-signers", write("known-signers", verifierLine(name, 1)),
185 "--approval-signers", write("approval-signers", stamp("1", "2026-11-16")+keys),
186 "--approval-signers-hardware", write("approval-signers-hardware", stamp("1", "2026-11-16")+header),
187 }
188 publish := func() string {
189 t.Helper()
190 stdout, stderr := os.Stdout, os.Stderr
191 os.Stdout, os.Stderr = devNull(t), devNull(t)
192 err := publishSigners(args)
193 os.Stdout, os.Stderr = stdout, stderr
194 if err != nil {
195 t.Fatal(err)
196 }
197 oid, _, err := resolveRef(s.repo, signersRef)
198 if err != nil {
199 t.Fatal(err)
200 }
201 return oid
202 }
203 first := publish()
204 for file, want := range map[string]string{signersApprovers: keys, signersHardware: header} {
205 if got, err := git(s.repo, "cat-file", "blob", first+":"+file); err != nil || got != want {
206 t.Errorf("the publication's %s holds %q (%v), want %q", file, got, err, want)
207 }
208 }
209 writes := func() int {
210 t.Helper()
211 log, err := os.ReadFile(filepath.Join(s.repo, ".git", "logs", signersRef))
212 if err != nil {
213 t.Fatal(err)
214 }
215 return strings.Count(string(log), "\n")
216 }
217 if n := writes(); n != 1 {
218 t.Fatalf("the publication's log holds %d writes, not one", n)
219 }
220
221 write("approval-signers", stamp("2", "2026-11-17")+keys)
222 write("approval-signers-hardware", stamp("2", "2026-11-17")+header)
223 if again := publish(); again != first {
224 t.Errorf("a new stamp moved the publication from %s to %s", first, again)
225 }
226 if n := writes(); n != 1 {
227 t.Errorf("a new stamp wrote the publication again: %d writes", n)
228 }
229
230 write("approval-signers", stamp("2", "2026-11-17")+keys+verifierLine("alice", 3))
231 if moved := publish(); moved == first || writes() != 2 {
232 t.Error("a new key left the publication where it was")
233 }
234}
235
236// A change made while a run publishes is published by that run, with
237// nothing to wake it again: after each publication the run reads every
238// input again and publishes again until a reading matches. The step runs
239// once the first round has read its inputs and before its ref moves,
240// which is where a wake from the path unit is lost. There the registry
241// revokes an approval key and main takes in an unrelated root.
242func TestAChangeWhileARunPublishesIsPublishedByIt(t *testing.T) {
243 s := newScratchRepo(t)
244 dir := t.TempDir()
245 write := func(file, content string) string {
246 path := filepath.Join(dir, file)
247 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
248 t.Fatal(err)
249 }
250 return path
251 }
252 const name = "valley-host/attestations"
253 args := []string{"--instance-repo", s.repo, "--name", name,
254 "--known-signers", write("known-signers", verifierLine(name, 1)),
255 "--approval-signers", write("approval-signers", verifierLine("operator", 2)+verifierLine("alice", 3)),
256 "--approval-signers-hardware", write("approval-signers-hardware", verifierLine("operator", 2)),
257 }
258 base := s.git("rev-parse", "refs/heads/main")
259 other := s.commit("an unrelated root")
260 merge := s.commit("the merge", base, other)
261 want, err := approval.InstanceID("sha1", []string{base, other})
262 if err != nil {
263 t.Fatal(err)
264 }
265 stdout, stderr := os.Stdout, os.Stderr
266 os.Stdout, os.Stderr = devNull(t), devNull(t)
267 var rounds []int
268 err = publishSignersWith(args, func(round int) {
269 rounds = append(rounds, round)
270 if round == 1 {
271 write("approval-signers", verifierLine("operator", 2))
272 s.git("update-ref", "refs/heads/main", merge)
273 }
274 })
275 os.Stdout, os.Stderr = stdout, stderr
276 if err != nil {
277 t.Fatal(err)
278 }
279 if len(rounds) != 2 {
280 t.Errorf("the run published %d times, want 2", len(rounds))
281 }
282 if got := s.git("cat-file", "blob", signersRef+":"+signersApprovers); got+"\n" != verifierLine("operator", 2) {
283 t.Errorf("the revoked key is still published: %q", got)
284 }
285 if got := s.git("cat-file", "blob", signersRef+":"+signersInstance); got != want {
286 t.Errorf("the publication's instance-id is %s, want %s, from both roots", got, want)
287 }
288}
289
290// A run whose inputs change again after every publication stops after
291// one round per input that can change and one more, and fails saying so.
292func TestARunWhoseInputsKeepChangingFails(t *testing.T) {
293 s := newScratchRepo(t)
294 dir := t.TempDir()
295 approvers := filepath.Join(dir, "approval-signers")
296 known := filepath.Join(dir, "known-signers")
297 for path, content := range map[string]string{approvers: "", known: verifierLine("valley-host/attestations", 1)} {
298 if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
299 t.Fatal(err)
300 }
301 }
302 stdout, stderr := os.Stdout, os.Stderr
303 os.Stdout, os.Stderr = devNull(t), devNull(t)
304 rounds := 0
305 err := publishSignersWith([]string{"--instance-repo", s.repo, "--known-signers", known, "--approval-signers", approvers}, func(round int) {
306 rounds = round
307 if err := os.WriteFile(approvers, []byte(verifierLine("operator", byte(10+round))), 0o644); err != nil {
308 t.Error(err)
309 }
310 })
311 os.Stdout, os.Stderr = stdout, stderr
312 // The known-signers file, main and the approval signers file can each
313 // change once, and one round more publishes the last of them.
314 if err == nil || !strings.Contains(err.Error(), "the inputs kept changing") || rounds != 4 {
315 t.Fatalf("after %d rounds: %v", rounds, err)
316 }
317 if got := s.git("cat-file", "blob", signersRef+":"+signersApprovers); got+"\n" != verifierLine("operator", 13) {
318 t.Errorf("the last round did not publish what it read: %q", got)
319 }
320}