Class: SaltHiera::Plugins::Efiles

Inherits:
Object
  • Object
show all
Defined in:
lib/salt_hiera/plugins/efiles.rb

Class Method Summary collapse

Class Method Details

.decrypt(cipherbinary) ⇒ Object

Raises:

  • (StandardError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/salt_hiera/plugins/efiles.rb', line 35

def self.decrypt cipherbinary

  public_key = Configuration.get "eyaml_public_key"
  private_key = Configuration.get "eyaml_private_key"

  raise StandardError, "pkcs7_public_key is not defined" unless public_key
  raise StandardError, "pkcs7_private_key is not defined" unless private_key

  private_key_pem = File.read private_key

  private_key_rsa = OpenSSL::PKey::RSA.new( private_key_pem )

  public_key_pem = File.read public_key

  public_key_x509 = OpenSSL::X509::Certificate.new( public_key_pem )

  ciphertext = Base64.decode64(cipherbinary)
  pkcs7 = OpenSSL::PKCS7.new( ciphertext )

  pkcs7.decrypt(private_key_rsa, public_key_x509)

end

.process_file(file) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/salt_hiera/plugins/efiles.rb', line 9

def self.process_file file

  key = file.split("/").last
  value = File.read file
  returnhash = { key => value }
  returnhash = self.recurse returnhash
  returnhash

end

.recurse(obj) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/salt_hiera/plugins/efiles.rb', line 19

def self.recurse obj
  if obj.is_a? Array
    obj.each.with_index do |element, index|
      obj[index] = self.recurse element
    end
  elsif obj.is_a? Hash
    obj.each do |k, v|
      obj[k] = self.recurse v
    end
  elsif obj.is_a? String
    obj = obj.gsub(/ENC\[PKCS7,(.*?)\]/) {|x| self.decrypt($1) }
  else
    obj
  end
end