Skip to content

Commit

Permalink
Added a way to group common secrets at one place in secrets.yml
Browse files Browse the repository at this point in the history
 - Earlier, if there were common secrets under all environments then
   they had to be duplicated under each environment in
   `config/secrets.yml`.
 - This commit adds a way to specify them under `common` key such that
   they will be loaded in all environments.
 - Environment specific secrets will override the common secrets if they
   are present under both sections.
  • Loading branch information
prathamesh-sonpatki committed Apr 25, 2015
1 parent 5d6b543 commit 45eba60
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Add a way to specify common secrets in `config/secrets.yml` which
will be loaded in all environments. Environment specific secrets
will override common secrets.

*Prathamesh Sonpatki*

* Remove sqlite support from `rails dbconsole`.

*Andrew White*
Expand Down
2 changes: 2 additions & 0 deletions railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ def secrets
require "erb"
all_secrets = YAML.load(ERB.new(IO.read(yaml)).result) || {}
env_secrets = all_secrets[Rails.env]
common_secrets = all_secrets['common']
secrets.merge!(common_secrets.symbolize_keys) if common_secrets
secrets.merge!(env_secrets.symbolize_keys) if env_secrets
end

Expand Down
26 changes: 26 additions & 0 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,32 @@ def index
end
end

test "secrets common to all environments can be specified in secrets.yml" do
app_file 'config/secrets.yml', <<-YAML
common:
foo_api_token: 1234567
YAML

require "#{app_path}/config/environment"

assert_equal 1234567, app.secrets.foo_api_token
end

test "environment specific secrets override common secrets" do
app_file 'config/secrets.yml', <<-YAML
common:
foo_api_token: 1234567
production:
foo_api_token: 99999999
YAML

with_rails_env "production" do
require "#{app_path}/config/environment"
end

assert_equal 99999999, app.secrets.foo_api_token
end

test "protect from forgery is the default in a new app" do
make_basic_app

Expand Down

0 comments on commit 45eba60

Please sign in to comment.