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.
Files changed (47) hide show
  1. checksums.yaml +5 -13
  2. data/.gitignore +1 -0
  3. data/.travis.yml +16 -2
  4. data/CHANGELOG.md +38 -4
  5. data/DOC_CHANGES.md +331 -15
  6. data/Gemfile +2 -1
  7. data/README.md +306 -71
  8. data/RELEASE_NOTES.md +63 -43
  9. data/Rakefile +9 -4
  10. data/appveyor.yml +42 -0
  11. data/ci.gemfile +15 -0
  12. data/knife-windows.gemspec +6 -4
  13. data/lib/chef/knife/bootstrap/windows-chef-client-msi.erb +96 -71
  14. data/lib/chef/knife/bootstrap_windows_base.rb +219 -37
  15. data/lib/chef/knife/bootstrap_windows_ssh.rb +22 -5
  16. data/lib/chef/knife/bootstrap_windows_winrm.rb +24 -24
  17. data/lib/chef/knife/core/windows_bootstrap_context.rb +146 -12
  18. data/lib/chef/knife/knife_windows_base.rb +33 -0
  19. data/lib/chef/knife/windows_cert_generate.rb +155 -0
  20. data/lib/chef/knife/windows_cert_install.rb +68 -0
  21. data/lib/chef/knife/windows_helper.rb +3 -1
  22. data/lib/chef/knife/windows_listener_create.rb +107 -0
  23. data/lib/chef/knife/winrm.rb +103 -206
  24. data/lib/chef/knife/winrm_base.rb +30 -11
  25. data/lib/chef/knife/winrm_knife_base.rb +218 -0
  26. data/lib/chef/knife/winrm_session.rb +81 -0
  27. data/lib/chef/knife/winrm_shared_options.rb +47 -0
  28. data/lib/chef/knife/wsman_endpoint.rb +44 -0
  29. data/lib/chef/knife/wsman_test.rb +96 -0
  30. data/lib/knife-windows/path_helper.rb +234 -0
  31. data/lib/knife-windows/version.rb +1 -1
  32. data/spec/assets/win_template_rendered_with_bootstrap_install_command.txt +217 -0
  33. data/spec/assets/win_template_rendered_without_bootstrap_install_command.txt +329 -0
  34. data/spec/assets/win_template_unrendered.txt +246 -0
  35. data/spec/functional/bootstrap_download_spec.rb +129 -35
  36. data/spec/spec_helper.rb +28 -1
  37. data/spec/unit/knife/bootstrap_options_spec.rb +150 -0
  38. data/spec/unit/knife/bootstrap_template_spec.rb +27 -26
  39. data/spec/unit/knife/bootstrap_windows_winrm_spec.rb +166 -32
  40. data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +151 -0
  41. data/spec/unit/knife/windows_cert_generate_spec.rb +90 -0
  42. data/spec/unit/knife/windows_cert_install_spec.rb +51 -0
  43. data/spec/unit/knife/windows_listener_create_spec.rb +76 -0
  44. data/spec/unit/knife/winrm_session_spec.rb +56 -0
  45. data/spec/unit/knife/winrm_spec.rb +403 -118
  46. data/spec/unit/knife/wsman_test_spec.rb +176 -0
  47. metadata +67 -21
@@ -0,0 +1,56 @@
1
+ #
2
+ # Author:: Steven Murawski <smurawski@chef.io>
3
+ # Copyright:: Copyright (c) 2015 Opscode, 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 'spec_helper'
20
+
21
+ Chef::Knife::Winrm.load_deps
22
+
23
+
24
+ describe Chef::Knife::WinrmSession do
25
+ let(:winrm_service) { double('WinRMWebService') }
26
+ let(:options) { { transport: :plaintext } }
27
+
28
+ before do
29
+ allow(WinRM::WinRMWebService).to receive(:new).and_return(winrm_service)
30
+ allow(winrm_service).to receive(:set_timeout)
31
+ end
32
+
33
+ subject { Chef::Knife::WinrmSession.new(options) }
34
+
35
+ describe "#initialize" do
36
+ context "when using sspinegotiate transport" do
37
+ let(:options) { { transport: :sspinegotiate } }
38
+
39
+ it "uses winrm-s" do
40
+ expect(Chef::Knife::WinrmSession).to receive(:load_windows_specific_gems)
41
+ subject
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "#relay_command" do
47
+ it "run command and display commands output" do
48
+ expect(winrm_service).to receive(:open_shell).ordered
49
+ expect(winrm_service).to receive(:run_command).ordered
50
+ expect(winrm_service).to receive(:get_command_output).ordered.and_return({})
51
+ expect(winrm_service).to receive(:cleanup_command).ordered
52
+ expect(winrm_service).to receive(:close_shell).ordered
53
+ subject.relay_command("cmd.exe echo 'hi'")
54
+ end
55
+ end
56
+ end
@@ -26,193 +26,478 @@ describe Chef::Knife::Winrm do
26
26
  @original_knife_config = Chef::Config[:knife].nil? ? nil : Chef::Config[:knife].dup
