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
@@ -18,6 +18,7 @@
18
18
 
19
19
  require 'spec_helper'
20
20
 
21
+ Chef::Knife::Winrm.load_deps
21
22
 
22
23
  describe Chef::Knife::BootstrapWindowsWinrm do
23
24
  before(:all) do
@@ -27,6 +28,7 @@ describe Chef::Knife::BootstrapWindowsWinrm do
27
28
  before do
28
29
  # Kernel.stub(:sleep).and_return 10
29
30
  allow(bootstrap).to receive(:sleep).and_return(10)
31
+ allow(File).to receive(:exist?).with(File.expand_path(Chef::Config[:validation_key])).and_return(true)
30
32
  end
31
33
 
32
34
  after do
@@ -34,64 +36,158 @@ describe Chef::Knife::BootstrapWindowsWinrm do
34
36
  allow(bootstrap).to receive(:sleep).and_return(10)
35
37
  end
36
38
 
37
- let (:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['winrm', '-d', 'windows-chef-client-msi', '-x', 'Administrator', 'localhost']) }
39
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['winrm', '-d', 'windows-chef-client-msi', '-x', 'Administrator', 'localhost']) }
40
+ let(:session) { Chef::Knife::Winrm::WinrmSession.new({ :host => 'winrm.cloudapp.net', :port => '5986', :transport => :ssl }) }
41
+
42
+ let(:initial_fail_count) { 4 }
43
+
44
+ context "knife secret-file && knife secret options are passed" do
45
+ before do
46
+ Chef::Config.reset
47
+ Chef::Config[:knife][:encrypted_data_bag_secret_file] = "/tmp/encrypted_data_bag_secret"
48
+ Chef::Config[:knife][:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_knife_secret_option"
49
+ end
50
+ it "gives preference to secret key passed under knife's secret-file option" do
51
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
52
+ Chef::Config[:knife][:encrypted_data_bag_secret_file]).
53
+ and_return("data_bag_secret_key_passed_under_knife_secret_file_option")
54
+ expect(bootstrap.load_correct_secret).to eq(
55
+ "data_bag_secret_key_passed_under_knife_secret_file_option")
56
+ end
57
+ end
58
+
59
+ context "cli secret-file && cli secret options are passed" do
60
+ before do
61
+ Chef::Config.reset
62
+ bootstrap.config[:encrypted_data_bag_secret_file] = "/tmp/encrypted_data_bag_secret"
63
+ bootstrap.config[:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_cli_secret_option"
64
+ end
65
+ it "gives preference to secret key passed under cli's secret-file option" do
66
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
67
+ bootstrap.config[:encrypted_data_bag_secret_file]).
68
+ and_return("data_bag_secret_key_passed_under_cli_secret_file_option")
69
+ expect(bootstrap.load_correct_secret).to eq(
70
+ "data_bag_secret_key_passed_under_cli_secret_file_option")
71
+ end
72
+ end
73
+
74
+ context "knife secret-file, knife secret, cli secret-file && cli secret options are passed" do
75
+ before do
76
+ Chef::Config.reset
77
+ Chef::Config[:knife][:encrypted_data_bag_secret_file] = "/tmp/knife_encrypted_data_bag_secret"
78
+ Chef::Config[:knife][:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_knife_secret_option"
79
+ bootstrap.config[:encrypted_data_bag_secret_file] = "/tmp/cli_encrypted_data_bag_secret"
80
+ bootstrap.config[:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_cli_secret_option"
81
+ end
82
+ it "gives preference to secret key passed under cli's secret-file option" do
83
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
84
+ Chef::Config[:knife][:encrypted_data_bag_secret_file]).
85
+ and_return("data_bag_secret_key_passed_under_knife_secret_file_option")
86
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
87
+ bootstrap.config[:encrypted_data_bag_secret_file]).
88
+ and_return("data_bag_secret_key_passed_under_cli_secret_file_option")
89
+ expect(bootstrap.load_correct_secret).to eq(
90
+ "data_bag_secret_key_passed_under_cli_secret_file_option")
91
+ end
92
+ end
93
+
94
+ context "knife secret-file && cli secret options are passed" do
95
+ before do
96
+ Chef::Config.reset
97
+ Chef::Config[:knife][:encrypted_data_bag_secret_file] = "/tmp/encrypted_data_bag_secret"
98
+ bootstrap.config[:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_cli_secret_option"
99
+ end
100
+ it "gives preference to secret key passed under cli's secret option" do
101
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
102
+ Chef::Config[:knife][:encrypted_data_bag_secret_file]).
103
+ and_return("data_bag_secret_key_passed_under_knife_secret_file_option")
104
+ expect(bootstrap.load_correct_secret).to eq(
105
+ "data_bag_secret_key_passed_under_cli_secret_option")
106
+ end
107
+ end
108
+
109
+ context "knife secret && cli secret-file options are passed" do
110
+ before do
111
+ Chef::Config.reset
112
+ Chef::Config[:knife][:encrypted_data_bag_secret] = "data_bag_secret_key_passed_under_knife_secret_option"
113
+ bootstrap.config[:encrypted_data_bag_secret_file] = "/tmp/encrypted_data_bag_secret"
114
+ end
115
+ it "gives preference to secret key passed under cli's secret-file option" do
116
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
117
+ bootstrap.config[:encrypted_data_bag_secret_file]).
118
+ and_return("data_bag_secret_key_passed_under_cli_secret_file_option")
119
+ expect(bootstrap.load_correct_secret).to eq(
120
+ "data_bag_secret_key_passed_under_cli_secret_file_option")
121
+ end
122
+ end
123
+
124
+ context "cli secret-file option is passed" do
125
+ before do
126
+ Chef::Config.reset
127
+ bootstrap.config[:encrypted_data_bag_secret_file] = "/tmp/encrypted_data_bag_secret"
128
+ end
129
+ it "takes the secret key passed under cli's secret-file option" do
130
+ allow(Chef::EncryptedDataBagItem).to receive(:load_secret).with(
131
+ bootstrap.config[:encrypted_data_bag_secret_file]).
132
+ and_return("data_bag_secret_key_passed_under_cli_secret_file_option")
133
+ expect(bootstrap.load_correct_secret).to eq(
134
+ "data_bag_secret_key_passed_under_cli_secret_file_option")
135
+ end
136
+ end
38
137
 
