-
Notifications
You must be signed in to change notification settings - Fork 22k
Ensure ActiveRecord::Encryption.config is always ready before access #52669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure ActiveRecord::Encryption.config is always ready before access #52669
Conversation
Thanks for this @maximerety, I can confirm this fixes the issue in rspec/rspec-rails#2779. 🙌🏻 |
1499846
to
ee50331
Compare
activerecord/lib/active_record/encryption/extended_deterministic_queries.rb
Outdated
Show resolved
Hide resolved
e118402
to
6c2d1f9
Compare
@rafaelfranca Thanks for reviewing, all comments addressed 🙏 |
6c2d1f9
to
4936e2f
Compare
Previously, ActiveRecord::Encryption configuration was deferred until ActiveRecord::Base was loaded. Therefore, accessing ActiveRecord::Encryption.config properties before ActiveRecord::Base was loaded would give incorrect results. ActiveRecord::Encryption now has its own loading hook so that its configuration is set as soon as needed. When ActiveRecord::Base is loaded, even lazily, it in turn triggers the loading of ActiveRecord::Encryption, thus preserving the original behavior of having its config ready before any use of ActiveRecord::Base.
4936e2f
to
6f7f624
Compare
Ensure ActiveRecord::Encryption.config is always ready before access
Ensure ActiveRecord::Encryption.config is always ready before access
Hi @maximerety @rafaelfranca - sorry to dig out this merged PR. I think it caused a regression in my app and I thought you might like to know in case there's a potential fix. Following the instructions in https://guides.rubyonrails.org/active_record_encryption.html#basic-usage I had set the credentials from the environment in an initializer: # config/initializers/active_record_encryption.rb
Rails.application.config.active_record.encryption.primary_key = ENV['ACTIVE_RECORD_ENCRYPTION_PRIMARY_KEY']
Rails.application.config.active_record.encryption.deterministic_key = ENV['ACTIVE_RECORD_ENCRYPTION_DETERMINISTIC_KEY']
Rails.application.config.active_record.encryption.key_derivation_salt = ENV['ACTIVE_RECORD_ENCRYPTION_KEY_DERIVATION_SALT'] My tests started failing on upgrade to 7.1.4.x because my credentials in Moving the config out of the initializer and into Would it be worth adding another check later to copy them across again if an initializer changes |
Motivation / Background
This Pull Request has been created in order to fix rspec/rspec-rails#2779.
Detail
Previously,
ActiveRecord::Encryption
configuration was deferred untilActiveRecord::Base
was loaded. Therefore, accessingActiveRecord::Encryption.config
properties beforeActiveRecord::Base
was loaded would give incorrect results.ActiveRecord::Encryption
now has its own loading hook so that its configuration is set as soon as needed.When
ActiveRecord::Base
is loaded, even lazily, it in turn triggers the loading ofActiveRecord::Encryption
, thus preserving the original behavior of having its config ready before any use ofActiveRecord::Base
.This fix complements one of my previous fixes in #50606.
Since both Rails 7.1.4 and 7.2.* versions are affected, I'd recommend backporting this fix to
7-1-stable
and7-2-stable
.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]