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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/chef/knife/helpers/winrm_knife_base.rb', line 35
def self.included(includer)
includer.class_eval do
@@ssl_warning_given = false
include Chef::Knife::WinrmBase
include Chef::Knife::WinrmSharedOptions
def validate_winrm_options!
winrm_auth_protocol = config[:winrm_authentication_protocol]
unless Chef::Knife::WinrmBase::WINRM_AUTH_PROTOCOL_LIST.include?(winrm_auth_protocol)
ui.error "Invalid value '#{winrm_auth_protocol}' for --winrm-authentication-protocol option."
ui.info "Valid values are #{Chef::Knife::WinrmBase::WINRM_AUTH_PROTOCOL_LIST.join(",")}."
exit 1
end
warn_no_ssl_peer_verification if resolve_no_ssl_peer_verification
end
def configure_session
validate_winrm_options!
resolve_session_options
resolve_target_nodes
session_from_list
end
def resolve_target_nodes
@list = case config[:manual]
when true
@name_args[0].split(" ")
when false
r = []
q = Chef::Search::Query.new
@action_nodes = q.search(:node, @name_args[0])[0]
@action_nodes.each do |item|
i = (item, config[:attribute])
r.push(i) unless i.nil?
end
r
end
if @list.length == 0
if @action_nodes.length == 0
ui.fatal("No nodes returned from search!")
else
ui.fatal("#{@action_nodes.length} #{@action_nodes.length > 1 ? "nodes" : "node"} found, " +
"but does not have the required attribute (#{config[:attribute]}) to establish the connection. " +
"Try setting another attribute to open the connection using --attribute.")
end
exit 10
end
end
def (data, nested_value_spec)
nested_value_spec.split(".").each do |attr|
if data.nil?
nil elsif data.respond_to?(attr.to_sym)
data = data.send(attr.to_sym)
elsif data.respond_to?(:[])
data = data[attr]
else
data = begin
data.send(attr.to_sym)
rescue NoMethodError
nil
end
end
end
( !data.is_a?(Array) && data.respond_to?(:to_hash) ) ? data.to_hash : data
end
def run_command(command = "")
relay_winrm_command(command)
check_for_errors!
@exit_code
end
def relay_winrm_command(command)
Chef::Log.debug(command)
@session_results = []
queue = Queue.new
@winrm_sessions.each { |s| queue << s }
num_sessions = config[:concurrency]
num_targets = @winrm_sessions.length
num_sessions = (num_sessions.nil? || num_sessions == 0) ? num_targets : [num_sessions, num_targets].min
num_sessions.times { queue << nil }
threads = []
num_sessions.times do
threads << Thread.new do
while session = queue.pop
run_command_in_thread(session, command)
end
end
end
threads.map(&:join)
@session_results
end
private
def run_command_in_thread(s, command)
@session_results << s.relay_command(command)
rescue WinRM::WinRMHTTPTransportError, WinRM::WinRMAuthorizationError => e
if authorization_error?(e)
unless config[:suppress_auth_failure]
ui.error "Failed to authenticate to #{s.host} as #{config[:winrm_user]}"
ui.info "Response: #{e.message}"
ui.info get_failed_authentication_hint
raise e
end
else
raise e
end
end
def get_failed_authentication_hint
if @session_opts[:basic_auth_only]
FAILED_BASIC_HINT
else
FAILED_NOT_BASIC_HINT
end
end
def authorization_error?(exception)
exception.is_a?(WinRM::WinRMAuthorizationError) ||
exception.message =~ /401/
end
def check_for_errors!
@exit_code ||= 0
@winrm_sessions.each do |session|
session_exit_code = session.exit_code
unless success_return_codes.include? session_exit_code.to_i
@exit_code = [@exit_code, session_exit_code.to_i].max
ui.error "Failed to execute command on #{session.host} return code #{session_exit_code}"
end
end
end
def success_return_codes
return [0] unless config[:returns]
@success_return_codes ||= config[:returns].split(",").collect(&:to_i)
end
def session_from_list
@list.each do |item|
Chef::Log.debug("Adding #{item}")
@session_opts[:host] = item
create_winrm_session(@session_opts)
end
end
def create_winrm_session(options = {})
session = Chef::Knife::WinrmSession.new(options)
@winrm_sessions ||= []
@winrm_sessions.push(session)
end
def resolve_session_options
config[:winrm_port] ||= ( config[:winrm_transport] == "ssl" ) ? "5986" : "5985"
@session_opts = {
user: resolve_winrm_user,
password: config[:winrm_password],
port: config[:winrm_port],
operation_timeout: resolve_winrm_session_timeout,
basic_auth_only: resolve_winrm_basic_auth,
disable_sspi: resolve_winrm_disable_sspi,
transport: resolve_winrm_transport,
no_ssl_peer_verification: resolve_no_ssl_peer_verification,
ssl_peer_fingerprint: resolve_ssl_peer_fingerprint,
shell: config[:winrm_shell],
codepage: config[:winrm_codepage],
}
if @session_opts[:user] && (not @session_opts[:password])
@session_opts[:password] = config[:winrm_password] = get_password
end
if @session_opts[:transport] == :kerberos
@session_opts.merge!(resolve_winrm_kerberos_options)
end
@session_opts[:ca_trust_path] = config[:ca_trust_file] if config[:ca_trust_file]
end
def resolve_winrm_user
user = config[:winrm_user]
if resolve_winrm_basic_auth ||
resolve_winrm_transport == :kerberos ||
user.include?("\\") ||
user.include?("@")
user
else
".\\#{user}"
end
end
def resolve_winrm_session_timeout
config[:session_timeout].to_i * 60 if config[:session_timeout]
end
def resolve_winrm_basic_auth
config[:winrm_authentication_protocol] == "basic"
end
def resolve_winrm_kerberos_options
kerberos_opts = {}
kerberos_opts[:keytab] = config[:kerberos_keytab_file] if config[:kerberos_keytab_file]
kerberos_opts[:realm] = config[:kerberos_realm] if config[:kerberos_realm]
kerberos_opts[:service] = config[:kerberos_service] if config[:kerberos_service]
kerberos_opts
end
def resolve_winrm_transport
transport = config[:winrm_transport].to_sym
if config.any? { |k, v| k.to_s =~ /kerberos/ && !v.nil? }
transport = :kerberos
elsif transport != :ssl && negotiate_auth?
transport = :negotiate
end
transport
end
def resolve_no_ssl_peer_verification
config[:ca_trust_file].nil? && config[:winrm_ssl_verify_mode] == :verify_none && resolve_winrm_transport == :ssl
end
def resolve_ssl_peer_fingerprint
config[:ssl_peer_fingerprint]
end
def resolve_winrm_disable_sspi
resolve_winrm_transport != :negotiate
end
def get_password
@password ||= ui.ask("Enter your password: ") { |q| q.echo = false }
end
def negotiate_auth?
config[:winrm_authentication_protocol] == "negotiate"
end
def warn_no_ssl_peer_verification
unless @@ssl_warning_given
@@ssl_warning_given = true
ui.warn(<<~WARN)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
SSL validation of HTTPS requests for the WinRM transport is disabled. HTTPS WinRM
connections are still encrypted, but knife is not able to detect forged replies
or spoofing attacks.
To fix this issue add an entry like this to your knife configuration file:
```
# Verify all WinRM HTTPS connections (default, recommended)
knife[:winrm_ssl_verify_mode] = :verify_peer
```
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
WARN
end
end
end
end
|