chef-config 14.9.13 → 14.10.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +1,34 @@
1
- # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
- # License:: Apache License, Version 2.0
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
-
16
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
- # NOTE: This file is generated by running `rake version` in the top level of
18
- # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
- # task instead.
20
- # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
-
22
- module ChefConfig
23
- CHEFCONFIG_ROOT = File.expand_path("../..", __FILE__)
24
- VERSION = "14.9.13".freeze
25
- end
26
-
27
- #
28
- # NOTE: the Chef::Version class is defined in version_class.rb
29
- #
30
- # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
- # Chef::Version class is for _cookbooks_ only, and cannot handle
32
- # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
- # Gem::Version class instead.
34
- #
1
+ # Copyright:: Copyright 2010-2016, Chef Software, Inc.
2
+ # License:: Apache License, Version 2.0
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17
+ # NOTE: This file is generated by running `rake version` in the top level of
18
+ # this repo. Do not edit this manually. Edit the VERSION file and run the rake
19
+ # task instead.
20
+ # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21
+
22
+ module ChefConfig
23
+ CHEFCONFIG_ROOT = File.expand_path("../..", __FILE__)
24
+ VERSION = "14.10.9".freeze
25
+ end
26
+
27
+ #
28
+ # NOTE: the Chef::Version class is defined in version_class.rb
29
+ #
30
+ # NOTE: DO NOT Use the Chef::Version class on ChefConfig::VERSIONs. The
31
+ # Chef::Version class is for _cookbooks_ only, and cannot handle
32
+ # pre-release versions like "10.14.0.rc.2". Please use Rubygem's
33
+ # Gem::Version class instead.
34
+ #
@@ -1,28 +1,28 @@
1
- #
2
- # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- module ChefConfig
19
-
20
- def self.windows?
21
- if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
- true
23
- else
24
- false
25
- end
26
- end
27
-
28
- end
1
+ #
2
+ # Copyright:: Copyright 2015-2016, Chef Software, Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ module ChefConfig
19
+
20
+ def self.windows?
21
+ if RUBY_PLATFORM =~ /mswin|mingw|windows/
22
+ true
23
+ else
24
+ false
25
+ end
26
+ end
27
+
28
+ end
@@ -1,281 +1,281 @@
1
- #
2
- # Author:: Daniel DeLeo (<dan@chef.io>)
3
- # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require "chef-config/config"
20
- require "chef-config/exceptions"
21
- require "chef-config/logger"
22
- require "chef-config/path_helper"
23
- require "chef-config/windows"
24
- require "chef-config/mixin/dot_d"
25
- require "chef-config/mixin/credentials"
26
-
27
- module ChefConfig
28
- class WorkstationConfigLoader
29
- include ChefConfig::Mixin::DotD
30
- include ChefConfig::Mixin::Credentials
31
-
32
- # Path to a config file requested by user, (e.g., via command line option). Can be nil
33
- attr_accessor :explicit_config_file
34
- # The name of a credentials profile. Can be nil
35
- attr_accessor :profile
36
- attr_reader :credentials_found
37
-
38
- # TODO: initialize this with a logger for Chef and Knife
39
- def initialize(explicit_config_file, logger = nil, profile: nil)
40
- @explicit_config_file = explicit_config_file
41
- @chef_config_dir = nil
42
- @config_location = nil
43
- @profile = profile
44
- @logger = logger || NullLogger.new
45
- @credentials_found = false
46
- end
47
-
48
- def no_config_found?
49
- config_location.nil? && !credentials_found
50
- end
51
-
52
- def config_location
53
- @config_location ||= (explicit_config_file || locate_local_config)
54
- end
55
-
56
- def chef_config_dir
57
- if @chef_config_dir.nil?
58
- @chef_config_dir = false
59
- full_path = working_directory.split(File::SEPARATOR)
60
- (full_path.length - 1).downto(0) do |i|
61
- candidate_directory = File.join(full_path[0..i] + [".chef"])
62
- if File.exist?(candidate_directory) && File.directory?(candidate_directory)
63
- @chef_config_dir = candidate_directory
64
- break
65
- end
66
- end
67
- end
68
- @chef_config_dir
69
- end
70
-
71
- def load
72
- load_credentials(profile)
73
- # Ignore it if there's no explicit_config_file and can't find one at a
74
- # default path.
75
- unless config_location.nil?
76
- if explicit_config_file && !path_exists?(config_location)
77
- raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
78
- end
79
-
80
- # Have to set Config.config_file b/c other config is derived from it.
81
- Config.config_file = config_location
82
- apply_config(IO.read(config_location), config_location)
83
- end
84
-
85
- load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir]
86
-
87
- apply_defaults
88
- end
89
-
90
- # (Private API, public for test purposes)
91
- def env
92
- ENV
93
- end
94
-
95
- # (Private API, public for test purposes)
96
- def path_exists?(path)
97
- Pathname.new(path).expand_path.exist?
98
- end
99
-
100
- private
101
-
102
- def have_config?(path)
103
- if path_exists?(path)
104
- logger.info("Using config at #{path}")
105
- true
106
- else
107
- logger.debug("Config not found at #{path}, trying next option")
108
- false
109
- end
110
- end
111
-
112
- def locate_local_config
113
- candidate_configs = []
114
-
115
- # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
116
- if env["KNIFE_HOME"]
117
- candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
118
- candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
119
- end
120
- # Look for $PWD/knife.rb
121
- if Dir.pwd
122
- candidate_configs << File.join(Dir.pwd, "config.rb")
123
- candidate_configs << File.join(Dir.pwd, "knife.rb")
124
- end
125
- # Look for $UPWARD/.chef/knife.rb
126
- if chef_config_dir
127
- candidate_configs << File.join(chef_config_dir, "config.rb")
128
- candidate_configs << File.join(chef_config_dir, "knife.rb")
129
- end
130
- # Look for $HOME/.chef/knife.rb
131
- PathHelper.home(".chef") do |dot_chef_dir|
132
- candidate_configs << File.join(dot_chef_dir, "config.rb")
133
- candidate_configs << File.join(dot_chef_dir, "knife.rb")
134
- end
135
-
136
- candidate_configs.find do |candidate_config|
137
- have_config?(candidate_config)
138
- end
139
- end
140
-
141
- def working_directory
142
- a = if ChefConfig.windows?
143
- env["CD"]
144
- else
145
- env["PWD"]
146
- end || Dir.pwd
147
-
148
- a
149
- end
150
-
151
- def apply_credentials(creds, profile)
152
- # Store the profile used in case other things want it.
153
- Config.profile ||= profile
154
- # Validate the credentials data.
155
- if creds.key?("node_name") && creds.key?("client_name")
156
- raise ChefConfig::ConfigurationError, "Do not specify both node_name and client_name. You should prefer client_name."
157
- end
158
- # Load credentials data into the Chef configuration.
159
- creds.each do |key, value|
160
- case key.to_s
161
- when "client_name"
162
- # Special case because it's weird to set your username via `node_name`.
163
- Config.node_name = value
164
- when "validation_key", "validator_key"
165
- extract_key(value, :validation_key, :validation_key_contents)
166
- when "client_key"
167
- extract_key(value, :client_key, :client_key_contents)
168
- when "knife"
169
- Config.knife.merge!(Hash[value.map { |k, v| [k.to_sym, v] }])
170
- else
171
- Config[key.to_sym] = value
172
- end
173
- end
174
- @credentials_found = true
175
- end
176
-
177
- def extract_key(key_value, config_path, config_contents)
178
- if key_value.start_with?("-----BEGIN RSA PRIVATE KEY-----")
179
- Config.send(config_contents, key_value)
180
- else
181
- abs_path = Pathname.new(key_value).expand_path(home_chef_dir)
182
- Config.send(config_path, abs_path)
183
- end
184
- end
185
-
186
- def home_chef_dir
187
- @home_chef_dir ||= PathHelper.home(".chef")
188
- end
189
-
190
- def apply_config(config_content, config_file_path)
191
- Config.from_string(config_content, config_file_path)
192
- rescue SignalException
193
- raise
194
- rescue SyntaxError => e
195
- message = ""
196
- message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
197
- message << "#{e.class.name}: #{e.message}\n"
198
- if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
199
- line = file_line[/:([\d]+)$/, 1].to_i
200
- message << highlight_config_error(config_file_path, line)
201
- end
202
- raise ChefConfig::ConfigurationError, message
203
- rescue Exception => e
204
- message = "You have an error in your config file #{config_file_path}\n\n"
205
- message << "#{e.class.name}: #{e.message}\n"
206
- filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
207
- filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
208
- unless filtered_trace.empty?
209
- line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
210
- message << highlight_config_error(config_file_path, line_nr.to_i)
211
- end
212
- raise ChefConfig::ConfigurationError, message
213
- end
214
-
215
- # Apply default configuration values for workstation-style tools.
216
- #
217
- # Global defaults should go in {ChefConfig::Config} instead, this is only
218
- # for things like `knife` and `chef`.
219
- #
220
- # @api private
221
- # @since 14.3
222
- # @return [void]
223
- def apply_defaults
224
- # If we don't have a better guess use the username.
225
- Config[:node_name] ||= Etc.getlogin
226
- # If we don't have a key (path or inline) check user.pem and $node_name.pem.
227
- unless Config.key?(:client_key) || Config.key?(:client_key_contents)
228
- key_path = find_default_key(["#{Config[:node_name]}.pem", "user.pem"])
229
- Config[:client_key] = key_path if key_path
230
- end
231
- # Similarly look for a validation key file, though this should be less
232
- # common these days.
233
- unless Config.key?(:validation_key) || Config.key?(:validation_key_contents)
234
- key_path = find_default_key(["#{Config[:validation_client_name]}.pem", "validator.pem", "validation.pem"])
235
- Config[:validation_key] = key_path if key_path
236
- end
237
- end
238
-
239
- # Look for a default key file.
240
- #
241
- # This searches for any of a list of possible default keys, checking both
242
- # the local `.chef/` folder and the home directory `~/.chef/`. Returns `nil`
243
- # if no matching file is found.
244
- #
245
- # @api private
246
- # @since 14.3
247
- # @param key_names [Array<String>] A list of possible filenames to check for.
248
- # The first one found will be returned.
249
- # @return [String, nil]
250
- def find_default_key(key_names)
251
- key_names.each do |filename|
252
- path = Pathname.new(filename)
253
- # If we have a config location (like ./.chef/), look there first.
254
- if config_location
255
- local_path = path.expand_path(File.dirname(config_location))
256
- return local_path.to_s if local_path.exist?
257
- end
258
- # Then check ~/.chef.
259
- home_path = path.expand_path(home_chef_dir)
260
- return home_path.to_s if home_path.exist?
261
- end
262
- nil
263
- end
264
-
265
- def highlight_config_error(file, line)
266
- config_file_lines = []
267
- IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
268
- if line == 1
269
- lines = config_file_lines[0..3]
270
- else
271
- lines = config_file_lines[Range.new(line - 2, line)]
272
- end
273
- "Relevant file content:\n" + lines.join("\n") + "\n"
274
- end
275
-
276
- def logger
277
- @logger
278
- end
279
-
280
- end
281
- end
1
+ #
2
+ # Author:: Daniel DeLeo (<dan@chef.io>)
3
+ # Copyright:: Copyright 2014-2016, Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require "chef-config/config"
20
+ require "chef-config/exceptions"
21
+ require "chef-config/logger"
22
+ require "chef-config/path_helper"
23
+ require "chef-config/windows"
24
+ require "chef-config/mixin/dot_d"
25
+ require "chef-config/mixin/credentials"
26
+
27
+ module ChefConfig
28
+ class WorkstationConfigLoader
29
+ include ChefConfig::Mixin::DotD
30
+ include ChefConfig::Mixin::Credentials
31
+
32
+ # Path to a config file requested by user, (e.g., via command line option). Can be nil
33
+ attr_accessor :explicit_config_file
34
+ # The name of a credentials profile. Can be nil
35
+ attr_accessor :profile
36
+ attr_reader :credentials_found
37
+
38
+ # TODO: initialize this with a logger for Chef and Knife
39
+ def initialize(explicit_config_file, logger = nil, profile: nil)
40
+ @explicit_config_file = explicit_config_file
41
+ @chef_config_dir = nil
42
+ @config_location = nil
43
+ @profile = profile
44
+ @logger = logger || NullLogger.new
45
+ @credentials_found = false
46
+ end
47
+
48
+ def no_config_found?
49
+ config_location.nil? && !credentials_found
50
+ end
51
+
52
+ def config_location
53
+ @config_location ||= (explicit_config_file || locate_local_config)
54
+ end
55
+
56
+ def chef_config_dir
57
+ if @chef_config_dir.nil?
58
+ @chef_config_dir = false
59
+ full_path = working_directory.split(File::SEPARATOR)
60
+ (full_path.length - 1).downto(0) do |i|
61
+ candidate_directory = File.join(full_path[0..i] + [".chef"])
62
+ if File.exist?(candidate_directory) && File.directory?(candidate_directory)
63
+ @chef_config_dir = candidate_directory
64
+ break
65
+ end
66
+ end
67
+ end
68
+ @chef_config_dir
69
+ end
70
+
71
+ def load
72
+ load_credentials(profile)
73
+ # Ignore it if there's no explicit_config_file and can't find one at a
74
+ # default path.
75
+ unless config_location.nil?
76
+ if explicit_config_file && !path_exists?(config_location)
77
+ raise ChefConfig::ConfigurationError, "Specified config file #{config_location} does not exist"
78
+ end
79
+
80
+ # Have to set Config.config_file b/c other config is derived from it.
81
+ Config.config_file = config_location
82
+ apply_config(IO.read(config_location), config_location)
83
+ end
84
+
85
+ load_dot_d(Config[:config_d_dir]) if Config[:config_d_dir]
86
+
87
+ apply_defaults
88
+ end
89
+
90
+ # (Private API, public for test purposes)
91
+ def env
92
+ ENV
93
+ end
94
+
95
+ # (Private API, public for test purposes)
96
+ def path_exists?(path)
97
+ Pathname.new(path).expand_path.exist?
98
+ end
99
+
100
+ private
101
+
102
+ def have_config?(path)
103
+ if path_exists?(path)
104
+ logger.info("Using config at #{path}")
105
+ true
106
+ else
107
+ logger.debug("Config not found at #{path}, trying next option")
108
+ false
109
+ end
110
+ end
111
+
112
+ def locate_local_config
113
+ candidate_configs = []
114
+
115
+ # Look for $KNIFE_HOME/knife.rb (allow multiple knives config on same machine)
116
+ if env["KNIFE_HOME"]
117
+ candidate_configs << File.join(env["KNIFE_HOME"], "config.rb")
118
+ candidate_configs << File.join(env["KNIFE_HOME"], "knife.rb")
119
+ end
120
+ # Look for $PWD/knife.rb
121
+ if Dir.pwd
122
+ candidate_configs << File.join(Dir.pwd, "config.rb")
123
+ candidate_configs << File.join(Dir.pwd, "knife.rb")
124
+ end
125
+ # Look for $UPWARD/.chef/knife.rb
126
+ if chef_config_dir
127
+ candidate_configs << File.join(chef_config_dir, "config.rb")
128
+ candidate_configs << File.join(chef_config_dir, "knife.rb")
129
+ end
130
+ # Look for $HOME/.chef/knife.rb
131
+ PathHelper.home(".chef") do |dot_chef_dir|
132
+ candidate_configs << File.join(dot_chef_dir, "config.rb")
133
+ candidate_configs << File.join(dot_chef_dir, "knife.rb")
134
+ end
135
+
136
+ candidate_configs.find do |candidate_config|
137
+ have_config?(candidate_config)
138
+ end
139
+ end
140
+
141
+ def working_directory
142
+ a = if ChefConfig.windows?
143
+ env["CD"]
144
+ else
145
+ env["PWD"]
146
+ end || Dir.pwd
147
+
148
+ a
149
+ end
150
+
151
+ def apply_credentials(creds, profile)
152
+ # Store the profile used in case other things want it.
153
+ Config.profile ||= profile
154
+ # Validate the credentials data.
155
+ if creds.key?("node_name") && creds.key?("client_name")
156
+ raise ChefConfig::ConfigurationError, "Do not specify both node_name and client_name. You should prefer client_name."
157
+ end
158
+ # Load credentials data into the Chef configuration.
159
+ creds.each do |key, value|
160
+ case key.to_s
161
+ when "client_name"
162
+ # Special case because it's weird to set your username via `node_name`.
163
+ Config.node_name = value
164
+ when "validation_key", "validator_key"
165
+ extract_key(value, :validation_key, :validation_key_contents)
166
+ when "client_key"
167
+ extract_key(value, :client_key, :client_key_contents)
168
+ when "knife"
169
+ Config.knife.merge!(Hash[value.map { |k, v| [k.to_sym, v] }])
170
+ else
171
+ Config[key.to_sym] = value
172
+ end
173
+ end
174
+ @credentials_found = true
175
+ end
176
+
177
+ def extract_key(key_value, config_path, config_contents)
178
+ if key_value.start_with?("-----BEGIN RSA PRIVATE KEY-----")
179
+ Config.send(config_contents, key_value)
180
+ else
181
+ abs_path = Pathname.new(key_value).expand_path(home_chef_dir)
182
+ Config.send(config_path, abs_path)
183
+ end
184
+ end
185
+
186
+ def home_chef_dir
187
+ @home_chef_dir ||= PathHelper.home(".chef")
188
+ end
189
+
190
+ def apply_config(config_content, config_file_path)
191
+ Config.from_string(config_content, config_file_path)
192
+ rescue SignalException
193
+ raise
194
+ rescue SyntaxError => e
195
+ message = ""
196
+ message << "You have invalid ruby syntax in your config file #{config_file_path}\n\n"
197
+ message << "#{e.class.name}: #{e.message}\n"
198
+ if file_line = e.message[/#{Regexp.escape(config_file_path)}:[\d]+/]
199
+ line = file_line[/:([\d]+)$/, 1].to_i
200
+ message << highlight_config_error(config_file_path, line)
201
+ end
202
+ raise ChefConfig::ConfigurationError, message
203
+ rescue Exception => e
204
+ message = "You have an error in your config file #{config_file_path}\n\n"
205
+ message << "#{e.class.name}: #{e.message}\n"
206
+ filtered_trace = e.backtrace.grep(/#{Regexp.escape(config_file_path)}/)
207
+ filtered_trace.each { |bt_line| message << " " << bt_line << "\n" }
208
+ unless filtered_trace.empty?
209
+ line_nr = filtered_trace.first[/#{Regexp.escape(config_file_path)}:([\d]+)/, 1]
210
+ message << highlight_config_error(config_file_path, line_nr.to_i)
211
+ end
212
+ raise ChefConfig::ConfigurationError, message
213
+ end
214
+
215
+ # Apply default configuration values for workstation-style tools.
216
+ #
217
+ # Global defaults should go in {ChefConfig::Config} instead, this is only
218
+ # for things like `knife` and `chef`.
219
+ #
220
+ # @api private
221
+ # @since 14.3
222
+ # @return [void]
223
+ def apply_defaults
224
+ # If we don't have a better guess use the username.
225
+ Config[:node_name] ||= Etc.getlogin
226
+ # If we don't have a key (path or inline) check user.pem and $node_name.pem.
227
+ unless Config.key?(:client_key) || Config.key?(:client_key_contents)
228
+ key_path = find_default_key(["#{Config[:node_name]}.pem", "user.pem"])
229
+ Config[:client_key] = key_path if key_path
230
+ end
231
+ # Similarly look for a validation key file, though this should be less
232
+ # common these days.
233
+ unless Config.key?(:validation_key) || Config.key?(:validation_key_contents)
234
+ key_path = find_default_key(["#{Config[:validation_client_name]}.pem", "validator.pem", "validation.pem"])
235
+ Config[:validation_key] = key_path if key_path
236
+ end
237
+ end
238
+
239
+ # Look for a default key file.
240
+ #
241
+ # This searches for any of a list of possible default keys, checking both
242
+ # the local `.chef/` folder and the home directory `~/.chef/`. Returns `nil`
243
+ # if no matching file is found.
244
+ #
245
+ # @api private
246
+ # @since 14.3
247
+ # @param key_names [Array<String>] A list of possible filenames to check for.
248
+ # The first one found will be returned.
249
+ # @return [String, nil]
250
+ def find_default_key(key_names)
251
+ key_names.each do |filename|
252
+ path = Pathname.new(filename)
253
+ # If we have a config location (like ./.chef/), look there first.
254
+ if config_location
255
+ local_path = path.expand_path(File.dirname(config_location))
256
+ return local_path.to_s if local_path.exist?
257
+ end
258
+ # Then check ~/.chef.
259
+ home_path = path.expand_path(home_chef_dir)
260
+ return home_path.to_s if home_path.exist?
261
+ end
262
+ nil
263
+ end
264
+
265
+ def highlight_config_error(file, line)
266
+ config_file_lines = []
267
+ IO.readlines(file).each_with_index { |l, i| config_file_lines << "#{(i + 1).to_s.rjust(3)}: #{l.chomp}" }
268
+ if line == 1
269
+ lines = config_file_lines[0..3]
270
+ else
271
+ lines = config_file_lines[Range.new(line - 2, line)]
272
+ end
273
+ "Relevant file content:\n" + lines.join("\n") + "\n"
274
+ end
275
+
276
+ def logger
277
+ @logger
278
+ end
279
+
280
+ end
281
+ end