Don't load app environment when editing credentials

This avoids missing key exceptions caused by code that tries to read the credentials before they have been added to the encrypted file, for example when editing the credentials for a new environment.
This commit is contained in:
Christos Zisopoulos 2018-12-26 00:03:20 +01:00
parent 6a8519ca89
commit a39aa99c81
3 changed files with 20 additions and 2 deletions

@ -11,10 +11,20 @@ def set_application_directory!
end
def require_application_and_environment!
require_application!
require_environment!
end
def require_application!
require ENGINE_PATH if defined?(ENGINE_PATH)
if defined?(APP_PATH)
require APP_PATH
end
end
def require_environment!
if defined?(APP_PATH)
Rails.application.require_environment!
end
end

@ -20,7 +20,7 @@ def help
end
def edit
require_application_and_environment!
require_application!
ensure_editor_available(command: "bin/rails credentials:edit") || (return)
@ -39,7 +39,7 @@ def edit
end
def show
require_application_and_environment!
require_application!
encrypted = Rails.application.encrypted(content_path, key_path: key_path)

@ -63,6 +63,14 @@ class Rails::Command::CredentialsCommandTest < ActiveSupport::TestCase
end
end
test "edit command does not raise when an initializer tries to acces non-existent credentials" do
app_file "config/initializers/raise_when_loaded.rb", <<-RUBY
Rails.application.credentials.missing_key!
RUBY
assert_match(/access_key_id: 123/, run_edit_command(environment: "qa"))
end
test "show credentials" do
assert_match(/access_key_id: 123/, run_show_command)
end