mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
bd10796419
* Fix reading comment only encrypted files When a encrypted file contains only comments then reading that files raises an error: NoMethodError: undefined method `deep_symbolize_keys' for false:FalseClass activesupport/lib/active_support/encrypted_configuration.rb:33:in `config' test/encrypted_configuration_test.rb:52:in `block in <class:EncryptedConfigurationTest>' This happens because the previous implementation returned a `{}` fallback for blank YAML strings. But it did not handle YAML strings that are present but still do not contain any _usefull_ YAML - like the file created by `Rails::Generators::EncryptedFileGenerator` which looks like this: # aws: # access_key_id: 123 # secret_access_key: 345 * Fix coding style violation * Add backwardscompatible with Psych versions that were shipped with Ruby <2.5 * Do not rely on railties for Active Support test * Simplify error handling * Improve test naming * Simplify file creation in test
73 lines
2.2 KiB
Ruby
73 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "abstract_unit"
|
|
require "active_support/encrypted_configuration"
|
|
|
|
class EncryptedConfigurationTest < ActiveSupport::TestCase
|
|
setup do
|
|
@credentials_config_path = File.join(Dir.tmpdir, "credentials.yml.enc")
|
|
|
|
@credentials_key_path = File.join(Dir.tmpdir, "master.key")
|
|
File.write(@credentials_key_path, ActiveSupport::EncryptedConfiguration.generate_key)
|
|
|
|
@credentials = ActiveSupport::EncryptedConfiguration.new(
|
|
config_path: @credentials_config_path, key_path: @credentials_key_path,
|
|
env_key: "RAILS_MASTER_KEY", raise_if_missing_key: true
|
|
)
|
|
end
|
|
|
|
teardown do
|
|
FileUtils.rm_rf @credentials_config_path
|
|
FileUtils.rm_rf @credentials_key_path
|
|
end
|
|
|
|
test "reading configuration by env key" do
|
|
FileUtils.rm_rf @credentials_key_path
|
|
|
|
begin
|
|
ENV["RAILS_MASTER_KEY"] = ActiveSupport::EncryptedConfiguration.generate_key
|
|
@credentials.write({ something: { good: true, bad: false } }.to_yaml)
|
|
|
|
assert @credentials[:something][:good]
|
|
assert_not @credentials.dig(:something, :bad)
|
|
assert_nil @credentials.fetch(:nothing, nil)
|
|
ensure
|
|
ENV["RAILS_MASTER_KEY"] = nil
|
|
end
|
|
end
|
|
|
|
test "reading configuration by key file" do
|
|
@credentials.write({ something: { good: true } }.to_yaml)
|
|
|
|
assert @credentials.something[:good]
|
|
end
|
|
|
|
test "reading comment-only configuration" do
|
|
@credentials.write("# comment")
|
|
|
|
assert_equal @credentials.config, {}
|
|
end
|
|
|
|
test "change configuration by key file" do
|
|
@credentials.write({ something: { good: true } }.to_yaml)
|
|
@credentials.change do |config_file|
|
|
config = YAML.load(config_file.read)
|
|
config_file.write config.merge(new: "things").to_yaml
|
|
end
|
|
|
|
assert @credentials.something[:good]
|
|
assert_equal "things", @credentials[:new]
|
|
end
|
|
|
|
test "raise error when writing an invalid format value" do
|
|
assert_raise(Psych::SyntaxError) do
|
|
@credentials.change do |config_file|
|
|
config_file.write "login: *login\n username: dummy"
|
|
end
|
|
end
|
|
end
|
|
|
|
test "raises key error when accessing config via bang method" do
|
|
assert_raise(KeyError) { @credentials.something! }
|
|
end
|
|
end
|