Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify recrypt command to allow recrypting file with different encryp… #232

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions features/recrypt.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ Feature: Recrypt
Then the exit status should be 1
And I run `eyaml decrypt -e test_input.eyaml`
Then the output should match /encrypted_string: DEC::PKCS7\[planet of the apes\]\!/
And I run `eyaml recrypt -d plaintext test_input.eyaml`
Then the exit status should be 0
And I run `eyaml decrypt -e test_input.eyaml`
Then the output should match /encrypted_string: DEC::PLAINTEXT\[planet of the apes\]\!/
And I run `eyaml recrypt -d pkcs7 test_input.eyaml`
Then the exit status should be 0
And I run `eyaml decrypt -e test_input.eyaml`
Then the output should match /encrypted_string: DEC::PKCS7\[planet of the apes\]\!/
6 changes: 6 additions & 0 deletions lib/hiera/backend/eyaml/parser/encrypted_tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'hiera/backend/eyaml/utils'
require 'hiera/backend/eyaml/encryptor'
require 'hiera/backend/eyaml'
require 'base64'


class Hiera
Expand Down Expand Up @@ -36,6 +37,11 @@ def to_encrypted(args={})
label = args[:label]
label_string = label.nil? ? '' : "#{label}: "
format = args[:format].nil? ? @format : args[:format]
encryption_method = args[:change_encryption]
if encryption_method != nil
@encryptor = Encryptor.find encryption_method
@cipher = Base64.encode64(@encryptor.encrypt @plain_text).strip
end
case format
when :block
# strip any white space
Expand Down
10 changes: 8 additions & 2 deletions lib/hiera/backend/eyaml/subcommands/recrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ module Subcommands
class Recrypt < Subcommand

def self.options
[]
[
{:name => :change_encryption,
:description => "Specify the new encryption method that should be used for the file",
:short => 'd',
:default => "pkcs7"}
]
end

def self.description
Expand All @@ -26,6 +31,7 @@ def self.validate options
options[:source] = :eyaml
options[:eyaml] = ARGV.shift
options[:input_data] = File.read options[:eyaml]
@change_encryption = options[:change_encryption]
options
end

Expand All @@ -38,7 +44,7 @@ def self.execute
decrypted_parser = Parser::ParserFactory.decrypted_parser
edited_tokens = decrypted_parser.parse(decrypted_input)

encrypted_output = edited_tokens.map{ |t| t.to_encrypted }.join
encrypted_output = edited_tokens.map{ |t| t.to_encrypted({:change_encryption => @change_encryption}) }.join

filename = Eyaml::Options[:eyaml]
File.open("#{filename}", 'w') { |file|
Expand Down