console: sweep expired app-bound customer credentials when ungated
PurgeCLIAuth left out expired kind=platform rows bound to an app, so the armed claim gate could not abort the sweep. On every console, gated or not, they piled up until app purge (#1295, BEFORE PRODUCTION item 2). They now go in a second statement of their own, so its failure never stops the ordinary purge, and its error is logged. Under the armed gate that statement is skipped: deleting an app-bound row needs the app's host-claim permit (withAutoAppSQL, a bucket admission per app), which housekeeping on the login path does not take. There app purge still removes them.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+62
−5
internal/console/authstore.go+17 −4internal/console/platform_credentials_test.go+45 −1
Intent
Attributed to this commit: 47.6 g CO₂e (6,142,220 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on platform-accounts-followups, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/authstore.go b/internal/console/authstore.go| index 009bc67a8..a38121aca 100644 |
| --- a/internal/console/authstore.go |
| +++ b/internal/console/authstore.go |
| @@ -527,14 +527,27 @@ func (s *Store) PurgeCLIAuth(now time.Time) { |
| // also end its channel. |
| // |
| // An app-bound per-customer credential (kind platform, an app) is left |
| - // out too. This DELETE holds no permit, and under the armed claim gate |
| - // one such row would abort the whole statement, so nothing expired |
| - // would be purged again. An expired one already resolves to nothing, |
| - // and app purge deletes it with its app. |
| + // out of this statement and swept by the next one. This DELETE holds no |
| + // permit, and under the armed claim gate one such row would abort the |
| + // whole statement, so nothing expired would be purged again. |
| if _, err := s.db.ExecContext(ctx, `DELETE FROM cli_tokens WHERE expires_at != 0 AND expires_at < ? AND kind != ? |
| AND NOT (kind = ? AND app != '')`, now.Unix(), cliKindLink, cliKindPlatform); err != nil { |
| log.Printf("console: purge expired tokens: %v", err) |
| } |
| + // Expired app-bound per-customer credentials, in a statement of their |
| + // own so a failure here never stops the ordinary purge above. Under the |
| + // armed claim gate they stay: a DELETE of an app-bound row needs that |
| + // app's host-claim permit (withAutoAppSQL, a bucket admission per app), |
| + // which housekeeping on the login path does not take. There an expired |
| + // one already resolves to nothing, and app purge deletes it with its |
| + // app. Everywhere else they are swept with the rest. |
| + if s.claimGated() { |
| + return |
| + } |
| + if _, err := s.db.ExecContext(ctx, `DELETE FROM cli_tokens WHERE expires_at != 0 AND expires_at < ? AND kind = ? AND app != ''`, |
| + now.Unix(), cliKindPlatform); err != nil { |
| + log.Printf("console: purge expired app-bound platform credentials: %v", err) |
| + } |
| } |
| // CLIAuthByUserCode resolves a typed code to the request it names, for the |
diff --git a/internal/console/platform_credentials_test.go b/internal/console/platform_credentials_test.go| index 17483dfa0..0b8c581cc 100644 |
| --- a/internal/console/platform_credentials_test.go |
| +++ b/internal/console/platform_credentials_test.go |
| @@ -1025,7 +1025,7 @@ func TestCustomerCredentialUnderTheGateIsSweptByARevoke(t *testing.T) { |
| // review, Fable I1): the expired-token sweep holds no permit. Under the |
| // armed claim gate an expired app-bound per-customer credential is left to |
| // app purge, and the sweep still purges an ordinary expired token instead |
| -// of aborting on that row. |
| +// of aborting on that row. Neither of the sweep's statements fails. |
| func TestExpiredTokenSweepUnderTheGateGoesOnPastACustomerCredential(t *testing.T) { |
| s, _, _ := sqlIngestFixture(t) |
| ctx := context.Background() |
| @@ -1054,7 +1054,11 @@ func TestExpiredTokenSweepUnderTheGateGoesOnPastACustomerCredential(t *testing.T |
| lastCLIPurge.Store(0) |
| defer lastCLIPurge.Store(0) |
| + logs := captureLog(t) |
| s.PurgeCLIAuth(later) |
| + if strings.Contains(logs.String(), "console: purge") { |
| + t.Fatalf("the sweep logged a failure under the gate:\n%s", logs) |
| + } |
| rows := func(raw string) int { |
| var n int |
| @@ -1074,6 +1078,46 @@ func TestExpiredTokenSweepUnderTheGateGoesOnPastACustomerCredential(t *testing.T |
| } |
| } |
| +// TestExpiredTokenSweepTakesAppBoundCustomerCredentials: on a console |
| +// without the claim gate the sweep deletes an expired app-bound |
| +// per-customer credential with the rest, and keeps one still in date. |
| +func TestExpiredTokenSweepTakesAppBoundCustomerCredentials(t *testing.T) { |
| + s := openTestStore(t) |
| + ctx := context.Background() |
| + p := mustAccount(t, s, "bcd") |
| + gen := grantPlatformGen(t, s, p.ID) |
| + reserveMint(t, s, "bcd", "aa11", "4812") |
| + c := customerOf("bcd", "bdf", "4812", "aa11") |
| + if err := s.CreateLinkedAccount(ctx, c, time.Now()); err != nil { |
| + t.Fatal(err) |
| + } |
| + now := time.Now() |
| + later := now.Add(CLITokenTTL + time.Hour) |
| + expired, live := now.Add(time.Hour).Unix(), later.Add(time.Hour).Unix() |
| + insertTokenRow(t, s, "deploy-expired", cliKindPlatform, c.ID, "blog", string(svcrole.Deploy), expired, p.ID, gen) |
| + insertTokenRow(t, s, "deploy-live", cliKindPlatform, c.ID, "blog", string(svcrole.Deploy), live, p.ID, gen) |
| + insertTokenRow(t, s, "publish-expired", cliKindPlatform, c.ID, "", string(svcrole.Publish), expired, p.ID, gen) |
| + user := mintUserToken(t, s, "paul@keymail.dev", now) |
| + |
| + lastCLIPurge.Store(0) |
| + defer lastCLIPurge.Store(0) |
| + logs := captureLog(t) |
| + s.PurgeCLIAuth(later) |
| + if strings.Contains(logs.String(), "console: purge") { |
| + t.Fatalf("the sweep logged a failure:\n%s", logs) |
| + } |
| + |
| + for raw, want := range map[string]int{"deploy-expired": 0, "deploy-live": 1, "publish-expired": 0, user: 0} { |
| + var n int |
| + if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM cli_tokens WHERE token_hash = ?`, hashToken(raw)).Scan(&n); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n != want { |
| + t.Errorf("rows for %s after the sweep = %d, want %d", raw, n, want) |
| + } |
| + } |
| +} |
| + |
| // TestRevokingAPlatformCredentialUnderTheGateEndsItsDeployMints: under the |
| // armed claim gate, the revoke of a platform credential ends the app-bound |
| // Deploy credential it minted with no permit (RevokeFree), and succeeds. |