| 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 live one: the integrator's key, whose existing |
| 31 | // attestations are filed under the host's name and which is published under |
| 32 | // its own. The name is inside the hash, so one key under two names is two |
| 33 | // verifier keys — the reason a signing name is recorded per key. |
| 34 | func TestOneKeyUnderTwoNamesIsTwoVerifierKeys(t *testing.T) { |
| 35 | const key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ" |
| 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+78dfd4e6+Adyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ", |
| 42 | "classic-laddie/attestations": "classic-laddie/attestations+9eb2dad8+Adyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ", |
| 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 AAAAB3NzaC1yc2EAAAADAQABAAAAIHo0Oc728AfV2EMn30DhTWSqdWhmY8xR6np/qf6U7xvn", |
| 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 | day := func(s string) time.Time { |
| 69 | t.Helper() |
| 70 | d, err := time.Parse(dateLayout, s) |
| 71 | if err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | return d |
| 75 | } |
| 76 | for _, c := range []struct { |
| 77 | expires string |
| 78 | on string |
| 79 | want bool |
| 80 | }{ |
| 81 | {"2026-10-01", "2026-09-30", false}, |
| 82 | {"2026-10-01", "2026-10-01", true}, |
| 83 | {"2026-10-01", "2026-10-02", true}, |
| 84 | {"", "2999-01-01", false}, |
| 85 | } { |
| 86 | got, err := hasExpired(c.expires, day(c.on)) |
| 87 | if err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | if got != c.want { |
| 91 | t.Errorf("expires %q on %s: expired = %v, want %v", c.expires, c.on, got, c.want) |
| 92 | } |
| 93 | } |
| 94 | if _, err := hasExpired("the first of never", day("2026-01-01")); err == nil { |
| 95 | t.Error("a malformed expiry compiled") |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // A grant naming a boundary the registry does not run must fail the render |
| 100 | // rather than compile into nothing: the schema refuses one, and a compiler |
| 101 | // that dropped it silently would turn a typo into a loss of access nobody |
| 102 | // asked for. |
| 103 | func TestAGrantAtAnUndeclaredBoundaryFailsTheRender(t *testing.T) { |
| 104 | r := registry{ |
| 105 | Boundaries: map[string]boundary{"push": {Kind: boundaryGitPush}}, |
| 106 | Principals: map[string]principal{ |
| 107 | "someone": { |
| 108 | Kind: "human", |
| 109 | Keys: []key{{Public: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ"}}, |
| 110 | Grants: map[string]grant{"publish": {Boundary: "bus"}}, |
| 111 | }, |
| 112 | }, |
| 113 | } |
| 114 | _, err := render(r, time.Now()) |
| 115 | if err == nil || !strings.Contains(err.Error(), "bus") { |
| 116 | t.Errorf("render error = %v, want one naming the undeclared boundary", err) |
| 117 | } |
| 118 | } |