39
- let(:initial_fail_count) { 4 }
40
138
  it 'should retry if a 401 is received from WinRM' do
41
- call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '401'}}
139
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError.new('', '401')}}
42
140
  call_result_sequence.push(0)
43
141
  allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
142
+ allow(bootstrap).to receive(:print)
143
+ allow(bootstrap.ui).to receive(:info)
44
144
 
45
145
  expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
46
146
  bootstrap.send(:wait_for_remote_response, 2)
47
147
  end
48
148
 
49
149
  it 'should retry if something other than a 401 is received from WinRM' do
50
- call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
150
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError.new('', '500')}}
51
151
  call_result_sequence.push(0)
52
152
  allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
153
+ allow(bootstrap).to receive(:print)
154
+ allow(bootstrap.ui).to receive(:info)
53
155
 
54
156
  expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
55
157
  bootstrap.send(:wait_for_remote_response, 2)
56
158
  end
57
159
 
58
- it 'should have a wait timeout of 2 minutes by default' do
59
- allow(bootstrap).to receive(:run_command).and_raise(WinRM::WinRMHTTPTransportError)
60
- allow(bootstrap).to receive(:create_bootstrap_bat_command).and_raise(SystemExit)
61
- expect(bootstrap).to receive(:wait_for_remote_response).with(2)
62
- allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
63
- bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
64
- expect { bootstrap.bootstrap }.to raise_error(SystemExit)
65
- end
66
-
67
160
  it 'should keep retrying at 10s intervals if the timeout in minutes has not elapsed' do
