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

Store secret_key_base in Rails.config for local environments. #48470

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
* Store `secret_key_base` in `Rails.config` for local environments.

Rails `secrets` have been deprecated in favor of `credentials`.
For the local environments the `secret_key_base` is now stored in
`Rails.config.secret_key_base` instead of the soft deprecated
`Rails.application.secrets.secret_key_base`.

*Petrik de Heus*

* Enable force_ssl=true in production by default: Force all access to the app over SSL,
use Strict-Transport-Security, and use secure cookies

Expand Down
15 changes: 9 additions & 6 deletions railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def secrets
# the correct place to store it is in the encrypted credentials file.
def secret_key_base
if Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"]
secrets.secret_key_base ||= generate_development_secret
config.secret_key_base ||= generate_development_secret
else
validate_secret_key_base(
ENV["SECRET_KEY_BASE"] || credentials.secret_key_base || secrets.secret_key_base
Expand Down Expand Up @@ -643,19 +643,22 @@ def ensure_generator_templates_added

private
def generate_development_secret
if secrets.secret_key_base.nil?
if config.secret_key_base.nil?
key_file = Rails.root.join("tmp/development_secret.txt")

if !File.exist?(key_file)
if File.exist?(key_file)
config.secret_key_base = File.binread(key_file)
elsif secrets.secret_key_base
config.secret_key_base = secrets.secret_key_base
else
random_key = SecureRandom.hex(64)
FileUtils.mkdir_p(key_file.dirname)
File.binwrite(key_file, random_key)
config.secret_key_base = File.binread(key_file)
end

secrets.secret_key_base = File.binread(key_file)
end

secrets.secret_key_base
config.secret_key_base
end

def build_request(env)
Expand Down
14 changes: 5 additions & 9 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ def index

app "development"

assert_not_nil app.secrets.secret_key_base
assert_not_nil app.secret_key_base
assert File.exist?(app_path("tmp/development_secret.txt"))
end

Expand Down Expand Up @@ -834,14 +834,14 @@ def index
assert_equal "3b7cd727ee24e8444053437c36cc66c3", app.secret_key_base
end

test "secret_key_base is copied from config to secrets when not set" do
test "secret_key_base is copied from config.secret_key_base when set" do
remove_file "config/secrets.yml"
app_file "config/initializers/secret_token.rb", <<-RUBY
Rails.application.config.secret_key_base = "3b7cd727ee24e8444053437c36cc66c3"
RUBY

app "development"
assert_equal "3b7cd727ee24e8444053437c36cc66c3", app.secrets.secret_key_base
assert_equal "3b7cd727ee24e8444053437c36cc66c3", app.secret_key_base
end

test "custom secrets saved in config/secrets.yml are loaded in app secrets" do
Expand Down Expand Up @@ -892,18 +892,14 @@ def index
assert_nil app.secrets.not_defined
end

test "config.secret_key_base over-writes a blank secrets.secret_key_base" do
test "config.secret_key_base over-writes a blank app.secret_key_base" do
app_file "config/initializers/secret_token.rb", <<-RUBY
Rails.application.config.secret_key_base = "iaminallyoursecretkeybase"
RUBY
app_file "config/secrets.yml", <<-YAML
development:
secret_key_base:
YAML

app "development"

assert_equal "iaminallyoursecretkeybase", app.secrets.secret_key_base
assert_equal "iaminallyoursecretkeybase", app.secret_key_base
end

test "that nested keys are symbolized the same as parents for hashes more than one level deep" do
Expand Down
2 changes: 1 addition & 1 deletion railties/test/isolation/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def self.name; "RailtiesTestApp"; end
@app.config.session_store :cookie_store, key: "_myapp_session"
@app.config.active_support.deprecation = :log
@app.config.log_level = :info
@app.secrets.secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"
@app.config.secret_key_base = "b3c631c314c0bbca50c1b2843150fe33"

yield @app if block_given?
@app.initialize!
Expand Down
2 changes: 1 addition & 1 deletion railties/test/path_generation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def initialize
super
app = self
@routes = TestSet.new ->(c) { app.controller = c }
secrets.secret_key_base = "foo"
config.secret_key_base = "foo"
end
def app; routes; end
}
Expand Down