Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not add master key when RAILS_MASTER_KEY env specified #31922

Merged
merged 1 commit into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def edit
require_application_and_environment!

ensure_editor_available(command: "bin/rails credentials:edit") || (return)
ensure_master_key_has_been_added
ensure_master_key_has_been_added if Rails.application.credentials.key.nil?
ensure_credentials_have_been_added

catch_editing_exceptions do
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/commands/encrypted/encrypted_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ def help

def edit(file_path)
require_application_and_environment!
encrypted = Rails.application.encrypted(file_path, key_path: options[:key])

ensure_editor_available(command: "bin/rails encrypted:edit") || (return)
ensure_encryption_key_has_been_added(options[:key])
ensure_encryption_key_has_been_added(options[:key]) if encrypted.key.nil?
ensure_encrypted_file_has_been_added(file_path, options[:key])

catch_editing_exceptions do
Expand Down
12 changes: 12 additions & 0 deletions railties/test/commands/credentials_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
assert_match(/api_key: abc/, run_show_command)
end

test "edit command does not add master key when `RAILS_MASTER_KEY` env specified" do
Dir.chdir(app_path) do
key = IO.binread("config/master.key").strip
FileUtils.rm("config/master.key")

switch_env("RAILS_MASTER_KEY", key) do
run_edit_command
assert_not File.exist?("config/master.key")
end
end
end

test "show credentials" do
assert_match(/access_key_id: 123/, run_show_command)
end
Expand Down
12 changes: 12 additions & 0 deletions railties/test/commands/encrypted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ class Rails::Command::EncryptedCommandTest < ActiveSupport::TestCase
end
end

test "edit command does not add master key when `RAILS_MASTER_KEY` env specified" do
Dir.chdir(app_path) do
key = IO.binread("config/master.key").strip
FileUtils.rm("config/master.key")

switch_env("RAILS_MASTER_KEY", key) do
run_edit_command("config/tokens.yml.enc")
assert_not File.exist?("config/master.key")
end
end
end

test "edit encrypts file with custom key" do
run_edit_command("config/tokens.yml.enc", key: "config/tokens.key")

Expand Down