Class: Chef::EncryptedDataBagItem::Decryptor::Version1Decryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/encrypted_data_bag_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encrypted_data, key) ⇒ Version1Decryptor

Returns a new instance of Version1Decryptor.



99
100
101
102
# File 'lib/chef/encrypted_data_bag_item.rb', line 99

def initialize(encrypted_data, key)
  @encrypted_data = encrypted_data
  @key = key
end

Instance Attribute Details

#encrypted_dataObject (readonly)

Returns the value of attribute encrypted_data.



96
97
98
# File 'lib/chef/encrypted_data_bag_item.rb', line 96

def encrypted_data
  @encrypted_data
end

#keyObject (readonly)

Returns the value of attribute key.



97
98
99
# File 'lib/chef/encrypted_data_bag_item.rb', line 97

def key
  @key
end

Instance Method Details

#assert_valid_cipher!Object



137
138
139
140
141
142
143
144
145
# File 'lib/chef/encrypted_data_bag_item.rb', line 137

def assert_valid_cipher!
  # In the future, chef may support configurable ciphers. For now, only
  # aes-256-cbc is supported.
  requested_cipher = @encrypted_data["cipher"]
  unless requested_cipher == ALGORITHM
    raise UnsupportedCipher,
      "Cipher '#{requested_cipher}' is not supported by this version of Chef. Available ciphers: ['#{ALGORITHM}']"
  end
end

#decrypted_dataObject



117
118
119
120
121
122
123
124
# File 'lib/chef/encrypted_data_bag_item.rb', line 117

def decrypted_data
  @decrypted_data ||= begin
    plaintext = openssl_decryptor.update(encrypted_bytes)
    plaintext << openssl_decryptor.final
  rescue OpenSSL::Cipher::CipherError => e
    raise DecryptionFailure, "Error decrypting data bag value: '#{e.message}'. Most likely the provided key is incorrect"
  end
end

#encrypted_bytesObject



109
110
111
# File 'lib/chef/encrypted_data_bag_item.rb', line 109

def encrypted_bytes
  Base64.decode64(@encrypted_data["encrypted_data"])
end

#for_decrypted_itemObject



104
105
106
# File 'lib/chef/encrypted_data_bag_item.rb', line 104

def for_decrypted_item
  Yajl::Parser.parse(decrypted_data)["json_wrapper"]
end

#ivObject



113
114
115
# File 'lib/chef/encrypted_data_bag_item.rb', line 113

def iv
  Base64.decode64(@encrypted_data["iv"])
end

#openssl_decryptorObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/encrypted_data_bag_item.rb', line 126

def openssl_decryptor
  @openssl_decryptor ||= begin
    assert_valid_cipher!
    d = OpenSSL::Cipher::Cipher.new(ALGORITHM)
    d.decrypt
    d.key = Digest::SHA256.digest(key)
    d.iv = iv
    d
  end
end