-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Prioritize letting the application find the secret_key_base #5634
Conversation
@@ -7,14 +7,12 @@ def initialize(application) | |||
end | |||
|
|||
def find | |||
if @application.respond_to?(:credentials) && key_exists?(@application.credentials) | |||
@application.credentials.secret_key_base |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this because it is redundant. Both credentials
and application.secret_key_base
were added in Rails 5.2 and if the key is found in credentials it will be returned.
lib/devise/secret_key_finder.rb
Outdated
@@ -7,14 +7,12 @@ def initialize(application) | |||
end | |||
|
|||
def find | |||
if @application.respond_to?(:credentials) && key_exists?(@application.credentials) | |||
@application.credentials.secret_key_base | |||
if @application.respond_to?(:secret_key_base) && key_exists?(@application) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if @application.respond_to?(:secret_key_base) && key_exists?(@application) | |
if @application.respond_to?(:secret_key_base) |
I think if Rails version is recent enough to respond here, the method is guaranteed to return a value or raise.
Thanks for raising this issue. I've started seeing this deprecation warning too since upgrading to Rails 7.1. In my development environment, the When the For now in my own codebase I've added this patch (🙈) into my patched_version = '4.9.3'
unless Gem.loaded_specs['devise'].version == patched_version
raise "Patch for Devise::SecretKeyFinder has not been tested with the " \
"installed Devise version. Review whether it's still needed, and either " \
"remove it or increment the patched_version."
end
# Patches Devise to skip using deprecated Application#secrets method.
# Can remove once https://github.com/heartcombo/devise/pull/5634 is resolved.
Devise::SecretKeyFinder.class_eval do
def find
@application.secret_key_base
end
end |
lib/devise/secret_key_finder.rb
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi guys.
Thanks @albus522 for the good work. Now that #5600 has been merged, the minimum Rails version for main is 6.0. So I think we can simplify the whole find method to just:
def find
@application.secret_key_base
end
or just delete the whole file, given that the only purpose of this class is to find the secret_key_base
and this is not a problem anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomascco looks like your suggestion was applied so you can re-review 🙂
Starting in Rails 5.2 Rails.application.secret_key_base is available to find or create the secret key. By prioritizing letting Rails tell us what the secret key is, we can avoid the secrets deprecation warning generated in Rails 7.1. However, there is a potential for a breaking change here. Rails uses a different priority order for secret key lookup than this key finder, so it is possible for us to find a secret key base that is not what the app is using. Rails will use ENV['SECRET_KEY_BASE'] over anything else, so if someone has a different key set in credentials or secrets, we are currently choosing a different key.
All supported rails versions implement app.secret_key_base so this can now be simplified
4736459
to
e3addf2
Compare
This is now simplified to rely on |
@alexpls less hacky workaround is to update your devise initalizer to set the secret key instead of waiting for Devise to find it. This is what I am currently doing in the app that initiated this PR.
|
@albus522 with great respect for the work you did here, I think this PR should be closed as a duplicate of PR #5645. Would you be willing to close this PR so we can all focus on that one? Reasons:
PS: I have no connection to PR #5645 or its author. |
Starting in Rails 5.2
Rails.application.secret_key_base
is available to find or create the secret key. By prioritizing letting Rails tell us what the secret key is, we can avoid being the trigger for the secrets deprecation warning generated in Rails 7.1. Also when devise triggers the deprecation, the warning is really hard to trace:However, there is a potential for a breaking change here. Rails uses a different priority order for secret key lookup than this key finder, so it is possible for us to find a secret key base that is different from what the app is using. Rails will use
ENV['SECRET_KEY_BASE']
over anything else, so if someone has a different key set in credentials or secrets, we are currently choosing a different key than the application.