Skip to content

Commit

Permalink
Add possibility to set custom Rails.application.credentials object.
Browse files Browse the repository at this point in the history
In multi environment application one can set custom file by:

```
config.before_initialize do
  self.credentials = encrypted("config/custom-credentials.yml.enc")
end
```
  • Loading branch information
morgoth committed Nov 29, 2017
1 parent f4c08d1 commit dfe3a88
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
12 changes: 12 additions & 0 deletions railties/CHANGELOG.md
@@ -1,3 +1,15 @@
* Add possibility to set custom `Rails.application.credentials` object.
In multi environment application one can set custom file by:

```
config.before_initialize do
self.credentials = encrypted("config/custom-credentials.yml.enc")
end
```

*Wojciech Wnętrzak*


## Rails 5.2.0.beta2 (November 28, 2017) ##

* No changes.
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -118,6 +118,7 @@ def find_root(from)
attr_accessor :assets, :sandbox
alias_method :sandbox?, :sandbox
attr_reader :reloaders, :reloader, :executor
attr_writer :credentials

delegate :default_url_options, :default_url_options=, to: :routes

Expand Down
36 changes: 36 additions & 0 deletions railties/test/application/credentials_test.rb
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "isolation/abstract_unit"

module ApplicationTests
class CredentialsTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation

setup :build_app
teardown :teardown_app

test "sets custom application credentials" do
Dir.chdir(app_path) do
File.write("config/master.key", ActiveSupport::EncryptedFile.generate_key)

file = ActiveSupport::EncryptedFile.new(
content_path: "config/custom-credentials.yml.enc",
key_path: "config/master.key",
env_key: "RAILS_MASTER_KEY"
)

file.write("aws_access_key_id: secret-key")
end

add_to_config <<-RUBY
config.before_initialize do
self.credentials = encrypted("config/custom-credentials.yml.enc")
end
RUBY

require "#{app_path}/config/environment"

assert_equal "secret-key", Rails.application.credentials.aws_access_key_id
end
end
end

0 comments on commit dfe3a88

Please sign in to comment.