Module: EncryptDataBag

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

Constant Summary collapse

VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.from_file(secret_file, input_file, output_file) ⇒ Object



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

def from_file(secret_file, input_file, output_file)
  secret = Chef::EncryptedDataBagItem.load_secret(secret_file)
  raw_item = IO.read(input_file)
  item = is_json_file?(input_file) ? JSON.parse(raw_item) : eval(raw_item)
  item = Hash[item.map { |k, v| [k.to_s, v] }]
  encrypted_item = Chef::EncryptedDataBagItem.encrypt_data_bag_item(item, secret)
  File.open(output_file, "w") do |file|
    if is_json_file?(output_file)
      file.print(JSON.pretty_generate(encrypted_item))
    else
      file.write(encrypted_item.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