Class: SecureDataBag::SecureDataBagItem::Decryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_data_bag/decryptor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encrypted_hash, encryption, key) ⇒ Decryptor

Returns a new instance of Decryptor.



15
16
17
18
19
# File 'lib/secure_data_bag/decryptor.rb', line 15

def initialize(encrypted_hash, encryption, key)
  @encryption = encryption
  @encrypted_hash = encrypted_hash
  @key = key
end

Instance Attribute Details

#encrypted_hashObject (readonly)

Returns the value of attribute encrypted_hash.



13
14
15
# File 'lib/secure_data_bag/decryptor.rb', line 13

def encrypted_hash
  @encrypted_hash
end

#encryptionObject (readonly)

Returns the value of attribute encryption.



12
13
14
# File 'lib/secure_data_bag/decryptor.rb', line 12

def encryption
  @encryption
end

#keyObject (readonly)

Returns the value of attribute key.



11
12
13
# File 'lib/secure_data_bag/decryptor.rb', line 11

def key
  @key
end

Instance Method Details

#decrypt_hash(hash) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/secure_data_bag/decryptor.rb', line 46

def decrypt_hash(hash)
  hash.each do |k,v|
    if encryption[:encoded_fields].include?(k)
      v = decrypt_value(v)
    elsif v.is_a? Hash
      v = decrypt_hash(v)
    end
    hash[k] = v
  end
  hash
end

#decrypt_value(value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/secure_data_bag/decryptor.rb', line 58

def decrypt_value(value)
  if value.is_a? String and not value.empty?
    value = Base64.decode64(value)
    value = openssl_decryptor.update(value)
    value << openssl_decryptor.final

    if value.include? "json_wrapper"
      value = Yajl::Parser.parse(value)["json_wrapper"]
    end
    @openssl_decryptor = nil
  end
  value
end

#decrypted_hashObject



40
41
42
43
44
# File 'lib/secure_data_bag/decryptor.rb', line 40

def decrypted_hash
  @decrypted_hash ||= begin
    decrypt_hash(encrypted_hash.dup)
  end
end

#decryption_error(e = nil) ⇒ Object



26
27
28
29
30
31
# File 'lib/secure_data_bag/decryptor.rb', line 26

def decryption_error(e=nil)
  msg = "Error decrypting data bag value"
  msg << ": '#{e.message}'" if e
  msg << ". Most likely the provided key is incorrect"
  msg
end

#for_decrypted_itemObject



21
22
23
24
# File 'lib/secure_data_bag/decryptor.rb', line 21

def for_decrypted_item
  pp "decrypted_hash"
  decrypted_hash
end

#ivObject



33
34
35
36
37
38
# File 'lib/secure_data_bag/decryptor.rb', line 33

def iv
  @iv ||= begin
    iv_string = encryption[:iv]
    Base64.decode64(iv_string)
  end
end

#openssl_decryptorObject



72
73
74
75
76
77
78
79
80
# File 'lib/secure_data_bag/decryptor.rb', line 72

def openssl_decryptor
  @openssl_decryptor ||= begin
    d = OpenSSL::Cipher::Cipher.new(encryption[:cipher])
    d.decrypt
    d.key = Digest::SHA256.digest(key)
    d.iv = iv
    d
  end
end