Module: Chef::Knife::SecureBagBase

Included in:
SecureBagEdit, SecureBagFromFile, SecureBagShow
Defined in:
lib/chef/knife/secure_bag_base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chef/knife/secure_bag_base.rb', line 7

def self.included(includer)
  includer.class_eval do
    deps do
      require 'secure_data_bag'
    end
  
    option :secret,
      short:  "-s SECRET",
      long:   "--secret",
      description: "The secret key to use to encrypt data bag item values"

    option :secret_file,
      long: "--secret-file SECRET_FILE",
      description: "A file containing a secret key to use to encrypt data bag item values"

    option :encoded_fields,
      long: "--encoded-fields FIELD1,FIELD2,FIELD3",
      description: "List of attribute keys for which to encode values",
      proc: Proc.new { |s| s.split(',') }
  end
end

Instance Method Details

#data_for_create(hash = {}) ⇒ Object



61
62
63
64
# File 'lib/chef/knife/secure_bag_base.rb', line 61

def data_for_create(hash={})
  hash[:id] = @data_bag_item_name
  hash
end

#data_for_save(hash) ⇒ Object



66
67
68
69
# File 'lib/chef/knife/secure_bag_base.rb', line 66

def data_for_save(hash)
  @encoded_fields = hash.delete(:_encoded_fields)
  hash
end

#encoded_fieldsObject



29
30
31
32
# File 'lib/chef/knife/secure_bag_base.rb', line 29

def encoded_fields
  config[:encoded_fields] || 
    Chef::Config[:knife][:secure_data_bag][:fields]
end

#read_secretObject



47
48
49
50
51
# File 'lib/chef/knife/secure_bag_base.rb', line 47

def read_secret
  if config[:secret] then config[:secret]
  else SecureDataBag::Item.load_secret(secret_file)
  end
end

#require_secretObject



53
54
55
56
57
58
59
# File 'lib/chef/knife/secure_bag_base.rb', line 53

def require_secret
  if not secret
    show_usage
    ui.fatal("A secret or secret_file must be specified")
    exit 1
  end
end

#secretObject



39
40
41
# File 'lib/chef/knife/secure_bag_base.rb', line 39

def secret
  @secret ||= read_secret
end

#secret_fileObject



34
35
36
37
# File 'lib/chef/knife/secure_bag_base.rb', line 34

def secret_file
  config[:secret_file] ||
    SecureDataBag::Item.secret_path
end

#use_encryptionObject



43
44
45
# File 'lib/chef/knife/secure_bag_base.rb', line 43

def use_encryption
  true
end