carlos / platform Public

console: one blocked app no longer stalls a removal's revocation (H4 r2)

The member-revocation drain attempts every app's revoke and every
door-key publish on its own, so an app under a stuck operation keeps
only the queue row, not the other apps' revocations. A console with no
signing key or bucket keeps the row instead of dropping unpublished
work. A failing row backs off from 10 s to a minute and, once older
than five minutes, each failure is logged as overdue.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev f4bca26056601c21914c4b6a3fa96faff223ac25 parent 5ee2013
3 files changed, +210 −26
  • internal/console/memberrevoke.go +89 −26
  • internal/console/memberrevoke_claim_test.go +77 −0
  • internal/console/memberrevoke_test.go +44 −0
diff --git a/internal/console/memberrevoke.go b/internal/console/memberrevoke.go
index 4b2d354bd..6a3375776 100644
--- a/internal/console/memberrevoke.go
+++ b/internal/console/memberrevoke.go
@@ -31,15 +31,27 @@ package console
// - door keys: the removal writes a member_revocations row in its own
// transaction; drainMemberRevocations republishes the account's
// door-key records and deletes the row only once every publish
-// landed. The browser door drains at once after the commit; the link
-// sync loop drains every linkSyncInterval (10 seconds) until it
-// succeeds, which also covers `console member remove` on the box.
+// landed. Each app is attempted on its own: one app that cannot
+// publish (or, in claims mode, cannot take its permit) never stops
+// another app's revoke or publish on the same pass; it only keeps the
+// row. (The row names no app: member_revocations is a non-app table,
+// which the claims census allows no app column.) The browser door
+// drains at once after the commit; the link sync loop retries until it
+// succeeds, which also covers `console member remove` on the box. A
+// console with no signing key or deployment bucket keeps the row:
+// nothing was published, so the work is not done.
// - link channels: the links row's revoke_synced flag, which the same
// loop already retries (syncLinkChannel).
//
// So a revoked door key is refused at the edge within the drain cadence
// (10 seconds, when the immediate publish fails) plus the edge's 30-second
// door-key record poll; a successful immediate publish leaves the poll.
+// Ten seconds is the first retry, not a bound: an app whose permit or
+// publish keeps failing keeps the row, retried with a backoff that doubles
+// to memberRevocationMaxBackoff (a minute), so a stuck row re-signs the
+// account's records at most once a minute. Each failure is the LinkSync
+// job's error on the operator pages, and once the row is older than
+// memberRevocationOverdue each failure is logged as overdue.
//
// A person's own CLI token (kind '') is not touched: it names no account,
// and its reach into this one already ended with the membership.
@@ -55,6 +67,20 @@ import (
"time"
)
+// memberRevocationOverdue is the age past which a queued row that still
+// fails is logged as overdue: a revocation the drain has not finished in
+// five minutes is blocked, not slow. memberRevocationMaxBackoff caps the
+// wait between a failing row's attempts.
+const (
+ memberRevocationOverdue = 5 * time.Minute
+ memberRevocationMaxBackoff = time.Minute
+)
+
+// errDoorKeyPublishUnavailable is a queued door-key publication on a
+// console with no signing key or deployment bucket: nothing was published,
+// so the row stays.
+var errDoorKeyPublishUnavailable = errors.New("console: door keys: no signing key or deployment bucket, so the record was not republished")
+
// memberRevocation is what one removal revoked outside cli_tokens, for the
// publish after the commit: the apps whose door-key record changed and the
// links whose channel must be revoked.
@@ -72,8 +98,9 @@ type memberRevocation struct {
// link credentials, links, invites) and queues the rest:
// drainMemberRevocations revokes each app's rows under that app's own SQL
// permit, the way every other gated write to those tables is made. Until
-// it has, the removed member's app-scoped credentials still resolve; the
-// drain runs at once after a browser removal and then every 10 seconds.
+// it has, the removed member's app-scoped credentials in that app still
+// resolve; the drain runs at once after a browser removal and then from
+// the link sync loop, each app on its own.
func revokeMemberCredentialsTx(tx *sql.Tx, accountID int64, address string, now time.Time, gated bool) (memberRevocation, error) {
var out memberRevocation
appFree := ""
@@ -198,13 +225,16 @@ func (s *Store) claimRawRevokeMemberAppCredentials(accountID int64, app, address
}
// migrateMemberRevocations creates the follow-up queue. Additive and
-// idempotent, called from Store.migrate.
+// idempotent, called from Store.migrate. attempts and next_at are a
+// failing row's backoff (Unix seconds; 0 is due now).
func (s *Store) migrateMemberRevocations() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS member_revocations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER NOT NULL,
address TEXT NOT NULL,
- created_at INTEGER NOT NULL
+ created_at INTEGER NOT NULL,
+ attempts INTEGER NOT NULL DEFAULT 0,
+ next_at INTEGER NOT NULL DEFAULT 0
);`)
return err
}
@@ -215,12 +245,14 @@ type memberRevocationRow struct {
AccountID int64
Address string
CreatedAt int64 // Unix seconds: the removal's time
+ Attempts int
+ NextAt int64 // Unix seconds: not retried before
}
// pendingMemberRevocations lists the queue, oldest first; accountID 0 is
// every account.
func (s *Store) pendingMemberRevocations(accountID int64) ([]memberRevocationRow, error) {
- rows, err := s.db.Query(`SELECT id, account_id, address, created_at FROM member_revocations
+ rows, err := s.db.Query(`SELECT id, account_id, address, created_at, attempts, next_at FROM member_revocations
WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID)
if err != nil {
return nil, err
@@ -229,7 +261,7 @@ WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID)
var out []memberRevocationRow
for rows.Next() {
var r memberRevocationRow
- if err := rows.Scan(&r.ID, &r.AccountID, &r.Address, &r.CreatedAt); err != nil {
+ if err := rows.Scan(&r.ID, &r.AccountID, &r.Address, &r.CreatedAt, &r.Attempts, &r.NextAt); err != nil {
return nil, err
}
out = append(out, r)
@@ -237,26 +269,53 @@ WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID)
return out, rows.Err()
}
+// deferMemberRevocation records a failed attempt on a row: the next is
+// not before linkSyncInterval doubled per earlier failure, at most
+// memberRevocationMaxBackoff.
+func (s *Store) deferMemberRevocation(row memberRevocationRow, now time.Time) error {
+ wait := memberRevocationMaxBackoff
+ if row.Attempts < 8 {
+ wait = min(linkSyncInterval<<row.Attempts, memberRevocationMaxBackoff)
+ }
+ _, err := s.db.Exec(`UPDATE member_revocations SET attempts = attempts + 1, next_at = ? WHERE id = ?`,
+ now.Add(wait).Unix(), row.ID)
+ return err
+}
+
// finishMemberRevocation deletes a drained row.
func (s *Store) finishMemberRevocation(id int64) error {
_, err := s.db.Exec(`DELETE FROM member_revocations WHERE id = ?`, id)
return err
}
-// drainMemberRevocations finishes queued removals: it republishes every
-// door-key record of the row's account (the revoke bumped the changed
-// apps' revisions; an unchanged record re-signs harmlessly) and deletes the
-// row once all of them landed. accountID 0 drains every account. A row
-// that fails stays for the next pass; the errors are returned joined.
+// drainMemberRevocations finishes queued removals: each app's revoke
+// under its own permit, then every door-key record of the row's account
+// republished (the revoke bumped the changed apps' revisions; an unchanged
+// record re-signs harmlessly). The row goes once all of it landed.
+// accountID 0 drains every account. Every app is attempted whatever another
+// app did; a row with any failure stays, retried after its backoff. The
+// errors are returned joined, each overdue one marked so.
func (s *Server) drainMemberRevocations(ctx context.Context, accountID int64) error {
rows, err := s.store.pendingMemberRevocations(accountID)
if err != nil {
return fmt.Errorf("member revocations: %w", err)
}
+ now := time.Now()
var errs []error
for _, row := range rows {
+ if row.NextAt > now.Unix() {
+ continue
+ }
if err := s.finishOneMemberRevocation(ctx, row); err != nil {
- errs = append(errs, fmt.Errorf("member revocation %d (account %d): %w", row.ID, row.AccountID, err))
+ err = fmt.Errorf("member revocation %d (account %d): %w", row.ID, row.AccountID, err)
+ if age := now.Sub(time.Unix(row.CreatedAt, 0)); age >= memberRevocationOverdue {
+ err = fmt.Errorf("%w (overdue: queued %s ago)", err, age.Round(time.Second))
+ log.Printf("console: OVERDUE %v", err)
+ }
+ errs = append(errs, err)
+ if derr := s.store.deferMemberRevocation(row, now); derr != nil {
+ errs = append(errs, derr)
+ }
continue
}
if err := s.store.finishMemberRevocation(row.ID); err != nil {
@@ -266,8 +325,12 @@ func (s *Server) drainMemberRevocations(ctx context.Context, accountID int64) er
return errors.Join(errs...)
}
-// finishOneMemberRevocation is one row's work. An account that is gone
-// has nothing left to publish.
+// finishOneMemberRevocation is one row's work: the revokes the removal
+// could not make in its own transaction (with the host claim on), each
+// under its app's permit, and the account's door-key publication. Each app
+// and each publish is attempted even when another failed, so a healthy
+// app's revoke lands and is published while a blocked app keeps the row.
+// An account that is gone has nothing left to do.
func (s *Server) finishOneMemberRevocation(ctx context.Context, row memberRevocationRow) error {
acct, err := s.store.AccountByID(row.AccountID)
if err != nil {
@@ -276,41 +339,41 @@ func (s *Server) finishOneMemberRevocation(ctx context.Context, row memberRevoca
if acct == nil {
return nil
}
- // With the host claim on, the removal could not revoke app-scoped rows
- // in its own transaction; each app's go here, under that app's permit.
// Only rows minted no later than the removal: a member added back
// since keeps what they minted after rejoining.
apps, err := memberCredentialApps(s.store.db, row.AccountID, row.Address, row.CreatedAt)
if err != nil {
return err
}
+ var errs []error
for _, app := range apps {
err := s.store.withAutoAppSQL(ctx, row.AccountID, app, "sql.RevokeMemberCredentials", func(v *Store) error {
return v.claimRawRevokeMemberAppCredentials(row.AccountID, app, row.Address, row.CreatedAt, time.Now())
})
if err != nil {
- return fmt.Errorf("%s: %w", app, err)
+ errs = append(errs, fmt.Errorf("app %s: revoke: %w", app, err))
}
}
- if len(s.cfg.Key) != ed25519.PrivateKeySize || s.cfg.Releases == nil {
- return nil // nothing to sign with: this console publishes no record
- }
keyApps, err := s.store.DoorKeyApps()
if err != nil {
- return err
+ return errors.Join(append(errs, err)...)
}
for _, a := range keyApps {
if a.AccountID != row.AccountID {
continue
}
+ if len(s.cfg.Key) != ed25519.PrivateKeySize || s.cfg.Releases == nil {
+ errs = append(errs, errDoorKeyPublishUnavailable)
+ break
+ }
pctx, cancel := context.WithTimeout(ctx, doorKeyPublishBudget)
err := s.publishDoorKeys(pctx, acct, a.App)
cancel()
if err != nil {
- return err
+ errs = append(errs, fmt.Errorf("app %s: publish: %w", a.App, err))
}
}
- return nil
+ return errors.Join(errs...)
}
// scanStrings reads one string column to the end and closes rows.
diff --git a/internal/console/memberrevoke_claim_test.go b/internal/console/memberrevoke_claim_test.go
index a3e79e96f..1977da6f3 100644
--- a/internal/console/memberrevoke_claim_test.go
+++ b/internal/console/memberrevoke_claim_test.go
@@ -1,10 +1,14 @@
package console
import (
+ "context"
"encoding/json"
"net/http"
+ "strings"
"testing"
"time"
+
+ "github.com/carlosframework/platform/internal/release"
)
// TestClaimsModeRemovalRevokesAppCredentials (security hunt H4-M1, review
@@ -42,3 +46,76 @@ func TestClaimsModeRemovalRevokesAppCredentials(t *testing.T) {
t.Errorf("live door keys after removal = %+v, %v; want none", keys, err)
}
}
+
+// TestBlockedAppDoesNotHoldBackOtherAppsRevocation (review round 2, Astra
+// P1-4 and new finding 1, Fable N2): with the host claim on, a removal
+// made by the box-side member tool queues its app revocations for the
+// link sync loop. One app under a stuck operation does not hold back
+// another: the healthy app's credentials are revoked and its door-key
+// record is republished on the same pass, the row stays (backed off) for
+// the stuck app, whose credentials go on the first pass after its
+// operation clears.
+func TestBlockedAppDoesNotHoldBackOtherAppsRevocation(t *testing.T) {
+ cx := newClaimCase(t)
+ const kim = "kim@keymail.dev"
+ if err := cx.st.AddMember(OpsAccountID, kim, RoleMember, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ bearer := mintUserToken(t, cx.st, kim, time.Now())
+ instance := map[string]string{}
+ for _, app := range []string{"kass", "titogo"} {
+ status, _, out := rawReq(t, http.MethodPost, cx.ts.URL+"/api/cli/apps/bab/"+app+"/instance-token", bearer, `{}`)
+ if status != http.StatusCreated {
+ t.Fatalf("mint %s instance token = %d %s", app, status, out)
+ }
+ var minted struct{ Token string }
+ if err := json.Unmarshal([]byte(out), &minted); err != nil || minted.Token == "" {
+ t.Fatalf("%s instance token answer %s: %v", app, out, err)
+ }
+ instance[app] = minted.Token
+ }
+ key := mintDoorKey(t, cx.consoleFixture, bearer)
+ if !readDoorRecord(t, cx.consoleFixture).Admits(key) {
+ t.Fatal("test bug: the minted key is not published")
+ }
+
+ // kass sorts first and is held by an operation this console does not own.
+ restore := cx.foreign(t, claimAppRef{Account: release.OpsAccount, App: "kass"})
+ if err := cx.st.MembershipTransition(cx.st.RemoveMemberWrite(OpsAccountID, kim)); err != nil {
+ t.Fatal(err)
+ }
+ err := cx.srv.linkSyncPass(context.Background())
+ if err == nil || !strings.Contains(err.Error(), "kass") || strings.Contains(err.Error(), "titogo") {
+ t.Fatalf("first pass = %v; want only kass's row failing", err)
+ }
+ if _, ok := cx.st.CLIPrincipal(instance["titogo"], time.Now()); ok {
+ t.Error("titogo's instance token still resolves while kass is blocked")
+ }
+ if keys, _, err := cx.st.DoorKeysLive(OpsAccountID, "titogo"); err != nil || len(keys) != 0 {
+ t.Errorf("titogo's live door keys = %+v, %v; want none", keys, err)
+ }
+ if readDoorRecord(t, cx.consoleFixture).Admits(key) {
+ t.Error("titogo's door-key record still admits the removed member's key while kass is blocked")
+ }
+ if _, ok := cx.st.CLIPrincipal(instance["kass"], time.Now()); !ok {
+ t.Fatal("test bug: kass's token was revoked through a stuck operation")
+ }
+ if rows, err := cx.st.pendingMemberRevocations(0); err != nil || len(rows) != 1 || rows[0].NextAt <= time.Now().Unix() {
+ t.Fatalf("queue after the first pass = %+v, %v; want the row kept, backed off", rows, err)
+ }
+
+ restore()
+ // As if the row's backoff had elapsed.
+ if _, err := cx.st.db.Exec(`UPDATE member_revocations SET next_at = 0`); err != nil {
+ t.Fatal(err)
+ }
+ if err := cx.srv.linkSyncPass(context.Background()); err != nil {
+ t.Fatalf("pass after kass's operation cleared: %v", err)
+ }
+ if _, ok := cx.st.CLIPrincipal(instance["kass"], time.Now()); ok {
+ t.Error("kass's instance token still resolves after its operation cleared")
+ }
+ if rows, err := cx.st.pendingMemberRevocations(0); err != nil || len(rows) != 0 {
+ t.Errorf("queue after the second pass = %+v, %v; want empty", rows, err)
+ }
+}
diff --git a/internal/console/memberrevoke_test.go b/internal/console/memberrevoke_test.go
index b0f562201..bc8ea7216 100644
--- a/internal/console/memberrevoke_test.go
+++ b/internal/console/memberrevoke_test.go
@@ -365,3 +365,47 @@ func TestServiceMintInFlightDuringRemovalLandsNothing(t *testing.T) {
})
}
}
+
+// TestRemovalPublicationWaitsForASigningKey (review round 2, Astra new
+// finding 4): a console with no signing key cannot republish the door-key
+// record, so the queued row stays and the pass says so; once the key is
+// back, the first pass after the row's backoff publishes and clears it. A
+// row queued past memberRevocationOverdue is reported as overdue.
+func TestRemovalPublicationWaitsForASigningKey(t *testing.T) {
+ fx, member := instanceFixture(t, "code-revoke-nokey")
+ key := mintDoorKey(t, fx, member)
+ if err := fx.st.MembershipTransition(fx.st.RemoveMemberWrite(OpsAccountID, "alice@keymail.dev")); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := fx.st.db.Exec(`UPDATE member_revocations SET created_at = ?`, time.Now().Add(-time.Hour).Unix()); err != nil {
+ t.Fatal(err)
+ }
+ signing := fx.srv.cfg.Key
+ fx.srv.cfg.Key = nil
+ err := fx.srv.linkSyncPass(context.Background())
+ if err == nil || !strings.Contains(err.Error(), "overdue") {
+ t.Fatalf("pass with no signing key = %v; want the row reported, overdue", err)
+ }
+ if rows, err := fx.st.pendingMemberRevocations(0); err != nil || len(rows) != 1 {
+ t.Fatalf("queue after a pass that could not publish = %+v, %v; want the row kept", rows, err)
+ }
+ fx.srv.cfg.Key = signing
+ if err := fx.srv.linkSyncPass(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ if !readDoorRecord(t, fx).Admits(key) {
+ t.Fatal("a pass inside the row's backoff published; want it to wait")
+ }
+ if _, err := fx.st.db.Exec(`UPDATE member_revocations SET next_at = 0`); err != nil { // the backoff elapses
+ t.Fatal(err)
+ }
+ if err := fx.srv.linkSyncPass(context.Background()); err != nil {
+ t.Fatal(err)
+ }
+ if readDoorRecord(t, fx).Admits(key) {
+ t.Error("the record still admits the removed member's key once the key is back")
+ }
+ if rows, err := fx.st.pendingMemberRevocations(0); err != nil || len(rows) != 0 {
+ t.Errorf("queue after publishing = %+v, %v; want empty", rows, err)
+ }
+}