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,218 @@
1
+ #
2
+ # Author:: Steven Murawski (<smurawski@chef.io)
3
+ # Copyright:: Copyright (c) 2015 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
+
20
+ require 'chef/knife'
21
+ require 'chef/knife/winrm_base'
22
+ require 'chef/knife/winrm_shared_options'
23
+ require 'chef/knife/knife_windows_base'
24
+
25
+ class Chef
26
+ class Knife
27
+ module WinrmCommandSharedFunctions
28
+ def self.included(includer)
29
+ includer.class_eval do
30
+
31
+ @@ssl_warning_given = false
32
+
33
+ include Chef::Knife::WinrmBase
34
+ include Chef::Knife::WinrmSharedOptions
35
+ include Chef::Knife::KnifeWindowsBase
36
+
37
+ def validate_options!
38
+ winrm_auth_protocol = locate_config_value(:winrm_authentication_protocol)
39
+
40
+ if ! Chef::Knife::WinrmBase::WINRM_AUTH_PROTOCOL_LIST.include?(winrm_auth_protocol)
41
+ ui.error "Invalid value '#{winrm_auth_protocol}' for --winrm-authentication-protocol option."
42
+ ui.info "Valid values are #{Chef::Knife::WinrmBase::WINRM_AUTH_PROTOCOL_LIST.join(",")}."
43
+ exit 1
44
+ end
45
+
46
+ if negotiate_auth? && !Chef::Platform.windows? && !(locate_config_value(:winrm_transport) == 'ssl')
47
+ ui.warn <<-eos.gsub /^\s+/, ""
48
+ You are using '--winrm-authentication-protocol negotiate' with
49
+ '--winrm-transport plaintext' on a non-Windows system which results in
50
+ unencrypted traffic. To avoid this warning and secure communication,
51
+ use '--winrm-transport ssl' instead of the plaintext transport,
52
+ or execute this command from a Windows system which enables encrypted
53
+ communication over plaintext with the negotiate authentication protocol.
54
+ eos
55
+ end
56
+
57
+ warn_no_ssl_peer_verification if resolve_no_ssl_peer_verification
58
+ end
59
+
60
+ #Overrides Chef::Knife#configure_session, as that code is tied to the SSH implementation
61
+ #Tracked by Issue # 3042 / https://github.com/chef/chef/issues/3042
62
+ def configure_session
63
+ validate_options!
64
+ resolve_session_options
65
+ resolve_target_nodes
66
+ session_from_list
67
+ end
68
+
69
+ def resolve_target_nodes
70
+ @list = case config[:manual]
71
+ when true
72
+ @name_args[0].split(" ")
73
+ when false
74
+ r = Array.new
75
+ q = Chef::Search::Query.new
76
+ @action_nodes = q.search(:node, @name_args[0])[0]
77
+ @action_nodes.each do |item|
78
+ i = extract_nested_value(item, config[:attribute])
79
+ r.push(i) unless i.nil?
80
+ end
81
+ r
82
+ end
83
+
84
+ if @list.length == 0
85
+ if @action_nodes.length == 0
86
+ ui.fatal("No nodes returned from search!")
87
+ else
88
+ ui.fatal("#{@action_nodes.length} #{@action_nodes.length > 1 ? "nodes":"node"} found, " +
89
+ "but does not have the required attribute (#{config[:attribute]}) to establish the connection. " +
90
+ "Try setting another attribute to open the connection using --attribute.")
91
+ end
92
+ exit 10
93
+ end
94
+ end
95
+
96
+ def validate_password
97
+ if @session_opts[:user] and (not @session_opts[:password])
98
+ @session_opts[:password] = Chef::Config[:knife][:winrm_password] = config[:winrm_password] = get_password
99
+ end
100
+ end
101
+
102
+ private
103
+
104
+ def session_from_list
105
+ @list.each do |item|
106
+ Chef::Log.debug("Adding #{item}")
107
+ @session_opts[:host] = item
108
+ create_winrm_session(@session_opts)
109
+ end
110
+ end
111
+
112
+ def create_winrm_session(options={})
113
+ session = Chef::Knife::WinrmSession.new(options)
114
+ @winrm_sessions ||= []
115
+ @winrm_sessions.push(session)
116
+ end
117
+
118
+ def resolve_session_options
119
+ @session_opts = {
120
+ user: resolve_winrm_user,
121
+ password: locate_config_value(:winrm_password),
122
+ port: locate_config_value(:winrm_port),
123
+ operation_timeout: resolve_winrm_session_timeout,
124
+ basic_auth_only: resolve_winrm_basic_auth,
125
+ disable_sspi: resolve_winrm_disable_sspi,
126
+ transport: resolve_winrm_transport,
127
+ no_ssl_peer_verification: resolve_no_ssl_peer_verification
128
+ }
129
+ if @session_opts[:transport] == :kerberos
130
+ @session_opts.merge!(resolve_winrm_kerberos_options)
131
+ end
132
+ @session_opts[:ca_trust_path] = locate_config_value(:ca_trust_file) if locate_config_value(:ca_trust_file)
133
+ end
134
+
135
+ def resolve_winrm_user
136
+ user = locate_config_value(:winrm_user)
137
+
138
+ # Prefixing with '.\' when using negotiate
139
+ # to auth user against local machine domain
140
+ if resolve_winrm_basic_auth ||
141
+ resolve_winrm_transport == :kerberos ||
142
+ user.include?("\\") ||
143
+ user.include?("@")
144
+ user
145
+ else
146
+ ".\\#{user}"
147
+ end
148
+ end
149
+
150
+ def resolve_winrm_session_timeout
151
+ #30 min (Default) OperationTimeout for long bootstraps fix for KNIFE_WINDOWS-8
152
+ locate_config_value(:session_timeout).to_i * 60 if locate_config_value(:session_timeout)
153
+ end
154
+
155
+ def resolve_winrm_basic_auth
156
+ locate_config_value(:winrm_authentication_protocol) == "basic"
157
+ end
158
+
159
+ def resolve_winrm_kerberos_options
160
+ kerberos_opts = {}
161
+ kerberos_opts[:keytab] = locate_config_value(:kerberos_keytab_file) if locate_config_value(:kerberos_keytab_file)
162
+ kerberos_opts[:realm] = locate_config_value(:kerberos_realm) if locate_config_value(:kerberos_realm)
163
+ kerberos_opts[:service] = locate_config_value(:kerberos_service) if locate_config_value(:kerberos_service)
164
+ kerberos_opts
165
+ end
166
+
167
+ def resolve_winrm_transport
168
+ transport = locate_config_value(:winrm_transport).to_sym
169
+ if config.any? {|k,v| k.to_s =~ /kerberos/ && !v.nil? }
170
+ transport = :kerberos
171
+ elsif Chef::Platform.windows? && transport != :ssl && negotiate_auth?
172
+ transport = :sspinegotiate
173
+ end
174
+
175
+ transport
176
+ end
177
+
178
+ def resolve_no_ssl_peer_verification
179
+ locate_config_value(:ca_trust_file).nil? && config[:winrm_ssl_verify_mode] == :verify_none && resolve_winrm_transport == :ssl
180
+ end
181
+
182
+ def resolve_winrm_disable_sspi
183
+ !Chef::Platform.windows? || resolve_winrm_transport == :ssl || !negotiate_auth?
184
+ end
185
+
186
+ def get_password
187
+ @password ||= ui.ask("Enter your password: ") { |q| q.echo = false }
188
+ end
189
+
190
+ def negotiate_auth?
191
+ locate_config_value(:winrm_authentication_protocol) == "negotiate"
192
+ end
193
+
194
+ def warn_no_ssl_peer_verification
195
+ if ! @@ssl_warning_given
196
+ @@ssl_warning_given = true
197
+ ui.warn(<<-WARN)
198
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
199
+ SSL validation of HTTPS requests for the WinRM transport is disabled. HTTPS WinRM
200
+ connections are still encrypted, but knife is not able to detect forged replies
201
+ or spoofing attacks.
202
+
203
+ To fix this issue add an entry like this to your knife configuration file:
204
+
205
+ ```
206
+ # Verify all WinRM HTTPS connections (default, recommended)
207
+ knife[:winrm_ssl_verify_mode] = :verify_peer
208
+ ```
209
+ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
210
+ WARN
211
+ end
212
+ end
213
+
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end
@@ -0,0 +1,81 @@
1
+ #
2
+ # Author:: Steven Murawski <smurawski@chef.io>
3
+ # Copyright:: Copyright (c) 2015 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 'winrm'
20
+
21
+ class Chef
22
+ class Knife
23
+ class WinrmSession
24
+ attr_reader :host, :endpoint, :port, :output, :error, :exit_code
25
+
26
+ def initialize(options)
27
+ @host = options[:host]
28
+ @port = options[:port]
29
+ url = "#{options[:host]}:#{options[:port]}/wsman"
30
+ scheme = options[:transport] == :ssl ? 'https' : 'http'
31
+ @endpoint = "#{scheme}://#{url}"
32
+
33
+ opts = Hash.new
34
+ opts = {:user => options[:user], :pass => options[:password], :basic_auth_only => options[:basic_auth_only], :disable_sspi => options[:disable_sspi], :no_ssl_peer_verification => options[:no_ssl_peer_verification]}
35
+ options[:transport] == :kerberos ? opts.merge!({:service => options[:service], :realm => options[:realm], :keytab => options[:keytab]}) : opts.merge!({:ca_trust_path => options[:ca_trust_path]})
36
+
37
+ Chef::Log.debug("WinRM::WinRMWebService options: #{opts}")
38
+ Chef::Log.debug("Endpoint: #{endpoint}")
39
+ Chef::Log.debug("Transport: #{options[:transport]}")
40
+
41
+ WinrmSession.load_windows_specific_gems if options[:transport] == :sspinegotiate
42
+ @winrm_session = WinRM::WinRMWebService.new(@endpoint, options[:transport], opts)
43
+ @winrm_session.set_timeout(options[:operation_timeout]) if options[:operation_timeout]
44
+ end
45
+
46
+ def relay_command(command)
47
+ remote_id = @winrm_session.open_shell
48
+ command_id = @winrm_session.run_command(remote_id, command)
49
+ Chef::Log.debug("#{@host}[#{remote_id}] => :run_command[#{command}]")
50
+ session_result = get_output(remote_id, command_id)
51
+ @winrm_session.cleanup_command(remote_id, command_id)
52
+ Chef::Log.debug("#{@host}[#{remote_id}] => :command_cleanup[#{command}]")
53
+ @exit_code = session_result[:exitcode]
54
+ @winrm_session.close_shell(remote_id)
55
+ Chef::Log.debug("#{@host}[#{remote_id}] => :shell_close")
56
+ end
57
+
58
+ def get_output(remote_id, command_id)
59
+ @winrm_session.get_command_output(remote_id, command_id) do |out,error|
60
+ print_data(@host, out) if out
61
+ print_data(@host, error, :red) if error
62
+ end
63
+ end
64
+
65
+ def print_data(host, data, color = :cyan)
66
+ if data =~ /\n/
67
+ data.split(/\n/).each { |d| print_data(host, d, color) }
68
+ elsif !data.nil?
69
+ print Chef::Knife::Winrm.ui.color(host, color)
70
+ puts " #{data}"
71
+ end
72
+ end
73
+
74
+ def self.load_windows_specific_gems
75
+ #checking for windows in case testing on linux
76
+ require 'winrm-s'
77
+ Chef::Log.debug("Applied 'winrm-s' monkey patch and trying WinRM communication with 'sspinegotiate'")
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,47 @@
1
+ #
2
+ # Author:: Steven Murawski (<smurawski@chef.io)
3
+ # Copyright:: Copyright (c) 2015 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 'chef/knife'
20
+ require 'chef/encrypted_data_bag_item'
21
+ require 'kconv'
22
+
23
+ class Chef
24
+ class Knife
25
+ module WinrmSharedOptions
26
+
27
+ # Shared command line options for knife winrm and knife wsman test
28
+ def self.included(includer)
29
+ includer.class_eval do
30
+ option :manual,
31
+ :short => "-m",
32
+ :long => "--manual-list",
33
+ :boolean => true,
34
+ :description => "QUERY is a space separated list of servers",
35
+ :default => false
36
+
37
+ option :attribute,
38
+ :short => "-a ATTR",
39
+ :long => "--attribute ATTR",
40
+ :description => "The attribute to use for opening the connection - default is fqdn",
41
+ :default => "fqdn"
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,44 @@
1
+ #
2
+ # Author:: Steven Murawski (<smurawski@chef.io)
3
+ # Copyright:: Copyright (c) 2015 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
+ class Chef
20
+ class Knife
21
+ class WsmanEndpoint
22
+ attr_accessor :host
23
+ attr_accessor :wsman_port
24
+ attr_accessor :wsman_url
25
+ attr_accessor :product_version
26
+ attr_accessor :protocol_version
27
+ attr_accessor :product_vendor
28
+ attr_accessor :response_status_code
29
+ attr_accessor :error_message
30
+
31
+ def initialize(name, port, url)
32
+ @host = name
33
+ @wsman_port = port
34
+ @wsman_url = url
35
+ end
36
+
37
+ def to_hash
38
+ hash = {}
39
+ instance_variables.each {|var| hash[var.to_s.delete("@")] = instance_variable_get(var) }
40
+ hash
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,96 @@
1
+ #
2
+ # Author:: Steven Murawski (<smurawski@chef.io>)
3
+ # Copyright:: Copyright (c) 2015 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 'httpclient'
20
+ require 'nokogiri'
21
+ require 'chef/knife'
22
+ require 'chef/knife/winrm_knife_base'
23
+ require 'chef/knife/wsman_endpoint'
24
+ require 'pry'
25
+
26
+ class Chef
27
+ class Knife
28
+ class WsmanTest < Knife
29
+
30
+ include Chef::Knife::WinrmCommandSharedFunctions
31
+
32
+ deps do
33
+ require 'chef/search/query'
34
+ end
35
+
36
+ banner "knife wsman test QUERY (options)"
37
+
38
+ def run
39
+ @config[:winrm_authentication_protocol] = 'basic'
40
+ configure_session
41
+ verify_wsman_accessiblity_for_nodes
42
+ end
43
+
44
+ def verify_wsman_accessiblity_for_nodes
45
+ error_count = 0
46
+ @winrm_sessions.each do |item|
47
+ Chef::Log.debug("checking for WSMAN availability at #{item.endpoint}")
48
+
49
+ xml = '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:wsmid="http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd"><s:Header/><s:Body><wsmid:Identify/></s:Body></s:Envelope>'
50
+ header = {
51
+ 'WSMANIDENTIFY' => 'unauthenticated',
52
+ 'Content-Type' => 'application/soap+xml; charset=UTF-8'
53
+ }
54
+ output_object = Chef::Knife::WsmanEndpoint.new(item.host, item.port, item.endpoint)
55
+ error_message = nil
56
+ begin
57
+ client = HTTPClient.new
58
+ response = client.post(item.endpoint, xml, header)
59
+ rescue Exception => e
60
+ error_message = e.message
61
+ else
62
+ ui.msg "Connected successfully to #{item.host} at #{item.endpoint}."
63
+ output_object.response_status_code = response.status_code
64
+ end
65
+
66
+ if response.nil? || output_object.response_status_code != 200
67
+ error_message = "No valid WSMan endoint listening at #{item.endpoint}."
68
+ else
69
+ doc = Nokogiri::XML response.body
70
+ namespace = 'http://schemas.dmtf.org/wbem/wsman/identity/1/wsmanidentity.xsd'
71
+ output_object.protocol_version = doc.xpath('//wsmid:ProtocolVersion', 'wsmid' => namespace).text
72
+ output_object.product_version = doc.xpath('//wsmid:ProductVersion', 'wsmid' => namespace).text
73
+ output_object.product_vendor = doc.xpath('//wsmid:ProductVendor', 'wsmid' => namespace).text
74
+ if output_object.protocol_version.to_s.strip.length == 0
75
+ error_message = "Endpoint #{item.endpoint} on #{item.host} does not appear to be a WSMAN endpoint."
76
+ end
77
+ end
78
+
79
+ unless error_message.nil?
80
+ ui.warn "Failed to connect to #{item.host} at #{item.endpoint}."
81
+ output_object.error_message = error_message
82
+ error_count += 1
83
+ end
84
+
85
+ if config[:verbosity] >= 1
86
+ output(output_object)
87
+ end
88
+ end
89
+ if error_count > 0
90
+ ui.error "Failed to connect to #{error_count} nodes."
91
+ exit 1
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end