Skip to content
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

[Fix #50604] Restore compatibility of Active Record Encryption configs with eager loading mode #50606

Merged

Conversation

maximerety
Copy link
Contributor

@maximerety maximerety commented Jan 5, 2024

Motivation / Background

Fixes #50604

This Pull Request fixes a source of incompatibility between the loading order of Active Record Encryption configs and the loading of AR models in eager loading mode.

Detail

This Pull Request removes an occurrence of after_initialize introduced in #48530, which is not fully compatible with the eager loading mode.

Previous concerns were raised in a discussion from the same PR.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rafaelfranca
Copy link
Member

This broke a test where the config is set in an intializer. That is because the ActiveRecord::Encryption.configure is called outside of the on_load. We should move it there.

@maximerety maximerety force-pushed the active-record-encryption-eager-load branch from 7f39522 to 5b9504d Compare January 6, 2024 19:00
@rails-bot rails-bot bot added the railties label Jan 6, 2024
@maximerety maximerety force-pushed the active-record-encryption-eager-load branch from 5b9504d to dc7236d Compare January 6, 2024 19:01
Copy link
Contributor Author

@maximerety maximerety left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke a test where the config is set in an intializer. That is because the ActiveRecord::Encryption.configure is called outside of the on_load. We should move it there.

@rafaelfranca Thanks for the hint.

I had a hard time adding new tests to demonstrate the issue because, to be honest, there are a lot of possible situations that could still not work as expected.

For example, people may reference ActiveRecord::Base in initializers (this is what is done in the new tests here, and others) and if you do this before setting the desired Rails.application.config.active_record.encryption properties, they will be ignored when configuring ActiveRecord::Encryption. That's why I'm regrouping all configs in a single initializer (config/initializers/active_record.rb) in the tests and declare them in the proper order.

This is the exact problem discussed initially: https://github.com/rails/rails/pull/48530/files#r1237624233.

But this is probably the price we have to pay to ensure that configurations are ready early enough in eager loading mode.

Finally, I'd like to propose a backport of this fix to 7-1-stable as I encountered it while migrating a project from 7.0 to 7.1. Would that be acceptable to you?

Copy link
Contributor

@jorgemanrubia jorgemanrubia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix 👍 , thanks @maximerety.

I think this case cause trouble when referencing models from initializers, but @rafaelfranca made a good point here about that #48530 (comment). Good to merge from my side 👍 .

Also, thanks for the fantastic stream of improvements on Active Record Encryption @maximerety 🙏.

@maximerety
Copy link
Contributor Author

maximerety commented Jan 9, 2024

Thanks @jorgemanrubia for your kind words 🙇

@rafaelfranca Let me know if you need anything else from me for this PR to be merged and backported to 7-1-stable 🙏

@maximerety maximerety force-pushed the active-record-encryption-eager-load branch from dc7236d to 4026afd Compare January 12, 2024 20:47
@maximerety
Copy link
Contributor Author

@rafaelfranca I've rebased and squashed the 2 commits (which I forgot to do initially).

@maximerety maximerety force-pushed the active-record-encryption-eager-load branch 2 times, most recently from 057f771 to 2464c52 Compare January 17, 2024 12:51
@maximerety
Copy link
Contributor Author

@jorgemanrubia Too bad I did not manage to have this merged in Rails 7.1.3, I think I'll have to be little more patient 😉 Do you know if I can do something else for this patch to land on main / 7-1-stable so that it'll be ready for a next patch version when that times come? Thanks.

@trevorturk
Copy link
Contributor

@maximerety you might consider pinging the Rails Discord to see if you can get a review?

…ding mode

Configure ActiveRecord::Encryption (ARE) on ActiveRecord::Base (AR)
loading, so that ARE configs are ready before AR models start using
`encrypts` to declare encrypted attributes.

This means that you can add ARE configurations in initializers, as long
as you don't trigger the loading of ActiveRecord::Base or your AR models
in prior initializers.
@maximerety maximerety force-pushed the active-record-encryption-eager-load branch from 94cec12 to d997c55 Compare February 12, 2024 16:58
@jorgemanrubia
Copy link
Contributor

@eileencodes could we merge this one please?

@eileencodes eileencodes merged commit 6314290 into rails:main Feb 12, 2024
4 checks passed
@eileencodes
Copy link
Member

Can someone do the backport PR? It doesn't cherry-pick cleanly and I don't have the bandwidth to fight conflicts right now. Thanks!

@maximerety maximerety deleted the active-record-encryption-eager-load branch February 13, 2024 08:35
maximerety pushed a commit to maximerety/rails that referenced this pull request Feb 13, 2024
…on-eager-load

[Fix rails#50604] Restore compatibility of Active Record Encryption configs with eager loading mode
@maximerety
Copy link
Contributor Author

Thanks @eileencodes, here is the backport PR to 7-1-stable: #51061.

rafaelfranca added a commit that referenced this pull request Feb 13, 2024
…yption-eager-load-7-1

Backport #50606 to 7-1-stable
@tenpaiyomi
Copy link

tenpaiyomi commented Jun 10, 2024

@jorgemanrubia @eileencodes I don't suppose either of you have an idea of when this will actually make it into a release, do you? I saw that the backport to 7-1-stable, #51061, got merged in about 4 months ago, and there seems to have been 3 separate 7.1.x releases since then but none of them actually include this fix which as left me stuck having to pin my rails gem version to the 7-1-stable branch instead of updating to the latest gem release.

So far it seems like it is on track to release with 7.2.x (it exists in the v7.2.0.beta2 tag) but my thought with the backport to the 7-1-stable branch PR was that it would be included in 7.1.x releases going forward as well.

@zzak
Copy link
Member

zzak commented Jun 10, 2024

@tenpaiyomi The last bugfix release was 7.1.3, before the backport was merged. So whenever 7.1.4 is out, that should include this. Thanks for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Active Record Encryption configs not ready in eager loading mode
7 participants