-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch
Rails.application.secret_key_base
to read from config/secrets…
….yml ...and generate if missing
- Loading branch information
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'securerandom' | ||
|
||
module Extensions | ||
module SecretKeyBase | ||
def secret_key_base | ||
@secret_key_base || | ||
begin | ||
self.secret_key_base = | ||
if Rails.env.local? || ENV["SECRET_KEY_BASE_DUMMY"] | ||
super | ||
else | ||
ENV["SECRET_KEY_BASE"] || Rails.application.credentials.secret_key_base || secret_from_file | ||
end | ||
end | ||
end | ||
|
||
# Read secret from config/secrets.yml, generate if missing. | ||
def secret_from_file | ||
file = Rails.root.join('config', 'secrets.yml') | ||
if file.exist? | ||
yaml = file.read | ||
secret = YAML.load(yaml, aliases: true).dig(Rails.env, 'secret_key_base') | ||
if secret.blank? | ||
lines = yaml.split("\n") | ||
index = lines.index { |line| line.match(/\A\s*secret_key_base:\s*# ! Set production secret here/) } | ||
if index | ||
puts 'Writing new production secret_key_base to config/secrets.yml' | ||
lines[index].sub!(/: \s*# ! Set production secret here/, ": #{SecureRandom.hex(64)}") | ||
File.write(file, lines.join("\n")) | ||
return secret_from_file | ||
end | ||
elsif !secret.start_with?('<%') | ||
return secret | ||
end | ||
end | ||
|
||
raise 'No secret key base found! Either set the SECRET_KEY_BASE environment variable, edit config/secrets.yml, or use Rails encrypted credentials.' | ||
end | ||
end | ||
end | ||
|
||
Rails::Application::Configuration.prepend Extensions::SecretKeyBase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters