diff options
author | Wojciech Wnętrzak <w.wnetrzak@gmail.com> | 2017-11-14 11:44:23 +0100 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2017-11-15 21:29:15 +0100 |
commit | 7a8728a03986489e1c843ed850afc2c16fb6eb06 (patch) | |
tree | 697428ddbb785a4ce32a77a43a5487914d28d3d1 /railties/lib/rails/commands/credentials | |
parent | ed100166874fb4a542c5aaba933a4cca5ed72269 (diff) | |
download | rails-7a8728a03986489e1c843ed850afc2c16fb6eb06.tar.gz rails-7a8728a03986489e1c843ed850afc2c16fb6eb06.tar.bz2 rails-7a8728a03986489e1c843ed850afc2c16fb6eb06.zip |
Add CLI to manage encrypted files/configs.
To edit/show encrypted file:
```
bin/rails encrypted:edit config/staging_tokens.yml.enc
bin/rails encrypted:edit config/staging_tokens.yml.enc --key config/staging.key
bin/rails encrypted:show config/staging_tokens.yml.enc
```
Also provides a backing Rails.application.encrypted API for Ruby access:
```ruby
Rails.application.encrypted("config/staging_tokens.yml.enc").read
Rails.application.encrypted("config/staging_tokens.yml.enc").config
Rails.application.encrypted("config/staging_tokens.yml.enc", key: "config/staging.key")
```
Diffstat (limited to 'railties/lib/rails/commands/credentials')
-rw-r--r-- | railties/lib/rails/commands/credentials/credentials_command.rb | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/railties/lib/rails/commands/credentials/credentials_command.rb b/railties/lib/rails/commands/credentials/credentials_command.rb index e5d3d01431..8085f07c2b 100644 --- a/railties/lib/rails/commands/credentials/credentials_command.rb +++ b/railties/lib/rails/commands/credentials/credentials_command.rb @@ -1,10 +1,13 @@ # frozen_string_literal: true require "active_support" +require "rails/command/helpers/editor" module Rails module Command class CredentialsCommand < Rails::Command::Base # :nodoc: + include Helpers::Editor + no_commands do def help say "Usage:\n #{self.class.banner}" @@ -16,41 +19,25 @@ module Rails def edit require_application_and_environment! - ensure_editor_available || (return) + ensure_editor_available(command: "bin/rails credentials:edit") || (return) ensure_master_key_has_been_added ensure_credentials_have_been_added - change_credentials_in_system_editor + catch_editing_exceptions do + change_credentials_in_system_editor + end say "New credentials encrypted and saved." - rescue Interrupt - say "Aborted changing credentials: nothing saved." - rescue ActiveSupport::EncryptedFile::MissingKeyError => error - say error.message end def show require_application_and_environment! + say Rails.application.credentials.read.presence || "No credentials have been added yet. Use bin/rails credentials:edit to change that." end private - def ensure_editor_available - if ENV["EDITOR"].to_s.empty? - say "No $EDITOR to open credentials in. Assign one like this:" - say "" - say %(EDITOR="mate --wait" bin/rails credentials:edit) - say "" - say "For editors that fork and exit immediately, it's important to pass a wait flag," - say "otherwise the credentials will be saved immediately with no chance to edit." - - false - else - true - end - end - def ensure_master_key_has_been_added master_key_generator.add_master_key_file master_key_generator.ignore_master_key_file |