| 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. |