Clarify Docker encryption migration and recovery safeguards

Closed
KI
9d ago
+3438-470

Existing Docker installations can lose access to encrypted data if they follow new-install setup commands, mistake unused keys in .env for active keys, or restart after an incomplete key change. This documentation-only change adds safeguards to the upstream encryption procedures.

Integrated main at dbeebf14c17a44d6ad9f0cfc6c2409fab07f8b11, including #4936, #3221, and #3190. Retain the upstream backfill manifest, ciphertext refusal, failure exit status, rotation tasks, and completion-gated plaintext fallback. Current Compose files require a user-supplied SECRET_KEY_BASE and already forward optional encryption keys.

Changes remain limited to .env.example and docs/hosting/docker.md:

  • Route existing installations away from configuration downloads and replacement-key generation.
  • Distinguish environment keys, Rails credentials, and derived keys; preserve the effective keys, credential files, and matching backup before conversion.
  • Stop writers before backing up, keep the same deployment and image, and preserve both key sets when data already uses old and new keys.
  • Separate Active Record recovery from settings recovery. A correct old secret can still produce old_secret_key_base_did_not_match for plaintext or differently encrypted settings; ok: true can also coexist with unresolved settings.
  • Provide a web-only manual recovery path after Active Record data is readable. Save replacements through the settings UI, verify the final dry run contains only current/not_stored entries and no undecryptable settings, then start workers.
  • Explain the current backfill-completion version and restart requirement instead of treating successful sign-in or a false ENV toggle as proof of migration.

Related to #3189 and #4648; complements #4936, #3221, and #3190.

Validation

Latest revision: 1ceaf6b8de1c9ae4c545b0953069cb2139e7d8e9. Validated in Docker volumes and isolated test databases:

  • Full Rails suite: 16,219 runs, 103,949 assertions, 0 failures, 0 errors, 9 skips.
  • Focused encryption, initializer, readiness, backfill, settings rotation, and at-rest tests with explicit test keys: 102 runs, 1,546 assertions, 0 failures, 0 errors, 0 skips.
  • Actual settings task and setters: wrong-secret refusal without writes; correct-old-secret failures for plaintext and another-key values; mixed conversion returning success with unresolved settings; repeat-run failure after partial coverage; replacement values encrypted with the current secret; clearing an unused setting and verifying the final clean preview. These checks ran in a rolled-back transaction.
  • Compose dry-run plans: up -d web selects only web/db/redis; up -d worker selects only worker/db/redis. Used local test images and verified no planned containers were created.
  • RuboCop, ERB lint, Biome, and Brakeman passed without source corrections. Brakeman used the repository's existing ignore file.
  • Documentation: 44 shell blocks, local links/anchors, and manual recovery/verification/startup order checked. One unchanged upstream block containing a <version>/<backup> placeholder was excluded from literal shell parsing.
  • Source comparison: 8,158 tracked files matched the validation snapshot; the local devcontainer port override remains excluded.

System tests and browser UI interaction were not run for this documentation-only diff. No production backfill, key rotation, or service startup was performed.


Summary by cubic

Corrects documentation that claimed self-hosted Active Record Encryption keys are auto-generated from SECRET_KEY_BASE, and documents the post-#3189 key derivation path. Sensitive fields are now encrypted at rest using keys derived from SECRET_KEY_BASE when no explicit keys or Rails credentials are configured; explicit keys remain optional for new installs, and SECRET_KEY_BASE alone only enables encryption on images containing the gating fix. Relates to #4648 and #3189.

  • Updates .env.example comments to require a unique, backed-up SECRET_KEY_BASE, explain derived keys, and note that explicit keys must be set as a complete set.
  • Adds Docker guide steps for generating explicit keys, forwarding them to web and worker via x-rails-env, and finishing new-install configuration before startup.
  • Routes existing instances to a staged backfill procedure before any setup downloads, requiring reuse of existing keys, preserved configuration, and stopped writes before backup.
  • Documents backfill dry-run/write modes, per-model failure checks, and encrypted fields outside the task's scope, and clarifies that the plaintext-compatibility flag is not proof of encryption.

Summary by CodeRabbit

  • Documentation
    • Clarified when self-hosted instances derive encryption keys from SECRET_KEY_BASE, and how explicit keys and Rails credentials take precedence. Partial environment-key sets prevent startup.
    • Added guidance for new and existing installations, forwarding keys through Compose, preserving effective keys and external credentials, and backing up before conversions.
    • Expanded conversion and key-rotation steps with write pauses, dry runs, validation, reports, failure handling, and rollback guidance.
    • Documented recovery for encrypted settings, including manual re-entry when values cannot be decrypted, and noted that explicit encryption keys do not protect sessions or Setting values.