| index 841addc41..7b449fc94 100644 |
| --- a/internal/console/memberrevoke.go |
| +++ b/internal/console/memberrevoke.go |
| @@ -26,17 +26,30 @@ package console |
| // Each is refused at its check the moment the transaction commits: |
| // CLIPrincipal reads revoked_at on every request (no cache), and |
| // linkPrincipalLive reads the links row. Door keys and link channels also |
| -// live outside this database; the caller publishes them after the commit |
| -// (afterMemberRemoved), the same way their own revoke doors do, and the |
| -// re-sign pass (12 hours) and linkSyncLoop (10 seconds) catch anything |
| -// that publish misses. |
| +// live outside this database, and their publication is durable: |
| +// |
| +// - 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. |
| +// - 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. |
| // |
| // 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. |
| |
| import ( |
| "context" |
| + "crypto/ed25519" |
| "database/sql" |
| + "errors" |
| + "fmt" |
| "log" |
| "net/http" |
| "time" |
| @@ -97,8 +110,115 @@ WHERE account_id = ? AND app = ? AND created_by IN (?, '') AND revoked_at = 0`, |
| return out, err |
| } |
| } |
| - _, err = tx.Exec(`DELETE FROM invites WHERE account_id = ? AND invited_by = ? AND accepted_at = 0`, accountID, address) |
| - return out, err |
| + if _, err := tx.Exec(`DELETE FROM invites WHERE account_id = ? AND invited_by = ? AND accepted_at = 0`, accountID, address); err != nil { |
| + return out, err |
| + } |
| + if len(out.doorKeyApps) > 0 { |
| + if _, err := tx.Exec(`INSERT INTO member_revocations (account_id, address, created_at) VALUES (?, ?, ?)`, |
| + accountID, address, now.Unix()); err != nil { |
| + return out, err |
| + } |
| + } |
| + return out, nil |
| +} |
| + |
| +// migrateMemberRevocations creates the follow-up queue. Additive and |
| +// idempotent, called from Store.migrate. |
| +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 |
| +);`) |
| + return err |
| +} |
| + |
| +// memberRevocationRow is one queued follow-up. |
| +type memberRevocationRow struct { |
| + ID int64 |
| + AccountID int64 |
| + Address string |
| +} |
| + |
| +// 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 FROM member_revocations |
| +WHERE ? = 0 OR account_id = ? ORDER BY id`, accountID, accountID) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer rows.Close() |
| + var out []memberRevocationRow |
| + for rows.Next() { |
| + var r memberRevocationRow |
| + if err := rows.Scan(&r.ID, &r.AccountID, &r.Address); err != nil { |
| + return nil, err |
| + } |
| + out = append(out, r) |
| + } |
| + return out, rows.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. |
| +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) |
| + } |
| + var errs []error |
| + for _, row := range rows { |
| + if err := s.finishOneMemberRevocation(ctx, row); err != nil { |
| + errs = append(errs, fmt.Errorf("member revocation %d (account %d): %w", row.ID, row.AccountID, err)) |
| + continue |
| + } |
| + if err := s.store.finishMemberRevocation(row.ID); err != nil { |
| + errs = append(errs, err) |
| + } |
| + } |
| + return errors.Join(errs...) |
| +} |
| + |
| +// finishOneMemberRevocation is one row's work. An account that is gone |
| +// has nothing left to publish. |
| +func (s *Server) finishOneMemberRevocation(ctx context.Context, row memberRevocationRow) error { |
| + acct, err := s.store.AccountByID(row.AccountID) |
| + if err != nil { |
| + return err |
| + } |
| + if acct == nil { |
| + return nil |
| + } |
| + if len(s.cfg.Key) != ed25519.PrivateKeySize || s.cfg.Releases == nil { |
| + return nil // nothing to sign with: this console publishes no record |
| + } |
| + apps, err := s.store.DoorKeyApps() |
| + if err != nil { |
| + return err |
| + } |
| + for _, a := range apps { |
| + if a.AccountID != row.AccountID { |
| + continue |
| + } |
| + pctx, cancel := context.WithTimeout(ctx, doorKeyPublishBudget) |
| + err := s.publishDoorKeys(pctx, acct, a.App) |
| + cancel() |
| + if err != nil { |
| + return err |
| + } |
| + } |
| + return nil |
| } |
| |
| // scanStrings reads one string column to the end and closes rows. |
| @@ -115,15 +235,16 @@ func scanStrings(rows *sql.Rows) ([]string, error) { |
| return out, rows.Err() |
| } |
| |
| -// afterMemberRemoved publishes what a committed removal revoked outside the |
| -// database: each changed door-key record (publishDoorKeys, as the revoke |
| -// door does) and each link's channel (syncLinkChannel, as unlink does). |
| -// Best-effort: the database is already the truth, and the re-sign pass and |
| -// linkSyncLoop retry anything that fails here. |
| +// afterMemberRemoved publishes, at once, what a committed removal revoked |
| +// outside the database: the queued door-key records (drainMemberRevocations) |
| +// and each link's channel (syncLinkChannel, as unlink does). A failure here |
| +// is retried by the link sync loop: the queue row and revoke_synced stay. |
| func (s *Server) afterMemberRemoved(r *http.Request, acct *Account, rev memberRevocation) { |
| - for _, app := range rev.doorKeyApps { |
| - s.republishDoorKeys(r, acct, app) |
| + dctx, dcancel := context.WithTimeout(context.WithoutCancel(r.Context()), doorKeyPublishBudget) |
| + if err := s.drainMemberRevocations(dctx, acct.ID); err != nil { |
| + log.Printf("console: door keys after member removal: %v (the link sync loop retries)", err) |
| } |
| + dcancel() |
| for _, id := range rev.links { |
| ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), linkFirstSyncTimeout) |
| if err := s.syncLinkChannel(ctx, id); err != nil { |