-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
172 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class HieraData | ||
class EYamlFile < YamlFile | ||
ENCRYPTED_PATTERN = /.*ENC\[.*\]/ | ||
|
||
def self.encrypted?(value) | ||
value.is_a?(String) && !!value.match(ENCRYPTED_PATTERN) | ||
end | ||
|
||
def self.decrypt_value(value, public_key:, private_key:) | ||
Hiera::Backend::Eyaml::Options["pkcs7_private_key"] = private_key | ||
Hiera::Backend::Eyaml::Options["pkcs7_public_key"] = public_key | ||
parser = Hiera::Backend::Eyaml::Parser::Parser.new([Hiera::Backend::Eyaml::Parser::EncHieraTokenType.new, Hiera::Backend::Eyaml::Parser::EncBlockTokenType.new]) | ||
tokens = parser.parse(value) | ||
tokens.map(&:to_plain_text).join | ||
end | ||
|
||
def self.encrypt_value(value, public_key:, private_key:) | ||
Hiera::Backend::Eyaml::Options["pkcs7_private_key"] = private_key | ||
Hiera::Backend::Eyaml::Options["pkcs7_public_key"] = public_key | ||
encryptor = Hiera::Backend::Eyaml::Encryptor.find | ||
ciphertext = encryptor.encode(encryptor.encrypt(value)) | ||
token = Hiera::Backend::Eyaml::Parser::EncToken.new(:block, value, encryptor, ciphertext, nil, ' ') | ||
token.to_encrypted format: :string | ||
end | ||
|
||
def content | ||
@content ||= | ||
begin | ||
super | ||
decrypt_content if @content && decrypt? | ||
@content | ||
end | ||
end | ||
|
||
private | ||
|
||
def decrypt_content | ||
public_key = @options[:public_key] | ||
private_key = @options[:private_key] | ||
@content.transform_values! do |value| | ||
if self.class.encrypted?(value) | ||
self.class.decrypt_value(value, public_key:, private_key:) | ||
else | ||
value | ||
end | ||
end | ||
end | ||
|
||
def decrypt? | ||
@options[:decrypt] && | ||
@options[:public_key] && | ||
@options[:private_key] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class HieraData | ||
module Util | ||
module_function | ||
|
||
def yaml_format(value) | ||
value.to_yaml.sub(/\A---(\n| )/, '').chomp | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
<%= turbo_frame_tag "lookup-result-frame" do %> | ||
<code><pre> | ||
<%= @result.to_yaml.sub(/^---$/, "") %> | ||
</pre></code> | ||
<code><pre><%= HieraData::Util.yaml_format(@result) %></pre></code> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters