Created
April 20, 2017 11:59
-
-
Save lonniev/785401dbb932d7522744f8259f474cd5 to your computer and use it in GitHub Desktop.
Small Ruby script borrowed from someone else to encrypt a data bag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
if ARGV.length < 2 | |
puts "usage: #{$0} databag.json new_encrypted_databag.json [encrypted_data_bag_secret]" | |
exit(1) | |
end | |
databag_file = ARGV[0] | |
out_file = ARGV[1] | |
if ARGV.length >= 3 | |
secret_file = ARGV[2] | |
end | |
require 'rubygems' | |
require 'json' | |
require 'chef/encrypted_data_bag_item' | |
require 'highline/import' | |
puts "Load #{databag_file}" | |
data = JSON.parse(File.read(databag_file)) | |
if secret_file | |
secret = File.read(secret_file) | |
else | |
secret = ask("Enter databag secret: ") { |q| q.echo = false } | |
end | |
unless secret && secret.length > 0 # could enforce secret length | |
puts "You must provide a secret password" | |
exit(2) | |
end | |
encrypted_data = Chef::EncryptedDataBagItem.encrypt_data_bag_item(data, secret) | |
puts "Write encrypted #{out_file}" | |
File.open(out_file, 'w') do |f| | |
f.print JSON.pretty_generate(encrypted_data) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment