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.



172
173
174
175
# File 'lib/chef/encrypted_data_bag_item.rb', line 172

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.



169
170
171
# File 'lib/chef/encrypted_data_bag_item.rb', line 169

def encrypted_data
  @encrypted_data
end

#keyObject (readonly)

Returns the value of attribute key.



170
171
172
# File 'lib/chef/encrypted_data_bag_item.rb', line 170

def key
  @key
end

Instance Method Details

#assert_valid_cipher!Object



210
211
212
213
214
215
216
217
218
# File 'lib/chef/encrypted_data_bag_item.rb', line 210

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



190
191
192
193
194
195
196
197
# File 'lib/chef/encrypted_data_bag_item.rb', line 190

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



182
183
184
# File 'lib/chef/encrypted_data_bag_item.rb', line 182

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

#for_decrypted_itemObject



177
178
179
# File 'lib/chef/encrypted_data_bag_item.rb', line 177

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

#ivObject



186
187
188
# File 'lib/chef/encrypted_data_bag_item.rb', line 186

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

#openssl_decryptorObject



199
200
201
202
203
204
205
206
207
208
# File 'lib/chef/encrypted_data_bag_item.rb', line 199

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