Parent

Class/Module Index [+]

Quicksearch

Chef::EncryptedDataBagItem

An EncryptedDataBagItem represents a read-only data bag item where all values, except for the value associated with the id key, have been encrypted.

EncrypedDataBagItem can be used in recipes to decrypt data bag item members.

Data bag item values are assumed to have been encrypted using the default symmetric encryption provided by Encryptor.encrypt where values are converted to YAML prior to encryption.

If the shared secret is not specified at initialization or load, then the contents of the file referred to in Chef::Config will be used as the secret. The default path is /etc/chef/encrypted_data_bag_secret

EncryptedDataBagItem is intended to provide a means to avoid storing data bag items in the clear on the Chef server. This provides some protection against a breach of the Chef server or of Chef server backup data. Because the secret must be stored in the clear on any node needing access to an EncryptedDataBagItem, this approach provides no protection of data bag items from actors with access to such nodes in the infrastructure.

Public Class Methods

encrypt_data_bag_item(plain_hash, secret) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 213
def self.encrypt_data_bag_item(plain_hash, secret)
  plain_hash.inject({}) do |h, (key, val)|
    h[key] = if key != "id"
               self.encrypt_value(val, secret)
             else
               val
             end
    h
  end
end
encrypt_value(value, key) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 230
def self.encrypt_value(value, key)
  Base64.encode64(self.cipher(:encrypt, value.to_yaml, key))
end
from_plain_hash(plain_hash, secret) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 209
def self.from_plain_hash(plain_hash, secret)
  self.new(self.encrypt_data_bag_item(plain_hash, secret), secret)
end
load(data_bag, name, secret = nil) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 224
def self.load(data_bag, name, secret = nil)
  raw_hash = Chef::DataBagItem.load(data_bag, name)
  secret = secret || self.load_secret
  self.new(raw_hash, secret)
end
load_secret(path=nil) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 234
def self.load_secret(path=nil)
  path = path || Chef::Config[:encrypted_data_bag_secret] || DEFAULT_SECRET_FILE
  secret = case path
           when /^\w+:\/\//
             # We have a remote key
             begin
               Kernel.open(path).read.strip
             rescue Errno::ECONNREFUSED
               raise ArgumentError, "Remote key not available from '#{path}'"
             rescue OpenURI::HTTPError
               raise ArgumentError, "Remote key not found at '#{path}'"
             end
           else
             if !File.exists?(path)
               raise Errno::ENOENT, "file not found '#{path}'"
             end
             IO.read(path).strip
           end
  if secret.size < 1
    raise ArgumentError, "invalid zero length secret in '#{path}'"
  end
  secret
end
new(enc_hash, secret) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 187
def initialize(enc_hash, secret)
  @enc_hash = enc_hash
  @secret = secret
end

Protected Class Methods

cipher(direction, data, key) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 260
def self.cipher(direction, data, key)
  cipher = OpenSSL::Cipher::Cipher.new(ALGORITHM)
  cipher.send(direction)
  cipher.pkcs5_keyivgen(key)
  ans = cipher.update(data)
  ans << cipher.final
  ans
end

Public Instance Methods

[](key) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 192
def [](key)
  value = @enc_hash[key]
  if key == "id" || value.nil?
    value
  else
    Decryptor.for(value, @secret).for_decrypted_item
  end
end
[]=(key, value) click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 201
def []=(key, value)
  raise ArgumentError, "assignment not supported for #{self.class}"
end
to_hash() click to toggle source
# File lib/chef/encrypted_data_bag_item.rb, line 205
def to_hash
  @enc_hash.keys.inject({}) { |hash, key| hash[key] = self[key]; hash }
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.