From 8a119faeadc3801000d84189896161e3c43ad303 Mon Sep 17 00:00:00 2001 From: Eli Young Date: Tue, 3 Feb 2015 13:07:07 -0800 Subject: [PATCH] (#136) Support \t and \r in block tokens Fixes #136. --- features/decrypts.feature | 1 + features/edit.feature | 11 ++++++----- features/parser.feature | 4 ++-- features/recrypt.feature | 2 +- features/sandbox/test_input.yaml | 11 +++++++++++ lib/hiera/backend/eyaml/parser/encrypted_tokens.rb | 10 +++++----- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/features/decrypts.feature b/features/decrypts.feature index 7ed4af21..6481b9f9 100644 --- a/features/decrypts.feature +++ b/features/decrypts.feature @@ -20,6 +20,7 @@ Feature: eyaml decrypting When I run `eyaml decrypt -e test_input.yaml` Then the output should match /encrypted_string: DEC::PKCS7\[planet of the apes\]\!/ And the output should match /encrypted_block: >\n\s+DEC::PKCS7\[gangs of new york\]\!/ + And the output should match /encrypted_tabbed_block: >\n\s+DEC::PKCS7\[gangs of new york\]\!/ And the output should match /\- DEC::PKCS7\[apocalypse now\]\!/ And the output should match /\- DEC::PKCS7\[the count of monte cristo\]\!/ And the output should match /\- array4/ diff --git a/features/edit.feature b/features/edit.feature index a6cc1e11..6b7dcee4 100644 --- a/features/edit.feature +++ b/features/edit.feature @@ -8,10 +8,11 @@ Feature: eyaml editing Given my EDITOR is set to "/bin/cat" When I run `bash -c 'cp test_input.yaml test_input.eyaml'` When I run `eyaml edit test_input.eyaml` - Then the output should match /encrypted_string: DEC\(1\)::PKCS7\[planet of the apes\]\!/ - And the output should match /encrypted_default_encryption_string: DEC\(3\)::PKCS7\[planet of the apes\]\!/ - And the output should match /encrypted_block: >\n\s+DEC\(5\)::PKCS7\[gangs of new york\]\!/ - And the output should match /encrypted_default_encryption_block: >\n\s+DEC\(7\)::PKCS7\[gangs of new york\]\!/ + Then the output should match /encrypted_string: DEC\(\d+\)::PKCS7\[planet of the apes\]\!/ + And the output should match /encrypted_default_encryption_string: DEC\(\d+\)::PKCS7\[planet of the apes\]\!/ + And the output should match /encrypted_block: >\n\s+DEC\(\d+\)::PKCS7\[gangs of new york\]\!/ + And the output should match /encrypted_tabbed_block: >\n\s+DEC\(\d+\)::PKCS7\[gangs of new york\]\!/ + And the output should match /encrypted_default_encryption_block: >\n\s+DEC\(\d\)::PKCS7\[gangs of new york\]\!/ And the output should match /\- DEC\(\d+\)::PKCS7\[apocalypse now\]\!/ And the output should match /\- DEC\(\d+\)::PKCS7\[the count of monte cristo\]\!/ And the output should match /\- array4/ @@ -27,7 +28,7 @@ Feature: eyaml editing And the output should match /\- >\n\s+i wondered lonely\s*\n\s+as a cloud/ And the output should match /\s+key5: DEC\(\d+\)::PKCS7\[value5\]\!/ And the output should match /\s+key6: DEC\(\d+\)::PKCS7\[value6\]\!/ - And the output should match /multi_encryption: DEC\(29\)::PLAINTEXT\[jammy\]\! DEC\(\d+\)::PKCS7\[dodger\]!/ + And the output should match /multi_encryption: DEC\(\d+\)::PLAINTEXT\[jammy\]\! DEC\(\d+\)::PKCS7\[dodger\]!/ Scenario: decrypting a eyaml file should add a preamble Given my EDITOR is set to "/bin/cat" diff --git a/features/parser.feature b/features/parser.feature index 516a8b77..13f7531f 100644 --- a/features/parser.feature +++ b/features/parser.feature @@ -15,7 +15,7 @@ Feature: Parser And I configure the keypair And I load a file called test_input.yaml When I parse the content - Then I should have 33 tokens + Then I should have 35 tokens Then token 1 should be a NonMatchToken Then token 2 should be a EncToken Then token 2 should start with "ENC[PKCS7,MIIBiQYJKoZIhvcNAQ" @@ -47,4 +47,4 @@ Feature: Parser When I parse the content And map it to index decrypted values Then decryption 1 should be "DEC(1)::PKCS7[planet of the apes]!" - Then decryption 11 should be "DEC(11)::PKCS7[the count of monte cristo]!" + Then decryption 13 should be "DEC(13)::PKCS7[the count of monte cristo]!" diff --git a/features/recrypt.feature b/features/recrypt.feature index dd8e24d7..f3182479 100644 --- a/features/recrypt.feature +++ b/features/recrypt.feature @@ -7,7 +7,7 @@ Feature: Recrypt And I configure the keypair And I load a file called test_input.yaml And I recrypt it twice - Then I should have 33 tokens + Then I should have 35 tokens Then the recrypted tokens should match Then the recrypted decrypted content should match Then the recrypted contents should differ diff --git a/features/sandbox/test_input.yaml b/features/sandbox/test_input.yaml index f06f3c74..79c0b258 100644 --- a/features/sandbox/test_input.yaml +++ b/features/sandbox/test_input.yaml @@ -18,6 +18,17 @@ encrypted_block: > ebOxxDBMBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBCWeGlYS5cQoX78L6LK /mczgCD/pI7usp1XPebnN8CngxHXuUjj5S+6IUpOW6l2JgUeWw==] +encrypted_tabbed_block: > + ENC[PKCS7,MIIBiQYJKoZIhvcNAQcDoIIBejCCAXYCAQAxggEhMIIBHQIBADAFMAACAQAw + DQYJKoZIhvcNAQEBBQAEggEAYzeWn3MBLhOs4hokxMCWcDd9VuwCylQRUQ0w + KwCObeORw8PJkCDvi5ZIA2YkrvYTT6u3/7KfAiHd0Rg1WLb9et0Mg/Fd3DFF + 7qhqOGHoQt3+4eKzlcikeR0/Lqrq2vTpqZ2Sw1CZ7Dn+Z4ll95p7lp97rb2J + kYTVroLYGWEcsS3JZLL4/l3z0bJbXNKKqJ1aHCAFq+wmWXeb6cDvvyHFg2N/ + vGPFEQjP7AbWhxHxXDbYIGcU073u5NtE40JXL8SH82iHxqRF8s9g6Dh5cmjg + AY2pkBD9e6N78NNx+PAJswsFAV4DOCbXdf2BisyYbM3na35MVfyb6ggDegrE + ebOxxDBMBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAEqBBCWeGlYS5cQoX78L6LK + /mczgCD/pI7usp1XPebnN8CngxHXuUjj5S+6IUpOW6l2JgUeWw==] + encrypted_default_encryption_block: > ENC[MIIBiQYJKoZIhvcNAQcDoIIBejCCAXYCAQAxggEhMIIBHQIBADAFMAACAQAw DQYJKoZIhvcNAQEBBQAEggEAYzeWn3MBLhOs4hokxMCWcDd9VuwCylQRUQ0w diff --git a/lib/hiera/backend/eyaml/parser/encrypted_tokens.rb b/lib/hiera/backend/eyaml/parser/encrypted_tokens.rb index 85c57dd1..19517a66 100644 --- a/lib/hiera/backend/eyaml/parser/encrypted_tokens.rb +++ b/lib/hiera/backend/eyaml/parser/encrypted_tokens.rb @@ -39,13 +39,13 @@ def to_encrypted(args={}) case format when :block # strip any white space - @cipher = @cipher.gsub(/ /m, "") + @cipher = @cipher.gsub(/[ \t]/, "") # normalize indentation - ciphertext = @cipher.gsub(/\n/, "\n" + @indentation) + ciphertext = @cipher.gsub(/[\n\r]/, "\n" + @indentation) chevron = (args[:use_chevron].nil? || args[:use_chevron]) ? ">\n" : '' "#{label_string}#{chevron}" + @indentation + "ENC[#{@encryptor.tag},#{ciphertext}]" when :string - ciphertext = @cipher.gsub(/\n/, "") + ciphertext = @cipher.gsub(/[\n\r]/, "") "#{label_string}ENC[#{@encryptor.tag},#{ciphertext}]" else raise "#{@format} is not a valid format" @@ -87,7 +87,7 @@ def initialize @string_token_type = EncStringTokenType.new() end def create_token(string) - @string_token_type.create_token(string.gsub(/[ \n]/, '')) + @string_token_type.create_token(string.gsub(/\s/, '')) end end @@ -103,7 +103,7 @@ def create_token(string) class EncBlockTokenType < EncTokenType def initialize - @regex = />\n(\s*)ENC\[(\w+,)?([a-zA-Z0-9\+\/ =\n]+?)\]/ + @regex = />\n(\s*)ENC\[(\w+,)?([a-zA-Z0-9\+\/=\s]+?)\]/ end def create_token(string) md = @regex.match(string)