68
- call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError, '500'}}
161
+ call_result_sequence = Array.new(initial_fail_count) {lambda {raise WinRM::WinRMHTTPTransportError.new('', '500')}}
69
162
  call_result_sequence.push(0)
70
163
  allow(bootstrap).to receive(:run_command).and_return(*call_result_sequence)
164
+ allow(bootstrap).to receive(:print)
165
+ allow(bootstrap.ui).to receive(:info)
71
166
 
72
167
  expect(bootstrap).to receive(:run_command).exactly(call_result_sequence.length).times
73
168
  bootstrap.send(:wait_for_remote_response, 2)
74
169
  end
75
170
 
76
- it "should exit bootstrap with non-zero status if the bootstrap fails" do
77
- command_status = 1
78
-
79
- #Stub out calls to create the session and just get the exit codes back
80
- winrm_mock = Chef::Knife::Winrm.new
81
- allow(Chef::Knife::Winrm).to receive(:new).and_return(winrm_mock)
82
- allow(winrm_mock).to receive(:configure_session)
83
- allow(winrm_mock).to receive(:winrm_command)
84
- session_mock = EventMachine::WinRM::Session.new
85
- allow(EventMachine::WinRM::Session).to receive(:new).and_return(session_mock)
86
- allow(session_mock).to receive(:exit_codes).and_return({"thishost" => command_status})
87
-
88
- #Skip over templating stuff and checking with the remote end
89
- allow(bootstrap).to receive(:create_bootstrap_bat_command)
90
- allow(bootstrap).to receive(:wait_for_remote_response)
91
-
92
- expect { bootstrap.run_with_pretty_exceptions }.to raise_error(SystemExit) { |e| expect(e.status).to eq(command_status) }
171
+ it 'should have a wait timeout of 2 minutes by default' do
172
+ allow(bootstrap).to receive(:run_command).and_raise(WinRM::WinRMHTTPTransportError.new('','500'))
173
+ allow(bootstrap).to receive(:create_bootstrap_bat_command).and_raise(SystemExit)
174
+ expect(bootstrap).to receive(:wait_for_remote_response).with(2)
175
+ allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
176
+ allow(bootstrap.ui).to receive(:info)
177
+ bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
178
+ expect { bootstrap.bootstrap }.to raise_error(SystemExit)
93
179
  end
94
180
 
181
+ it 'should not a wait for timeout on Errno::ECONNREFUSED' do
182
+ allow(bootstrap).to receive(:run_command).and_raise(Errno::ECONNREFUSED.new)
183
+ allow(bootstrap.ui).to receive(:info)
184
+ bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
185
+ expect(bootstrap.ui).to receive(:error).with("Connection refused connecting to localhost:5985.")
186
+
187
+ # wait_for_remote_response is protected method, So define singleton test method to call it.
188
+ bootstrap.define_singleton_method(:test_wait_for_remote_response){wait_for_remote_response(bootstrap.options[:auth_timeout][:default])}
189
+ expect { bootstrap.test_wait_for_remote_response }.to raise_error(Errno::ECONNREFUSED)
190
+ end
95
191
 
96
192
  it 'should stop retrying if more than 2 minutes has elapsed' do
97
193
  times = [ Time.new(2014, 4, 1, 22, 25), Time.new(2014, 4, 1, 22, 51), Time.new(2014, 4, 1, 22, 28) ]
@@ -99,8 +195,46 @@ describe Chef::Knife::BootstrapWindowsWinrm do
99
195
  run_command_result = lambda {raise WinRM::WinRMHTTPTransportError, '401'}
100
196
  allow(bootstrap).to receive(:validate_name_args!).and_return(nil)
101
197
  allow(bootstrap).to receive(:run_command).and_return(run_command_result)
198
+ allow(bootstrap).to receive(:print)
199
+ allow(bootstrap.ui).to receive(:info)
200
+ allow(bootstrap.ui).to receive(:error)
102
201
  expect(bootstrap).to receive(:run_command).exactly(1).times
