gunk-dev/the-valley

identity_test.go

source48c28e253996main's published tip history
7.9 KiB
1package main
2
3import (
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.
15func 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.
34func 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
50func 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.
67func 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
91func 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.
104func 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 AAAAC3NzaC1lZDI1NTE5AAAAINyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ"}},
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.
127func 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.
144func 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.
154func governedRegistry() registry {
155 const public = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINyd1zTLxcPuMDojZpjOdUxGj6AYl6RP7yiWb3Cd/fOQ"
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 AAAAC3NzaC1lZDI1NTE5AAAAIAvpmbUKEDVsejgv2vxWaY/t4xl0JNnjFswb9SxcG9GG"}},
171 Grants: map[string]grant{"push": {Boundary: "push"}},
172 Expires: "2027-01-01",
173 },
174 },
175 }
176}
177
178func expiring(p principal, expires string) principal {
179 p.Expires = expires
180 return p
181}
182
183func 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.
192func 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}