27
27
  end
28
28
 
29
- after(:all) do
29
+ after do
30
30
  Chef::Config.configuration = @original_config
31
31
  Chef::Config[:knife] = @original_knife_config if @original_knife_config
32
32
  end
33
33
 
34
- before do
35
- @knife = Chef::Knife::Winrm.new
36
- @knife.config[:attribute] = "fqdn"
37
- @node_foo = Chef::Node.new
38
- @node_foo.automatic_attrs[:fqdn] = "foo.example.org"
39
- @node_foo.automatic_attrs[:ipaddress] = "10.0.0.1"
40
- @node_bar = Chef::Node.new
41
- @node_bar.automatic_attrs[:fqdn] = "bar.example.org"
42
- @node_bar.automatic_attrs[:ipaddress] = "10.0.0.2"
43
- @node_bar.automatic_attrs[:ec2][:public_hostname] = "somewhere.com"
44
- end
45
-
46
- describe "#configure_session" do
34
+ describe "#resolve_target_nodes" do
47
35
  before do
36
+ @knife = Chef::Knife::Winrm.new
37
+ @knife.config[:attribute] = "fqdn"
38
+ @node_foo = Chef::Node.new
39
+ @node_foo.automatic_attrs[:fqdn] = "foo.example.org"
40
+ @node_foo.automatic_attrs[:ipaddress] = "10.0.0.1"
41
+ @node_bar = Chef::Node.new
42
+ @node_bar.automatic_attrs[:fqdn] = "bar.example.org"
43
+ @node_bar.automatic_attrs[:ipaddress] = "10.0.0.2"
44
+ @node_bar.automatic_attrs[:ec2][:public_hostname] = "somewhere.com"
48
45
  @query = double("Chef::Search::Query")
49
46
  end
50
47
 
51
48
  context "when there are some hosts found but they do not have an attribute to connect with" do
52
49
  before do
53
50
  @knife.config[:manual] = false
51
+ @knife.config[:winrm_password] = 'P@ssw0rd!'
54
52
  allow(@query).to receive(:search).and_return([[@node_foo, @node_bar]])
55
53
  @node_foo.automatic_attrs[:fqdn] = nil
56
54
  @node_bar.automatic_attrs[:fqdn] = nil
57
55
  allow(Chef::Search::Query).to receive(:new).and_return(@query)
58
56
  end
59
57
 
60
- it "should raise a specific error (KNIFE-222)" do
58
+ it "raises a specific error (KNIFE-222)" do
61
59
  expect(@knife.ui).to receive(:fatal).with(/does not have the required attribute/)
62
60
  expect(@knife).to receive(:exit).with(10)
63
- @knife.configure_session
61
+ @knife.configure_chef
62
+ @knife.resolve_target_nodes
64
63
  end
65
64
  end
66
65
 
67
66
  context "when there are nested attributes" do
68
67
  before do
69
68
  @knife.config[:manual] = false
69
+ @knife.config[:winrm_password] = 'P@ssw0rd!'
70
70
  allow(@query).to receive(:search).and_return([[@node_foo, @node_bar]])
71
71
  allow(Chef::Search::Query).to receive(:new).and_return(@query)
72
72
  end
73
73
 
74
- it "should use nested attributes (KNIFE-276)" do
74
+ it "uses the nested attributes (KNIFE-276)" do
75
75
  @knife.config[:attribute] = "ec2.public_hostname"
76
- allow(@knife).to receive(:session_from_list)
77
- @knife.configure_session
78
-
76
+ @knife.configure_chef
77
+ @knife.resolve_target_nodes
79
78
  end
80
79
  end