103
- bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
202
+ bootstrap.config[:auth_timeout] = bootstrap.options[:auth_timeout][:default]
104
203
  expect { bootstrap.bootstrap }.to raise_error RuntimeError
105
204
  end
205
+
206
+ context "when validation_key is not present" do
207
+ context "using chef 11", :chef_lt_12_only do
208
+ before do
209
+ allow(File).to receive(:exist?).with(File.expand_path(Chef::Config[:validation_key])).and_return(false)
210
+ end
211
+
212
+ it 'raises an exception if validation_key is not present in chef 11' do
213
+ expect(bootstrap.ui).to receive(:error)
214
+ expect { bootstrap.bootstrap }.to raise_error(SystemExit)
215
+ end
216
+ end
217
+
218
+ context "using chef 12", :chef_gte_12_only do
219
+ before do
220
+ allow(File).to receive(:exist?).with(File.expand_path(Chef::Config[:validation_key])).and_return(false)
221
+ bootstrap.client_builder = instance_double("Chef::Knife::Bootstrap::ClientBuilder", :run => nil, :client_path => nil)
222
+ Chef::Config[:knife] = {:chef_node_name => 'foo.example.com'}
223
+ end
224
+
225
+ it 'raises an exception if winrm_authentication_protocol is basic and transport is plaintext' do
226
+ Chef::Config[:knife] = {:winrm_authentication_protocol => 'basic', :winrm_transport => 'plaintext', :chef_node_name => 'foo.example.com'}
227
+ expect(bootstrap.ui).to receive(:error)
228
+ expect { bootstrap.run }.to raise_error(SystemExit)
229
+ end
230
+
231
+ it 'raises an exception if chef_node_name is not present ' do
232
+ Chef::Config[:knife] = {:chef_node_name => nil}
233
+ expect(bootstrap.client_builder).not_to receive(:run)
234
+ expect(bootstrap.client_builder).not_to receive(:client_path)
235
+ expect(bootstrap.ui).to receive(:error)
236
+ expect { bootstrap.bootstrap }.to raise_error(SystemExit)
237
+ end
238
+ end
239
+ end
106
240
  end
