knife-windows 0.8.2 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -13
- data/.gitignore +1 -0
- data/.travis.yml +16 -2
- data/CHANGELOG.md +38 -4
- data/DOC_CHANGES.md +331 -15
- data/Gemfile +2 -1
- data/README.md +306 -71
- data/RELEASE_NOTES.md +63 -43
- data/Rakefile +9 -4
- data/appveyor.yml +42 -0
- data/ci.gemfile +15 -0
- data/knife-windows.gemspec +6 -4
- data/lib/chef/knife/bootstrap/windows-chef-client-msi.erb +96 -71
- data/lib/chef/knife/bootstrap_windows_base.rb +219 -37
- data/lib/chef/knife/bootstrap_windows_ssh.rb +22 -5
- data/lib/chef/knife/bootstrap_windows_winrm.rb +24 -24
- data/lib/chef/knife/core/windows_bootstrap_context.rb +146 -12
- data/lib/chef/knife/knife_windows_base.rb +33 -0
- data/lib/chef/knife/windows_cert_generate.rb +155 -0
- data/lib/chef/knife/windows_cert_install.rb +68 -0
- data/lib/chef/knife/windows_helper.rb +3 -1
- data/lib/chef/knife/windows_listener_create.rb +107 -0
- data/lib/chef/knife/winrm.rb +103 -206
- data/lib/chef/knife/winrm_base.rb +30 -11
- data/lib/chef/knife/winrm_knife_base.rb +218 -0
- data/lib/chef/knife/winrm_session.rb +81 -0
- data/lib/chef/knife/winrm_shared_options.rb +47 -0
- data/lib/chef/knife/wsman_endpoint.rb +44 -0
- data/lib/chef/knife/wsman_test.rb +96 -0
- data/lib/knife-windows/path_helper.rb +234 -0
- data/lib/knife-windows/version.rb +1 -1
- data/spec/assets/win_template_rendered_with_bootstrap_install_command.txt +217 -0
- data/spec/assets/win_template_rendered_without_bootstrap_install_command.txt +329 -0
- data/spec/assets/win_template_unrendered.txt +246 -0
- data/spec/functional/bootstrap_download_spec.rb +129 -35
- data/spec/spec_helper.rb +28 -1
- data/spec/unit/knife/bootstrap_options_spec.rb +150 -0
- data/spec/unit/knife/bootstrap_template_spec.rb +27 -26
- data/spec/unit/knife/bootstrap_windows_winrm_spec.rb +166 -32
- data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +151 -0
- data/spec/unit/knife/windows_cert_generate_spec.rb +90 -0
- data/spec/unit/knife/windows_cert_install_spec.rb +51 -0
- data/spec/unit/knife/windows_listener_create_spec.rb +76 -0
- data/spec/unit/knife/winrm_session_spec.rb +56 -0
- data/spec/unit/knife/winrm_spec.rb +403 -118
- data/spec/unit/knife/wsman_test_spec.rb +176 -0
- metadata +67 -21
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Author:: Mukta Aphale (<mukta.aphale@clogeny.com>)
|
|
2
|
+
# Copyright:: Copyright (c) 2014 Opscode, 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
|
+
require 'chef/knife'
|
|
19
|
+
require 'chef/knife/winrm_base'
|
|
20
|
+
require 'openssl'
|
|
21
|
+
|
|
22
|
+
class Chef
|
|
23
|
+
class Knife
|
|
24
|
+
class WindowsListenerCreate < Knife
|
|
25
|
+
|
|
26
|
+
banner "knife windows listener create (options)"
|
|
27
|
+
|
|
28
|
+
option :cert_install,
|
|
29
|
+
:short => "-c CERT_PATH",
|
|
30
|
+
:long => "--cert-install CERT_PATH",
|
|
31
|
+
:description => "Adds specified certificate to the Windows Certificate Store's Local Machine personal store before creating listener."
|
|
32
|
+
|
|
33
|
+
option :port,
|
|
34
|
+
:short => "-p PORT",
|
|
35
|
+
:long => "--port PORT",
|
|
36
|
+
:description => "Specify port. Default is 5986",
|
|
37
|
+
:default => "5986"
|
|
38
|
+
|
|
39
|
+
option :hostname,
|
|
40
|
+
:short => "-h HOSTNAME",
|
|
41
|
+
:long => "--hostname HOSTNAME",
|
|
42
|
+
:description => "Hostname on the listener. Default is blank",
|
|
43
|
+
:default => ""
|
|
44
|
+
|
|
45
|
+
option :cert_thumbprint,
|
|
46
|
+
:short => "-t THUMBPRINT",
|
|
47
|
+
:long => "--cert-thumbprint THUMBPRINT",
|
|
48
|
+
:description => "Thumbprint of the certificate. Required only if --cert-install option is not used."
|
|
49
|
+
|
|
50
|
+
option :cert_passphrase,
|
|
51
|
+
:short => "-cp PASSWORD",
|
|
52
|
+
:long => "--cert-passphrase PASSWORD",
|
|
53
|
+
:description => "Password for certificate."
|
|
54
|
+
|
|
55
|
+
def get_cert_passphrase
|
|
56
|
+
print "Enter given certificate's passphrase (empty for no passphrase):"
|
|
57
|
+
passphrase = STDIN.gets
|
|
58
|
+
passphrase.strip
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def run
|
|
62
|
+
STDOUT.sync = STDERR.sync = true
|
|
63
|
+
|
|
64
|
+
if Chef::Platform.windows?
|
|
65
|
+
begin
|
|
66
|
+
if config[:cert_install]
|
|
67
|
+
config[:cert_passphrase] = get_cert_passphrase unless config[:cert_passphrase]
|
|
68
|
+
result = %x{powershell.exe -Command " '#{config[:cert_passphrase]}' | certutil -importPFX '#{config[:cert_install]}' AT_KEYEXCHANGE"}
|
|
69
|
+
if $?.exitstatus
|
|
70
|
+
ui.info "Certificate installed to Certificate Store"
|
|
71
|
+
result = %x{powershell.exe -Command " echo (Get-PfxCertificate #{config[:cert_install]}).thumbprint "}
|
|
72
|
+
ui.info "Certificate Thumbprint: #{result}"
|
|
73
|
+
config[:cert_thumbprint] = result.strip
|
|
74
|
+
else
|
|
75
|
+
ui.error "Error installing certificate to Certificate Store"
|
|
76
|
+
ui.error result
|
|
77
|
+
exit 1
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
unless config[:cert_thumbprint]
|
|
82
|
+
ui.error "Please specify the --cert-thumbprint"
|
|
83
|
+
exit 1
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
result = %x{winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname="#{config[:hostname]}";CertificateThumbprint="#{config[:cert_thumbprint]}";Port="#{config[:port]}"}}
|
|
87
|
+
Chef::Log.debug result
|
|
88
|
+
|
|
89
|
+
if ($?.exitstatus == 0)
|
|
90
|
+
ui.info "WinRM listener created with Port: #{config[:port]} and CertificateThumbprint: #{config[:cert_thumbprint]}"
|
|
91
|
+
else
|
|
92
|
+
ui.error "Error creating WinRM listener. use -VV for more details."
|
|
93
|
+
exit 1
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
rescue => e
|
|
97
|
+
puts "ERROR: + #{e}"
|
|
98
|
+
end
|
|
99
|
+
else
|
|
100
|
+
ui.error "WinRM listener can be created on Windows system only"
|
|
101
|
+
exit 1
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
data/lib/chef/knife/winrm.rb
CHANGED
|
@@ -17,67 +17,88 @@
|
|
|
17
17
|
#
|
|
18
18
|
|
|
19
19
|
require 'chef/knife'
|
|
20
|
-
require 'chef/knife/
|
|
20
|
+
require 'chef/knife/winrm_knife_base'
|
|
21
|
+
require 'chef/knife/windows_cert_generate'
|
|
22
|
+
require 'chef/knife/windows_cert_install'
|
|
23
|
+
require 'chef/knife/windows_listener_create'
|
|
24
|
+
require 'chef/knife/winrm_session'
|
|
25
|
+
require 'chef/knife/knife_windows_base'
|
|
21
26
|
|
|
22
27
|
class Chef
|
|
23
28
|
class Knife
|
|
24
|
-
class Winrm < Knife
|
|
29
|
+
class Winrm < Knife
|
|
25
30
|
|
|
26
|
-
include Chef::Knife::
|
|
31
|
+
include Chef::Knife::WinrmCommandSharedFunctions
|
|
32
|
+
include Chef::Knife::KnifeWindowsBase
|
|
33
|
+
|
|
34
|
+
FAILED_BASIC_HINT ||= "Hint: Please check winrm configuration 'winrm get winrm/config/service' AllowUnencrypted flag on remote server."
|
|
35
|
+
FAILED_NOT_BASIC_HINT ||= <<-eos.gsub /^\s+/, ""
|
|
36
|
+
Hint: Make sure to prefix domain usernames with the correct domain name.
|
|
37
|
+
Hint: Local user names should be prefixed with computer name or IP address.
|
|
38
|
+
EXAMPLE: my_domain\\user_namer
|
|
39
|
+
eos
|
|
27
40
|
|
|
28
41
|
deps do
|
|
29
42
|
require 'readline'
|
|
30
43
|
require 'chef/search/query'
|
|
31
|
-
require 'em-winrm'
|
|
32
44
|
end
|
|
33
45
|
|
|
34
46
|
attr_writer :password
|
|
35
47
|
|
|
36
|
-
banner "knife winrm QUERY COMMAND (options)"
|
|
37
|
-
|
|
38
|
-
option :attribute,
|
|
39
|
-
:short => "-a ATTR",
|
|
40
|
-
:long => "--attribute ATTR",
|
|
41
|
-
:description => "The attribute to use for opening the connection - default is fqdn",
|
|
42
|
-
:default => "fqdn"
|
|
48
|
+
banner "knife winrm QUERY COMMAND (options)"
|
|
43
49
|
|
|
44
50
|
option :returns,
|
|
45
51
|
:long => "--returns CODES",
|
|
46
52
|
:description => "A comma delimited list of return codes which indicate success",
|
|
47
|
-
:default => "0"
|
|
53
|
+
:default => "0"
|
|
48
54
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
:long => "--manual-list",
|
|
52
|
-
:boolean => true,
|
|
53
|
-
:description => "QUERY is a space separated list of servers",
|
|
54
|
-
:default => false
|
|
55
|
+
def run
|
|
56
|
+
STDOUT.sync = STDERR.sync = true
|
|
55
57
|
|
|
58
|
+
configure_session
|
|
59
|
+
validate_password
|
|
60
|
+
execute_remote_command
|
|
61
|
+
end
|
|
56
62
|
|
|
57
|
-
def
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
63
|
+
def execute_remote_command
|
|
64
|
+
begin
|
|
65
|
+
case @name_args[1]
|
|
66
|
+
when "interactive"
|
|
67
|
+
interactive
|
|
68
|
+
else
|
|
69
|
+
relay_winrm_command(@name_args[1..-1].join(" "))
|
|
70
|
+
|
|
71
|
+
if config[:returns]
|
|
72
|
+
check_for_errors!
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Knife seems to ignore the return value of this method,
|
|
76
|
+
# so we exit to force the process exit code for this
|
|
77
|
+
# subcommand if returns is set
|
|
78
|
+
exit @exit_code if @exit_code && @exit_code != 0
|
|
79
|
+
@exit_code || 0
|
|
67
80
|
end
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
81
|
+
rescue WinRM::WinRMHTTPTransportError, WinRM::WinRMAuthorizationError => e
|
|
82
|
+
if authorization_error?(e)
|
|
83
|
+
if ! config[:suppress_auth_failure]
|
|
84
|
+
# Display errors if the caller hasn't opted to retry
|
|
85
|
+
ui.error "Failed to authenticate to #{@name_args[0].split(" ")} as #{locate_config_value(:winrm_user)}"
|
|
86
|
+
ui.info "Response: #{e.message}"
|
|
87
|
+
ui.info get_failed_authentication_hint
|
|
88
|
+
raise e
|
|
89
|
+
end
|
|
90
|
+
@exit_code = 401
|
|
91
|
+
else
|
|
92
|
+
raise e
|
|
71
93
|
end
|
|
72
|
-
s
|
|
73
94
|
end
|
|
74
|
-
|
|
75
95
|
end
|
|
76
96
|
|
|
77
|
-
def
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
97
|
+
def relay_winrm_command(command)
|
|
98
|
+
Chef::Log.debug(command)
|
|
99
|
+
@winrm_sessions.each do |s|
|
|
100
|
+
s.relay_command(command)
|
|
101
|
+
end
|
|
81
102
|
end
|
|
82
103
|
|
|
83
104
|
# TODO: Copied from Knife::Core:GenericPresenter. Should be extracted
|
|
@@ -100,105 +121,48 @@ class Chef
|
|
|
100
121
|
( !data.kind_of?(Array) && data.respond_to?(:to_hash) ) ? data.to_hash : data
|
|
101
122
|
end
|
|
102
123
|
|
|
103
|
-
|
|
124
|
+
private
|
|
104
125
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
126
|
+
def interactive
|
|
127
|
+
puts "WARN: Deprecated functionality. This will not be supported in future knife-windows releases."
|
|
128
|
+
puts "Connected to #{ui.list(session.servers.collect { |s| ui.color(s.host, :cyan) }, :inline, " and ")}"
|
|
129
|
+
puts
|
|
130
|
+
puts "To run a command on a list of servers, do:"
|
|
131
|
+
puts " on SERVER1 SERVER2 SERVER3; COMMAND"
|
|
132
|
+
puts " Example: on latte foamy; echo foobar"
|
|
133
|
+
puts
|
|
134
|
+
puts "To exit interactive mode, use 'quit!'"
|
|
135
|
+
puts
|
|
136
|
+
while 1
|
|
137
|
+
command = read_line
|
|
138
|
+
case command
|
|
139
|
+
when 'quit!'
|
|
140
|
+
puts 'Bye!'
|
|
141
|
+
break
|
|
142
|
+
when /^on (.+?); (.+)$/
|
|
143
|
+
raw_list = $1.split(" ")
|
|
144
|
+
server_list = Array.new
|
|
145
|
+
@winrm_sessions.each do |session_server|
|
|
146
|
+
server_list << session_server if raw_list.include?(session_server.host)
|
|
147
|
+
end
|
|
148
|
+
command = $2
|
|
149
|
+
relay_winrm_command(command, server_list)
|
|
121
150
|
else
|
|
122
|
-
|
|
123
|
-
"but does not have the required attribute (#{config[:attribute]}) to establish the connection. " +
|
|
124
|
-
"Try setting another attribute to open the connection using --attribute.")
|
|
151
|
+
relay_winrm_command(command)
|
|
125
152
|
end
|
|
126
|
-
exit 10
|
|
127
153
|
end
|
|
128
|
-
session_from_list(list)
|
|
129
154
|
end
|
|
130
155
|
|
|
131
|
-
def
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
session_opts[:port] = Chef::Config[:knife][:winrm_port] || config[:winrm_port]
|
|
138
|
-
session_opts[:keytab] = Chef::Config[:knife][:kerberos_keytab_file] if Chef::Config[:knife][:kerberos_keytab_file]
|
|
139
|
-
session_opts[:realm] = Chef::Config[:knife][:kerberos_realm] if Chef::Config[:knife][:kerberos_realm]
|
|
140
|
-
session_opts[:service] = Chef::Config[:knife][:kerberos_service] if Chef::Config[:knife][:kerberos_service]
|
|
141
|
-
session_opts[:ca_trust_path] = Chef::Config[:knife][:ca_trust_file] if Chef::Config[:knife][:ca_trust_file]
|
|
142
|
-
session_opts[:operation_timeout] = 1800 # 30 min OperationTimeout for long bootstraps fix for KNIFE_WINDOWS-8
|
|
143
|
-
|
|
144
|
-
## If you have a \\ in your name you need to use NTLM domain authentication
|
|
145
|
-
username_contains_domain = session_opts[:user].split("\\").length.eql?(2)
|
|
146
|
-
|
|
147
|
-
if username_contains_domain
|
|
148
|
-
# We cannot use basic_auth for domain authentication
|
|
149
|
-
session_opts[:basic_auth_only] = false
|
|
150
|
-
else
|
|
151
|
-
session_opts[:basic_auth_only] = true
|
|
156
|
+
def check_for_errors!
|
|
157
|
+
@winrm_sessions.each do |session|
|
|
158
|
+
session_exit_code = session.exit_code
|
|
159
|
+
unless success_return_codes.include? session_exit_code.to_i
|
|
160
|
+
@exit_code = session_exit_code.to_i
|
|
161
|
+
ui.error "Failed to execute command on #{session.host} return code #{session_exit_code}"
|
|
152
162
|
end
|
|
153
|
-
|
|
154
|
-
if config.keys.any? {|k| k.to_s =~ /kerberos/ }
|
|
155
|
-
session_opts[:transport] = :kerberos
|
|
156
|
-
session_opts[:basic_auth_only] = false
|
|
157
|
-
else
|
|
158
|
-
session_opts[:transport] = (Chef::Config[:knife][:winrm_transport] || config[:winrm_transport]).to_sym
|
|
159
|
-
|
|
160
|
-
if Chef::Platform.windows? && session_opts[:transport] == :plaintext && username_contains_domain
|
|
161
|
-
ui.warn("Switching to Negotiate authentication, Basic does not support Domain Authentication")
|
|
162
|
-
# windows - force only encrypted communication
|
|
163
|
-
require 'winrm-s'
|
|
164
|
-
session_opts[:transport] = :sspinegotiate
|
|
165
|
-
session_opts[:disable_sspi] = false
|
|
166
|
-
else
|
|
167
|
-
session_opts[:disable_sspi] = true
|
|
168
|
-
end
|
|
169
|
-
if session_opts[:user] and
|
|
170
|
-
(not session_opts[:password])
|
|
171
|
-
session_opts[:password] = Chef::Config[:knife][:winrm_password] = config[:winrm_password] = get_password
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
session.use(item, session_opts)
|
|
176
|
-
|
|
177
|
-
@longest = item.length if item.length > @longest
|
|
178
|
-
end
|
|
179
|
-
session
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
def print_data(host, data, color = :cyan)
|
|
183
|
-
if data =~ /\n/
|
|
184
|
-
data.split(/\n/).each { |d| print_data(host, d, color) }
|
|
185
|
-
else
|
|
186
|
-
padding = @longest - host.length
|
|
187
|
-
print ui.color(host, color)
|
|
188
|
-
padding.downto(0) { print " " }
|
|
189
|
-
puts data.chomp
|
|
190
163
|
end
|
|
191
164
|
end
|
|
192
165
|
|
|
193
|
-
def winrm_command(command, subsession=nil)
|
|
194
|
-
subsession ||= session
|
|
195
|
-
subsession.relay_command(command)
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
def get_password
|
|
199
|
-
@password ||= ui.ask("Enter your password: ") { |q| q.echo = false }
|
|
200
|
-
end
|
|
201
|
-
|
|
202
166
|
# Present the prompt and read a single line from the console. It also
|
|
203
167
|
# detects ^D and returns "exit" in that case. Adds the input to the
|
|
204
168
|
# history, unless the input is empty. Loops repeatedly until a non-empty
|
|
@@ -220,95 +184,28 @@ class Chef
|
|
|
220
184
|
end
|
|
221
185
|
end
|
|
222
186
|
|
|
223
|
-
def reader
|
|
187
|
+
def reader
|
|
224
188
|
Readline
|
|
225
189
|
end
|
|
226
190
|
|
|
227
|
-
def
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
puts "To run a command on a list of servers, do:"
|
|
231
|
-
puts " on SERVER1 SERVER2 SERVER3; COMMAND"
|
|
232
|
-
puts " Example: on latte foamy; echo foobar"
|
|
233
|
-
puts
|
|
234
|
-
puts "To exit interactive mode, use 'quit!'"
|
|
235
|
-
puts
|
|
236
|
-
while 1
|
|
237
|
-
command = read_line
|
|
238
|
-
case command
|
|
239
|
-
when 'quit!'
|
|
240
|
-
puts 'Bye!'
|
|
241
|
-
session.close
|
|
242
|
-
break
|
|
243
|
-
when /^on (.+?); (.+)$/
|
|
244
|
-
raw_list = $1.split(" ")
|
|
245
|
-
server_list = Array.new
|
|
246
|
-
session.servers.each do |session_server|
|
|
247
|
-
server_list << session_server if raw_list.include?(session_server.host)
|
|
248
|
-
end
|
|
249
|
-
command = $2
|
|
250
|
-
winrm_command(command, session.on(*server_list))
|
|
251
|
-
else
|
|
252
|
-
winrm_command(command)
|
|
253
|
-
end
|
|
254
|
-
end
|
|
191
|
+
def authorization_error?(exception)
|
|
192
|
+
exception.is_a?(WinRM::WinRMAuthorizationError) ||
|
|
193
|
+
exception.message =~ /401/
|
|
255
194
|
end
|
|
256
195
|
|
|
257
|
-
def
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
unless success_return_codes.include? value.to_i
|
|
262
|
-
@exit_code = value.to_i
|
|
263
|
-
ui.error "Failed to execute command on #{host} return code #{value}"
|
|
264
|
-
end
|
|
265
|
-
end
|
|
266
|
-
|
|
196
|
+
def success_return_codes
|
|
197
|
+
#Redundant if the CLI options parsing occurs
|
|
198
|
+
return [0] unless config[:returns]
|
|
199
|
+
return @success_return_codes ||= config[:returns].split(',').collect {|item| item.to_i}
|
|
267
200
|
end
|
|
268
201
|
|
|
269
|
-
def
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
@longest = 0
|
|
275
|
-
|
|
276
|
-
configure_session
|
|
277
|
-
|
|
278
|
-
case @name_args[1]
|
|
279
|
-
when "interactive"
|
|
280
|
-
interactive
|
|
281
|
-
else
|
|
282
|
-
winrm_command(@name_args[1..-1].join(" "))
|
|
283
|
-
|
|
284
|
-
if config[:returns]
|
|
285
|
-
check_for_errors! session.exit_codes
|
|
286
|
-
end
|
|
287
|
-
|
|
288
|
-
session.close
|
|
289
|
-
|
|
290
|
-
# Knife seems to ignore the return value of this method,
|
|
291
|
-
# so we exit to force the process exit code for this
|
|
292
|
-
# subcommand if returns is set
|
|
293
|
-
exit @exit_code if @exit_code && @exit_code != 0
|
|
294
|
-
@exit_code || 0
|
|
295
|
-
end
|
|
296
|
-
rescue WinRM::WinRMHTTPTransportError => e
|
|
297
|
-
case e.message
|
|
298
|
-
when /401/
|
|
299
|
-
if ! config[:suppress_auth_failure]
|
|
300
|
-
# Display errors if the caller hasn't opted to retry
|
|
301
|
-
ui.error "Failed to authenticate to #{@name_args[0].split(" ")} as #{config[:winrm_user]}"
|
|
302
|
-
ui.info "Response: #{e.message}"
|
|
303
|
-
raise e
|
|
304
|
-
end
|
|
305
|
-
@exit_code = 401
|
|
306
|
-
else
|
|
307
|
-
raise e
|
|
308
|
-
end
|
|
202
|
+
def get_failed_authentication_hint
|
|
203
|
+
if @session_opts[:basic_auth_only]
|
|
204
|
+
FAILED_BASIC_HINT
|
|
205
|
+
else
|
|
206
|
+
FAILED_NOT_BASIC_HINT
|
|
309
207
|
end
|
|
310
208
|
end
|
|
311
|
-
|
|
312
209
|
end
|
|
313
210
|
end
|
|
314
211
|
end
|
|
@@ -24,6 +24,9 @@ class Chef
|
|
|
24
24
|
class Knife
|
|
25
25
|
module WinrmBase
|
|
26
26
|
|
|
27
|
+
# It includes supported WinRM authentication protocol.
|
|
28
|
+
WINRM_AUTH_PROTOCOL_LIST ||= %w{basic negotiate kerberos}
|
|
29
|
+
|
|
27
30
|
# :nodoc:
|
|
28
31
|
# Would prefer to do this in a rational way, but can't be done b/c of
|
|
29
32
|
# Mixlib::CLI's design :(
|
|
@@ -48,11 +51,19 @@ class Chef
|
|
|
48
51
|
:description => "The WinRM password",
|
|
49
52
|
:proc => Proc.new { |key| Chef::Config[:knife][:winrm_password] = key }
|
|
50
53
|
|
|
54
|
+
option :winrm_transport,
|
|
55
|
+
:short => "-t TRANSPORT",
|
|
56
|
+
:long => "--winrm-transport TRANSPORT",
|
|
57
|
+
:description => "The WinRM transport type. valid choices are [ssl, plaintext]",
|
|
58
|
+
:default => 'plaintext',
|
|
59
|
+
:proc => Proc.new { |transport| Chef::Config[:knife][:winrm_port] = '5986' if transport == 'ssl'
|
|
60
|
+
Chef::Config[:knife][:winrm_transport] = transport }
|
|
61
|
+
|
|
51
62
|
option :winrm_port,
|
|
52
63
|
:short => "-p PORT",
|
|
53
64
|
:long => "--winrm-port PORT",
|
|
54
|
-
:description => "The WinRM port, by default this is 5985",
|
|
55
|
-
:default =>
|
|
65
|
+
:description => "The WinRM port, by default this is '5985' for 'plaintext' and '5986' for 'ssl' winrm transport",
|
|
66
|
+
:default => '5985',
|
|
56
67
|
:proc => Proc.new { |key| Chef::Config[:knife][:winrm_port] = key }
|
|
57
68
|
|
|
58
69
|
option :identity_file,
|
|
@@ -60,15 +71,8 @@ class Chef
|
|
|
60
71
|
:long => "--identity-file IDENTITY_FILE",
|
|
61
72
|
:description => "The SSH identity file used for authentication"
|
|
62
73
|
|
|
63
|
-
option :winrm_transport,
|
|
64
|
-
:short => "-t TRANSPORT",
|
|
65
|
-
:long => "--winrm-transport TRANSPORT",
|
|
66
|
-
:description => "The WinRM transport type. valid choices are [ssl, plaintext]",
|
|
67
|
-
:default => 'plaintext',
|
|
68
|
-
:proc => Proc.new { |transport| Chef::Config[:knife][:winrm_transport] = transport }
|
|
69
|
-
|
|
70
74
|
option :kerberos_keytab_file,
|
|
71
|
-
:short => "-
|
|
75
|
+
:short => "-T KEYTAB_FILE",
|
|
72
76
|
:long => "--keytab-file KEYTAB_FILE",
|
|
73
77
|
:description => "The Kerberos keytab file used for authentication",
|
|
74
78
|
:proc => Proc.new { |keytab| Chef::Config[:knife][:kerberos_keytab_file] = keytab }
|
|
@@ -91,9 +95,24 @@ class Chef
|
|
|
91
95
|
:description => "The Certificate Authority (CA) trust file used for SSL transport",
|
|
92
96
|
:proc => Proc.new { |trust| Chef::Config[:knife][:ca_trust_file] = trust }
|
|
93
97
|
|
|
98
|
+
option :winrm_ssl_verify_mode,
|
|
99
|
+
:long => "--winrm-ssl-verify-mode SSL_VERIFY_MODE",
|
|
100
|
+
:description => "The WinRM peer verification mode. Valid choices are [verify_peer, verify_none]",
|
|
101
|
+
:default => :verify_peer,
|
|
102
|
+
:proc => Proc.new { |verify_mode| verify_mode.to_sym }
|
|
103
|
+
|
|
104
|
+
option :winrm_authentication_protocol,
|
|
105
|
+
:long => "--winrm-authentication-protocol AUTHENTICATION_PROTOCOL",
|
|
106
|
+
:description => "The authentication protocol used during WinRM communication. The supported protocols are #{WINRM_AUTH_PROTOCOL_LIST.join(',')}. Default is 'negotiate'.",
|
|
107
|
+
:default => "negotiate",
|
|
108
|
+
:proc => Proc.new { |protocol| Chef::Config[:knife][:winrm_authentication_protocol] = protocol }
|
|
109
|
+
|
|
110
|
+
option :session_timeout,
|
|
111
|
+
:long => "--session-timeout Minutes",
|
|
112
|
+
:description => "The timeout for the client for the maximum length of the WinRM session",
|
|
113
|
+
:default => 30
|
|
94
114
|
end
|
|
95
115
|
end
|
|
96
|
-
|
|
97
116
|
end
|
|
98
117
|
end
|
|
99
118
|
end
|