80
+ end
81
+
82
+ describe "#configure_session" do
83
+ let(:winrm_user) { 'testuser' }
84
+ let(:transport) { 'plaintext' }
85
+ let(:protocol) { 'basic' }
86
+ let(:knife_args) do
87
+ [
88
+ '-m', 'localhost',
89
+ '-x', winrm_user,
90
+ '-P', 'testpassword',
91
+ '-t', transport,
92
+ '--winrm-authentication-protocol', protocol,
93
+ 'echo helloworld'
94
+ ]
95
+ end
96
+ let(:winrm_session) { double('winrm_session') }
97
+ let(:winrm_service) { double('WinRMWebService') }
98
+
99
+ before do
100
+ allow(winrm_service).to receive(:set_timeout)
101
+ end
102
+
103
+ subject { Chef::Knife::Winrm.new(knife_args) }
81
104
 
82
- describe Chef::Knife::Winrm do
83
- context "when executing the run command which sets the process exit code" do
84
- before(:each) do
85
- Chef::Config[:knife] = {:winrm_transport => :http}
86
- @winrm = Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', 'echo helloworld'])
105
+ context "when configuring the WinRM user name" do
106
+ context "when basic auth is used" do
107
+ let(:protocol) { 'basic' }
108
+
109
+ it "passes user name as given in options" do
110
+ expect(Chef::Knife::WinrmSession).to receive(:new) do |opts|
111
+ expect(opts[:user]).to eq(winrm_user)
112
+ end.and_return(winrm_session)
113
+ subject.configure_session
87
114
  end
115
+ end
116
+
117
+ context "when negotiate auth is used" do
118
+ let(:protocol) { 'negotiate' }
88
119
 
89
- after(:each) do
90
- Chef::Config.configuration = @original_config
91
- Chef::Config[:knife] = @original_knife_config if @original_knife_config
120
+ context "when user is prefixed with realm" do
121
+ let(:winrm_user) { "my_realm\\myself" }
122
+
123
+ it "passes user name as given in options" do
124
+ expect(Chef::Knife::WinrmSession).to receive(:new) do |opts|
125
+ expect(opts[:user]).to eq(winrm_user)
126
+ end.and_return(winrm_session)
127
+ subject.configure_session
128
+ end
92
129
  end
93
130
 
94
- it "should return with 0 if the command succeeds" do
95
- allow(@winrm).to receive(:winrm_command).and_return(0)
96
- exit_code = @winrm.run
97
- expect(exit_code).to be_zero
131
+ context "when user realm is included via email format" do
132
+ let(:winrm_user) { "myself@my_realm.com" }
133
+
134
+ it "passes user name as given in options" do
135
+ expect(Chef::Knife::WinrmSession).to receive(:new) do |opts|
136
+ expect(opts[:user]).to eq(winrm_user)
137
+ end.and_return(winrm_session)
138
+ subject.configure_session
139
+ end
98
140
  end
99
141
 
100
- it "should exit the process with exact exit status if the command fails and returns config is set to 0" do
101
- command_status = 510
102
- @winrm.config[:returns] = "0"
103
- Chef::Config[:knife][:returns] = [0]
104
- allow(@winrm).to receive(:winrm_command).and_return(command_status)
105
- session_mock = EventMachine::WinRM::Session.new
106
- allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
107
- allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
108
- begin
109
- @winrm.run
110
- expect(0).to eq(510)
111
- rescue Exception => e
112
- expect(e.status).to eq(command_status)
142
+ context "when a local user is given" do
143
+ it "prefixes user with the dot (local) realm" do
144
+ expect(Chef::Knife::WinrmSession).to receive(:new) do |opts|
145
+ expect(opts[:user]).to eq(".\\#{winrm_user}")
146
+ end.and_return(winrm_session)
147
+ subject.configure_session
113
148
  end
114
149
  end
