module Chef::EncryptedDataBagItem::CheckEncrypted

Common code for checking if a data bag appears encrypted

Public Instance Methods

encrypted?(raw_data) click to toggle source

Tries to autodetect if the item's raw hash appears to be encrypted.

# File lib/chef/encrypted_data_bag_item/check_encrypted.rb, line 26
def encrypted?(raw_data)
  data = raw_data.reject { |k, _| k == "id" } # Remove the "id" key.
  # Assume hashes containing only the "id" key are not encrypted.
  # Otherwise, remove the keys that don't appear to be encrypted and compare
  # the result with the hash. If some entry has been removed, then some entry
  # doesn't appear to be encrypted and we assume the entire hash is not encrypted.
  data.empty? ? false : data.reject { |_, v| !looks_like_encrypted?(v) } == data
end

Private Instance Methods

looks_like_encrypted?(data) click to toggle source

Checks if data looks like it has been encrypted by Chef::EncryptedDataBagItem::Encryptor::VersionXEncryptor. Returns true only when there is an exact match between the VersionXEncryptor keys and the hash's keys.

# File lib/chef/encrypted_data_bag_item/check_encrypted.rb, line 41
def looks_like_encrypted?(data)
  return false unless data.is_a?(Hash) && data.key?("version")

  case data["version"]
    when 1
      Chef::EncryptedDataBagItem::Encryptor::Version1Encryptor.encryptor_keys.sort == data.keys.sort
    when 2
      Chef::EncryptedDataBagItem::Encryptor::Version2Encryptor.encryptor_keys.sort == data.keys.sort
    when 3
      Chef::EncryptedDataBagItem::Encryptor::Version3Encryptor.encryptor_keys.sort == data.keys.sort
    else
      false # version means something else... assume not encrypted.
  end
end