winrm 1.7.2 → 1.7.3

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 (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -10
  3. data/.rspec +3 -3
  4. data/.rubocop.yml +10 -0
  5. data/.travis.yml +12 -12
  6. data/Gemfile +9 -9
  7. data/LICENSE +202 -202
  8. data/README.md +194 -194
  9. data/Rakefile +36 -36
  10. data/Vagrantfile +9 -9
  11. data/appveyor.yml +42 -42
  12. data/bin/rwinrm +97 -97
  13. data/changelog.md +3 -0
  14. data/lib/winrm.rb +42 -42
  15. data/lib/winrm/command_executor.rb +15 -0
  16. data/lib/winrm/command_output_decoder.rb +53 -53
  17. data/lib/winrm/exceptions/exceptions.rb +57 -57
  18. data/lib/winrm/helpers/iso8601_duration.rb +58 -58
  19. data/lib/winrm/helpers/powershell_script.rb +42 -42
  20. data/lib/winrm/http/response_handler.rb +82 -82
  21. data/lib/winrm/http/transport.rb +3 -3
  22. data/lib/winrm/output.rb +43 -43
  23. data/lib/winrm/soap_provider.rb +39 -39
  24. data/lib/winrm/version.rb +1 -1
  25. data/lib/winrm/winrm_service.rb +547 -547
  26. data/preamble +17 -17
  27. data/spec/auth_timeout_spec.rb +16 -16
  28. data/spec/cmd_spec.rb +102 -102
  29. data/spec/command_executor_spec.rb +29 -0
  30. data/spec/command_output_decoder_spec.rb +37 -37
  31. data/spec/config-example.yml +19 -19
  32. data/spec/exception_spec.rb +50 -50
  33. data/spec/issue_184_spec.rb +67 -67
  34. data/spec/issue_59_spec.rb +23 -23
  35. data/spec/matchers.rb +74 -74
  36. data/spec/output_spec.rb +110 -110
  37. data/spec/powershell_spec.rb +97 -97
  38. data/spec/response_handler_spec.rb +59 -59
  39. data/spec/spec_helper.rb +73 -73
  40. data/spec/stubs/responses/get_command_output_response.xml.erb +13 -13
  41. data/spec/stubs/responses/open_shell_v1.xml +19 -19
  42. data/spec/stubs/responses/open_shell_v2.xml +20 -20
  43. data/spec/stubs/responses/soap_fault_v1.xml +36 -36
  44. data/spec/stubs/responses/soap_fault_v2.xml +42 -42
  45. data/spec/stubs/responses/wmi_error_v2.xml +41 -41
  46. data/spec/transport_spec.rb +124 -124
  47. data/spec/winrm_options_spec.rb +76 -76
  48. data/spec/winrm_primitives_spec.rb +51 -51
  49. data/spec/wql_spec.rb +14 -14
  50. data/winrm.gemspec +40 -40
  51. metadata +2 -2
data/Vagrantfile CHANGED
@@ -1,9 +1,9 @@
1
- # encoding: UTF-8
2
- # -*- mode: ruby -*-
3
- # vi: set ft=ruby :
4
-
5
- VAGRANTFILE_API_VERSION = '2'
6
-
7
- Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8
- config.vm.box = 'mwrock/Windows2012R2'
9
- end
1
+ # encoding: UTF-8
2
+ # -*- mode: ruby -*-
3
+ # vi: set ft=ruby :
4
+
5
+ VAGRANTFILE_API_VERSION = '2'
6
+
7
+ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8
+ config.vm.box = 'mwrock/Windows2012R2'
9
+ end
data/appveyor.yml CHANGED
@@ -1,42 +1,42 @@
1
- version: "master-{build}"
2
-
3
- os: Windows Server 2012 R2
4
- platform:
5
- - x64
6
-
7
- environment:
8
- winrm_user: test_user
9
- winrm_pass: Pass@word1
10
-
11
- matrix:
12
- - ruby_version: "21"
13
- winrm_endpoint: http://localhost:5985/wsman
14
-
15
- clone_folder: c:\projects\winrm
16
- clone_depth: 1
17
- branches:
18
- only:
19
- - master
20
-
21
- install:
22
- - ps: net user /add $env:winrm_user $env:winrm_pass
23
- - ps: net localgroup administrators $env:winrm_user /add
24
- - ps: $env:winrm_cert = (New-SelfSignedCertificate -DnsName localhost -CertStoreLocation cert:\localmachine\my).Thumbprint
25
- - ps: winrm create winrm/config/Listener?Address=*+Transport=HTTPS "@{Hostname=`"localhost`";CertificateThumbprint=`"$($env:winrm_cert)`"}"
26
- - ps: winrm set winrm/config/client/auth '@{Basic="true"}'
27
- - ps: winrm set winrm/config/service/auth '@{Basic="true"}'
28
- - ps: winrm set winrm/config/service/auth '@{CbtHardeningLevel="Strict"}'
29
- - ps: winrm set winrm/config/service '@{AllowUnencrypted="true"}'
30
- - ps: $env:PATH="C:\Ruby$env:ruby_version\bin;$env:PATH"
31
- - ps: Write-Host $env:PATH
32
- - ps: ruby --version
33
- - ps: gem --version
34
- - ps: gem install bundler --quiet --no-ri --no-rdoc
35
- - ps: bundler --version
36
-
37
- build_script:
38
- - bundle install || bundle install || bundle install
39
-
40
- test_script:
41
- - SET SPEC_OPTS=--format progress
42
- - bundle exec rake integration
1
+ version: "master-{build}"
2
+
3
+ os: Windows Server 2012 R2
4
+ platform:
5
+ - x64
6
+
7
+ environment:
8
+ winrm_user: test_user
9
+ winrm_pass: Pass@word1
10
+
11
+ matrix:
12
+ - ruby_version: "21"
13
+ winrm_endpoint: http://localhost:5985/wsman
14
+
15
+ clone_folder: c:\projects\winrm
16
+ clone_depth: 1
17
+ branches:
18
+ only:
19
+ - master
20
+
21
+ install:
22
+ - ps: net user /add $env:winrm_user $env:winrm_pass
23
+ - ps: net localgroup administrators $env:winrm_user /add
24
+ - ps: $env:winrm_cert = (New-SelfSignedCertificate -DnsName localhost -CertStoreLocation cert:\localmachine\my).Thumbprint
25
+ - ps: winrm create winrm/config/Listener?Address=*+Transport=HTTPS "@{Hostname=`"localhost`";CertificateThumbprint=`"$($env:winrm_cert)`"}"
26
+ - ps: winrm set winrm/config/client/auth '@{Basic="true"}'
27
+ - ps: winrm set winrm/config/service/auth '@{Basic="true"}'
28
+ - ps: winrm set winrm/config/service/auth '@{CbtHardeningLevel="Strict"}'
29
+ - ps: winrm set winrm/config/service '@{AllowUnencrypted="true"}'
30
+ - ps: $env:PATH="C:\Ruby$env:ruby_version\bin;$env:PATH"
31
+ - ps: Write-Host $env:PATH
32
+ - ps: ruby --version
33
+ - ps: gem --version
34
+ - ps: gem install bundler --quiet --no-ri --no-rdoc
35
+ - ps: bundler --version
36
+
37
+ build_script:
38
+ - bundle install || bundle install || bundle install
39
+
40
+ test_script:
41
+ - SET SPEC_OPTS=--format progress
42
+ - bundle exec rake integration
data/bin/rwinrm CHANGED
@@ -1,97 +1,97 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # Copyright 2014 Shawn Neal <sneal@sneal.net>
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
- # rubocop:disable all
19
-
20
- $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
21
-
22
- require 'readline'
23
- require 'io/console'
24
- require 'winrm'
25
-
26
- def help_msg
27
- puts 'Usage: rwinrm user@host'
28
- puts ''
29
- end
30
-
31
- def parse_options
32
- options = {}
33
- fail 'Missing required options' unless ARGV.length == 1
34
-
35
- m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
36
- fail "#{ARGV[0]} is an invalid host" unless m
37
- options[:user] = m[:user]
38
- options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"
39
-
40
- # Get the password
41
- print 'Password: '
42
- options[:pass] = STDIN.noecho(&:gets).chomp
43
- puts
44
-
45
- # Set some defaults required by WinRM WS
46
- options[:auth_type] = :plaintext
47
- options[:basic_auth_only] = true
48
-
49
- options
50
- rescue StandardError => e
51
- puts e.message
52
- help_msg
53
- exit 1
54
- end
55
-
56
- def repl(options)
57
- client = WinRM::WinRMWebService.new(
58
- options[:endpoint],
59
- options[:auth_type].to_sym,
60
- options)
61
-
62
- client.set_timeout(3600)
63
- shell_id = client.open_shell
64
- command_id = client.run_command(shell_id, 'cmd', "/K prompt [#{ARGV[0]}]$P$G")
65
-
66
- read_thread = Thread.new do
67
- client.get_command_output(shell_id, command_id) do |stdout, stderr|
68
- STDOUT.write stdout
69
- STDERR.write stderr
70
- end
71
- end
72
- read_thread.abort_on_exception = true
73
-
74
- while (buf = Readline.readline('', true))
75
- if buf =~ /^exit/
76
- read_thread.exit
77
- client.cleanup_command(shell_id, command_id)
78
- client.close_shell(shell_id)
79
- exit 0
80
- else
81
- client.write_stdin(shell_id, command_id, "#{buf}\r\n")
82
- end
83
- end
84
- rescue Interrupt
85
- puts 'exiting'
86
- # ctrl-c
87
- rescue WinRM::WinRMAuthorizationError
88
- puts 'Authentication failed, bad user name or password'
89
- exit 1
90
- rescue StandardError => e
91
- puts e.message
92
- exit 1
93
- end
94
-
95
- repl(parse_options)
96
-
97
- # rubocop:enable all
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ # Copyright 2014 Shawn Neal <sneal@sneal.net>
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
+ # rubocop:disable all
19
+
20
+ $LOAD_PATH.push File.expand_path('../../lib', __FILE__)
21
+
22
+ require 'readline'
23
+ require 'io/console'
24
+ require 'winrm'
25
+
26
+ def help_msg
27
+ puts 'Usage: rwinrm user@host'
28
+ puts ''
29
+ end
30
+
31
+ def parse_options
32
+ options = {}
33
+ fail 'Missing required options' unless ARGV.length == 1
34
+
35
+ m = /^(?<user>[a-z0-9\.\!\$ _-]+)@{1}(?<host>[a-z0-9\.\-]+)(?<port>:[0-9]+)?/i.match(ARGV[0])
36
+ fail "#{ARGV[0]} is an invalid host" unless m
37
+ options[:user] = m[:user]
38
+ options[:endpoint] = "http://#{m[:host]}#{m[:port] || ':5985'}/wsman"
39
+
40
+ # Get the password
41
+ print 'Password: '
42
+ options[:pass] = STDIN.noecho(&:gets).chomp
43
+ puts
44
+
45
+ # Set some defaults required by WinRM WS
46
+ options[:auth_type] = :plaintext
47
+ options[:basic_auth_only] = true
48
+
49
+ options
50
+ rescue StandardError => e
51
+ puts e.message
52
+ help_msg
53
+ exit 1
54
+ end
55
+
56
+ def repl(options)
57
+ client = WinRM::WinRMWebService.new(
58
+ options[:endpoint],
59
+ options[:auth_type].to_sym,
60
+ options)
61
+
62
+ client.set_timeout(3600)
63
+ shell_id = client.open_shell
64
+ command_id = client.run_command(shell_id, 'cmd', "/K prompt [#{ARGV[0]}]$P$G")
65
+
66
+ read_thread = Thread.new do
67
+ client.get_command_output(shell_id, command_id) do |stdout, stderr|
68
+ STDOUT.write stdout
69
+ STDERR.write stderr
70
+ end
71
+ end
72
+ read_thread.abort_on_exception = true
73
+
74
+ while (buf = Readline.readline('', true))
75
+ if buf =~ /^exit/
76
+ read_thread.exit
77
+ client.cleanup_command(shell_id, command_id)
78
+ client.close_shell(shell_id)
79
+ exit 0
80
+ else
81
+ client.write_stdin(shell_id, command_id, "#{buf}\r\n")
82
+ end
83
+ end
84
+ rescue Interrupt
85
+ puts 'exiting'
86
+ # ctrl-c
87
+ rescue WinRM::WinRMAuthorizationError
88
+ puts 'Authentication failed, bad user name or password'
89
+ exit 1
90
+ rescue StandardError => e
91
+ puts e.message
92
+ exit 1
93
+ end
94
+
95
+ repl(parse_options)
96
+
97
+ # rubocop:enable all
data/changelog.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # WinRM Gem Changelog
2
2
 
