class Chef::Knife::HitoriDataBagEnc

Public Instance Methods

check_data(data) click to toggle source
# File lib/chef/knife/hitori_data_bag_enc.rb, line 40
def check_data(data)
  unless data['id'] == config[:item]
    ui.error ui.color(%Q|ITEM=#{config[:item]} must have {"id": "#{config[:item]}"}, but #{config[:json_file]} does not.|)
    return false
  end
  return true
end
create_data_bag() click to toggle source
# File lib/chef/knife/hitori_data_bag_enc.rb, line 27
def create_data_bag
  data_bag_path = Chef::Config[:data_bag_path]
  secret = Chef::EncryptedDataBagItem.load_secret(secret_file_path)
  data = JSON.parse(File.read(config[:json_file]))
  exit 1 unless check_data(data)
  encrypted_data = Chef::EncryptedDataBagItem.encrypt_data_bag_item(data, secret)
  bag_dir = "#{data_bag_path}/#{config[:bag]}"
  FileUtils.mkpath(bag_dir)
  write_path = "#{bag_dir}/#{config[:item]}.json"
  File.write(write_path, encrypted_data.to_json)
  ui.info ui.color("Created encrypted data bag item at #{write_path}", :green)
end
run() click to toggle source
# File lib/chef/knife/hitori_data_bag_enc.rb, line 19
def run
  update_environment(config[:environment]) if config[:environment]
  config[:bag], config[:item] = @name_args
  exit 1 unless validate

  create_data_bag
end
secret_file_path() click to toggle source
# File lib/chef/knife/hitori_data_bag_enc.rb, line 48
def secret_file_path
  config[:secret_file] || Chef::Config[:encrypted_data_bag_secret]
end
validate() click to toggle source
# File lib/chef/knife/hitori_data_bag_enc.rb, line 52
def validate
  if config[:bag].nil? || config[:item].nil?
    ui.error('Please specify BAG and ITEM')
    return false
  end

  if config[:json_file].nil?
    ui.error('You have not provided a json file for encryption')
    return false
  end

  unless secret_file_path
    ui.error('Please specify EncryptKey by Chef Config "encrypted_data_bag_secret" or --secret-file')
    return false
  end

  return true
end