Module: EncryptDataBag

Defined in:
lib/encrypt_data_bag.rb,
lib/encrypt_data_bag/version.rb

Constant Summary collapse

VERSION =
"1.1.0"

Class Method Summary collapse

Class Method Details

.from_file(secret_file, input_file, output_file, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/encrypt_data_bag.rb', line 10

def from_file(secret_file, input_file, output_file, options={})
  secret = Chef::EncryptedDataBagItem.load_secret(secret_file)
  input = IO.read(input_file)
  item = is_json_file?(input_file) ? JSON.parse(input) : eval(input)
  item = Hash[item.map { |k, v| [k.to_s, v] }]
  output = if options[:decrypt]
    Chef::EncryptedDataBagItem.new(item, secret).to_hash
  else
    Chef::EncryptedDataBagItem.encrypt_data_bag_item(item, secret)
  end
  File.open(output_file, "w") do |file|
    if is_json_file?(output_file)
      file.print(JSON.pretty_generate(output))
    else
      file.write(output.pretty_inspect)
    end
  end
end

.is_json_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/encrypt_data_bag.rb', line 6

def is_json_file?(file)
  File.extname(file) == ".json"
end