3
+ # 1.7.3
4
+ - Open a new shell if the current shell has been deleted
5
+
3
6
  # 1.7.2
4
7
  - Fix regression where BOM appears in 2008R2 output and is not stripped
5
8
 
data/lib/winrm.rb CHANGED
@@ -1,42 +1,42 @@
1
- # encoding: UTF-8
2
- #
3
- # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'date'
18
- require 'logging'
19
- require_relative 'winrm/version'
20
-
21
- # Main WinRM module entry point
22
- module WinRM
23
- # Enable logging if it is requested. We do this before
24
- # anything else so that we can setup the output before
25
- # any logging occurs.
26
- if ENV['WINRM_LOG'] && ENV['WINRM_LOG'] != ''
27
- begin
28
- Logging.logger.root.level = ENV['WINRM_LOG']
29
- Logging.logger.root.appenders = Logging.appenders.stderr
30
- rescue ArgumentError
31
- # This means that the logging level wasn't valid
32
- $stderr.puts "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
33
- $stderr.puts ''
34
- $stderr.puts 'Please use one of the standard log levels: ' \
35
- 'debug, info, warn, or error'
36
- end
37
- end
38
- end
39
-
40
- require 'winrm/output'
41
- require 'winrm/helpers/iso8601_duration'
42
- require 'winrm/soap_provider'
1
+ # encoding: UTF-8
2
+ #
3
+ # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'date'
18
+ require 'logging'
19
+ require_relative 'winrm/version'
20
+
21
+ # Main WinRM module entry point
22
+ module WinRM
23
+ # Enable logging if it is requested. We do this before
24
+ # anything else so that we can setup the output before
25
+ # any logging occurs.
26
+ if ENV['WINRM_LOG'] && ENV['WINRM_LOG'] != ''
27
+ begin
28
+ Logging.logger.root.level = ENV['WINRM_LOG']
29
+ Logging.logger.root.appenders = Logging.appenders.stderr
30
+ rescue ArgumentError
31
+ # This means that the logging level wasn't valid
32
+ $stderr.puts "Invalid WINRM_LOG level is set: #{ENV['WINRM_LOG']}"
33
+ $stderr.puts ''
34
+ $stderr.puts 'Please use one of the standard log levels: ' \
35
+ 'debug, info, warn, or error'
36
+ end
37
+ end
38
+ end
39
+
40
+ require 'winrm/output'
41
+ require 'winrm/helpers/iso8601_duration'
42
+ require 'winrm/soap_provider'
@@ -86,6 +86,8 @@ module WinRM
86
86
  # @return [WinRM::Output] output object with stdout, stderr, and
