| 1 | package main |
| 2 | |
| 3 | import ( |
| 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. |
| 15 | func 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. |
| 27 | func 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 | if got := s.git("cat-file", "blob", published()+":"+file); got != strings.TrimSuffix(want, "\n") { |
| 59 | t.Errorf("the publication's %s holds %q, want %q", file, got, want) |
| 60 | } |
| 61 | } |
| 62 | identity := func(roots ...string) string { |
| 63 | t.Helper() |
| 64 | id, err := approval.InstanceID("sha1", roots) |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | return id |
| 69 | } |
| 70 | both := []string{"--approval-signers", approvers, "--approval-signers-hardware", hardware} |
| 71 | |
| 72 | base := s.git("rev-parse", "refs/heads/main") |
| 73 | if err := publish(both...); err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if got := s.git("ls-tree", "--name-only", published()); got != "approval-signers\napproval-signers-hardware\ninstance-id\nintegrator-name\nknown-signers" { |
| 77 | t.Fatalf("the publication holds %q", got) |
| 78 | } |
| 79 | holds(signersApprovers, "# compiled\n"+key("operator", 2)+key("alice", 3)) |
| 80 | holds(signersHardware, "# compiled\n"+key("operator", 2)) |
| 81 | holds(signersInstance, identity(base)) |
| 82 | |
| 83 | first := published() |
| 84 | if err := publish(both...); err != nil || published() != first { |
| 85 | t.Fatalf("publishing what is published moved the ref (%v)", err) |
| 86 | } |
| 87 | |
| 88 | // The registry revokes every approval key: the files hold no key, and |
| 89 | // are published as they are. |
| 90 | write("approval-signers", "# compiled\n") |
| 91 | write("approval-signers-hardware", "# compiled\n") |
| 92 | if err := publish(both...); err != nil { |
| 93 | t.Fatal(err) |
| 94 | } |
| 95 | holds(signersApprovers, "# compiled\n") |
| 96 | holds(signersHardware, "# compiled\n") |
| 97 | |
| 98 | // A host that configures no approval keys publishes empty files. |
| 99 | write("approval-signers", key("operator", 2)) |
| 100 | if err := publish("--approval-signers", approvers); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | holds(signersApprovers, key("operator", 2)) |
| 104 | holds(signersHardware, "") |
| 105 | if err := publish(); err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | holds(signersApprovers, "") |
| 109 | holds(signersHardware, "") |
| 110 | |
| 111 | // A named file that is missing or does not read publishes nothing. |
| 112 | last := published() |
| 113 | for what, args := range map[string][]string{ |
| 114 | "a missing approval signers file": {"--approval-signers", filepath.Join(dir, "missing")}, |
| 115 | "a missing hardware file": {"--approval-signers", approvers, "--approval-signers-hardware", filepath.Join(dir, "missing")}, |
| 116 | "an approval signers file of prose": {"--approval-signers", write("prose", "not a verifier key\n")}, |
| 117 | } { |
| 118 | if err := publish(args...); err == nil { |
| 119 | t.Errorf("%s was published", what) |
| 120 | } |
| 121 | if published() != last { |
| 122 | t.Errorf("%s moved the publication", what) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // The identity is the lineage's: a clone shares it, an unrelated root |
| 127 | // integrated into main changes it, and a shallow clone has none. |
| 128 | clone := filepath.Join(t.TempDir(), "clone.git") |
| 129 | s.git("clone", "--quiet", "--bare", s.repo, clone) |
| 130 | if id, err := instanceID(clone); err != nil || id != identity(base) { |
| 131 | t.Errorf("the clone's identity is %q (%v), want the original's", id, err) |
| 132 | } |
| 133 | other := s.commit("an unrelated root") |
| 134 | merge := s.commit("the merge", base, other) |
| 135 | s.git("update-ref", "refs/heads/main", merge) |
| 136 | if err := publish(); err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | holds(signersInstance, identity(base, other)) |
| 140 | shallow := filepath.Join(t.TempDir(), "shallow.git") |
| 141 | s.git("clone", "--quiet", "--bare", "--depth", "1", "file://"+s.repo, shallow) |
| 142 | if _, err := instanceID(shallow); err == nil || !strings.Contains(err.Error(), "is shallow") { |
| 143 | t.Errorf("a shallow repository's identity: %v", err) |
| 144 | } |
| 145 | |
| 146 | // An instance repository with no main yet publishes the rest. |
| 147 | empty := filepath.Join(t.TempDir(), "empty.git") |
| 148 | s.git("init", "--quiet", "--bare", empty) |
| 149 | s.repo = empty |
| 150 | if err := publish(); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | if got := s.git("ls-tree", "--name-only", published()); got != "approval-signers\napproval-signers-hardware\nintegrator-name\nknown-signers" { |
| 154 | t.Errorf("the publication of a repository with no main holds %q", got) |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // A change made while a run publishes is published by that run, with |
| 159 | // nothing to wake it again: after each publication the run reads every |
| 160 | // input again and publishes again until a reading matches. The step runs |
| 161 | // once the first round has read its inputs and before its ref moves, |
| 162 | // which is where a wake from the path unit is lost. There the registry |
| 163 | // revokes an approval key and main takes in an unrelated root. |
| 164 | func TestAChangeWhileARunPublishesIsPublishedByIt(t *testing.T) { |
| 165 | s := newScratchRepo(t) |
| 166 | dir := t.TempDir() |
| 167 | write := func(file, content string) string { |
| 168 | path := filepath.Join(dir, file) |
| 169 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | return path |
| 173 | } |
| 174 | const name = "valley-host/attestations" |
| 175 | args := []string{"--instance-repo", s.repo, "--name", name, |
| 176 | "--known-signers", write("known-signers", verifierLine(name, 1)), |
| 177 | "--approval-signers", write("approval-signers", verifierLine("operator", 2)+verifierLine("alice", 3)), |
| 178 | "--approval-signers-hardware", write("approval-signers-hardware", verifierLine("operator", 2)), |
| 179 | } |
| 180 | base := s.git("rev-parse", "refs/heads/main") |
| 181 | other := s.commit("an unrelated root") |
| 182 | merge := s.commit("the merge", base, other) |
| 183 | want, err := approval.InstanceID("sha1", []string{base, other}) |
| 184 | if err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | stdout, stderr := os.Stdout, os.Stderr |
| 188 | os.Stdout, os.Stderr = devNull(t), devNull(t) |
| 189 | var rounds []int |
| 190 | err = publishSignersWith(args, func(round int) { |
| 191 | rounds = append(rounds, round) |
| 192 | if round == 1 { |
| 193 | write("approval-signers", verifierLine("operator", 2)) |
| 194 | s.git("update-ref", "refs/heads/main", merge) |
| 195 | } |
| 196 | }) |
| 197 | os.Stdout, os.Stderr = stdout, stderr |
| 198 | if err != nil { |
| 199 | t.Fatal(err) |
| 200 | } |
| 201 | if len(rounds) != 2 { |
| 202 | t.Errorf("the run published %d times, want 2", len(rounds)) |
| 203 | } |
| 204 | if got := s.git("cat-file", "blob", signersRef+":"+signersApprovers); got+"\n" != verifierLine("operator", 2) { |
| 205 | t.Errorf("the revoked key is still published: %q", got) |
| 206 | } |
| 207 | if got := s.git("cat-file", "blob", signersRef+":"+signersInstance); got != want { |
| 208 | t.Errorf("the publication's instance-id is %s, want %s, from both roots", got, want) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // A run whose inputs change again after every publication stops after |
| 213 | // one round per input that can change and one more, and fails saying so. |
| 214 | func TestARunWhoseInputsKeepChangingFails(t *testing.T) { |
| 215 | s := newScratchRepo(t) |
| 216 | dir := t.TempDir() |
| 217 | approvers := filepath.Join(dir, "approval-signers") |
| 218 | known := filepath.Join(dir, "known-signers") |
| 219 | for path, content := range map[string]string{approvers: "", known: verifierLine("valley-host/attestations", 1)} { |
| 220 | if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 221 | t.Fatal(err) |
| 222 | } |
| 223 | } |
| 224 | stdout, stderr := os.Stdout, os.Stderr |
| 225 | os.Stdout, os.Stderr = devNull(t), devNull(t) |
| 226 | rounds := 0 |
| 227 | err := publishSignersWith([]string{"--instance-repo", s.repo, "--known-signers", known, "--approval-signers", approvers}, func(round int) { |
| 228 | rounds = round |
| 229 | if err := os.WriteFile(approvers, []byte(verifierLine("operator", byte(10+round))), 0o644); err != nil { |
| 230 | t.Error(err) |
| 231 | } |
| 232 | }) |
| 233 | os.Stdout, os.Stderr = stdout, stderr |
| 234 | // The known-signers file, main and the approval signers file can each |
| 235 | // change once, and one round more publishes the last of them. |
| 236 | if err == nil || !strings.Contains(err.Error(), "the inputs kept changing") || rounds != 4 { |
| 237 | t.Fatalf("after %d rounds: %v", rounds, err) |
| 238 | } |
| 239 | if got := s.git("cat-file", "blob", signersRef+":"+signersApprovers); got+"\n" != verifierLine("operator", 13) { |
| 240 | t.Errorf("the last round did not publish what it read: %q", got) |
| 241 | } |
| 242 | } |