Class: Hiera::Backend::Eyaml::Actions::EncryptAction

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/backend/eyaml/actions/encrypt_action.rb

Constant Summary collapse

REGEX_DECRYPTED_BLOCK =
/>\n(\s*)DEC(::\w+)?\[(.+)\]\!/
REGEX_DECRYPTED_STRING =
/DEC(::\w+)?\[(.+)\]\!/

Class Method Summary collapse

Class Method Details

.executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/hiera/backend/eyaml/actions/encrypt_action.rb', line 13

def self.execute 

  case Eyaml::Options[:source]
  when :eyaml
    encryptions = []

    # blocks
    output = Eyaml::Options[:input_data].gsub( REGEX_DECRYPTED_BLOCK ) { |match|
      indentation = $1
      encryption_scheme = parse_encryption_scheme( $2 )
      encryptor = Encryptor.find encryption_scheme
      ciphertext = encryptor.encode( encryptor.encrypt($3) ).gsub(/\n/, "\n" + indentation)
      ">\n" + indentation + "ENC[#{encryptor.tag},#{ciphertext}]"
    }

    # strings
    output.gsub( REGEX_DECRYPTED_STRING ) { |match|
      encryption_scheme = parse_encryption_scheme( $1 )
      encryptor = Encryptor.find encryption_scheme
      ciphertext = encryptor.encode( encryptor.encrypt($2) ).gsub(/\n/, "")
      "ENC[#{encryptor.tag},#{ciphertext}]"
    }

  else
    encryptor = Encryptor.find
    ciphertext = encryptor.encode( encryptor.encrypt(Eyaml::Options[:input_data]) )
    self.format :data => "ENC[#{encryptor.tag},#{ciphertext}]", :structure => Eyaml::Options[:output], :label => Eyaml::Options[:label]
  end

end