Module: Chef::Sugar::DataBag

Extended by:
DataBag
Included in:
DataBag
Defined in:
lib/chef/sugar/data_bag.rb

Defined Under Namespace

Classes: EncryptedDataBagSecretNotGiven

Instance Method Summary collapse

Instance Method Details

#data_bag_item_for_environment(node, bag, id) ⇒ Hash

This algorithm attempts to find the data bag entry for the current node's Chef environment. If there are no environment-specific values, the “default” bucket is used. The data bag must follow the schema:

{
  "default": {...},
  "environment_name": {...},
  "other_environment": {...},
}

Parameters:

  • node (Node)

    the current Chef node

  • bag (String)

    the name of the data bag

  • id (String)

    the id of the data bag

Returns:

  • (Hash)

116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef/sugar/data_bag.rb', line 116

def data_bag_item_for_environment(node, bag, id)
  data = Chef::DataBagItem.load(bag, id)

  if data[node.chef_environment]
    Chef::Log.debug "Using #{node.chef_environment} as the key"
    data[node.chef_environment]
  else
    Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
    data['default']
  end
end

#encrypted_data_bag_item(bag, id, secret = nil) ⇒ Hash

Helper method for loading an encrypted data bag item in a similar syntax/recipe DSL method.

Parameters:

  • bag (String)

    the name of the encrypted data bag

  • id (String)

    the id of the encrypted data bag

  • secret (String) (defaults to: nil)

    the encrypted data bag secret raw value

Returns:

  • (Hash)

49
50
51
52
53
54
55
56
57
58
# File 'lib/chef/sugar/data_bag.rb', line 49

def encrypted_data_bag_item(bag, id, secret = nil)
  Chef::Log.debug "Loading encrypted data bag item #{bag}/#{id}"

  if secret.nil? && Chef::Config[:encrypted_data_bag_secret].nil?
    raise EncryptedDataBagSecretNotGiven.new
  end

  secret ||= File.read(Chef::Config[:encrypted_data_bag_secret]).strip
  Chef::EncryptedDataBagItem.load(bag, id, secret)
end

#encrypted_data_bag_item_for_environment(node, bag, id, secret = nil) ⇒ Hash

This algorithm attempts to find the data bag entry for the current node's Chef environment. If there are no environment-specific values, the “default” bucket is used. The data bag must follow the schema:

{
  "default": {...},
  "environment_name": {...},
  "other_environment": {...},
}

Parameters:

  • node (Node)

    the current Chef node

  • bag (String)

    the name of the encrypted data bag

  • id (String)

    the id of the encrypted data bag

  • secret (String) (defaults to: nil)

    the encrypted data bag secret (default's to the Chef::Config value)

Returns:

  • (Hash)

83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chef/sugar/data_bag.rb', line 83

def encrypted_data_bag_item_for_environment(node, bag, id, secret = nil)
  data = encrypted_data_bag_item(bag, id, secret)

  if data[node.chef_environment]
    Chef::Log.debug "Using #{node.chef_environment} as the key"
    data[node.chef_environment]
  else
    Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
    data['default']
  end
end