@@ -0,0 +1,151 @@
1
+ #
2
+ # Author:: Bryan McLellan <btm@loftninjas.org>
3
+ # Copyright:: Copyright (c) 2014 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 'spec_helper'
20
+
21
+ describe Chef::Knife::Core::WindowsBootstrapContext do
22
+ let(:mock_bootstrap_context) { Chef::Knife::Core::WindowsBootstrapContext.new({ }, nil, { :knife => {} }) }
23
+
24
+ before do
25
+ allow(Chef::Knife::Core::WindowsBootstrapContext).to receive(:new).and_return(mock_bootstrap_context)
26
+ end
27
+
28
+ describe "validation_key", :chef_gte_12_only do
29
+ before do
30
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:validation_key => "C:\\chef\\key.pem"))
31
+ end
32
+
33
+ it "should return false if validation_key does not exist" do
34
+ allow(::File).to receive(:expand_path)
35
+ allow(::File).to receive(:exist?).and_return(false)
36
+ expect(mock_bootstrap_context.validation_key).to eq(false)
37
+ end
38
+ end
39
+
40
+ describe "latest_current_windows_chef_version_query" do
41
+ it "returns the major version of the current version of Chef" do
42
+ stub_const("Chef::VERSION", '11.1.2')
43
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=11")
44
+ end
45
+
46
+ it "does not add prerelease if the version of Chef installed is a prerelease" do
47
+ stub_const("Chef::VERSION", '42.0.1.alpha.1')
48
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).not_to match(/&prerelease=true/)
49
+ end
50
+
51
+ it "does add prerelease if the version specified to be installed is a prerelease" do
52
+ allow(mock_bootstrap_context).to receive(:knife_config).and_return(Mash.new(:bootstrap_version => "12.0.0.alpha.1"))
53
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=12.0.0.alpha.1&prerelease=true")
54
+ end
55
+
56
+ context "when the prerelease config option is set" do
57
+ before do
58
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:prerelease => true))
59
+ end
60
+
61
+ it "sets prerelease to true in the returned string" do
62
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&prerelease=true")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "msi_url" do
68
+ context "when config option is not set" do
69
+ before do
70
+ expect(mock_bootstrap_context).to receive(:latest_current_windows_chef_version_query).and_return("&v=something")
71
+ end
72
+
73
+ it "returns a chef.io msi url with minimal url parameters" do
74
+ reference_url = "https://www.chef.io/chef/download?p=windows&v=something"
75
+ expect(mock_bootstrap_context.msi_url).to eq(reference_url)
76
+ end
77
+
78
+ it "returns a chef.io msi url with provided url parameters substituted" do
79
+ reference_url = "https://www.chef.io/chef/download?p=windows&pv=machine&m=arch&DownloadContext=ctx&v=something"
80
+ expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(reference_url)
81
+ end
82
+ end
83
+
84
+ context "when msi_url config option is set" do
85
+ let(:custom_url) { "file://something" }
86
+
87
+ before do
88
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:msi_url => custom_url))
89
+ end
90
+
91
+ it "returns the overriden url" do
92
+ expect(mock_bootstrap_context.msi_url).to eq(custom_url)
93
+ end
94
+
95
+ it "doesn't introduce any unnecessary query parameters if provided by the template" do
96
+ expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(custom_url)
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "bootstrap_install_command for bootstrap through WinRM" do
102
+ context "when bootstrap_install_command option is passed on CLI" do
103
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['--bootstrap-install-command', 'chef-client']) }
104
+ before do
105
+ bootstrap.config[:bootstrap_install_command] = "chef-client"
106
+ end
107
+
108
+ it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
109
+ expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
110
+ end
111
+
112
+ after do
113
+ bootstrap.config.delete(:bootstrap_install_command)
114
+ Chef::Config[:knife].delete(:bootstrap_install_command)
115
+ end
116
+ end
117
+
118
+ context "when bootstrap_install_command option is not passed on CLI" do
119
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new([]) }
120
+ it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
121
+ expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
122
+ end
123
+ end
124
+ end
125
+
126
+ describe "bootstrap_install_command for bootstrap through SSH" do
127
+ context "when bootstrap_install_command option is passed on CLI" do
128
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new(['--bootstrap-install-command', 'chef-client']) }
129
+ before do
130
+ bootstrap.config[:bootstrap_install_command] = "chef-client"
131
+ end
132
+
133
+ it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
134
+ expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
135
+ end
136
+
137
+ after do
138
+ bootstrap.config.delete(:bootstrap_install_command)
139
+ Chef::Config[:knife].delete(:bootstrap_install_command)
140
+ end
141
+ end
142
+
143
+ context "when bootstrap_install_command option is not passed on CLI" do
144
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new([]) }
145
+ it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
146
+ expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
147
+ end
148
+ end
149
+ end
150
+
151
+ end
@@ -0,0 +1,90 @@
1
+ #
2
+ # Author:: Mukta Aphale <mukta.aphale@clogeny.com>
3
+ # Copyright:: Copyright (c) 2014 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
+ require 'chef/knife/windows_cert_generate'
21
+ require 'openssl'
22
+
23
+ describe Chef::Knife::WindowsCertGenerate do
24
+ before(:all) do
25
+ @certgen = Chef::Knife::WindowsCertGenerate.new(["-H","something.mydomain.com"])
26
+ end
27
+
28
+ it "generates RSA key pair" do
29
+ @certgen.config[:key_length] = 2048
30
+ key = @certgen.generate_keypair
31
+ expect(key).to be_instance_of OpenSSL::PKey::RSA
32
+ end
33
+
34
+ it "generates X509 certificate" do
35
+ @certgen.config[:domain] = "test.com"
36
+ @certgen.config[:cert_validity] = "24"
37
+ key = @certgen.generate_keypair
38
+ certificate = @certgen.generate_certificate key
39
+ expect(certificate).to be_instance_of OpenSSL::X509::Certificate
40
+ end
41
+
42
+ it "writes certificate to file" do
43
+ expect(File).to receive(:open).exactly(3).times
44
+ cert = double(OpenSSL::X509::Certificate.new)
45
+ key = double(OpenSSL::PKey::RSA.new)
46
+ @certgen.config[:cert_passphrase] = "password"
47
+ expect(OpenSSL::PKCS12).to receive(:create).with("password", "winrmcert", key, cert)
48
+ @certgen.write_certificate_to_file cert, "test", key
49
+ end
50
+
51
+ context "when creating certificate files" do
52
+ before do
53
+ @certgen.thumbprint = "TEST_THUMBPRINT"
54
+ allow(Dir).to receive(:glob).and_return([])
55
+ allow(@certgen).to receive(:generate_keypair)
56
+ allow(@certgen).to receive(:generate_certificate)
57
+ expect(@certgen.ui).to receive(:info).with("Generated Certificates:")
58
+ expect(@certgen.ui).to receive(:info).with("- winrmcert.pfx - PKCS12 format key pair. Contains public and private keys, can be used with an SSL server.")
59
+ expect(@certgen.ui).to receive(:info).with("- winrmcert.b64 - Base64 encoded PKCS12 key pair. Contains public and private keys, used by some cloud provider API's to configure SSL servers.")
60
+ expect(@certgen.ui).to receive(:info).with("- winrmcert.pem - Base64 encoded public certificate only. Required by the client to connect to the server.")
61
+ expect(@certgen.ui).to receive(:info).with("Certificate Thumbprint: TEST_THUMBPRINT")
62
+ end
63
+
64
+ it "writes out certificates" do
65
+ @certgen.config[:output_file] = 'winrmcert'
66
+
67
+ expect(@certgen).to receive(:certificates_already_exist?).and_return(false)
68
+ expect(@certgen).to receive(:write_certificate_to_file)
69
+ @certgen.run
70
+ end
71
+
72
+ it "prompts when certificates already exist" do
73
+ file_path = 'winrmcert'
74
+ @certgen.config[:output_file] = file_path
75
+
76
+ allow(Dir).to receive(:glob).and_return([file_path])
77
+ expect(@certgen).to receive(:confirm).with("Do you really want to overwrite existing certificates")
78
+ expect(@certgen).to receive(:write_certificate_to_file)
79
+ @certgen.run
80
+ end
81
+
82
+ it "creates certificate on specified file path" do
83
+ file_path = "/tmp/winrmcert"
84
+ @certgen.name_args = [file_path]
85
+
86
+ expect(@certgen).to receive(:write_certificate_to_file) # FIXME: this should be testing that we get /tmp/winrmcert as the filename
87
+ @certgen.run
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,51 @@
1
+ #
2
+ # Author:: Mukta Aphale <mukta.aphale@clogeny.com>
3
+ # Copyright:: Copyright (c) 2014 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
+ require 'chef/knife/windows_cert_install'
21
+
22
+ describe Chef::Knife::WindowsCertInstall do
23
+ context "on Windows" do
24
+ before do
25
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
26
+ @certinstall = Chef::Knife::WindowsCertInstall.new
27
+ end
28
+
29
+ it "installs certificate" do
30
+ @certinstall.name_args = ["test-path"]
31
+ @certinstall.config[:cert_passphrase] = "your-secret!"
32
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
33
+ expect(@certinstall).to receive(:`).with("powershell.exe -Command \" 'your-secret!' | certutil -importPFX 'test-path' AT_KEYEXCHANGE\"")
34
+ expect(@certinstall.ui).to receive(:info).with("Certificate added to Certificate Store")
35
+ expect(@certinstall.ui).to receive(:info).with("Adding certificate to the Windows Certificate Store...")
36
+ @certinstall.run
37
+ end
38
+ end
39
+
40
+ context "not on Windows" do
41
+ before do
42
+ allow(Chef::Platform).to receive(:windows?).and_return(false)
43
+ @listener = Chef::Knife::WindowsListenerCreate.new
44
+ end
45
+
46
+ it "exits with an error" do
47
+ expect(@listener.ui).to receive(:error)
48
+ expect { @listener.run }.to raise_error(SystemExit)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,76 @@
1
+ #
2
+ # Author:: Mukta Aphale <mukta.aphale@clogeny.com>
3
+ # Copyright:: Copyright (c) 2014 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
+ require 'chef/knife/windows_listener_create'
21
+
22
+ describe Chef::Knife::WindowsListenerCreate do
23
+ context "on Windows" do
24
+ before do
25
+ allow(Chef::Platform).to receive(:windows?).and_return(true)
26
+ @listener = Chef::Knife::WindowsListenerCreate.new
27
+ end
28
+
29
+ it "creates winrm listener" do
30
+ @listener.config[:hostname] = "host"
31
+ @listener.config[:cert_thumbprint] = "CERT-THUMBPRINT"
32
+ @listener.config[:port] = "5986"
33
+ expect(@listener).to receive(:`).with("winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=\"host\";CertificateThumbprint=\"CERT-THUMBPRINT\";Port=\"5986\"}")
34
+ expect(@listener.ui).to receive(:info).with("WinRM listener created with Port: 5986 and CertificateThumbprint: CERT-THUMBPRINT")
35
+ @listener.run
36
+ end
37
+
38
+ it "raise an error on command failure" do
39
+ @listener.config[:hostname] = "host"
40
+ @listener.config[:cert_thumbprint] = "CERT-THUMBPRINT"
41
+ @listener.config[:port] = "5986"
42
+ @listener.config[:basic_auth] = true
43
+ expect(@listener).to receive(:`).with("winrm create winrm/config/Listener?Address=*+Transport=HTTPS @{Hostname=\"host\";CertificateThumbprint=\"CERT-THUMBPRINT\";Port=\"5986\"}")
44
+ expect($?).to receive(:exitstatus).and_return(100)
45
+ expect(@listener.ui).to receive(:error).with("Error creating WinRM listener. use -VV for more details.")
46
+ expect(@listener.ui).to_not receive(:info).with("WinRM listener created with Port: 5986 and CertificateThumbprint: CERT-THUMBPRINT")
47
+ expect { @listener.run }.to raise_error(SystemExit)
48
+ end
49
+
50
+ it "creates winrm listener with cert install option" do
51
+ @listener.config[:hostname] = "host"
52
+ @listener.config[:cert_thumbprint] = "CERT-THUMBPRINT"
53
+ @listener.config[:port] = "5986"
54
+ @listener.config[:cert_install] = true
55
+ allow(@listener).to receive(:get_cert_passphrase).and_return("your-secret!")
56
+ expect(@listener).to receive(:`).with("powershell.exe -Command \" 'your-secret!' | certutil -importPFX 'true' AT_KEYEXCHANGE\"")
57
+ expect(@listener).to receive(:`).with("powershell.exe -Command \" echo (Get-PfxCertificate true).thumbprint \"")
58
+ expect(@listener.ui).to receive(:info).with("Certificate installed to Certificate Store")
59
+ expect(@listener.ui).to receive(:info).with("Certificate Thumbprint: ")
60
+ allow(@listener).to receive(:puts)
61
+ @listener.run
62
+ end
63
+ end
64
+
65
+ context "not on Windows" do
66
+ before do
67
+ allow(Chef::Platform).to receive(:windows?).and_return(false)
68
+ @listener = Chef::Knife::WindowsListenerCreate.new
69
+ end
70
+
71
+ it "exits with an error" do
72
+ expect(@listener.ui).to receive(:error)
73
+ expect { @listener.run }.to raise_error(SystemExit)
74
+ end
75
+ end
76
+ end