Skip to content

Commit b641e21

Browse files
jkogarapixeltrix
authored andcommitted
Make secrets available in the configuration context
Application developers may need to access secrets from within config files so use before_configuration instead of before_initialize.
1 parent 867e648 commit b641e21

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

lib/rails/secrets/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def secrets=(secrets)
2020
end
2121

2222
class Railtie < ::Rails::Railtie
23-
config.before_initialize do |app|
23+
config.before_configuration do |app|
2424
app.paths.add "config/secrets", with: "config/secrets.yml"
2525
app.extend(InstanceMethods)
2626
end

test/dummy/config/environments/development.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
# This option may cause significant delays in view rendering with a large
2727
# number of complex assets.
2828
config.assets.debug = true
29+
30+
# Test whether secrets can be read in a configuration context
31+
config.secret_environment_config = Rails.application.secrets.secret_key_base
2932
end

test/dummy/config/environments/production.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@
7777

7878
# Use default logging formatter so that PID and timestamp are not suppressed.
7979
config.log_formatter = ::Logger::Formatter.new
80+
81+
# Test whether secrets can be read in a configuration context
82+
config.secret_environment_config = Rails.application.secrets.secret_key_base
8083
end

test/dummy/config/environments/test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@
3333

3434
# Print deprecation notices to the stderr.
3535
config.active_support.deprecation = :stderr
36+
37+
# Test whether secrets can be read in a configuration context
38+
config.secret_environment_config = Rails.application.secrets.secret_key_base
3639
end

test/rails_secrets_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class RailsSecretsTest < ActiveSupport::TestCase
88
assert_equal "9f4a992", app.secrets.api_token
99
end
1010

11+
test "a config value set in the configuration stage is set to the correct value" do
12+
assert_equal app.secrets.secret_key_base, app.config.secret_environment_config
13+
end
14+
1115
test "config.secret_key_base is copied from secrets.secret_key_base" do
1216
assert_equal app.secrets.secret_key_base, app.config.secret_key_base
1317
end

0 commit comments

Comments
 (0)