forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rails#28128 from rails/revert-28127-revert-28038-e…
…ncrypted-secrets Revert "Revert "Add encrypted secrets""
- Loading branch information
Showing
18 changed files
with
485 additions
and
39 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
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
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
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,52 @@ | ||
=== Storing Encrypted Secrets in Source Control | ||
|
||
The Rails `secrets` commands helps encrypting secrets to slim a production | ||
environment's `ENV` hash. It's also useful for atomic deploys: no need to | ||
coordinate key changes to get everything working as the keys are shipped | ||
with the code. | ||
|
||
=== Setup | ||
|
||
Run `bin/rails secrets:setup` to opt in and generate the `config/secrets.yml.key` | ||
and `config/secrets.yml.enc` files. | ||
|
||
The latter contains all the keys to be encrypted while the former holds the | ||
encryption key. | ||
|
||
Don't lose the key! Put it in a password manager your team can access. | ||
Should you lose it no one, including you, will be able to access any encrypted | ||
secrets. | ||
Don't commit the key! Add `config/secrets.yml.key` to your source control's | ||
ignore file. If you use Git, Rails handles this for you. | ||
|
||
Rails also looks for the key in `ENV["RAILS_MASTER_KEY"]` if that's easier to | ||
manage. | ||
|
||
You could prepend that to your server's start command like this: | ||
|
||
RAILS_MASTER_KEY="im-the-master-now-hahaha" server.start | ||
|
||
|
||
The `config/secrets.yml.enc` has much the same format as `config/secrets.yml`: | ||
|
||
production: | ||
secret_key_base: so-secret-very-hidden-wow | ||
payment_processing_gateway_key: much-safe-very-gaedwey-wow | ||
|
||
But that's where the similarities between `secrets.yml` and `secrets.yml.enc` | ||
end, e.g. no keys from `secrets.yml` will be moved to `secrets.yml.enc` and | ||
be encrypted. | ||
|
||
A `shared:` top level key is also supported such that any keys there is merged | ||
into the other environments. | ||
|
||
=== Editing Secrets | ||
|
||
After `bin/rails secrets:setup`, run `bin/rails secrets:edit`. | ||
|
||
That command opens a temporary file in `$EDITOR` with the decrypted contents of | ||
`config/secrets.yml.enc` to edit the encrypted secrets. | ||
|
||
When the temporary file is next saved the contents are encrypted and written to | ||
`config/secrets.yml.enc` while the file itself is destroyed to prevent secrets | ||
from leaking. |
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,36 @@ | ||
require "active_support" | ||
require "rails/secrets" | ||
|
||
module Rails | ||
module Command | ||
class SecretsCommand < Rails::Command::Base # :nodoc: | ||
def help | ||
say "Usage:\n #{self.class.banner}" | ||
say "" | ||
say self.class.desc | ||
end | ||
|
||
def setup | ||
require "rails/generators" | ||
require "rails/generators/rails/encrypted_secrets/encrypted_secrets_generator" | ||
|
||
Rails::Generators::EncryptedSecretsGenerator.start | ||
end | ||
|
||
def edit | ||
require_application_and_environment! | ||
|
||
Rails::Secrets.read_for_editing do |tmp_path| | ||
puts "Waiting for secrets file to be saved. Abort with Ctrl-C." | ||
system("\$EDITOR #{tmp_path}") | ||
end | ||
|
||
puts "New secrets encrypted and saved." | ||
rescue Interrupt | ||
puts "Aborted changing encrypted secrets: nothing saved." | ||
rescue Rails::Secrets::MissingKeyError => error | ||
say error.message | ||
end | ||
end | ||
end | ||
end |
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
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
66 changes: 66 additions & 0 deletions
66
railties/lib/rails/generators/rails/encrypted_secrets/encrypted_secrets_generator.rb
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,66 @@ | ||
require "rails/generators/base" | ||
require "rails/secrets" | ||
|
||
module Rails | ||
module Generators | ||
class EncryptedSecretsGenerator < Base | ||
def add_secrets_key_file | ||
unless File.exist?("config/secrets.yml.key") || File.exist?("config/secrets.yml.enc") | ||
key = Rails::Secrets.generate_key | ||
|
||
say "Adding config/secrets.yml.key to store the encryption key: #{key}" | ||
say "" | ||
say "Save this in a password manager your team can access." | ||
say "" | ||
say "If you lose the key, no one, including you, can access any encrypted secrets." | ||
|
||
say "" | ||
create_file "config/secrets.yml.key", key | ||
say "" | ||
end | ||
end | ||
|
||
def ignore_key_file | ||
if File.exist?(".gitignore") | ||
unless File.read(".gitignore").include?(key_ignore) | ||
say "Ignoring config/secrets.yml.key so it won't end up in Git history:" | ||
say "" | ||
append_to_file ".gitignore", key_ignore | ||
say "" | ||
end | ||
else | ||
say "IMPORTANT: Don't commit config/secrets.yml.key. Add this to your ignore file:" | ||
say key_ignore, :on_green | ||
say "" | ||
end | ||
end | ||
|
||
def add_encrypted_secrets_file | ||
unless File.exist?("config/secrets.yml.enc") | ||
say "Adding config/secrets.yml.enc to store secrets that needs to be encrypted." | ||
say "" | ||
|
||
template "config/secrets.yml.enc" do |prefill| | ||
say "" | ||
say "For now the file contains this but it's been encrypted with the generated key:" | ||
say "" | ||
say prefill, :on_green | ||
say "" | ||
|
||
Secrets.encrypt(prefill) | ||
end | ||
|
||
say "You can edit encrypted secrets with `bin/rails secrets:edit`." | ||
|
||
say "Add this to your config/environments/production.rb:" | ||
say "config.read_encrypted_secrets = true" | ||
end | ||
end | ||
|
||
private | ||
def key_ignore | ||
[ "", "# Ignore encrypted secrets key file.", "config/secrets.yml.key", "" ].join("\n") | ||
end | ||
end | ||
end | ||
end |
3 changes: 3 additions & 0 deletions
3
railties/lib/rails/generators/rails/encrypted_secrets/templates/config/secrets.yml.enc
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,3 @@ | ||
# See `secrets.yml` for tips on generating suitable keys. | ||
# production: | ||
# external_api_key: 1466aac22e6a869134be3d09b9e89232fc2c2289… |
Oops, something went wrong.