Add SsoProvider to security:backfill_encryption task

Closed
IT
17d ago
+91-0

Summary

  • SsoProvider#client_secret is encrypted (app/models/sso_provider.rb:9), but security:backfill_encryption never covered SsoProvider — it backfills users, invitations, sessions, mobile devices, and provider items/accounts, but the model was missing from the list.
  • Result: existing rows kept plaintext client_secret values. Any subsequent read (validation runs on every update!, including Admin::SsoProvidersController#toggle) tried to decrypt the plaintext as an encrypted JSON envelope and raised ActiveRecord::Encryption::Errors::Decryption, causing production 500s.
  • Fix: add results[:sso_providers] = backfill_model(SsoProvider, %i[client_secret], batch_size, dry_run) to the task. client_secret is a plain string column, so the existing safe_read_field raw-value fallback (no jsonb parsing needed) already handles it.

Fixes the crash reported in Sentry issue 7733832060.

Test plan

  • Added a regression test (test/lib/tasks/security_backfill_test.rb) that seeds a plaintext client_secret, runs the task, and asserts the value round-trips through decryption and is stored as ciphertext.
  • ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY=test ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY=test ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT=test bin/rails test test/lib/tasks/security_backfill_test.rb — 3 runs, 0 failures.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • The security backfill now encrypts plaintext SSO provider client secrets while keeping them readable.
    • Secrets that are already encrypted remain unchanged on subsequent backfill runs.
    • Plaintext secrets are encrypted even when the system allows unencrypted data to be read.
    • Existing batch processing and dry-run behavior remain supported.
  • Tests

    • Added coverage for encrypting plaintext secrets, preserving existing ciphertext, and handling configurations that allow unencrypted data.