Skip to content

Instantly share code, notes, and snippets.

@yfeldblum
Forked from jvehent/gist:3650690
Created September 6, 2012 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yfeldblum/3650851 to your computer and use it in GitHub Desktop.
Save yfeldblum/3650851 to your computer and use it in GitHub Desktop.
# cookbooks/keymaster/libraries/decrypt.rb
module Keymaster
module_function
def decrypt_data_bag_item(item)
Chef::Log.info("yeah !")
end
end
# cookbooks/whatever/metadata.rb
depends "keymaster"
# cookbooks/whatever/recipes/default.rb
item = data_bag_item("some-bag", "some-item")
Keymaster.decrypt_data_bag_item(item)
@jvehent
Copy link

jvehent commented Sep 6, 2012

# cookbooks/keymaster/libraries/decrypt.rb

#class Chef::Recipe::Keymaster
module Keymaster
  module_function

  def decrypt_data_bag_item(databag, item, key_location)
    Chef::Log.info("Decrypting '#{databag}::#{item}' with '#{key_location}'")
    if not File.exists?(key_location)
      raise IOError, "No such file '#{key_location}'"
    end
    decryption_key = Chef::EncryptedDataBagItem.load_secret(key_location)
    content = Chef::EncryptedDataBagItem.load(databag, item,
                                              decryption_key).to_hash
    return content
  end
end
# cookbooks/whatever/metadata.rb
depends "keymaster"
# cookbooks/whatever/recipes/default.rb
dbag_data = Keymaster.decrypt_data_bag_item('credentials',
                                            'securestuff',
                                            '/etc/keys/key.sec')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment