Class: Chef::Knife::SecureBagEdit

Inherits:
Knife::DataBagEdit
  • Object
show all
Includes:
SecureDataBag::BaseMixin, SecureDataBag::DefaultsMixin, SecureDataBag::ExportMixin, SecureDataBag::SecretsMixin
Defined in:
lib/chef/knife/secure_bag_edit.rb

Instance Method Summary collapse

Instance Method Details

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/knife/secure_bag_edit.rb', line 18

def run
  if @name_args.length != 2
    stdout.puts 'You must supply the data bag and an item to edit.'
    stdout.puts opt_parser
    exit 1
  end

  config_defaults_for_data_bag!(@name_args[0])

  # Load the SecureBagItem, EncryptedDataBagItem or DataBagItem
  item = load_item(@name_args[0], @name_args[1], )
   = item..dup
  item.encryption_format = 'plain'

  # Allow the user to modify the content
  data = item.to_hash(metadata: true)
  data[::SecureDataBag::METADATA_KEY] = 

  # Edit the hash
  edited_item = edit_hash(data)
   = edited_item.delete(::SecureDataBag::METADATA_KEY)

  # Generate a new SecureBagItem
  item_to_save = ::SecureDataBag::Item
                 .from_hash(edited_item, )
  item_to_save.data_bag @name_args[0]
  item_to_save['id'] = @name_args[1]

  item_to_save.save
  stdout.puts("Saved as #{@name_args[0]}[#{@name_args[1]}]")

  export!(@name_args[0], @name_args[1], item_to_save) if should_export?

  if config[:print_after]
    data_to_print = item_to_save.to_hash
    stdout.puts(Chef::JSONCompat.to_json_pretty(data_to_print))
  end
end