| 1 | package console |
| 2 | |
| 3 | // Door keys (#175 P7, apidoorkeys.go): the console's database is the |
| 4 | // authority, and the bucket holds a signed, revisioned, expiring projection |
| 5 | // of it that one serialized publisher rebuilds from the database every time. |
| 6 | |
| 7 | import ( |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "strings" |
| 14 | "sync" |
| 15 | "sync/atomic" |
| 16 | "testing" |
| 17 | "time" |
| 18 | |
| 19 | "github.com/carlosframework/platform/internal/doorkey" |
| 20 | "github.com/carlosframework/platform/internal/release" |
| 21 | "github.com/carlosframework/platform/internal/svcrole" |
| 22 | ) |
| 23 | |
| 24 | func doorKeysURL(fx *consoleFixture) string { return fx.ts.URL + "/api/cli/apps/bab/titogo/door-keys" } |
| 25 | |
| 26 | func readDoorRecord(t *testing.T, fx *consoleFixture) doorkey.Record { |
| 27 | t.Helper() |
| 28 | wire, _, err := fx.rel.Get(context.Background(), doorkey.RecordKey("bab", "titogo")) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | rec, err := doorkey.Verify(fx.pub, wire, time.Now()) |
| 33 | if err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | return rec |
| 37 | } |
| 38 | |
| 39 | // mintDoorKey mints one key through the door and returns the parsed token. |
| 40 | func mintDoorKey(t *testing.T, fx *consoleFixture, token string) doorkey.Token { |
| 41 | t.Helper() |
| 42 | code, _, out := rawReq(t, http.MethodPost, doorKeysURL(fx), token, `{"label":"router"}`) |
| 43 | if code != http.StatusCreated { |
| 44 | t.Fatalf("mint = %d %s", code, out) |
| 45 | } |
| 46 | var minted struct{ Key, ID string } |
| 47 | if err := json.Unmarshal([]byte(out), &minted); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | tok, err := doorkey.ParseToken(minted.Key) |
| 51 | if err != nil || tok.ID != minted.ID { |
| 52 | t.Fatalf("minted key %q: %v", minted.Key, err) |
| 53 | } |
| 54 | return tok |
| 55 | } |
| 56 | |
| 57 | // putDoorObject overwrites whatever sits at the app's record key. |
| 58 | func putDoorObject(t *testing.T, fx *consoleFixture, body []byte) { |
| 59 | t.Helper() |
| 60 | key := doorkey.RecordKey("bab", "titogo") |
| 61 | _, etag, err := fx.rel.Get(context.Background(), key) |
| 62 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if _, err := fx.rel.Put(context.Background(), key, body, etag); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func signedDoorRecord(t *testing.T, fx *consoleFixture, rev uint64, keys []doorkey.Entry) []byte { |
| 71 | t.Helper() |
| 72 | now := time.Now().UTC() |
| 73 | wire, err := doorkey.Sign(fx.priv, doorkey.Record{V: 1, Account: "bab", App: "titogo", Revision: rev, |
| 74 | Keys: keys, SignedAt: now, ExpiresAt: now.Add(time.Hour)}) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | return wire |
| 79 | } |
| 80 | |
| 81 | func TestDoorKeysMintListRevoke(t *testing.T) { |
| 82 | fx, token := instanceFixture(t, "code-door-keys") |
| 83 | code, _, out := rawReq(t, http.MethodPost, doorKeysURL(fx), token, `{"label":"router"}`) |
| 84 | if code != http.StatusCreated { |
| 85 | t.Fatalf("mint = %d %s", code, out) |
| 86 | } |
| 87 | var minted struct{ Key, ID, Account, App string } |
| 88 | json.Unmarshal([]byte(out), &minted) |
| 89 | tok, err := doorkey.ParseToken(minted.Key) |
| 90 | if err != nil || tok.ID != minted.ID { |
| 91 | t.Fatalf("minted key %q: %v", minted.Key, err) |
| 92 | } |
| 93 | if minted.Account != "bab" || minted.App != "titogo" { |
| 94 | t.Fatalf("mint answered account/app %q/%q", minted.Account, minted.App) |
| 95 | } |
| 96 | rec := readDoorRecord(t, fx) |
| 97 | if !rec.Admits(tok) || rec.Revision != 1 { |
| 98 | t.Fatalf("record %+v does not admit the minted key at revision 1", rec) |
| 99 | } |
| 100 | code, _, out = rawReq(t, http.MethodGet, doorKeysURL(fx), token, "") |
| 101 | if code != http.StatusOK || !strings.Contains(out, minted.ID) || strings.Contains(out, "sha256") || |
| 102 | !strings.Contains(out, "router") { |
| 103 | t.Fatalf("list = %d %s", code, out) |
| 104 | } |
| 105 | if code, _, out = rawReq(t, http.MethodPost, doorKeysURL(fx)+"/"+minted.ID+"/revoke", token, ""); code != http.StatusOK { |
| 106 | t.Fatalf("revoke = %d %s", code, out) |
| 107 | } |
| 108 | if rec := readDoorRecord(t, fx); rec.Admits(tok) || rec.Revision != 2 { |
| 109 | t.Fatalf("after revoke: %+v", rec) |
| 110 | } |
| 111 | // Revoked is gone from the list, and a second revoke is an unknown id. |
| 112 | if code, _, out = rawReq(t, http.MethodGet, doorKeysURL(fx), token, ""); code != http.StatusOK || strings.Contains(out, minted.ID) { |
| 113 | t.Fatalf("list after revoke = %d %s", code, out) |
| 114 | } |
| 115 | if code, _, out = rawReq(t, http.MethodPost, doorKeysURL(fx)+"/"+minted.ID+"/revoke", token, ""); code != http.StatusNotFound { |
| 116 | t.Fatalf("second revoke = %d %s, want 404", code, out) |
| 117 | } |
| 118 | if code, _, out = rawReq(t, http.MethodPost, doorKeysURL(fx)+"/0123456789abcdef/revoke", token, ""); code != http.StatusNotFound { |
| 119 | t.Fatalf("unknown id revoke = %d %s, want 404", code, out) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // Finding 1: whatever sits in the bucket, the console publishes from its |
| 124 | // database. A record copied in from another app's path is overwritten, not |
| 125 | // extended. |
| 126 | func TestDoorKeysPublishFromTheDatabaseNotTheBucket(t *testing.T) { |
| 127 | fx, token := instanceFixture(t, "code-door-keys-copy") |
| 128 | _, planted, _ := doorkey.NewToken("bab", "titogo") |
| 129 | now := time.Now().UTC() |
| 130 | forged, _ := doorkey.Sign(fx.priv, doorkey.Record{V: 1, Account: "bab", App: "titogo", Revision: 99, |
| 131 | Keys: []doorkey.Entry{planted}, SignedAt: now, ExpiresAt: now.Add(time.Hour)}) |
| 132 | fx.rel.Put(context.Background(), doorkey.RecordKey("bab", "titogo"), forged, "") |
| 133 | if code, _, out := rawReq(t, http.MethodPost, doorKeysURL(fx), token, `{}`); code != http.StatusCreated { |
| 134 | t.Fatalf("mint = %d %s", code, out) |
| 135 | } |
| 136 | rec := readDoorRecord(t, fx) |
| 137 | for _, e := range rec.Keys { |
| 138 | if e.ID == planted.ID { |
| 139 | t.Fatal("the console carried a key it never minted into its signed record") |
| 140 | } |
| 141 | } |
| 142 | if len(rec.Keys) != 1 { |
| 143 | t.Fatalf("record carries %d keys, want the one minted", len(rec.Keys)) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestDoorKeysRefuseServiceCredentialsAndReadOnlyConsoles(t *testing.T) { |
| 148 | fx, _ := instanceFixture(t, "code-door-keys-svc") |
| 149 | admin := serviceToken(t, fx.st, OpsAccountID, "root", "", svcrole.Admin) |
| 150 | for _, c := range []struct{ method, url, body string }{ |
| 151 | {http.MethodPost, doorKeysURL(fx), `{}`}, |
| 152 | {http.MethodGet, doorKeysURL(fx), ""}, |
| 153 | {http.MethodPost, doorKeysURL(fx) + "/0123456789abcdef/revoke", ""}, |
| 154 | } { |
| 155 | if code, _, out := rawReq(t, c.method, c.url, admin, c.body); code != http.StatusForbidden { |
| 156 | t.Errorf("%s %s with an admin service credential = %d %s, want 403", c.method, c.url, code, out) |
| 157 | } |
| 158 | } |
| 159 | if _, _, err := fx.rel.Get(context.Background(), doorkey.RecordKey("bab", "titogo")); err == nil { |
| 160 | t.Fatal("a refused service credential left a door-key record") |
| 161 | } |
| 162 | |
| 163 | ro := newConsoleFixtureKeyed(t, false) |
| 164 | now := time.Now() |
| 165 | if _, err := ro.st.CreateAppIn(OpsAccountID, "titogo", "paul@keymail.dev", now); err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | if err := ro.st.AddMember(OpsAccountID, "alice@keymail.dev", RoleMember, now); err != nil { |
| 169 | t.Fatal(err) |
| 170 | } |
| 171 | member := cliToken(t, ro, "alice@keymail.dev", "code-door-keys-ro") |
| 172 | if code, _, out := rawReq(t, http.MethodPost, doorKeysURL(ro), member, `{}`); code != http.StatusServiceUnavailable { |
| 173 | t.Fatalf("mint on a read-only console = %d %s, want 503", code, out) |
| 174 | } |
| 175 | if code, _, out := rawReq(t, http.MethodPost, doorKeysURL(ro)+"/0123456789abcdef/revoke", member, ""); code != http.StatusServiceUnavailable { |
| 176 | t.Fatalf("revoke on a read-only console = %d %s, want 503", code, out) |
| 177 | } |
| 178 | if _, _, err := ro.rel.Get(context.Background(), doorkey.RecordKey("bab", "titogo")); err == nil { |
| 179 | t.Fatal("a read-only console wrote a door-key record") |
| 180 | } |
| 181 | if keys, _, err := ro.st.DoorKeysLive(OpsAccountID, "titogo"); err != nil || len(keys) != 0 { |
| 182 | t.Fatalf("a read-only console stored keys: %v %v", keys, err) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestDoorKeysRefuseANinthKey(t *testing.T) { |
| 187 | fx, token := instanceFixture(t, "code-door-keys-full") |
| 188 | for i := 0; i < doorkey.MaxKeys; i++ { |
| 189 | mintDoorKey(t, fx, token) |
| 190 | } |
| 191 | if code, _, out := rawReq(t, http.MethodPost, doorKeysURL(fx), token, `{}`); code != http.StatusConflict { |
| 192 | t.Fatalf("ninth mint = %d %s, want 409", code, out) |
| 193 | } |
| 194 | rec := readDoorRecord(t, fx) |
| 195 | if len(rec.Keys) != doorkey.MaxKeys || rec.Revision != doorkey.MaxKeys { |
| 196 | t.Fatalf("record after a refused ninth: %d keys at revision %d", len(rec.Keys), rec.Revision) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestDoorKeyResignRefreshesExpiry(t *testing.T) { |
| 201 | fx, token := instanceFixture(t, "code-door-keys-resign") |
| 202 | tok := mintDoorKey(t, fx, token) |
| 203 | before := readDoorRecord(t, fx) |
| 204 | acct, err := fx.st.AccountByID(OpsAccountID) |
| 205 | if err != nil { |
| 206 | t.Fatal(err) |
| 207 | } |
| 208 | time.Sleep(2 * time.Millisecond) |
| 209 | if err := fx.srv.publishDoorKeys(context.Background(), acct, "titogo"); err != nil { |
| 210 | t.Fatal(err) |
| 211 | } |
| 212 | after := readDoorRecord(t, fx) |
| 213 | if after.Revision != before.Revision { |
| 214 | t.Fatalf("a renewal moved the revision %d -> %d", before.Revision, after.Revision) |
| 215 | } |
| 216 | if !after.ExpiresAt.After(before.ExpiresAt) || !after.SignedAt.After(before.SignedAt) { |
| 217 | t.Fatalf("a renewal did not refresh the expiry: %v -> %v", before.ExpiresAt, after.ExpiresAt) |
| 218 | } |
| 219 | if !after.Admits(tok) { |
| 220 | t.Fatal("a renewal dropped a live key") |
| 221 | } |
| 222 | if _, rev, _ := fx.st.DoorKeysLive(OpsAccountID, "titogo"); rev != before.Revision { |
| 223 | t.Fatalf("a renewal moved the database's revision to %d", rev) |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // blockingStore holds the first Put to one key until release is closed, |
| 228 | // signalling entered when it gets there. |
| 229 | type blockingStore struct { |
| 230 | release.Store |
| 231 | key string |
| 232 | once sync.Once |
| 233 | entered chan struct{} |
| 234 | release chan struct{} |
| 235 | puts atomic.Int32 // Puts to key, counted before any blocking |
| 236 | } |
| 237 | |
| 238 | func (b *blockingStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| 239 | if key == b.key { |
| 240 | b.puts.Add(1) |
| 241 | first := false |
| 242 | b.once.Do(func() { first = true }) |
| 243 | if first { |
| 244 | close(b.entered) |
| 245 | <-b.release |
| 246 | } |
| 247 | } |
| 248 | return b.Store.Put(ctx, key, body, ifMatch) |
| 249 | } |
| 250 | |
| 251 | // Round 2 finding 5: a renewal that read revision N must not land after a |
| 252 | // revoke published N+1. The renewal is held inside its Put, under the |
| 253 | // publication lock, while the revoke writes the database and queues behind |
| 254 | // the lock. The bucket must end on the revoke's revision. |
| 255 | func TestDoorKeyRevokeIsNotOverwrittenByARacingRenewal(t *testing.T) { |
| 256 | fx, token := instanceFixture(t, "code-door-keys-race") |
| 257 | tok := mintDoorKey(t, fx, token) |
| 258 | acct, err := fx.st.AccountByID(OpsAccountID) |
| 259 | if err != nil { |
| 260 | t.Fatal(err) |
| 261 | } |
| 262 | bs := &blockingStore{Store: fx.rel, key: doorkey.RecordKey("bab", "titogo"), |
| 263 | entered: make(chan struct{}), release: make(chan struct{})} |
| 264 | fx.srv.cfg.Releases = bs |
| 265 | |
| 266 | var wg sync.WaitGroup |
| 267 | var renewErr error |
| 268 | wg.Add(1) |
| 269 | go func() { |
| 270 | defer wg.Done() |
| 271 | renewErr = fx.srv.publishDoorKeys(context.Background(), acct, "titogo") |
| 272 | }() |
| 273 | <-bs.entered // the renewal has signed revision 1 and is holding the lock |
| 274 | |
| 275 | var revokeCode int |
| 276 | var revokeOut string |
| 277 | wg.Add(1) |
| 278 | go func() { |
| 279 | defer wg.Done() |
| 280 | revokeCode, _, revokeOut = rawReq(t, http.MethodPost, doorKeysURL(fx)+"/"+tok.ID+"/revoke", token, "") |
| 281 | }() |
| 282 | // The revoke's database write lands before it reaches the lock. |
| 283 | deadline := time.Now().Add(5 * time.Second) |
| 284 | for { |
| 285 | if _, rev, _ := fx.st.DoorKeysLive(OpsAccountID, "titogo"); rev == 2 { |
| 286 | break |
| 287 | } |
| 288 | if time.Now().After(deadline) { |
| 289 | t.Fatal("the revoke never wrote the database") |
| 290 | } |
| 291 | time.Sleep(time.Millisecond) |
| 292 | } |
| 293 | close(bs.release) |
| 294 | wg.Wait() |
| 295 | if renewErr != nil { |
| 296 | t.Fatalf("renewal: %v", renewErr) |
| 297 | } |
| 298 | if revokeCode != http.StatusOK { |
| 299 | t.Fatalf("revoke = %d %s", revokeCode, revokeOut) |
| 300 | } |
| 301 | // Exactly two writes pins the per-app lock itself, not just the outcome |
| 302 | // (Fable's final review): without the lock the revoke writes while the |
| 303 | // renewal is held, the renewal's stale-ETag Put then fails, and its retry |
| 304 | // makes a third. The ETag check alone would still land revision 2. |
| 305 | if n := bs.puts.Load(); n != 2 { |
| 306 | t.Fatalf("%d Puts to the record, want 2: the revoke did not wait for the renewal's lock", n) |
| 307 | } |
| 308 | rec := readDoorRecord(t, fx) |
| 309 | if rec.Revision != 2 || rec.Admits(tok) { |
| 310 | t.Fatalf("bucket after the race: revision %d, admits the revoked key %v", rec.Revision, rec.Admits(tok)) |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // Round 2 finding 6: a database whose counter is behind the bucket (a |
| 315 | // restore) publishes above the bucket's revision, never at or below it. |
| 316 | func TestDoorKeyPublishAdvancesPastTheBucketRevision(t *testing.T) { |
| 317 | fx, token := instanceFixture(t, "code-door-keys-advance") |
| 318 | first := mintDoorKey(t, fx, token) |
| 319 | if rec := readDoorRecord(t, fx); rec.Revision != 1 { |
| 320 | t.Fatalf("first publish at revision %d", rec.Revision) |
| 321 | } |
| 322 | putDoorObject(t, fx, signedDoorRecord(t, fx, 7, nil)) |
| 323 | second := mintDoorKey(t, fx, token) |
| 324 | rec := readDoorRecord(t, fx) |
| 325 | if rec.Revision != 8 { |
| 326 | t.Fatalf("published revision %d over a bucket at 7, want 8", rec.Revision) |
| 327 | } |
| 328 | if !rec.Admits(first) || !rec.Admits(second) || len(rec.Keys) != 2 { |
| 329 | t.Fatalf("advanced record lost the database's keys: %+v", rec.Keys) |
| 330 | } |
| 331 | if _, rev, err := fx.st.DoorKeysLive(OpsAccountID, "titogo"); err != nil || rev != 8 { |
| 332 | t.Fatalf("database counter = %d %v, want 8", rev, err) |
| 333 | } |
| 334 | // An equal revision is the renewal case: republished as it is. |
| 335 | acct, _ := fx.st.AccountByID(OpsAccountID) |
| 336 | if err := fx.srv.publishDoorKeys(context.Background(), acct, "titogo"); err != nil { |
| 337 | t.Fatal(err) |
| 338 | } |
| 339 | if rec := readDoorRecord(t, fx); rec.Revision != 8 { |
| 340 | t.Fatalf("renewal at an equal revision published %d, want 8", rec.Revision) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | // Round 2 finding 4: replacing an existing object works, and a tampered |
| 345 | // object is overwritten rather than trusted. |
| 346 | func TestDoorKeyPublishReplacesExistingAndTamperedObjects(t *testing.T) { |
| 347 | fx, token := instanceFixture(t, "code-door-keys-replace") |
| 348 | a := mintDoorKey(t, fx, token) |
| 349 | b := mintDoorKey(t, fx, token) // the second Put replaces the first object |
| 350 | if rec := readDoorRecord(t, fx); rec.Revision != 2 || !rec.Admits(a) || !rec.Admits(b) { |
| 351 | t.Fatalf("after two mints: %+v", rec) |
| 352 | } |
| 353 | putDoorObject(t, fx, []byte("not a door-key record")) |
| 354 | if code, _, out := rawReq(t, http.MethodPost, doorKeysURL(fx)+"/"+a.ID+"/revoke", token, ""); code != http.StatusOK { |
| 355 | t.Fatalf("revoke over a tampered object = %d %s", code, out) |
| 356 | } |
| 357 | rec := readDoorRecord(t, fx) |
| 358 | if rec.Revision != 3 || rec.Admits(a) || !rec.Admits(b) { |
| 359 | t.Fatalf("after revoking over garbage: revision %d, keys %+v", rec.Revision, rec.Keys) |
| 360 | } |
| 361 | // A validly shaped record signed by some other key is tampering too. |
| 362 | other := newConsoleFixture(t) // a different signing key |
| 363 | now := time.Now().UTC() |
| 364 | foreign, _ := doorkey.Sign(other.priv, doorkey.Record{V: 1, Account: "bab", App: "titogo", Revision: 50, |
| 365 | SignedAt: now, ExpiresAt: now.Add(time.Hour)}) |
| 366 | putDoorObject(t, fx, foreign) |
| 367 | acct, _ := fx.st.AccountByID(OpsAccountID) |
| 368 | if err := fx.srv.publishDoorKeys(context.Background(), acct, "titogo"); err != nil { |
| 369 | t.Fatal(err) |
| 370 | } |
| 371 | if rec := readDoorRecord(t, fx); rec.Revision != 3 || !rec.Admits(b) { |
| 372 | t.Fatalf("a foreign-signed revision was trusted: revision %d", rec.Revision) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // casConflictStore loses the first n Puts to one key. |
| 377 | type casConflictStore struct { |
| 378 | release.Store |
| 379 | key string |
| 380 | mu sync.Mutex |
| 381 | lose int |
| 382 | puts int |
| 383 | } |
| 384 | |
| 385 | func (c *casConflictStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| 386 | if key == c.key { |
| 387 | c.mu.Lock() |
| 388 | c.puts++ |
| 389 | lose := c.puts <= c.lose |
| 390 | c.mu.Unlock() |
| 391 | if lose { |
| 392 | return "", release.ErrCASConflict |
| 393 | } |
| 394 | } |
| 395 | return c.Store.Put(ctx, key, body, ifMatch) |
| 396 | } |
| 397 | |
| 398 | // Step 5: a lost compare-and-swap rebuilds and retries, at most three times. |
| 399 | func TestDoorKeyPublishRetriesAConflictAtMostThreeTimes(t *testing.T) { |
| 400 | fx, token := instanceFixture(t, "code-door-keys-cas") |
| 401 | mintDoorKey(t, fx, token) |
| 402 | acct, _ := fx.st.AccountByID(OpsAccountID) |
| 403 | |
| 404 | cs := &casConflictStore{Store: fx.rel, key: doorkey.RecordKey("bab", "titogo"), lose: 2} |
| 405 | fx.srv.cfg.Releases = cs |
| 406 | if err := fx.srv.publishDoorKeys(context.Background(), acct, "titogo"); err != nil { |
| 407 | t.Fatalf("two conflicts then a win: %v", err) |
| 408 | } |
| 409 | if cs.puts != 3 { |
| 410 | t.Fatalf("%d puts, want 3", cs.puts) |
| 411 | } |
| 412 | |
| 413 | cs = &casConflictStore{Store: fx.rel, key: doorkey.RecordKey("bab", "titogo"), lose: 100} |
| 414 | fx.srv.cfg.Releases = cs |
| 415 | if err := fx.srv.publishDoorKeys(context.Background(), acct, "titogo"); !errors.Is(err, release.ErrCASConflict) { |
| 416 | t.Fatalf("persistent conflict = %v, want ErrCASConflict", err) |
| 417 | } |
| 418 | if cs.puts != 3 { |
| 419 | t.Fatalf("%d puts under a persistent conflict, want 3", cs.puts) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | // The re-sign pass covers every app with a revision row. |
| 424 | func TestDoorKeyResignPassCoversEveryApp(t *testing.T) { |
| 425 | fx, token := instanceFixture(t, "code-door-keys-pass") |
| 426 | mintDoorKey(t, fx, token) |
| 427 | apps, err := fx.st.DoorKeyApps() |
| 428 | if err != nil || len(apps) != 1 || apps[0].App != "titogo" || apps[0].Sqid != "bab" || apps[0].AccountID != OpsAccountID { |
| 429 | t.Fatalf("DoorKeyApps = %+v %v", apps, err) |
| 430 | } |
| 431 | before := readDoorRecord(t, fx) |
| 432 | time.Sleep(2 * time.Millisecond) |
| 433 | fx.srv.resignDoorKeys(context.Background()) |
| 434 | if after := readDoorRecord(t, fx); !after.ExpiresAt.After(before.ExpiresAt) { |
| 435 | t.Fatal("the re-sign pass did not renew the record") |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | // mintTestDoorKey stores one live door key for (account, app) directly. |
| 440 | func mintTestDoorKey(t *testing.T, s *Store, accountID int64, app, id string) { |
| 441 | t.Helper() |
| 442 | if _, err := s.DoorKeyInsert(accountID, app, doorkey.Entry{ID: id, SHA256: strings.Repeat("a", 64)}); err != nil { |
| 443 | t.Fatalf("DoorKeyInsert(%d, %s): %v", accountID, app, err) |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // TestPurgeAppRowLeavesNoCredentialsForTheName (#219 item 1): purging frees |
| 448 | // the name, so a later app of that name must not inherit the purged app's |
| 449 | // tokens (either kind) or door keys. Before the fix all three survived the |
| 450 | // purge: the tokens still authenticated, and the door keys stayed live and |
| 451 | // were re-signed every 12 hours. |
| 452 | func TestPurgeAppRowLeavesNoCredentialsForTheName(t *testing.T) { |
| 453 | s := openTestStore(t) |
| 454 | now := time.Now() |
| 455 | acct := mustAccount(t, s, "amadan") |
| 456 | if _, err := s.CreateAppIn(acct.ID, "titogo", "paul@keymail.dev", now); err != nil { |
| 457 | t.Fatal(err) |
| 458 | } |
| 459 | appTok, err := s.NewAppToken(acct.ID, "titogo", "paul@keymail.dev", "router", now) |
| 460 | if err != nil { |
| 461 | t.Fatal(err) |
| 462 | } |
| 463 | svcTok, err := s.NewServicePrincipal(acct.ID, "hub", "titogo", svcrole.Publish, "paul@keymail.dev", now) |
| 464 | if err != nil { |
| 465 | t.Fatal(err) |
| 466 | } |
| 467 | mintTestDoorKey(t, s, acct.ID, "titogo", "0123456789abcdef") |
| 468 | _, revBefore, err := s.DoorKeysLive(acct.ID, "titogo") |
| 469 | if err != nil { |
| 470 | t.Fatal(err) |
| 471 | } |
| 472 | |
| 473 | if err := s.TrashAppRow(acct.ID, "titogo", now); err != nil { |
| 474 | t.Fatal(err) |
| 475 | } |
| 476 | if err := s.PurgeAppRow(acct.ID, "titogo"); err != nil { |
| 477 | t.Fatal(err) |
| 478 | } |
| 479 | |
| 480 | if _, ok := s.CLIPrincipal(appTok, now); ok { |
| 481 | t.Error("the purged app's app token still authenticates") |
| 482 | } |
| 483 | if _, ok := s.CLIPrincipal(svcTok, now); ok { |
| 484 | t.Error("the purged app's service credential still authenticates") |
| 485 | } |
| 486 | keys, rev, err := s.DoorKeysLive(acct.ID, "titogo") |
| 487 | if err != nil { |
| 488 | t.Fatal(err) |
| 489 | } |
| 490 | if len(keys) != 0 { |
| 491 | t.Errorf("door keys after purge = %+v, want none live", keys) |
| 492 | } |
| 493 | // The revision climbs, so the empty record outranks what boxes admitted. |
| 494 | if rev <= revBefore { |
| 495 | t.Errorf("door-key revision after purge = %d, want above %d", rev, revBefore) |
| 496 | } |
| 497 | |
| 498 | // The name is clean for its next owner: a new app of the same name, and |
| 499 | // a rename onto it, are not refused over leftovers. |
| 500 | if _, err := s.CreateAppIn(acct.ID, "titogo", "paul@keymail.dev", now); err != nil { |
| 501 | t.Fatal(err) |
| 502 | } |
| 503 | if _, err := s.CreateAppIn(acct.ID, "other", "paul@keymail.dev", now); err != nil { |
| 504 | t.Fatal(err) |
| 505 | } |
| 506 | if err := s.TrashAppRow(acct.ID, "titogo", now); err != nil { |
| 507 | t.Fatal(err) |
| 508 | } |
| 509 | if err := s.PurgeAppRow(acct.ID, "titogo"); err != nil { |
| 510 | t.Fatal(err) |
| 511 | } |
| 512 | if err := s.RenameAppIn(acct.ID, "other", "titogo"); err != nil { |
| 513 | t.Errorf("RenameAppIn onto a purged name = %v, want it allowed", err) |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // TestPurgeAppRowKeepsALiveAppsCredentials: the credential sweep rides on the |
| 518 | // row delete, so a live (untrashed) app, which PurgeAppRow refuses, keeps |
| 519 | // everything. |
| 520 | func TestPurgeAppRowKeepsALiveAppsCredentials(t *testing.T) { |
| 521 | s := openTestStore(t) |
| 522 | now := time.Now() |
| 523 | acct := mustAccount(t, s, "amadan") |
| 524 | if _, err := s.CreateAppIn(acct.ID, "titogo", "paul@keymail.dev", now); err != nil { |
| 525 | t.Fatal(err) |
| 526 | } |
| 527 | appTok, err := s.NewAppToken(acct.ID, "titogo", "paul@keymail.dev", "router", now) |
| 528 | if err != nil { |
| 529 | t.Fatal(err) |
| 530 | } |
| 531 | mintTestDoorKey(t, s, acct.ID, "titogo", "0123456789abcdef") |
| 532 | if err := s.PurgeAppRow(acct.ID, "titogo"); err != nil { |
| 533 | t.Fatal(err) |
| 534 | } |
| 535 | if _, ok := s.CLIPrincipal(appTok, now); !ok { |
| 536 | t.Error("a refused purge of a live app revoked its token") |
| 537 | } |
| 538 | if keys, _, _ := s.DoorKeysLive(acct.ID, "titogo"); len(keys) != 1 { |
| 539 | t.Errorf("a refused purge of a live app touched its door keys: %+v", keys) |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | // TestMoveAppLeavesNoLiveDoorKeysForTheName (#219 item 1): door keys name |
| 544 | // their account, so they cannot follow a moved app. The source's must not |
| 545 | // stay live for a later app of that name, and a purged predecessor's keys at |
| 546 | // the destination must not answer for the app that moved in. |
| 547 | func TestMoveAppLeavesNoLiveDoorKeysForTheName(t *testing.T) { |
| 548 | s := openTestStore(t) |
| 549 | now := time.Now() |
| 550 | from := mustAccount(t, s, "amadan") |
| 551 | to := mustAccount(t, s, "other") |
| 552 | if _, err := s.CreateAppIn(from.ID, "titogo", "paul@keymail.dev", now); err != nil { |
| 553 | t.Fatal(err) |
| 554 | } |
| 555 | mintTestDoorKey(t, s, from.ID, "titogo", "0123456789abcdef") |
| 556 | // A leftover at the destination, the shape a purge before this fix left. |
| 557 | mintTestDoorKey(t, s, to.ID, "titogo", "fedcba9876543210") |
| 558 | if err := s.MoveApp("titogo", from.ID, to.ID); err != nil { |
| 559 | t.Fatalf("MoveApp: %v", err) |
| 560 | } |
| 561 | for _, side := range []*Account{from, to} { |
| 562 | keys, _, err := s.DoorKeysLive(side.ID, "titogo") |
| 563 | if err != nil { |
| 564 | t.Fatal(err) |
| 565 | } |
| 566 | if len(keys) != 0 { |
| 567 | t.Errorf("%s: live door keys for titogo after the move = %+v, want none", side.Sqid, keys) |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |