Module: Chef::Knife::SecureDataBag::ExportMixin

Defined in:
lib/chef/knife/secure_data_bag/export_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Steps to execute when the mixin is include. In this case specifically, add additional command line options related to exporting.

Since:

  • 3.0.0



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chef/knife/secure_data_bag/export_mixin.rb', line 11

def self.included(base)
  base.option :export,
    description: 'Whether to export the data_bag item',
    long: '--export',
    boolean: true

  base.option :export_format,
    description: 'Format to export the data_bag_item as. If unset, this will default to the encryption format.',
    long: '--export-format [plain|encrypted|nested]'

  base.option :export_root,
    long: '--export-root PATH',
    description: 'Path containing data_bag folders and items'
end

Instance Method Details

#export!(data_bag, item_name, item) ⇒ Object

Export the item to the filesystem.

Parameters:

  • data_bag (String)

    the data_bag to upload to

  • item_name (String)

    the data_bag_item id to upload to

  • item (SecureDataBag::Item)

    the item to upload

Since:

  • 3.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/knife/secure_data_bag/export_mixin.rb', line 43

def export!(data_bag, item_name, item)
  item.encryption_format = export_format
  data = item.to_hash(encrypt: true)

  if export_root.nil?
    ui.fatal('Export root is not defined')
    show_usage
    exit 1
  end

  export_file_path = export_path(data_bag, item_name)
  unless ::File.directory?(::File.dirname(export_file_path))
    ui.fatal("Export directory does not exist: #{export_file_path}")
    show_usage
    exit 1
  end

  ::File.open(export_file_path, 'w') do |f|
    f.write(Chef::JSONCompat.to_json_pretty(data))
  end

  display_path = export_file_path.sub(%r{/^#{export_root}/}, '')
  stdout.puts("Exported to #{display_path}")
end

#should_export?Boolean

Should knife subcommands save data_bag_items to disk after uploading them to the Chef server.

Returns:

  • (Boolean)

Since:

  • 3.0.0



30
31
32
33
34
35
36
# File 'lib/chef/knife/secure_data_bag/export_mixin.rb', line 30

def should_export?
  if config[:export].nil?
    Chef::Config[:knife][:secure_data_bag][:export_on_upload]
  else
    config[:export]
  end
end