| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "reflect" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "the-valley/webui/export" |
| 16 | ) |
| 17 | |
| 18 | // fails runs one pass through run, the program's entry point, and fails the |
| 19 | // test unless the pass exits 1 having sent and written nothing: to rv when |
| 20 | // it is given, and otherwise to a fresh --out directory. It returns what |
| 21 | // the pass said on stderr. |
| 22 | func (v *testValley) fails(rv *testReceiver, more ...string) string { |
| 23 | v.t.Helper() |
| 24 | out := filepath.Join(v.t.TempDir(), "export") |
| 25 | to := []string{"--out", out} |
| 26 | if rv != nil { |
| 27 | rv.taken() |
| 28 | to = []string{"--receiver", rv.server.URL} |
| 29 | } |
| 30 | var stdout, stderr bytes.Buffer |
| 31 | if code := run(context.Background(), v.args(append(to, more...)...), &stdout, &stderr); code != 1 { |
| 32 | v.t.Fatalf("the pass exited %d, want 1\nstdout:\n%s\nstderr:\n%s", code, stdout.String(), stderr.String()) |
| 33 | } |
| 34 | if rv != nil { |
| 35 | sameRequests(v.t, "a failed pass", rv.taken()) |
| 36 | } else if _, err := os.Stat(out); !errors.Is(err, os.ErrNotExist) { |
| 37 | v.t.Errorf("a failed pass wrote %s", out) |
| 38 | } |
| 39 | return stderr.String() |
| 40 | } |
| 41 | |
| 42 | func mustSay(t *testing.T, what, said, want string) { |
| 43 | t.Helper() |
| 44 | if !strings.Contains(said, want) { |
| 45 | t.Errorf("%s: the pass did not say %q:\n%s", what, want, said) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // elsewhere makes a second instance beside the first, with projects of the |
| 50 | // same names: valley, which allows app and site; app, which declares |
| 51 | // itself public and carries a sentinel of its own; and site, with no main. |
| 52 | // It returns the instance repository. |
| 53 | func (v *testValley) elsewhere(sentinel string) string { |
| 54 | v.t.Helper() |
| 55 | srv := filepath.Join(v.root, "elsewhere") |
| 56 | for _, name := range []string{"valley", "app", "site"} { |
| 57 | v.run("", "git", "init", "--quiet", "--bare", filepath.Join(srv, name+".git")) |
| 58 | } |
| 59 | for _, name := range []string{"valley", "app"} { |
| 60 | work := filepath.Join(v.root, "elsewhere-work", name) |
| 61 | v.run("", "git", "init", "--quiet", work) |
| 62 | v.run(work, "git", "remote", "add", "origin", filepath.Join(srv, name+".git")) |
| 63 | v.work["elsewhere/"+name] = work |
| 64 | } |
| 65 | v.write(filepath.Join(v.work["elsewhere/valley"], "policy", "instance", "floor.cue"), "package verification\n") |
| 66 | v.declare("elsewhere/valley", `instance: projects: { |
| 67 | app: visibility: "public" |
| 68 | site: visibility: "public" |
| 69 | }`) |
| 70 | v.write(filepath.Join(v.work["elsewhere/app"], "policy", "valley"), filepath.Join(srv, "valley.git")+"\n") |
| 71 | v.write(filepath.Join(v.work["elsewhere/app"], "source.txt"), sentinel+"\n") |
| 72 | v.declare("elsewhere/app", `project: visibility: "public"`) |
| 73 | list := v.runInput("", "app\nsite\nvalley\n", "git", "-C", filepath.Join(srv, "valley.git"), "hash-object", "-w", "--stdin") |
| 74 | v.run("", "git", "-C", filepath.Join(srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list)) |
| 75 | return filepath.Join(srv, "valley.git") |
| 76 | } |
| 77 | |
| 78 | // Two instances publish a project called app. When the instance repository |
| 79 | // the publisher is given points at the other with policy/valley, the CLI |
| 80 | // would read the other's app, and the pass refuses before it reads any |
| 81 | // project. |
| 82 | func TestAnotherInstanceIsNeverRead(t *testing.T) { |
| 83 | v := newValley(t) |
| 84 | other := v.elsewhere("elsewhere-app-source-c3d1") |
| 85 | v.write(filepath.Join(v.work["valley"], "policy", "valley"), other+"\n") |
| 86 | v.commit("valley", "valley: point at another instance") |
| 87 | |
| 88 | mustSay(t, "--out", v.fails(nil), "holds policy/valley on main") |
| 89 | rv := newReceiver(t, v) |
| 90 | mustSay(t, "--receiver", v.fails(rv), "holds policy/valley on main") |
| 91 | if _, err := os.Stat(filepath.Join(v.root, "cache", "valley-web-publish", "repos", "app.git")); !errors.Is(err, os.ErrNotExist) { |
| 92 | t.Errorf("the pass fetched a project before it refused the instance repository") |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // A policy/valley that lands on the instance repository's main while the |
| 97 | // CLI reads is caught: the read is confirmed against main once it is done, |
| 98 | // and the confirmation refuses the pointer. Without it, the other |
| 99 | // instance's app would be published. |
| 100 | func TestAPointerLandingDuringAReadIsCaught(t *testing.T) { |
| 101 | v := newValley(t) |
| 102 | other := v.elsewhere("elsewhere-app-source-c3d1") |
| 103 | work := v.work["valley"] |
| 104 | v.run(work, "git", "checkout", "--quiet", "-b", "pointer") |
| 105 | v.write(filepath.Join(work, "policy", "valley"), other+"\n") |
| 106 | v.run(work, "git", "add", "--all") |
| 107 | v.run(work, "git", "commit", "--quiet", "-m", "valley: point at another instance") |
| 108 | pointer := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD")) |
| 109 | v.run(work, "git", "push", "--quiet", "origin", "pointer") |
| 110 | |
| 111 | // The CLI's first command moves main to the pointer before it runs. |
| 112 | wrapper := filepath.Join(v.root, "bin", "valley") |
| 113 | data, err := os.ReadFile(wrapper) |
| 114 | if err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | shebang, rest, _ := strings.Cut(string(data), "\n") |
| 118 | moved := filepath.Join(v.root, "moved") |
| 119 | v.write(wrapper, fmt.Sprintf("%s\nif [ ! -e %s ]; then : > %s; git -C %s update-ref refs/heads/main %s; fi\n%s", |
| 120 | shebang, moved, moved, filepath.Join(v.srv, "valley.git"), pointer, rest)) |
| 121 | |
| 122 | mustSay(t, "a pointer landing during the read", v.fails(nil), "holds policy/valley on main") |
| 123 | if _, err := os.Stat(moved); err != nil { |
| 124 | t.Errorf("the CLI never ran, so the pointer was refused before the read: %v", err) |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // A project the instance allows that cannot be read fails the pass, which |
| 129 | // sends and writes nothing: published without it, the export would take |
| 130 | // it off the web. A project with no main yet is private, and the pass |
| 131 | // goes on. |
| 132 | func TestAProjectThatCannotBeReadFailsThePass(t *testing.T) { |
| 133 | cases := []struct { |
| 134 | name string |
| 135 | upload bool |
| 136 | breaks func(v *testValley) []string |
| 137 | says string |
| 138 | }{ |
| 139 | { |
| 140 | name: "the pending listing cannot read site", |
| 141 | upload: true, |
| 142 | breaks: func(v *testValley) []string { |
| 143 | if err := os.Rename(filepath.Join(v.srv, "site.git"), filepath.Join(v.srv, "site.moved")); err != nil { |
| 144 | v.t.Fatal(err) |
| 145 | } |
| 146 | return nil |
| 147 | }, |
| 148 | says: "site: the pending listing could not read it", |
| 149 | }, |
| 150 | { |
| 151 | name: "site's main cannot be fetched into its mirror", |
| 152 | breaks: func(v *testValley) []string { |
| 153 | mirror := filepath.Join(v.root, "cache", "valley-web-publish", "repos", "site.git") |
| 154 | v.run("", "git", "init", "--quiet", "--bare", mirror) |
| 155 | v.run("", "git", "-C", mirror, "config", "core.repositoryformatversion", "99") |
| 156 | return nil |
| 157 | }, |
| 158 | says: "site: could not read main", |
| 159 | }, |
| 160 | { |
| 161 | name: "discovery fails, and the CLI lists its own repository alone", |
| 162 | breaks: func(v *testValley) []string { |
| 163 | list := v.runInput("", "app\nsite\nvalley\nnot a name\n", "git", "-C", filepath.Join(v.srv, "valley.git"), "hash-object", "-w", "--stdin") |
| 164 | v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list)) |
| 165 | return nil |
| 166 | }, |
| 167 | says: "valley plan --all lists valley, and the instance repository publishes app, not a name, site, valley", |
| 168 | }, |
| 169 | { |
| 170 | name: "the instance publishes no project list", |
| 171 | breaks: func(v *testValley) []string { |
| 172 | v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", "-d", projectsRef) |
| 173 | return nil |
| 174 | }, |
| 175 | says: "could not read main and refs/the-valley/projects in the instance repository", |
| 176 | }, |
| 177 | { |
| 178 | name: "the schema cannot be read", |
| 179 | breaks: func(v *testValley) []string { return []string{"--schema", filepath.Join(v.root, "no-such.cue")} }, |
| 180 | says: "does not load", |
| 181 | }, |
| 182 | } |
| 183 | for _, c := range cases { |
| 184 | t.Run(c.name, func(t *testing.T) { |
| 185 | v := newValley(t) |
| 186 | more := c.breaks(v) |
| 187 | var rv *testReceiver |
| 188 | if c.upload { |
| 189 | rv = newReceiver(t, v) |
| 190 | } |
| 191 | mustSay(t, c.name, v.fails(rv, more...), c.says) |
| 192 | }) |
| 193 | } |
| 194 | |
| 195 | t.Run("a project with no main is private", func(t *testing.T) { |
| 196 | v := newValley(t) |
| 197 | v.run("", "git", "init", "--quiet", "--bare", filepath.Join(v.srv, "fresh.git")) |
| 198 | list := v.runInput("", "app\nfresh\nsite\nvalley\n", "git", "-C", filepath.Join(v.srv, "valley.git"), "hash-object", "-w", "--stdin") |
| 199 | v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", projectsRef, strings.TrimSpace(list)) |
| 200 | v.declare("valley", `instance: projects: { |
| 201 | app: visibility: "public" |
| 202 | fresh: visibility: "public" |
| 203 | site: visibility: "public" |
| 204 | }`) |
| 205 | stdout, _ := v.publish("--out", filepath.Join(t.TempDir(), "export")) |
| 206 | mustSay(t, "a project with no main", stdout, "public: app; private: fresh, site, valley") |
| 207 | }) |
| 208 | } |
| 209 | |
| 210 | // The bundles of an export are held to --max-bundles together, and the |
| 211 | // state and the cache to --max-storage, before anything is sent. What a |
| 212 | // program prints is held to a bound: git's output past it fails the call, |
| 213 | // and a declaration whose export is past cue's bound is invalid. |
| 214 | func TestBoundsStopThePass(t *testing.T) { |
| 215 | v := newValley(t) |
| 216 | rv := newReceiver(t, v) |
| 217 | mustSay(t, "--max-bundles", v.fails(rv, "--max-bundles", "100"), "more than --max-bundles 100") |
| 218 | mustSay(t, "--max-storage", v.fails(nil, "--max-storage", "1K"), "more than --max-storage 1024") |
| 219 | for _, size := range []string{"0", "-1", "1X", "K", "9000000000T"} { |
| 220 | var stdout, stderr bytes.Buffer |
| 221 | if code := run(context.Background(), v.args("--out", t.TempDir(), "--max-bundles", size), &stdout, &stderr); code != 2 { |
| 222 | t.Errorf("--max-bundles %s: the pass exited %d, want 2", size, code) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | big := v.runInput("", strings.Repeat("x", maxGitOutput+1), "git", "-C", filepath.Join(v.srv, "app.git"), "hash-object", "-w", "--stdin") |
| 227 | if _, err := git(context.Background(), filepath.Join(v.srv, "app.git"), "cat-file", "blob", strings.TrimSpace(big)); !errors.Is(err, errTooLong) { |
| 228 | t.Errorf("git printing more than %d bytes returned %v, want errTooLong", maxGitOutput, err) |
| 229 | } |
| 230 | |
| 231 | v.declare("valley", `import "list" |
| 232 | |
| 233 | instance: projects: {for i in list.Range(0, 9000, 1) {"project-with-a-long-name-for-the-bound-on-cue-output-\(i)": visibility: "public"}}`) |
| 234 | stdout, stderr := v.publish("--out", filepath.Join(t.TempDir(), "export")) |
| 235 | mustSay(t, "a declaration past cue's bound", stdout, "public: none") |
| 236 | mustSay(t, "a declaration past cue's bound", stderr, "cue exported more than") |
| 237 | } |
| 238 | |
| 239 | // objects is every object a repository holds, decompressed, as git |
| 240 | // cat-file --batch-all-objects prints them. |
| 241 | func (v *testValley) objects(repo string) string { |
| 242 | v.t.Helper() |
| 243 | return v.run("", "git", "-C", repo, "cat-file", "--batch-all-objects", "--batch") |
| 244 | } |
| 245 | |
| 246 | // unbundled is every object a bundle carries, decompressed. |
| 247 | func (v *testValley) unbundled(bundle string) string { |
| 248 | v.t.Helper() |
| 249 | repo := filepath.Join(v.t.TempDir(), "unbundled.git") |
| 250 | v.run("", "git", "init", "--quiet", "--bare", repo) |
| 251 | v.run("", "git", "-C", repo, "bundle", "unbundle", bundle) |
| 252 | return v.objects(repo) |
| 253 | } |
| 254 | |
| 255 | // noSentinels fails the test if any private sentinel appears in what. |
| 256 | func noSentinels(t *testing.T, where, what string) { |
| 257 | t.Helper() |
| 258 | for _, s := range []string{sentinelSource, sentinelBody, sentinelComment} { |
| 259 | if strings.Contains(what, s) { |
| 260 | t.Errorf("%s holds the private sentinel %s", where, s) |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // inActive runs f over the generation the receiver serves. |
| 266 | func (rv *testReceiver) inActive(f func(export.Generation)) { |
| 267 | rv.t.Helper() |
| 268 | rv.mu.Lock() |
| 269 | defer rv.mu.Unlock() |
| 270 | g, release, err := rv.store.Open() |
| 271 | defer release() |
| 272 | if err != nil { |
| 273 | rv.t.Fatal(err) |
| 274 | } |
| 275 | f(g) |
| 276 | } |
| 277 | |
| 278 | // No private sentinel leaves the host: not in export.json, and not in any |
| 279 | // object of any bundle, whether the export is written to --out or |
| 280 | // uploaded and activated by a receiver. |
| 281 | func TestPrivateSentinelsNeverLeave(t *testing.T) { |
| 282 | v := newValley(t) |
| 283 | held := v.objects(filepath.Join(v.srv, "site.git")) |
| 284 | for _, s := range []string{sentinelSource, sentinelBody, sentinelComment} { |
| 285 | if !strings.Contains(held, s) { |
| 286 | t.Fatalf("site's repository does not hold the sentinel %s, so the test proves nothing", s) |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | dir, _, _ := v.out() |
| 291 | data, err := os.ReadFile(filepath.Join(dir, export.ExportFile)) |
| 292 | if err != nil { |
| 293 | t.Fatal(err) |
| 294 | } |
| 295 | noSentinels(t, "--out's export.json", string(data)) |
| 296 | bundles, _ := filepath.Glob(filepath.Join(dir, "bundles", "*")) |
| 297 | if len(bundles) != 1 { |
| 298 | t.Fatalf("--out holds the bundles %v, want app's alone", bundles) |
| 299 | } |
| 300 | noSentinels(t, "--out's bundle of app", v.unbundled(bundles[0])) |
| 301 | |
| 302 | rv := newReceiver(t, v) |
| 303 | v.publish("--receiver", rv.server.URL) |
| 304 | rv.inActive(func(g export.Generation) { |
| 305 | data, err := os.ReadFile(g.ExportPath()) |
| 306 | if err != nil { |
| 307 | t.Fatal(err) |
| 308 | } |
| 309 | noSentinels(t, "the receiver's export.json", string(data)) |
| 310 | repos, _ := filepath.Glob(filepath.Join(g.Dir, "repos", "*")) |
| 311 | if len(repos) != 1 { |
| 312 | t.Fatalf("the receiver serves the repositories %v, want app's alone", repos) |
| 313 | } |
| 314 | noSentinels(t, "the receiver's repository of app", v.objects(repos[0])) |
| 315 | }) |
| 316 | } |
| 317 | |
| 318 | // A project that declares itself private again is gone from the next |
| 319 | // export the receiver activates: from export.json and from its |
| 320 | // repositories. |
| 321 | func TestAProjectMadePrivateLeavesTheReceiver(t *testing.T) { |
| 322 | v := newValley(t) |
| 323 | rv := newReceiver(t, v) |
| 324 | v.declare("site", `project: visibility: "public"`) |
| 325 | v.publish("--receiver", rv.server.URL) |
| 326 | if got := names(rv.active()); !reflect.DeepEqual(got, []string{"app", "site"}) { |
| 327 | t.Fatalf("the receiver serves %v, want app and site", got) |
| 328 | } |
| 329 | |
| 330 | v.declare("site", `project: visibility: "private"`) |
| 331 | stdout, _ := v.publish("--receiver", rv.server.URL) |
| 332 | mustSay(t, "site made private", stdout, "public: app; private: site, valley") |
| 333 | if got := names(rv.active()); !reflect.DeepEqual(got, []string{"app"}) { |
| 334 | t.Errorf("the receiver serves %v once site is private, want app alone", got) |
| 335 | } |
| 336 | rv.inActive(func(g export.Generation) { |
| 337 | repo, _ := g.Repo("site") |
| 338 | if _, err := os.Stat(repo); !errors.Is(err, os.ErrNotExist) { |
| 339 | t.Errorf("the receiver's active generation still holds site's repository") |
| 340 | } |
| 341 | data, err := os.ReadFile(g.ExportPath()) |
| 342 | if err != nil { |
| 343 | t.Fatal(err) |
| 344 | } |
| 345 | noSentinels(t, "the receiver's export.json once site is private", string(data)) |
| 346 | }) |
| 347 | } |
| 348 | |
| 349 | // landedChangeID is a change of app's that the integrator lands as a new |
| 350 | // commit, so only its transfer statement shows it landed. |
| 351 | const landedChangeID = "kpqrstuvwxyzkpqrstuvwxyzkpqrstuv" |
| 352 | |
| 353 | // publishSigners publishes, in the instance repository, the signers a |
| 354 | // client verifies the integrator's landings with, as the host's signers |
| 355 | // publisher writes them: a tree at refs/the-valley/signers holding the |
| 356 | // known-signers file and the integrator's name. The integrator's key is |
| 357 | // made here; its verifier line is the file's one line. |
| 358 | func (v *testValley) publishSigners(name string) (key string) { |
| 359 | v.t.Helper() |
| 360 | key = filepath.Join(v.root, "integrator.key") |
| 361 | v.run("", "ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "integrator", "-f", key) |
| 362 | instance := filepath.Join(v.srv, "valley.git") |
| 363 | keys := v.runInput("", v.run("", "attest", "key", "--key", key, "--name", name), "git", "-C", instance, "hash-object", "-w", "--stdin") |
| 364 | named := v.runInput("", name, "git", "-C", instance, "hash-object", "-w", "--stdin") |
| 365 | tree := v.runInput("", fmt.Sprintf("100644 blob %s\tintegrator-name\n100644 blob %s\tknown-signers\n", |
| 366 | strings.TrimSpace(named), strings.TrimSpace(keys)), "git", "-C", instance, "mktree") |
| 367 | v.run("", "git", "-C", instance, "update-ref", "refs/the-valley/signers", strings.TrimSpace(tree)) |
| 368 | return key |
| 369 | } |
| 370 | |
| 371 | // landByIntegration readies a change on app and lands it as the |
| 372 | // integrator does: a new commit on main, the outcome record, and the |
| 373 | // transfer statement, signed with key under name. |
| 374 | func (v *testValley) landByIntegration(key, name string) { |
| 375 | v.t.Helper() |
| 376 | work, bare := v.work["app"], filepath.Join(v.srv, "app.git") |
| 377 | v.run(work, "git", "checkout", "--quiet", "-b", "landed", "main") |
| 378 | v.write(filepath.Join(work, "landed.txt"), "landed\n") |
| 379 | v.run(work, "git", "add", "landed.txt") |
| 380 | v.run(work, "git", "commit", "--quiet", "-m", "app: a change the integrator lands") |
| 381 | head := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD")) |
| 382 | v.run(work, "git", "push", "--quiet", "origin", "landed") |
| 383 | v.valley("app", "ready", "--change", landedChangeID, "landed") |
| 384 | // main moves on first, so the landing is a new commit and never the |
| 385 | // change's head. |
| 386 | v.run(work, "git", "checkout", "--quiet", "main") |
| 387 | v.write(filepath.Join(work, "moved.txt"), "moved\n") |
| 388 | v.commit("app", "app: main moves on") |
| 389 | tip := strings.TrimSpace(v.run(work, "git", "rev-parse", "main")) |
| 390 | v.run(work, "git", "checkout", "--quiet", "-b", "landing", "main") |
| 391 | v.run(work, "git", "cherry-pick", head) |
| 392 | landed := strings.TrimSpace(v.run(work, "git", "rev-parse", "HEAD")) |
| 393 | v.run(work, "git", "push", "--quiet", "origin", "landing:main") |
| 394 | v.run(work, "git", "checkout", "--quiet", "main") |
| 395 | v.run(work, "git", "pull", "--quiet", "--ff-only", "origin", "main") |
| 396 | |
| 397 | request := "c-" + landedChangeID |
| 398 | memo := v.runInput("", fmt.Sprintf("%s %s 0123456789abcdef land\n", head, tip), "git", "-C", bare, "hash-object", "-w", "--stdin") |
| 399 | v.run("", "git", "-C", bare, "update-ref", "refs/the-valley/integration-outcomes/main/"+request, strings.TrimSpace(memo)) |
| 400 | digest := func(rev string) string { |
| 401 | out := strings.TrimSpace(v.run("", "attest", "digest", "--repo", work, "--rev", rev)) |
| 402 | _, d, _ := strings.Cut(out, ":") |
| 403 | return d |
| 404 | } |
| 405 | statement, err := json.Marshal(map[string]any{ |
| 406 | "statementType": "the-valley/attestation/v1", |
| 407 | "subject": map[string]any{"primary": "valley-tree-v1", |
| 408 | "digest": map[string]string{"valley-tree-v1": digest(landed), "git-sha1": landed}}, |
| 409 | "predicateType": "the-valley/integration/transfer/v1", |
| 410 | "predicate": map[string]any{"change": request, "target": "refs/heads/main", |
| 411 | "base": map[string]string{"valley-tree-v1": digest(head)}, |
| 412 | "policy": map[string]string{"sha256": strings.Repeat("ab", 32), "git-sha1": tip}, "required": "none"}, |
| 413 | }) |
| 414 | if err != nil { |
| 415 | v.t.Fatal(err) |
| 416 | } |
| 417 | path := filepath.Join(v.root, "statement.json") |
| 418 | v.write(path, string(statement)) |
| 419 | note := v.runInput("", v.run("", "attest", "render", path), "attest", "sign", "--key", key, "--name", name) |
| 420 | blob := v.runInput("", note, "git", "-C", bare, "hash-object", "-w", "--stdin") |
| 421 | tree := v.runInput("", "100644 blob "+strings.TrimSpace(blob)+"\ttransfer.note\n", "git", "-C", bare, "mktree") |
| 422 | v.run("", "git", "-C", bare, "update-ref", |
| 423 | fmt.Sprintf("refs/the-valley/attestations/%s/%08x", digest(landed), len(name)), strings.TrimSpace(tree)) |
| 424 | } |
| 425 | |
| 426 | // The CLI the publisher runs verifies a landing with the signers the |
| 427 | // instance repository publishes, and with nothing the caller's environment |
| 428 | // names: a landed change is not exported as ready for approval. Without |
| 429 | // the publication the landing is not believed, and the change is. |
| 430 | func TestLandingsAreVerifiedWithThePublishedSigners(t *testing.T) { |
| 431 | v := newValley(t) |
| 432 | needTool(t, "attest") |
| 433 | needTool(t, "ssh-keygen") |
| 434 | const signer = "valley-host/integration" |
| 435 | key := v.publishSigners(signer) |
| 436 | v.landByIntegration(key, signer) |
| 437 | |
| 438 | // The caller's own material names the signer with another key, under |
| 439 | // which the statement would not verify. |
| 440 | other := filepath.Join(v.root, "other.key") |
| 441 | v.run("", "ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", "other", "-f", other) |
| 442 | otherSigners := filepath.Join(v.root, "other-signers") |
| 443 | v.write(otherSigners, v.run("", "attest", "key", "--key", other, "--name", signer)) |
| 444 | t.Setenv("VALLEY_KNOWN_SIGNERS", otherSigners) |
| 445 | t.Setenv("VALLEY_INTEGRATOR_SIGNER", signer) |
| 446 | |
| 447 | changes := func() []string { |
| 448 | _, _, e := v.out() |
| 449 | ids := []string{} |
| 450 | for _, c := range e.Projects[0].Changes { |
| 451 | ids = append(ids, c.ChangeID) |
| 452 | } |
| 453 | return ids |
| 454 | } |
| 455 | if got := changes(); !reflect.DeepEqual(got, []string{changeID}) { |
| 456 | t.Errorf("with the published signers, app's changes ready for approval are %v, want %s alone", got, changeID) |
| 457 | } |
| 458 | v.run("", "git", "-C", filepath.Join(v.srv, "valley.git"), "update-ref", "-d", "refs/the-valley/signers") |
| 459 | got := changes() |
| 460 | if len(got) != 2 || !strings.Contains(strings.Join(got, " "), landedChangeID) { |
| 461 | t.Errorf("with no published signers, app's changes ready for approval are %v, want the landed change among them", got) |
| 462 | } |
| 463 | } |