Module: Chef::Knife::Core::EncryptedAttributeEditorOptions

Included in:
EncryptedAttributeCreate, EncryptedAttributeEdit, EncryptedAttributeUpdate
Defined in:
lib/chef/knife/core/encrypted_attribute_editor_options.rb

Overview

Reads knife encrypted attribute edit commands arguments

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object

Reopens EncryptedAttributeEditorOptions class to define knife argument options.

Parameters:

  • includer (Class)

    includer class.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 31

def self.included(includer)
  includer.class_eval do
    # Helper method to set the encrypted attributes configuration.
    def self.encrypted_attributes_option_set(key, value)
      Chef::Config[:knife][:encrypted_attributes][key] = value
    end

    # Helper method to add a value to an encrypted configuration array
    # option.
    def self.encrypted_attributes_option_push(key, value)
      unless Chef::Config[:knife][:encrypted_attributes][key]
             .is_a?(Array)
        Chef::Config[:knife][:encrypted_attributes][key] = []
      end
      Chef::Config[:knife][:encrypted_attributes][key] << value
    end

    option :encrypted_attribute_version,
           long: '--encrypted-attribute-version VERSION',
           description: 'Encrypted Attribute protocol version to use',
           proc: ->(i) { encrypted_attributes_option_set(:version, i) }

    option :encrypted_attribute_partial_search,
           short: '-P',
           long: '--disable-partial-search',
           description: 'Disable partial search',
           boolean: true,
           proc:
            (lambda do |_i|
              encrypted_attributes_option_set(:partial_search, false)
            end)

    option :encrypted_attribute_client_search,
           short: '-C CLIENT_SEARCH_QUERY',
           long: '--client-search CLIENT_SEARCH_QUERY',
           description:
             'Client search query. Can be specified multiple times',
           proc:
             (lambda do |i|
               encrypted_attributes_option_push(:client_search, i)
             end)

    option :encrypted_attribute_node_search,
           short: '-N NODE_SEARCH_QUERY',
           long: '--node-search NODE_SEARCH_QUERY',
           description:
             'Node search query. Can be specified multiple times',
           proc:
             ->(i) { encrypted_attributes_option_push(:node_search, i) }

    option :encrypted_attribute_users,
           short: '-U USER',
           long: '--encrypted-attribute-user USER',
           description:
             'User name to allow access to. Can be specified multiple '\
             'times',
           proc: ->(i) { encrypted_attributes_option_push(:users, i) }

    # TODO: option :keys
  end # includer.class_eval
end

Instance Method Details

#edit_data(data = nil, format = 'plain') ⇒ String

Edits data using an editor.

Modified Chef::Knife::UI#edit_data method with plain text format support

Parameters:

  • data (String) (defaults to: nil)

    default data value to edit.

  • format (String) (defaults to: 'plain')

    'plain' or 'json'.

Returns:

  • (String)

    resulting data value after edition.

Raises:

  • (RuntimeError)

    if the editing command fails.


164
165
166
167
168
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 164

def edit_data(data = nil, format = 'plain')
  output = edit_data_string_to_obj(data, format)
  output = edit_data_run_editor(output)
  edit_data_obj_to_string(output, format)
end

#edit_data_obj_to_string(data, format) ⇒ String

Converts Ruby values to string.

Parameters:

  • data (Mixed)

    Ruby value to convert.

  • format (String)

    'plain' or 'json'.

Returns:

  • (String)

    value encoded as string.


116
117
118
119
120
121
122
123
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 116

def edit_data_obj_to_string(data, format)
  case format
  when 'JSON', 'json'
    YAJL_NAMESPACE::Parser.parse(data)
  else
    data
  end
end

#edit_data_run_editor(data) ⇒ String

Edits a data using the system editor.

Creates a temporal file to edit the data value.

Parameters:

  • data (String)

    data to edit.

Returns:

  • (String)

    the data after the editing.

Raises:

  • (RuntimeError)

    if the editing command fails.


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 142

def edit_data_run_editor(data)
  return if config[:disable_editing]
  result = nil
  Tempfile.open(%w(knife-edit- .json)) do |tf|
    tf.sync = true
    tf.puts(data)
    tf.close
    edit_data_run_editor_command(tf.path)
    result = IO.read(tf.path)
  end
  result
end

#edit_data_run_editor_command(path) ⇒ Object

Runs system editor.

Parameters:

  • path (String)

    path file to edit.

Returns:

  • void

Raises:

  • (RuntimeError)

    if the editing command fails.


130
131
132
133
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 130

def edit_data_run_editor_command(path)
  return if system("#{config[:editor]} #{path}")
  fail 'Please set EDITOR environment variable'
end

#edit_data_string_to_obj(data, format) ⇒ String, Mixed

Converts a string data to a Ruby value.

Parameters:

  • data (String)

    data to convert.

  • format (String)

    'plain' or 'json'.

Returns:

  • (String, Mixed)

    Ruby value.


98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/chef/knife/core/encrypted_attribute_editor_options.rb', line 98

def edit_data_string_to_obj(data, format)
  case format
  when 'JSON', 'json'
    if data.nil?
      {}
    else
      Chef::JSONCompat.to_json_pretty(data, quirks_mode: true)
    end
  else
    data.nil? ? '' : data
  end
end