knife-windows 1.7.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +26 -26
  3. data/CHANGELOG.md +139 -135
  4. data/DOC_CHANGES.md +22 -22
  5. data/Gemfile +13 -13
  6. data/README.md +404 -404
  7. data/RELEASE_NOTES.md +9 -9
  8. data/appveyor.yml +39 -39
  9. data/ci.gemfile +16 -16
  10. data/knife-windows.gemspec +26 -26
  11. data/lib/chef/knife/bootstrap/windows-chef-client-msi.erb +246 -246
  12. data/lib/chef/knife/bootstrap_windows_base.rb +443 -443
  13. data/lib/chef/knife/bootstrap_windows_ssh.rb +116 -116
  14. data/lib/chef/knife/bootstrap_windows_winrm.rb +102 -102
  15. data/lib/chef/knife/core/windows_bootstrap_context.rb +378 -378
  16. data/lib/chef/knife/knife_windows_base.rb +33 -33
  17. data/lib/chef/knife/windows_cert_generate.rb +155 -155
  18. data/lib/chef/knife/windows_cert_install.rb +68 -68
  19. data/lib/chef/knife/windows_helper.rb +36 -36
  20. data/lib/chef/knife/windows_listener_create.rb +107 -107
  21. data/lib/chef/knife/winrm.rb +122 -122
  22. data/lib/chef/knife/winrm_base.rb +128 -128
  23. data/lib/chef/knife/winrm_knife_base.rb +307 -307
  24. data/lib/chef/knife/winrm_session.rb +98 -98
  25. data/lib/chef/knife/winrm_shared_options.rb +47 -47
  26. data/lib/chef/knife/wsman_endpoint.rb +44 -44
  27. data/lib/chef/knife/wsman_test.rb +118 -118
  28. data/lib/knife-windows/path_helper.rb +242 -234
  29. data/lib/knife-windows/version.rb +6 -6
  30. data/spec/assets/fake_trusted_certs/excluded.txt +2 -0
  31. data/spec/assets/fake_trusted_certs/github.pem +42 -0
  32. data/spec/assets/fake_trusted_certs/google.crt +41 -0
  33. data/spec/assets/win_fake_trusted_cert_script.txt +89 -0
  34. data/spec/assets/win_template_rendered_with_bootstrap_install_command.txt +223 -223
  35. data/spec/assets/win_template_rendered_with_bootstrap_install_command_on_12_5_client.txt +223 -223
  36. data/spec/assets/win_template_rendered_without_bootstrap_install_command.txt +335 -335
  37. data/spec/assets/win_template_rendered_without_bootstrap_install_command_on_12_5_client.txt +335 -335
  38. data/spec/assets/win_template_unrendered.txt +246 -246
  39. data/spec/dummy_winrm_connection.rb +21 -21
  40. data/spec/functional/bootstrap_download_spec.rb +236 -236
  41. data/spec/spec_helper.rb +94 -94
  42. data/spec/unit/knife/bootstrap_options_spec.rb +157 -157
  43. data/spec/unit/knife/bootstrap_template_spec.rb +98 -98
  44. data/spec/unit/knife/bootstrap_windows_winrm_spec.rb +423 -423
  45. data/spec/unit/knife/core/windows_bootstrap_context_spec.rb +213 -177
  46. data/spec/unit/knife/windows_cert_generate_spec.rb +90 -90
  47. data/spec/unit/knife/windows_cert_install_spec.rb +51 -51
  48. data/spec/unit/knife/windows_listener_create_spec.rb +76 -76
  49. data/spec/unit/knife/winrm_session_spec.rb +95 -95
  50. data/spec/unit/knife/winrm_spec.rb +500 -500
  51. data/spec/unit/knife/wsman_test_spec.rb +209 -209
  52. metadata +7 -3
@@ -1,177 +1,213 @@
1
- #
2
- # Author:: Bryan McLellan <btm@loftninjas.org>
3
- # Copyright:: Copyright (c) 2014-2016 Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require '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 "fips" do
29
- before do
30
- Chef::Config[:fips] = fips_mode
31
- end
32
-
33
- after do
34
- Chef::Config.reset!
35
- end
36
-
37
- context "when fips is set" do
38
- let(:fips_mode) { true }
39
-
40
- it "sets fips mode in the client.rb" do
41
- expect(mock_bootstrap_context.config_content).to match(/fips true/)
42
- end
43
- end
44
-
45
- context "when fips is not set" do
46
- let(:fips_mode) { false }
47
-
48
- it "sets fips mode in the client.rb" do
49
- expect(mock_bootstrap_context.config_content).not_to match(/fips true/)
50
- end
51
- end
52
- end
53
-
54
- describe "validation_key", :chef_gte_12_only do
55
- before do
56
- mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:validation_key => "C:\\chef\\key.pem"))
57
- end
58
-
59
- it "should return false if validation_key does not exist" do
60
- allow(::File).to receive(:expand_path)
61
- allow(::File).to receive(:exist?).and_return(false)
62
- expect(mock_bootstrap_context.validation_key).to eq(false)
63
- end
64
- end
65
-
66
- describe "latest_current_windows_chef_version_query" do
67
- it "returns the major version of the current version of Chef" do
68
- stub_const("Chef::VERSION", '11.1.2')
69
- expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=11")
70
- end
71
-
72
- it "does not add prerelease if the version of Chef installed is a prerelease" do
73
- stub_const("Chef::VERSION", '42.0.1.alpha.1')
74
- expect(mock_bootstrap_context.latest_current_windows_chef_version_query).not_to match(/&prerelease=true/)
75
- end
76
-
77
- it "does add prerelease if the version specified to be installed is a prerelease" do
78
- allow(mock_bootstrap_context).to receive(:knife_config).and_return(Mash.new(:bootstrap_version => "12.0.0.alpha.1"))
79
- expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=12.0.0.alpha.1&prerelease=true")
80
- end
81
-
82
- context "when the prerelease config option is set" do
83
- before do
84
- mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:prerelease => true))
85
- end
86
-
87
- it "sets prerelease to true in the returned string" do
88
- expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&prerelease=true")
89
- end
90
- end
91
- end
92
-
93
- describe "msi_url" do
94
- context "when config option is not set" do
95
- before do
96
- expect(mock_bootstrap_context).to receive(:latest_current_windows_chef_version_query).and_return("&v=something")
97
- end
98
-
99
- it "returns a chef.io msi url with minimal url parameters" do
100
- reference_url = "https://www.chef.io/chef/download?p=windows&v=something"
101
- expect(mock_bootstrap_context.msi_url).to eq(reference_url)
102
- end
103
-
104
- it "returns a chef.io msi url with provided url parameters substituted" do
105
- reference_url = "https://www.chef.io/chef/download?p=windows&pv=machine&m=arch&DownloadContext=ctx&v=something"
106
- expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(reference_url)
107
- end
108
- end
109
-
110
- context "when msi_url config option is set" do
111
- let(:custom_url) { "file://something" }
112
-
113
- before do
114
- mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:msi_url => custom_url))
115
- end
116
-
117
- it "returns the overriden url" do
118
- expect(mock_bootstrap_context.msi_url).to eq(custom_url)
119
- end
120
-
121
- it "doesn't introduce any unnecessary query parameters if provided by the template" do
122
- expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(custom_url)
123
- end
124
- end
125
- end
126
-
127
- describe "bootstrap_install_command for bootstrap through WinRM" do
128
- context "when bootstrap_install_command option is passed on CLI" do
129
- let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['--bootstrap-install-command', 'chef-client']) }
130
- before do
131
- bootstrap.config[:bootstrap_install_command] = "chef-client"
132
- end
133
-
134
- it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
135
- expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
136
- end
137
-
138
- after do
139
- bootstrap.config.delete(:bootstrap_install_command)
140
- Chef::Config[:knife].delete(:bootstrap_install_command)
141
- end
142
- end
143
-
144
- context "when bootstrap_install_command option is not passed on CLI" do
145
- let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new([]) }
146
- it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
147
- expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
148
- end
149
- end
150
- end
151
-
152
- describe "bootstrap_install_command for bootstrap through SSH" do
153
- context "when bootstrap_install_command option is passed on CLI" do
154
- let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new(['--bootstrap-install-command', 'chef-client']) }
155
- before do
156
- bootstrap.config[:bootstrap_install_command] = "chef-client"
157
- end
158
-
159
- it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
160
- expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
161
- end
162
-
163
- after do
164
- bootstrap.config.delete(:bootstrap_install_command)
165
- Chef::Config[:knife].delete(:bootstrap_install_command)
166
- end
167
- end
168
-
169
- context "when bootstrap_install_command option is not passed on CLI" do
170
- let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new([]) }
171
- it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
172
- expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
173
- end
174
- end
175
- end
176
-
177
- end
1
+ #
2
+ # Author:: Bryan McLellan <btm@loftninjas.org>
3
+ # Copyright:: Copyright (c) 2014-2016 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require '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 "fips" do
29
+ before do
30
+ Chef::Config[:fips] = fips_mode
31
+ end
32
+
33
+ after do
34
+ Chef::Config.reset!
35
+ end
36
+
37
+ context "when fips is set" do
38
+ let(:fips_mode) { true }
39
+
40
+ it "sets fips mode in the client.rb" do
41
+ expect(mock_bootstrap_context.config_content).to match(/fips true/)
42
+ end
43
+ end
44
+
45
+ context "when fips is not set" do
46
+ let(:fips_mode) { false }
47
+
48
+ it "sets fips mode in the client.rb" do
49
+ expect(mock_bootstrap_context.config_content).not_to match(/fips true/)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "trusted_certs_script" do
55
+ let(:mock_cert_dir) { ::File.absolute_path(::File.join('spec','assets','fake_trusted_certs')) }
56
+ let(:script_output) { mock_bootstrap_context.trusted_certs_script }
57
+ let(:crt_files) { ::Dir.glob(::File.join(mock_cert_dir, "*.crt")) }
58
+ let(:pem_files) { ::Dir.glob(::File.join(mock_cert_dir, "*.pem")) }
59
+ let(:other_files) { ::Dir.glob(::File.join(mock_cert_dir, "*"))-crt_files-pem_files }
60
+
61
+ before do
62
+ mock_bootstrap_context.instance_variable_set(:@chef_config, Mash.new(:trusted_certs_dir => mock_cert_dir))
63
+ end
64
+
65
+ it "should echo every .crt file in the trusted_certs directory" do
66
+ crt_files.each do |f|
67
+ echo_file = ::File.read(f).gsub(/^/, "echo.")
68
+ expect(script_output).to include(::File.join('trusted_certs',::File.basename(f)))
69
+ expect(script_output).to include(echo_file)
70
+ end
71
+ end
72
+
73
+ it "should echo every .pem file in the trusted_certs directory" do
74
+ pem_files.each do |f|
75
+ echo_file = ::File.read(f).gsub(/^/, "echo.")
76
+ expect(script_output).to include(::File.join('trusted_certs',::File.basename(f)))
77
+ expect(script_output).to include(echo_file)
78
+ end
79
+ end
80
+
81
+ it "should not echo files which aren't .crt or .pem files" do
82
+ other_files.each do |f|
83
+ echo_file = ::File.read(f).gsub(/^/, "echo.")
84
+ expect(script_output).to_not include(::File.join('trusted_certs',::File.basename(f)))
85
+ expect(script_output).to_not include(echo_file)
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "validation_key", :chef_gte_12_only do
91
+ before do
92
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:validation_key => "C:\\chef\\key.pem"))
93
+ end
94
+
95
+ it "should return false if validation_key does not exist" do
96
+ allow(::File).to receive(:expand_path)
97
+ allow(::File).to receive(:exist?).and_return(false)
98
+ expect(mock_bootstrap_context.validation_key).to eq(false)
99
+ end
100
+ end
101
+
102
+ describe "latest_current_windows_chef_version_query" do
103
+ it "returns the major version of the current version of Chef" do
104
+ stub_const("Chef::VERSION", '11.1.2')
105
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=11")
106
+ end
107
+
108
+ it "does not add prerelease if the version of Chef installed is a prerelease" do
109
+ stub_const("Chef::VERSION", '42.0.1.alpha.1')
110
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).not_to match(/&prerelease=true/)
111
+ end
112
+
113
+ it "does add prerelease if the version specified to be installed is a prerelease" do
114
+ allow(mock_bootstrap_context).to receive(:knife_config).and_return(Mash.new(:bootstrap_version => "12.0.0.alpha.1"))
115
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&v=12.0.0.alpha.1&prerelease=true")
116
+ end
117
+
118
+ context "when the prerelease config option is set" do
119
+ before do
120
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:prerelease => true))
121
+ end
122
+
123
+ it "sets prerelease to true in the returned string" do
124
+ expect(mock_bootstrap_context.latest_current_windows_chef_version_query).to eq("&prerelease=true")
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "msi_url" do
130
+ context "when config option is not set" do
131
+ before do
132
+ expect(mock_bootstrap_context).to receive(:latest_current_windows_chef_version_query).and_return("&v=something")
133
+ end
134
+
135
+ it "returns a chef.io msi url with minimal url parameters" do
136
+ reference_url = "https://www.chef.io/chef/download?p=windows&v=something"
137
+ expect(mock_bootstrap_context.msi_url).to eq(reference_url)
138
+ end
139
+
140
+ it "returns a chef.io msi url with provided url parameters substituted" do
141
+ reference_url = "https://www.chef.io/chef/download?p=windows&pv=machine&m=arch&DownloadContext=ctx&v=something"
142
+ expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(reference_url)
143
+ end
144
+ end
145
+
146
+ context "when msi_url config option is set" do
147
+ let(:custom_url) { "file://something" }
148
+
149
+ before do
150
+ mock_bootstrap_context.instance_variable_set(:@config, Mash.new(:msi_url => custom_url))
151
+ end
152
+
153
+ it "returns the overriden url" do
154
+ expect(mock_bootstrap_context.msi_url).to eq(custom_url)
155
+ end
156
+
157
+ it "doesn't introduce any unnecessary query parameters if provided by the template" do
158
+ expect(mock_bootstrap_context.msi_url('machine', 'arch', 'ctx')).to eq(custom_url)
159
+ end
160
+ end
161
+ end
162
+
163
+ describe "bootstrap_install_command for bootstrap through WinRM" do
164
+ context "when bootstrap_install_command option is passed on CLI" do
165
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new(['--bootstrap-install-command', 'chef-client']) }
166
+ before do
167
+ bootstrap.config[:bootstrap_install_command] = "chef-client"
168
+ end
169
+
170
+ it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
171
+ expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
172
+ end
173
+
174
+ after do
175
+ bootstrap.config.delete(:bootstrap_install_command)
176
+ Chef::Config[:knife].delete(:bootstrap_install_command)
177
+ end
178
+ end
179
+
180
+ context "when bootstrap_install_command option is not passed on CLI" do
181
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsWinrm.new([]) }
182
+ it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
183
+ expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
184
+ end
185
+ end
186
+ end
187
+
188
+ describe "bootstrap_install_command for bootstrap through SSH" do
189
+ context "when bootstrap_install_command option is passed on CLI" do
190
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new(['--bootstrap-install-command', 'chef-client']) }
191
+ before do
192
+ bootstrap.config[:bootstrap_install_command] = "chef-client"
193
+ end
194
+
195
+ it "sets the bootstrap_install_command option under Chef::Config::Knife object" do
196
+ expect(Chef::Config[:knife][:bootstrap_install_command]).to eq("chef-client")
197
+ end
198
+
199
+ after do
200
+ bootstrap.config.delete(:bootstrap_install_command)
201
+ Chef::Config[:knife].delete(:bootstrap_install_command)
202
+ end
203
+ end
204
+
205
+ context "when bootstrap_install_command option is not passed on CLI" do
206
+ let(:bootstrap) { Chef::Knife::BootstrapWindowsSsh.new([]) }
207
+ it "does not set the bootstrap_install_command option under Chef::Config::Knife object" do
208
+ expect(Chef::Config[:knife][:bootstrap_install_command]). to eq(nil)
209
+ end
210
+ end
211
+ end
212
+
213
+ end
@@ -1,90 +1,90 @@
1
- #
2
- # Author:: Mukta Aphale <mukta.aphale@clogeny.com>
3
- # Copyright:: Copyright (c) 2014-2016 Chef Software, Inc.
4
- # License:: Apache License, Version 2.0
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
-
19
- require '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
1
+ #
2
+ # Author:: Mukta Aphale <mukta.aphale@clogeny.com>
3
+ # Copyright:: Copyright (c) 2014-2016 Chef Software, Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require '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