150
+ end
151
+ end
152
+
153
+ context "when configuring the WinRM transport" do
154
+ before(:all) do
155
+ @winrm_session = Object.new
156
+ @winrm_session.define_singleton_method(:set_timeout){|timeout| ""}
157
+ end
158
+
159
+ context "kerberos option is set" do
160
+ let(:winrm_command_http) { Chef::Knife::Winrm.new([
161
+ '-m', 'localhost',
162
+ '-x', 'testuser',
163
+ '-P', 'testpassword',
164
+ '--winrm-authentication-protocol', 'basic',
165
+ '--kerberos-realm', 'realm',
166
+ 'echo helloworld'
167
+ ]) }
168
+
169
+ it "sets the transport to kerberos" do
170
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5985/wsman', :kerberos, anything).and_return(@winrm_session)
171
+ winrm_command_http.configure_chef
172
+ winrm_command_http.configure_session
173
+ end
174
+ end
175
+
176
+ context "kerberos option is set but nil" do
177
+ let(:winrm_command_http) { Chef::Knife::Winrm.new([
178
+ '-m', 'localhost',
179
+ '-x', 'testuser',
180
+ '-P', 'testpassword',
181
+ '--winrm-authentication-protocol', 'basic',
182
+ 'echo helloworld'
183
+ ]) }
184
+
185
+ it "sets the transport to plaintext" do
186
+ winrm_command_http.config[:kerberos_realm] = nil
187
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5985/wsman', :plaintext, anything).and_return(@winrm_session)
188
+ winrm_command_http.configure_chef
189
+ winrm_command_http.configure_session
190
+ end
191
+ end
192
+
193
+ context "on windows workstations" do
194
+ let(:protocol) { 'negotiate' }
115
195
 
116
- it "should exit the process with non-zero status if the command fails and returns config is set to 0" do
117
- command_status = 1
118
- @winrm.config[:returns] = "0,53"
119
- Chef::Config[:knife][:returns] = [0,53]
120
- allow(@winrm).to receive(:winrm_command).and_return(command_status)
121
- session_mock = EventMachine::WinRM::Session.new
122
- allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
123
- allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
124
- expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
196
+ before do
197
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
125
198
  end
126
199
 
127
- it "should exit the process with a zero status if the command returns an expected non-zero status" do
128
- command_status = 53
129
- Chef::Config[:knife][:returns] = [0,53]
130
- allow(@winrm).to receive(:winrm_command).and_return(command_status)
131
- session_mock = EventMachine::WinRM::Session.new
132
- allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
133
- allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
134
- exit_code = @winrm.run
135
- expect(exit_code).to be_zero
200
+ it "defaults to negotiate when on a Windows host" do
201
+ expect(Chef::Knife::WinrmSession).to receive(:new) do |opts|
202
+ expect(opts[:transport]).to eq(:sspinegotiate)
203
+ end.and_return(winrm_session)
204
+ subject.configure_session
136
205
  end
206
+ end
137
207
 
138
- it "should exit the process with a zero status if the command returns an expected non-zero status" do
139
- command_status = 53
140
- Chef::Config[:knife][:returns] = [0,53]
141
- allow(@winrm).to receive(:winrm_command).and_return(command_status)
142
- session_mock = EventMachine::WinRM::Session.new
143
- allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
144
- allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
145
- exit_code = @winrm.run
146
- expect(exit_code).to be_zero
208
+ context "on non-windows workstations" do
209
+ before do
210
+ allow(Chef::Platform).to receive(:windows?).and_return(false)
147
211
  end
148
212
 
149
- it "should exit the process with 100 if command execution raises an exception other than 401" do
150
- allow(@winrm).to receive(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '500')
151
- expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(100) }
213
+ let(:winrm_command_http) { Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '-t', 'plaintext', '--winrm-authentication-protocol', 'basic', 'echo helloworld']) }
214
+
215
+ it "defaults to the http uri scheme" do
216
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :plaintext)).and_call_original
217
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5985/wsman', anything, anything).and_return(@winrm_session)
218
+ winrm_command_http.configure_chef
219
+ winrm_command_http.configure_session
152
220
  end
153
221
 
154
- it "should exit the process with 100 if command execution raises a 401" do
155
- allow(@winrm).to receive(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '401')
156
- expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(100) }
222
+ it "sets the operation timeout and verifes default" do
223
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :plaintext)).and_call_original
224
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5985/wsman', anything, anything).and_return(@winrm_session)
225
+ expect(@winrm_session).to receive(:set_timeout).with(1800)
226
+ winrm_command_http.configure_chef
227
+ winrm_command_http.configure_session
157
228
  end
158
229
 
159
- it "should exit the process with 0 if command execution raises a 401 and suppress_auth_failure is set to true" do
160
- @winrm.config[:suppress_auth_failure] = true
161
- allow(@winrm).to receive(:winrm_command).and_raise(WinRM::WinRMHTTPTransportError, '401')
162
- exit_code = @winrm.run_with_pretty_exceptions
163
- expect(exit_code).to eq(401)
230
+ it "sets the user specified winrm port" do
231
+ Chef::Config[:knife] = {winrm_port: "5988"}
232
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :plaintext)).and_call_original
233
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5988/wsman', anything, anything).and_return(@winrm_session)
234
+ winrm_command_http.configure_chef
235
+ winrm_command_http.configure_session
164
236
  end
165
237
 
166
- context "validate sspinegotiate transport option" do
167
- before do
168
- Chef::Config[:knife] = {:winrm_transport => :plaintext}
169
- allow(@winrm).to receive(:winrm_command).and_return(0)
170
- end
238
+ let(:winrm_command_timeout) { Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '--winrm-authentication-protocol', 'basic', '--session-timeout', '10', 'echo helloworld']) }
171
239
 
172
- it "should have winrm opts transport set to sspinegotiate for windows" do
173
- @winrm.config[:winrm_user] = "domain\\testuser"
174
- allow(Chef::Platform).to receive(:windows?).and_return(true)
175
- allow(@winrm).to receive(:require).with('winrm-s').and_return(true)
176
- expect(@winrm.session).to receive(:use).with("localhost", {:user=>"domain\\testuser", :password=>"testpassword", :port=>nil, :operation_timeout=>1800, :basic_auth_only=>false, :transport=>:sspinegotiate, :disable_sspi=>false})
177
- exit_code = @winrm.run
178
- end
240
+ it "sets operation timeout and verify 10 Minute timeout" do
241
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :plaintext)).and_call_original
242
+ expect(WinRM::WinRMWebService).to receive(:new).with('http://localhost:5985/wsman', anything, anything).and_return(@winrm_session)
243
+ expect(@winrm_session).to receive(:set_timeout).with(600)
244
+ winrm_command_timeout.configure_chef
245
+ winrm_command_timeout.configure_session
246
+ end
247
+
248
+ let(:winrm_command_https) { Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '--winrm-transport', 'ssl', 'echo helloworld']) }
179
249
 
180
- it "should use the winrm monkey patch for windows" do
181
- @winrm.config[:winrm_user] = "domain\\testuser"
182
- allow(Chef::Platform).to receive(:windows?).and_return(true)
183
- expect(@winrm).to receive(:require).with('winrm-s')
250
+ it "uses the https uri scheme if the ssl transport is specified" do
251
+ Chef::Config[:knife] = {:winrm_transport => 'ssl'}
252
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
253
+ expect(WinRM::WinRMWebService).to receive(:new).with('https://localhost:5986/wsman', anything, anything).and_return(@winrm_session)
254
+ winrm_command_https.configure_chef
255
+ winrm_command_https.configure_session
256
+ end
257
+
258
+ it "uses the winrm port '5986' by default for ssl transport" do
259
+ Chef::Config[:knife] = {:winrm_transport => 'ssl'}
260
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
261
+ expect(WinRM::WinRMWebService).to receive(:new).with('https://localhost:5986/wsman', anything, anything).and_return(@winrm_session)
262
+ winrm_command_https.configure_chef
263
+ winrm_command_https.configure_session
264
+ end
265
+
266
+ it "defaults to validating the server when the ssl transport is used" do
267
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
268
+ expect(WinRM::WinRMWebService).to receive(:new).with(anything, anything, hash_including(:no_ssl_peer_verification => false)).and_return(@winrm_session)
269
+ winrm_command_https.configure_chef
270
+ winrm_command_https.configure_session
271
+ end
272
+
273
+ let(:winrm_command_verify_peer) { Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '--winrm-transport', 'ssl', '--winrm-ssl-verify-mode', 'verify_peer', 'echo helloworld'])}
184
274
 
185
- exit_code = @winrm.run
275
+ it "validates the server when the ssl transport is used and the :winrm_ssl_verify_mode option is not configured to :verify_none" do
276
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
277
+ expect(WinRM::WinRMWebService).to receive(:new).with(anything, anything, hash_including(:no_ssl_peer_verification => false)).and_return(@winrm_session)
278
+ winrm_command_verify_peer.configure_chef
279
+ winrm_command_verify_peer.configure_session
280
+ end
281
+
282
+ context "when setting verify_none" do
283
+ let(:transport) { 'ssl' }
284
+
285
+ before { knife_args << '--winrm-ssl-verify-mode' << 'verify_none' }
286
+
287
+ it "does not validate the server when the ssl transport is used and the :winrm_ssl_verify_mode option is set to :verify_none" do
288
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
289
+ expect(WinRM::WinRMWebService).to receive(:new).with(anything, anything, hash_including(:no_ssl_peer_verification => true)).and_return(@winrm_session)
290
+ subject.configure_chef
291
+ subject.configure_session
186
292
  end