87
87
  # exit code
88
88
  def run_cmd(command, arguments = [], &block)
89
+ tries ||= 2
90
+
89
91
  reset if command_count > max_commands
90
92
  ensure_open_shell!
91
93
 
@@ -95,6 +97,19 @@ module WinRM
95
97
  result = service.get_command_output(shell, command_id, &block)
96
98
  end
97
99
  result
100
+ rescue WinRMWSManFault => e
101
+ # Fault code 2150858843 may be raised if the shell id that the command is sent on
102
+ # has been closed. One might see this on a target windows machine that has just
103
+ # been provisioned and restarted the winrm service at the end of its provisioning
104
+ # routine. If this hapens, we should give the command one more try.
105
+ if e.fault_code == '2150858843' && (tries -= 1) > 0
106
+ service.logger.debug('[WinRM] opening new shell since the current one was deleted')
107
+ @shell = nil
108
+ open
109
+ retry
110
+ else
111
+ raise
112
+ end
98
113
  end
99
114
 
100
115
  # Run a Powershell script that resides on the local box.
@@ -1,53 +1,53 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
-
17
- require 'base64'
18
-
19
- module WinRM
20
- # Handles decoding a raw output response
21
- class CommandOutputDecoder
22
- # Decode the raw SOAP output into decoded and human consumable text,
23
- # Decodes and replaces invalid unicode characters.
24
- # @param raw_output [String] The raw encoded output
25
- # @return [String] The decoded output
26
- def decode(raw_output)
27
- decoded_text = decode_raw_output(raw_output)
28
- decoded_text = handle_invalid_encoding(decoded_text)
29
- decoded_text = remove_bom(decoded_text)
30
- decoded_text
31
- end
32
-
33
- private
34
-
35
- def decode_raw_output(raw_output)
36
- Base64.decode64(raw_output).force_encoding('utf-8')
37
- end
38
-
39
- def handle_invalid_encoding(decoded_text)
40
- return decoded_text if decoded_text.valid_encoding?
41
- if decoded_text.respond_to?(:scrub)
42
- decoded_text.scrub
43
- else
44
- decoded_text.encode('utf-16', invalid: :replace, undef: :replace).encode('utf-8')
45
- end
46
- end
47
-
48
- def remove_bom(decoded_text)
49
- # remove BOM which 2008R2 applies
50
- decoded_text.sub("\xEF\xBB\xBF", '')
51
- end
52
- end
53
- end
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Copyright 2016 Shawn Neal <sneal@sneal.net>
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'base64'
18
+
19
+ module WinRM
20
+ # Handles decoding a raw output response
21
+ class CommandOutputDecoder
22
+ # Decode the raw SOAP output into decoded and human consumable text,
23
+ # Decodes and replaces invalid unicode characters.
24
+ # @param raw_output [String] The raw encoded output
25
+ # @return [String] The decoded output
26
+ def decode(raw_output)
27
+ decoded_text = decode_raw_output(raw_output)
28
+ decoded_text = handle_invalid_encoding(decoded_text)
29
+ decoded_text = remove_bom(decoded_text)
30
+ decoded_text
31
+ end
32
+
33
+ private
34
+
35
+ def decode_raw_output(raw_output)
36
+ Base64.decode64(raw_output).force_encoding('utf-8')
37
+ end
38
+
39
+ def handle_invalid_encoding(decoded_text)
40
+ return decoded_text if decoded_text.valid_encoding?
41
+ if decoded_text.respond_to?(:scrub)
42
+ decoded_text.scrub
43
+ else
44
+ decoded_text.encode('utf-16', invalid: :replace, undef: :replace).encode('utf-8')
45
+ end
46
+ end
47
+
48
+ def remove_bom(decoded_text)
49
+ # remove BOM which 2008R2 applies
50
+ decoded_text.sub("\xEF\xBB\xBF", '')
51
+ end
52
+ end
53
+ end