feat: backfill and verify OAuth values moving to organization keys

BF
1d ago
+2237-119

AIM-1006

Summary

  • Hourly backfill (server/internal/background/organization_encryption_backfill.go): one schedule and one activity, registered on the shared queue only. Each run first resumes clears that never finished and sweeps finalized organizations for straggling platform-key copies (SQL only, no KMS), then backfills and verifies the due dual_write organizations for oauth-client-secrets and oauth-session-tokens, least recently attempted first. A problem in one organization doesn't stop the others; the activity fails (and retries) only when something failed and nothing succeeded. It stops starting new work near its time limit or when cancelled, and heartbeats between items.
  • Due organizations (orgencryption.Migrator): values still wait for an organization-key copy; or there's no clean verification since the last backfill started and that attempt is at least 6 hours old (so a failing verification isn't repeated every hour); or a run started but never recorded its end and its claim expired.
  • On demand: background.StartOrganizationEncryptionBackfill starts one organization's run with a queue-scoped workflow ID and USE_EXISTING, for the admin endpoint in AIM-1007. A run that cannot start fails without retry with OrganizationEncryptionBackfillRunningErrorType or OrganizationEncryptionBackfillRejectedErrorType.
  • One run at a time (orgencryption.Runner): a run claims the transition row with a 10 minute lease, only while the use case is still in dual_write at the decided generation, and holds the claim through verification. It records its end (only if it still holds the claim) even after its own 5 minute deadline passes. A failed verification is recorded as the run's error. A refused claim returns ErrBackfillRunning or ErrFenced.
  • Finalize timing: checkFinalizeTiming now requires a clean verification since the last backfill started rather than completed, because verification now happens inside the run.
  • Straggler sweep: new Transitions.SweepPlatformCopies clears and counts platform-key copies without changing transition state. Each column with stragglers gets an error log and the gram.orgencryption.transition.stragglers counter, by kind (platform_only or beside_org_copy). It covers organizations that reached org_kms within the last 7 days. Stalled clears are resumed at most two per run, an hour or more after finalize, in random order.
  • Empty ciphertext: the session token waiting, sealed and count queries treat a '' platform-key copy as no value (rows written before remote_sessions.access_token_encrypted was nullable). Clearing still removes it.
  • Wired into the worker and single-process dev mode when organization encryption is configured.

Motivation

Organizations enrolled in dual_write still hold OAuth values written before enrollment, with only a platform-key copy. Those values need organization-key copies, proven readable, before an organization can be finalized and its platform-key copies destroyed. After finalize, a platform-key copy written by a writer that decided before the move would never be cleared, so a sweep catches and reports any. The run loops inside one activity with SQL work lists rather than starting a workflow per organization, and the claim on the transition row keeps the hourly and on-demand runs from working on the same organization at once.

Temporal actions/month ≈ 1,440 per namespace (one schedule start and one activity per hour, plus at most one retry per failed run) plus throttled heartbeats (a few per run, at most ~25), scales with fixed.


Summary by cubic

Adds an hourly background job that backfills and verifies OAuth client secrets and session tokens for organizations still holding only platform-key copies from before they enrolled in dual_write, and starts one organization's run on demand for the admin endpoint.

The job lists due organizations in SQL and processes them inside one activity rather than starting a workflow per organization, claiming each transition row for the duration of its run so hourly and on-demand runs never work the same organization at once. It resumes platform-key clears that never finished and sweeps recently finalized organizations for platform-key copies written after clearing, logging each column found and counting by kind.

  • Finalize now requires a clean verification since the last backfill started rather than completed, because verification happens inside the run.
  • A failed verification is recorded as the run's error instead of leaving the organization to retry hourly.
  • Session token queries treat an empty platform-key ciphertext as no value, so rows written before the column became nullable aren't counted as waiting, unreadable, or verified.
  • Adds a server patch changeset documenting the new background, on-demand, and straggler-sweep behavior.