187
293
 
188
- context "when domain name not given" do
189
- it "should skip winrm monkey patch for windows" do
190
- @winrm.config[:winrm_user] = "testuser"
191
- allow(Chef::Platform).to receive(:windows?).and_return(true)
192
- expect(@winrm).to_not receive(:require).with('winrm-s')
294
+ it "prints warning output when the :winrm_ssl_verify_mode set to :verify_none to disable server validation" do
295
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
296
+ expect(WinRM::WinRMWebService).to receive(:new).with(anything, anything, hash_including(:no_ssl_peer_verification => true)).and_return(@winrm_session)
297
+ expect(subject).to receive(:warn_no_ssl_peer_verification)
193
298
 
194
- exit_code = @winrm.run
195
- end
299
+ subject.configure_chef
300
+ subject.configure_session
196
301
  end
197
302
 
198
- context "when local domain name given" do
199
- it "should use the winrm monkey patch for windows" do
200
- @winrm.config[:winrm_user] = ".\\testuser"
201
- allow(Chef::Platform).to receive(:windows?).and_return(true)
202
- expect(@winrm).to receive(:require).with('winrm-s')
303
+ context "when transport is plaintext" do
304
+ let(:transport) { 'plaintext' }
305
+
306
+ it "does not print warning re ssl server validation" do
307
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :plaintext)).and_call_original
308
+ expect(WinRM::WinRMWebService).to receive(:new).and_return(winrm_service)
309
+ expect(subject).to_not receive(:warn_no_ssl_peer_verification)
203
310
 
204
- exit_code = @winrm.run
311
+ subject.configure_chef
312
+ subject.configure_session
205
313
  end
206
314
  end
315
+ end
207
316
 
208
- it "should not have winrm opts transport set to sspinegotiate for unix" do
209
- allow(Chef::Platform).to receive(:windows?).and_return(false)
317
+ let(:winrm_command_ca_trust) { Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '--winrm-transport', 'ssl', '--ca-trust-file', '~/catrustroot', '--winrm-ssl-verify-mode', 'verify_none', 'echo helloworld'])}
210
318
 
211
- expect(@winrm.session).to receive(:use).with("localhost", {:user=>"testuser", :password=>"testpassword", :port=>nil, :operation_timeout=>1800, :basic_auth_only=>true, :transport=>:plaintext, :disable_sspi=>true})
212
- exit_code = @winrm.run
213
- end
319
+ it "validates the server when the ssl transport is used and the :ca_trust_file option is specified even if the :winrm_ssl_verify_mode option is set to :verify_none" do
320
+ expect(Chef::Knife::WinrmSession).to receive(:new).with(hash_including(:transport => :ssl)).and_call_original
321
+ expect(WinRM::WinRMWebService).to receive(:new).with(anything, anything, hash_including(:no_ssl_peer_verification => false)).and_return(@winrm_session)
322
+ winrm_command_ca_trust.configure_chef
323
+ winrm_command_ca_trust.configure_session
214
324
  end
