generated from dxw/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5.x devise-two-factor upgrade phase 1
All non-Rails 7 steps already completed from https://github.com/devise-two-factor/devise-two-factor/blob/main/UPGRADING.md#phase-1-upgrading-devise-two-factor-as-part-of-rails-7-upgrade ## Pre-release: Prior to release of this, we need to generate `ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY`, `ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY`, and `ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT` for each environment, and make sure they are preloaded in AWS. These are required by Rails 7 for https://guides.rubyonrails.org/active_record_encryption.html which `devise_two_factor` 5.x uses to encrypt `otp_secret`s (4.x used the `attr_encrypted` gem). `bin/rails db:encryption:init` will generate all three; they are assigned on startup in `application.rb`. ## Post-release Run: `rails runner db/data/20250117151047_regenerate_otp_secrets.rb` on each environment in turn. `legacy_otp_secret` will cover the period that "new" otp_secrets are NULL. ## Phase 2 Cleanup release We can remove the old DB columns and `legacy_otp_secret` in a separate release: https://github.com/devise-two-factor/devise-two-factor/blob/main/UPGRADING.md#phase-2-clean-up In our case we won't need to copy otp_secret; because we're SMS-only we're able to do that in the Phase 1 release data migration. NOTES: - given that this is the first commit to `schema.rb` since the upgrade, some bits like `precision` go away with the Rails 7.0 upgrade
- Loading branch information
Showing
9 changed files
with
143 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Run me with `rails runner db/data/20250117151047_regenerate_otp_secrets.rb` | ||
|
||
# On migrating from devise-two-factor 4.x to 5.x, we need to regenerate all our OTP secrets. | ||
# 4.x used three columns, encrypted_otp_secret, encrypted_otp_secret_iv, and encrypted_otp_secret_salt. | ||
# 5.x uses a single JSON-containing VARCHAR containing multiple values, for example: | ||
# {"p":"yrnrIYTCt+/YGEg2F8QCfieMHJ02zjEi","h":{"iv":"fmmU6Jx5f+XEg9u0","at":"LozZUSLUGwo6US0sW39Vmw==","e":"QVNDSUktOEJJVA=="}} | ||
# | ||
# Once `otp_secret` has been populated, we can do a Phase 2 release to remove the old encrypted_otp_secret, | ||
# encrypted_otp_secret_iv, and encrypted_otp_secret_salt columns. | ||
User.transaction do | ||
User.find_each do |user| | ||
user.update!(otp_secret: User.generate_otp_secret) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddOtpSecretToUser < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :users, :otp_secret, :string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters