- <%= @result.to_yaml.sub(/^---$/, "") %>
-
+ <%= HieraData::Util.yaml_format(@result) %>
<% end %>
diff --git a/test/models/hiera_data/e_yaml_file_test.rb b/test/models/hiera_data/e_yaml_file_test.rb
new file mode 100644
index 00000000..c0aed2bd
--- /dev/null
+++ b/test/models/hiera_data/e_yaml_file_test.rb
@@ -0,0 +1,51 @@
+require 'test_helper'
+
+class HieraData
+ class EYamlFileTest < ActiveSupport::TestCase
+ test "::encrypted? detects encrypted values" do
+ assert_equal false, EYamlFile.encrypted?("string")
+
+ assert_equal true, EYamlFile.encrypted?(ciphertext)
+ end
+
+ test "::decrypt_value can decrypt a value" do
+ assert_equal "top secret", EYamlFile.decrypt_value(ciphertext, public_key:, private_key:)
+ end
+
+ test "::encrypt_value can encrypt values" do
+ encrypted_value = EYamlFile.encrypt_value("top secret", public_key:, private_key:)
+
+ assert_match /\AENC\[.+\]\z/, encrypted_value
+ assert_no_match /top secret/, encrypted_value
+ end
+
+ test "#content can decrypt top level encrypted values" do
+ file = EYamlFile.new(
+ path: config_dir.join("common.yaml"),
+ options: {
+ public_key:, private_key: , decrypt: true
+ }
+ )
+
+ assert_equal "c8hoduj5", file.content["c8hoduj5"].chomp
+ end
+
+ private
+
+ def ciphertext
+ "ENC[PKCS7,MIIBeQYJKoZIhvcNAQcDoIIBajCCAWYCAQAxggEhMIIBHQIBADAFMAACAQEwDQYJKoZIhvcNAQEBBQAEggEAe2qPOZxi519fmMyaH47BN1oEnDcluk5ec0jlugSzyInd3v2qirncMYVcAvjg2ckjhWX4h458ZJJuDpT5+ediNG+OQ/BAO+QgjHu7eAR8imjBmeFbjN+dl90y4Lh0S4b/ihpcJ8N9qASWvCePmKafjwFaKNjc6Dws05OQ+G/oBIiXGkXJsE6kbT1qX9DrovHEO6Ve2dANUYmiw1oC8cyqSPi8aBeDdBmZJCQyDrx37QTXf8+b0aVAMG4KPEI1vdoO10ElAsof8Mwx60HkUCCSXRZ2fACp5ODf+hgg9B7Z4eFRxIf4VuqPI+b4pcvPRS/PExI2E99YXIyJz86DD7KPFjA8BgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBAgGnfhv3yX43m4aHwqBAB9gBAHgnAZ17HQe3wMCQ2pPuh8]"
+ end
+
+ def config_dir
+ Pathname.new(Rails.configuration.hdm["config_dir"]).join('environments', 'eyaml', 'data')
+ end
+
+ def public_key
+ Rails.root.join("test/fixtures/files/puppet/environments/eyaml/keys/public_key.pkcs7.pem")
+ end
+
+ def private_key
+ Rails.root.join("test/fixtures/files/puppet/environments/eyaml/keys/private_key.pkcs7.pem")
+ end
+ end
+end
diff --git a/test/models/hiera_data/util_test.rb b/test/models/hiera_data/util_test.rb
new file mode 100644
index 00000000..4fe030fb
--- /dev/null
+++ b/test/models/hiera_data/util_test.rb
@@ -0,0 +1,17 @@
+require 'test_helper'
+
+class HieraData
+ class UtilTest < ActiveSupport::TestCase
+ test "::yaml_format converts a hash to yaml and removes leading `---`" do
+ hash = { "test" => 23 }
+
+ assert_equal "test: 23", Util.yaml_format(hash)
+ end
+
+ test "::yaml_format converts an integer to yaml and removes leading `---`" do
+ integer = 23
+
+ assert_equal "23", Util.yaml_format(integer)
+ end
+ end
+end
diff --git a/test/models/hiera_data/yaml_file_test.rb b/test/models/hiera_data/yaml_file_test.rb
index 8b60ffca..66eaae7d 100644
--- a/test/models/hiera_data/yaml_file_test.rb
+++ b/test/models/hiera_data/yaml_file_test.rb
@@ -75,7 +75,7 @@ class YamlFileTest < ActiveSupport::TestCase
test "#content_for_key returns empty array as array" do
file = HieraData::YamlFile.new(path: config_dir.join("nodes/testhost.yaml"))
- assert_equal "[]\n", file.content_for_key('classes')
+ assert_equal "[]", file.content_for_key('classes')
end
test "#write_key goes fine" do
@@ -131,7 +131,7 @@ def config_dir
end
def key_as_string
- <<~HEREDOC
+ <<~HEREDOC.chomp
tp::conf:
postfix:
template: foobar/postfix/main.cf.epp
diff --git a/test/models/hiera_data_test.rb b/test/models/hiera_data_test.rb
index 74384bdb..ea305cc0 100644
--- a/test/models/hiera_data_test.rb
+++ b/test/models/hiera_data_test.rb
@@ -13,11 +13,11 @@ class HieraDataTest < ActiveSupport::TestCase
test "#search_key returns key data for all given files" do
hiera = HieraData.new('development')
expected_result = {
- "nodes/testhost.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: hostname\n"},
+ "nodes/testhost.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: hostname"},
"role/hdm_test-development.yaml" => {file_present: false, file_writable: true, replaced_from_git: false, key_present: false, value: nil},
- "role/hdm_test.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: hostname-role\n"},
+ "role/hdm_test.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: hostname-role"},
"zone/internal.yaml" => {file_present: false, file_writable: false, replaced_from_git: false, key_present: false, value: nil },
- "common.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: common::hostname\n"}
+ "common.yaml" => {file_present: true, file_writable: true, replaced_from_git: false, key_present: true, value: "hostname: common::hostname"}
}
facts = {"fqdn" => "testhost", "role" => "hdm_test", "env" => "development", "zone" => "internal"}