325
+ end
326
+ end
327
+ end
328
+
329
+ describe "#run" do
330
+ before(:each) do
331
+ Chef::Config[:knife] = {:winrm_transport => 'plaintext'}
332
+ @winrm = Chef::Knife::Winrm.new(['-m', 'localhost', '-x', 'testuser', '-P', 'testpassword', '--winrm-authentication-protocol', 'basic', 'echo helloworld'])
333
+ end
334
+
335
+ it "returns with 0 if the command succeeds" do
336
+ allow(@winrm).to receive(:relay_winrm_command).and_return(0)
337
+ return_code = @winrm.run
338
+ expect(return_code).to be_zero
339
+ end
340
+
341
+ it "exits with exact exit status if the command fails and returns config is set to 0" do
342
+ command_status = 510
343
+ session_mock = Chef::Knife::WinrmSession.new({:transport => :plaintext, :host => 'localhost', :port => '5985'})
344
+
345
+ @winrm.config[:returns] = "0"
346
+ Chef::Config[:knife][:returns] = [0]
347
+
348
+ allow(@winrm).to receive(:relay_winrm_command)
349
+ allow(@winrm.ui).to receive(:error)
350
+ allow(Chef::Knife::WinrmSession).to receive(:new).and_return(session_mock)
351
+ allow(session_mock).to receive(:exit_code).and_return(command_status)
352
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
353
+ end
354
+
355
+ it "exits with non-zero status if the command fails and returns config is set to 0" do
356
+ command_status = 1
357
+ @winrm.config[:returns] = "0,53"
358
+ Chef::Config[:knife][:returns] = [0,53]
359
+ allow(@winrm).to receive(:relay_winrm_command).and_return(command_status)
360
+ allow(@winrm.ui).to receive(:error)
361
+ session_mock = Chef::Knife::WinrmSession.new({:transport => :plaintext, :host => 'localhost', :port => '5985'})
362
+ allow(Chef::Knife::WinrmSession).to receive(:new).and_return(session_mock)
363
+ allow(session_mock).to receive(:exit_code).and_return(command_status)
364
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
365
+ end
366
+
367
+ it "exits with a zero status if the command returns an expected non-zero status" do
368
+ command_status = 53
369
+ Chef::Config[:knife][:returns] = [0,53]
370
+ allow(@winrm).to receive(:relay_winrm_command).and_return(command_status)
371
+ session_mock = Chef::Knife::WinrmSession.new({:transport => :plaintext, :host => 'localhost', :port => '5985'})
372
+ allow(Chef::Knife::WinrmSession).to receive(:new).and_return(session_mock)
373
+ allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
374
+ exit_code = @winrm.run
375
+ expect(exit_code).to be_zero
376
+ end
377
+
378
+ it "exits with a zero status if the command returns an expected non-zero status" do
379
+ command_status = 53
380
+ @winrm.config[:returns] = '0,53'
381
+ allow(@winrm).to receive(:relay_winrm_command).and_return(command_status)
382
+ session_mock = Chef::Knife::WinrmSession.new({:transport => :plaintext, :host => 'localhost', :port => '5985'})
383
+ allow(Chef::Knife::WinrmSession).to receive(:new).and_return(session_mock)
384
+ allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
385
+ exit_code = @winrm.run
386
+ expect(exit_code).to be_zero
387
+ end
388
+
389
+ it "exits with 100 and no hint if command execution raises an exception other than 401" do
390
+ allow(@winrm).to receive(:relay_winrm_command).and_raise(WinRM::WinRMHTTPTransportError.new('', '500'))
391
+ allow(@winrm.ui).to receive(:error)
392
+ expect(@winrm.ui).to_not receive(:info)
393
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(100) }
394
+ end
395
+
396
+ it "exits with 100 if command execution raises a 401" do
397
+ allow(@winrm).to receive(:relay_winrm_command).and_raise(WinRM::WinRMHTTPTransportError.new('', '401'))
398
+ allow(@winrm.ui).to receive(:info)
399
+ allow(@winrm.ui).to receive(:error)
400
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(100) }
401
+ end
402
+
403
+ it "exits with 0 if command execution raises a 401 and suppress_auth_failure is set to true" do
404
+ @winrm.config[:suppress_auth_failure] = true
405
+ allow(@winrm).to receive(:relay_winrm_command).and_raise(WinRM::WinRMHTTPTransportError.new('', '401'))
406
+ exit_code = @winrm.run_with_pretty_exceptions
407
+ expect(exit_code).to eq(401)
408
+ end
409
+
410
+ it "prints a hint on failure for negotiate authentication" do
411
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
412
+ @winrm.config[:winrm_transport] = "plaintext"
413
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
414
+ allow(Chef::Knife::WinrmSession).to receive(:new)
415
+ allow(@winrm).to receive(:relay_winrm_command).and_raise(WinRM::WinRMAuthorizationError.new)
416
+ allow(@winrm.ui).to receive(:error)
417
+ allow(@winrm.ui).to receive(:info)
418
+ expect(@winrm.ui).to receive(:info).with(Chef::Knife::Winrm::FAILED_NOT_BASIC_HINT)
419
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit)
420
+ end
421
+
422
+ it "prints a hint on failure for basic authentication" do
423
+ @winrm.config[:winrm_authentication_protocol] = "basic"
424
+ @winrm.config[:winrm_transport] = "plaintext"
425
+ allow(@winrm).to receive(:relay_winrm_command).and_raise(WinRM::WinRMHTTPTransportError.new('', '401'))
426
+ allow(@winrm.ui).to receive(:error)
427
+ allow(@winrm.ui).to receive(:info)
428
+ expect(@winrm.ui).to receive(:info).with(Chef::Knife::Winrm::FAILED_BASIC_HINT)
429
+ expect { @winrm.run_with_pretty_exceptions }.to raise_error(SystemExit)
430
+ end
431
+
432
+ context "when winrm_authentication_protocol specified" do
433
+ before do
434
+ Chef::Config[:knife] = {:winrm_transport => 'plaintext'}
435
+ allow(@winrm).to receive(:relay_winrm_command).and_return(0)
436
+ end
437
+
438
+ it "sets sspinegotiate transport on windows for 'negotiate' authentication" do
439
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
440
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
441
+ expect(@winrm).to receive(:create_winrm_session).with({:user=>".\\testuser", :password=>"testpassword", :port=>"5985", :no_ssl_peer_verification => false, :basic_auth_only=>false, :operation_timeout=>1800, :transport=>:sspinegotiate, :disable_sspi=>false, :host=>"localhost"})
442
+ exit_code = @winrm.run
443
+ end
444
+
445
+ it "does not have winrm opts transport set to sspinegotiate for unix" do
446
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
447
+ allow(Chef::Platform).to receive(:windows?).and_return(false)
448
+ allow(@winrm).to receive(:exit)
449
+ expect(@winrm).to receive(:create_winrm_session).with({:user=>".\\testuser", :password=>"testpassword", :port=>"5985", :no_ssl_peer_verification=>false, :basic_auth_only=>false, :operation_timeout=>1800, :transport=>:plaintext, :disable_sspi=>true, :host=>"localhost"})
450
+ exit_code = @winrm.run
451
+ end
452
+
453
+ it "applies winrm monkey patch on windows if 'negotiate' authentication and 'plaintext' transport is specified", :windows_only => true do
454
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
455
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
456
+ allow(@winrm.ui).to receive(:warn)
457
+ @winrm.run
458
+ end
459
+
460
+ it "raises an error if value is other than [basic, negotiate, kerberos]" do
461
+ @winrm.config[:winrm_authentication_protocol] = "invalid"
462
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
463
+ expect(@winrm.ui).to receive(:error)
464
+ expect { @winrm.run }.to raise_error(SystemExit)
465
+ end
466
+
467
+ it "skips the winrm monkey patch for 'basic' authentication" do
468
+ @winrm.config[:winrm_authentication_protocol] = "basic"
469
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
470
+ @winrm.run
471
+ end
472
+
473
+ it "skips the winrm monkey patch for 'kerberos' authentication" do
474
+ @winrm.config[:winrm_authentication_protocol] = "kerberos"
475
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
476
+ @winrm.run
477
+ end
478
+
479
+ it "skips the winrm monkey patch for 'ssl' transport and 'negotiate' authentication" do
480
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
481
+ @winrm.config[:winrm_transport] = "ssl"
482
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
483
+ @winrm.run
484
+ end
485
+
486
+ it "disables sspi and skips the winrm monkey patch for 'ssl' transport and 'basic' authentication" do
487
+ @winrm.config[:winrm_authentication_protocol] = "basic"
488
+ @winrm.config[:winrm_transport] = "ssl"
489
+ @winrm.config[:winrm_port] = "5986"
490
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
491
+ expect(@winrm).to receive(:create_winrm_session).with({:user=>"testuser", :password=>"testpassword", :port=>"5986", :no_ssl_peer_verification=>false, :basic_auth_only=>true, :operation_timeout=>1800, :transport=>:ssl, :disable_sspi=>true, :host=>"localhost"})
492
+ @winrm.run
493
+ end
215
494
 
495
+ it "prints a warning on linux for unencrypted negotiate authentication" do
496
+ @winrm.config[:winrm_authentication_protocol] = "negotiate"
497
+ @winrm.config[:winrm_transport] = "plaintext"
498
+ allow(Chef::Platform).to receive(:windows?).and_return(false)
499
+ expect(@winrm.ui).to receive(:warn).once
500
+ expect { @winrm.run }.to_not raise_error(SystemExit)
216
501
  end
217
502
  end
218
503
  end