Class: Chef::Knife::DataBagShow

Inherits:
Chef::Knife show all
Includes:
DataBagSecretOptions
Defined in:
lib/chef/knife/data_bag_show.rb

Instance Attribute Summary

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods included from DataBagSecretOptions

#encryption_secret_provided?, #encryption_secret_provided_ignore_encrypt_flag?, included, #read_secret, #validate_secrets

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_loader, subcommands, subcommands_by_category, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::PathSanity

#enforce_path_sanity

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Method Details

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chef/knife/data_bag_show.rb', line 36

def run
  display = case @name_args.length
  when 2 # Bag and Item names provided
    secret = encryption_secret_provided_ignore_encrypt_flag? ? read_secret : nil
    raw_data = Chef::DataBagItem.load(@name_args[0], @name_args[1]).raw_data
    encrypted = encrypted?(raw_data)

    if encrypted && secret
      # Users do not need to pass --encrypt to read data, we simply try to use the provided secret
      ui.info("Encrypted data bag detected, decrypting with provided secret.")
      raw = Chef::EncryptedDataBagItem.load(@name_args[0],
                                            @name_args[1],
                                            secret)
      format_for_display(raw.to_hash)
    elsif encrypted && !secret
      ui.warn("Encrypted data bag detected, but no secret provided for decoding.  Displaying encrypted data.")
      format_for_display(raw_data)
    else
      ui.info("Unencrypted data bag detected, ignoring any provided secret options.")
      format_for_display(raw_data)
    end

  when 1 # Only Bag name provided
    format_list_for_display(Chef::DataBag.load(@name_args[0]))
  else
    stdout.puts opt_parser
    exit(1)
  end
  output(display)
end