| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/base64" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // The key hash is the note format's own, and a compiler that gets it wrong |
| 11 | // writes verifier lines nothing can match. Two vectors pin it. The first is |
| 12 | // the one attest itself is held to (attest/attest_test.go): sum.golang.org |
| 13 | // publishes its verifier key, so the reference implementation has already |
| 14 | // computed the answer. |
| 15 | func TestKeyHashMatchesTheReferenceImplementation(t *testing.T) { |
| 16 | const ( |
| 17 | name = "sum.golang.org" |
| 18 | public = "Ac4zctda0e5eza+HJyk9SxEdh+s3Ux18htTTAD8OuAn8" |
| 19 | want = "033de0ae" |
| 20 | ) |
| 21 | raw, err := base64.StdEncoding.DecodeString(public) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | if got := verifierKey(name, raw[1:]); got != name+"+"+want+"+"+public { |
| 26 | t.Errorf("verifierKey(%s) = %s, want the published line", name, got) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // The second vector is the worked registry's integrator key, under its own |
| 31 | // name and under the host's attestations name. The name is inside the hash, |
| 32 | // so one key under two names is two verifier keys — the reason a signing |
| 33 | // name is recorded per key. |
| 34 | func TestOneKeyUnderTwoNamesIsTwoVerifierKeys(t *testing.T) { |
| 35 | const key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEGTRJkpUO9Z+nujKT+u5Ho7jcovPYu070iz6poQIKoI" |
| 36 | pub, err := sshEd25519Public(key) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | for name, want := range map[string]string{ |
| 41 | "integrator": "integrator+a3f65322+AUGTRJkpUO9Z+nujKT+u5Ho7jcovPYu070iz6poQIKoI", |
| 42 | "valley-host/attestations": "valley-host/attestations+d6a0b338+AUGTRJkpUO9Z+nujKT+u5Ho7jcovPYu070iz6poQIKoI", |
| 43 | } { |
| 44 | if got := verifierKey(name, pub); got != want { |
| 45 | t.Errorf("verifierKey(%s) = %s, want %s", name, got, want) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestPublicKeysThatAreNotEd25519AreRefused(t *testing.T) { |
| 51 | for _, line := range []string{ |
| 52 | "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQ", |
| 53 | "ssh-ed25519", |
| 54 | "ssh-ed25519 not-base64!", |
| 55 | // An ed25519 line whose blob names another algorithm. |
| 56 | "ssh-ed25519 AAAAB3NzaC1yc2EAAAADAQABAAAAIFtx7r+B/QI75Oohpr5+TQIEmTBstSuby0rsG9d4T8vx", |
| 57 | } { |
| 58 | if _, err := sshEd25519Public(line); err == nil { |
| 59 | t.Errorf("%q was read as an ed25519 key", line) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Expiry is what makes a machine credential a revocation already scheduled |
| 65 | // (bd-8a591dc), so the day it lands on is worth pinning: the declared day is |
| 66 | // the first day the entry no longer counts. |
| 67 | func TestAnEntryExpiresOnTheDayItDeclares(t *testing.T) { |
| 68 | for _, c := range []struct { |
| 69 | expires string |
| 70 | on string |
| 71 | want bool |
| 72 | }{ |
| 73 | {"2026-10-01", "2026-09-30", false}, |
| 74 | {"2026-10-01", "2026-10-01", true}, |
| 75 | {"2026-10-01", "2026-10-02", true}, |
| 76 | {"", "2999-01-01", false}, |
| 77 | } { |
| 78 | got, err := hasExpired(c.expires, day(t, c.on)) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if got != c.want { |
| 83 | t.Errorf("expires %q on %s: expired = %v, want %v", c.expires, c.on, got, c.want) |
| 84 | } |
| 85 | } |
| 86 | if _, err := hasExpired("the first of never", day(t, "2026-01-01")); err == nil { |
| 87 | t.Error("a malformed expiry compiled") |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func day(t *testing.T, s string) time.Time { |
| 92 | t.Helper() |
| 93 | d, err := time.Parse(dateLayout, s) |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | return d |
| 98 | } |
| 99 | |
| 100 | // A grant naming a boundary the registry does not run must fail the render |
| 101 | // rather than compile into nothing: the schema refuses one, and a compiler |
| 102 | // that dropped it silently would turn a typo into a loss of access nobody |
| 103 | // asked for. |
| 104 | func TestAGrantAtAnUndeclaredBoundaryFailsTheRender(t *testing.T) { |
| 105 | r := registry{ |
| 106 | Boundaries: map[string]boundary{"push": {Kind: boundaryGitPush}}, |
| 107 | Principals: map[string]principal{ |
| 108 | "someone": { |
| 109 | Kind: "human", |
| 110 | Keys: []key{{Public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEGTRJkpUO9Z+nujKT+u5Ho7jcovPYu070iz6poQIKoI"}}, |
| 111 | Grants: map[string]grant{"publish": {Boundary: "bus"}}, |
| 112 | }, |
| 113 | }, |
| 114 | } |
| 115 | _, err := render(r, time.Now(), nil) |
| 116 | if err == nil || !strings.Contains(err.Error(), "bus") { |
| 117 | t.Errorf("render error = %v, want one naming the undeclared boundary", err) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // The registry's own stream cannot be left in nobody's hand (dcr-2f03be3). |
| 122 | // The state is arrived at by the clock rather than declared, which is why |
| 123 | // the compiler refuses it and the schema cannot: one registry, unchanged, |
| 124 | // renders the day before its only governing entry expires and fails the day |
| 125 | // it does. The entry still holding a push grant does not save it — a |
| 126 | // boundary of another kind is not governance. |
| 127 | func TestARenderThatOrphansGovernanceIsRefused(t *testing.T) { |
| 128 | r := governedRegistry() |
| 129 | r.Principals["founder"] = expiring(r.Principals["founder"], "2026-10-01") |
| 130 | if _, err := render(r, day(t, "2026-09-30"), nil); err != nil { |
| 131 | t.Fatalf("the day before the only governing entry expired: %v", err) |
| 132 | } |
| 133 | for _, on := range []string{"2026-10-01", "2026-11-01"} { |
| 134 | _, err := render(r, day(t, on), nil) |
| 135 | if err == nil || !strings.Contains(err.Error(), "governanceOrphaned") { |
| 136 | t.Errorf("render on %s: error = %v, want one naming governanceOrphaned", on, err) |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // A registry that governs nothing on any day is refused too. The schema |
| 142 | // already rejects the document (genesisGovernance), so this is the compiler |
| 143 | // holding the same floor over whatever reaches it. |
| 144 | func TestARegistryGoverningNothingIsRefused(t *testing.T) { |
| 145 | r := governedRegistry() |
| 146 | r.Principals["founder"] = withGrants(r.Principals["founder"], map[string]grant{"push": {Boundary: "push"}}) |
| 147 | _, err := render(r, day(t, "2026-01-01"), nil) |
| 148 | if err == nil || !strings.Contains(err.Error(), "governanceOrphaned") { |
| 149 | t.Errorf("render error = %v, want one naming governanceOrphaned", err) |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // A registry that compiles: one entry governing, one pushing. |
| 154 | func governedRegistry() registry { |
| 155 | const public = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEGTRJkpUO9Z+nujKT+u5Ho7jcovPYu070iz6poQIKoI" |
| 156 | return registry{ |
| 157 | Boundaries: map[string]boundary{ |
| 158 | "push": {Kind: boundaryGitPush}, |
| 159 | "registry": {Kind: boundaryRegistry}, |
| 160 | }, |
| 161 | Genesis: "founder", |
| 162 | Principals: map[string]principal{ |
| 163 | "founder": { |
| 164 | Kind: "human", |
| 165 | Keys: []key{{Public: public, Signs: "founder"}}, |
| 166 | Grants: map[string]grant{"govern": {Boundary: "registry"}}, |
| 167 | }, |
| 168 | "runner": { |
| 169 | Kind: "machine", |
| 170 | Keys: []key{{Public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIO0AEJMcSrXCOYKlyVrdVWG07RF2SIM/8gaI/8oDEMRI"}}, |
| 171 | Grants: map[string]grant{"push": {Boundary: "push"}}, |
| 172 | Expires: "2027-01-01", |
| 173 | }, |
| 174 | }, |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | func expiring(p principal, expires string) principal { |
| 179 | p.Expires = expires |
| 180 | return p |
| 181 | } |
| 182 | |
| 183 | func withGrants(p principal, g map[string]grant) principal { |
| 184 | p.Grants = g |
| 185 | return p |
| 186 | } |
| 187 | |
| 188 | // sshd authorizes a key by the first entry naming it, so a key authorized |
| 189 | // as two principals pushes as whichever line comes first. Both ways of |
| 190 | // getting there are refused: two registry entries holding one key, and a |
| 191 | // registry key the host also declares by hand under another tag. |
| 192 | func TestOneKeyAuthorizesOnePrincipal(t *testing.T) { |
| 193 | r := governedRegistry() |
| 194 | founder := r.Principals["founder"] |
| 195 | founder.Grants = map[string]grant{"push": {Boundary: "push"}, "govern": {Boundary: "registry"}} |
| 196 | r.Principals["founder"] = founder |
| 197 | if _, err := render(r, day(t, "2026-09-30"), nil); err != nil { |
| 198 | t.Fatalf("two principals with two keys: %v", err) |
| 199 | } |
| 200 | |
| 201 | blob := strings.Fields(r.Principals["runner"].Keys[0].Public)[1] |
| 202 | for _, declared := range []string{"", "someone-else"} { |
| 203 | _, err := render(r, day(t, "2026-09-30"), map[string]string{blob: declared}) |
| 204 | if err == nil || !strings.Contains(err.Error(), "declared on this host") { |
| 205 | t.Errorf("runner's key declared as %q: error = %v, want one naming the declared entry", declared, err) |
| 206 | } |
| 207 | } |
| 208 | if _, err := render(r, day(t, "2026-09-30"), map[string]string{blob: "runner"}); err != nil { |
| 209 | t.Errorf("runner's key declared as runner: %v", err) |
| 210 | } |
| 211 | |
| 212 | runner := r.Principals["runner"] |
| 213 | runner.Keys = founder.Keys |
| 214 | r.Principals["runner"] = runner |
| 215 | if _, err := render(r, day(t, "2026-09-30"), nil); err == nil || !strings.Contains(err.Error(), "one key authorizes one principal") { |
| 216 | t.Errorf("one key under two principals: error = %v", err) |
| 217 | } |
| 218 | } |