Skip to content

Instantly share code, notes, and snippets.

@nabeken
Last active December 19, 2015 17:18
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 nabeken/5989715 to your computer and use it in GitHub Desktop.
Save nabeken/5989715 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'json'
require 'slop'
require 'chef/encrypted_data_bag_item'
def usage
"Load JSON from STDIN and then convert JSON to encrypt data bag.
Usage: #{File.basename(__FILE__)} -s [secret_file]"
end
begin
opts = Slop.parse!(:strict => true, :help => true) do
banner usage
on :s, 'secret-file', "secret file", {:argument => true, :required => true}
end
rescue => e
STDERR.puts e
STDERR.puts usage
exit 1
end
unless File.pipe?(STDIN)
STDERR.puts "failed to load JSON from STDIN"
STDERR.puts usage
exit 1
end
begin
bags = JSON.parse(STDIN.read)
secret = Chef::EncryptedDataBagItem.load_secret(opts[:'secret-file'])
ebags = Chef::EncryptedDataBagItem.new(bags, secret)
new_bags = {}
bags.each_key do |k|
new_bags[k] = begin
ebags[k]
rescue
bags[k]
end
end
puts JSON.pretty_generate(Chef::EncryptedDataBagItem.encrypt_data_bag_item(new_bags, secret))
rescue => e
STDERR.puts "failed to encrypt JSON"
STDERR.puts e
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment