winrm 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -10
  3. data/.rubocop.yml +26 -26
  4. data/.travis.yml +11 -11
  5. data/Gemfile +3 -3
  6. data/README.md +263 -260
  7. data/Rakefile +34 -34
  8. data/Vagrantfile +6 -6
  9. data/WinrmAppveyor.psm1 +31 -31
  10. data/appveyor.yml +51 -51
  11. data/changelog.md +107 -104
  12. data/lib/winrm.rb +39 -39
  13. data/lib/winrm/connection.rb +83 -83
  14. data/lib/winrm/connection_opts.rb +91 -91
  15. data/lib/winrm/exceptions.rb +76 -76
  16. data/lib/winrm/http/response_handler.rb +96 -96
  17. data/lib/winrm/http/transport.rb +424 -424
  18. data/lib/winrm/http/transport_factory.rb +68 -68
  19. data/lib/winrm/output.rb +59 -59
  20. data/lib/winrm/psrp/create_pipeline.xml.erb +167 -167
  21. data/lib/winrm/psrp/fragment.rb +70 -70
  22. data/lib/winrm/psrp/init_runspace_pool.xml.erb +224 -224
  23. data/lib/winrm/psrp/message.rb +130 -130
  24. data/lib/winrm/psrp/message_data.rb +42 -42
  25. data/lib/winrm/psrp/message_data/base.rb +49 -49
  26. data/lib/winrm/psrp/message_data/error_record.rb +68 -68
  27. data/lib/winrm/psrp/message_data/pipeline_host_call.rb +32 -32
  28. data/lib/winrm/psrp/message_data/pipeline_output.rb +54 -49
  29. data/lib/winrm/psrp/message_data/pipeline_state.rb +40 -40
  30. data/lib/winrm/psrp/message_data/runspacepool_host_call.rb +32 -32
  31. data/lib/winrm/psrp/message_data/runspacepool_state.rb +39 -39
  32. data/lib/winrm/psrp/message_data/session_capability.rb +36 -36
  33. data/lib/winrm/psrp/message_defragmenter.rb +62 -62
  34. data/lib/winrm/psrp/message_factory.rb +75 -75
  35. data/lib/winrm/psrp/message_fragmenter.rb +60 -60
  36. data/lib/winrm/psrp/powershell_output_decoder.rb +144 -139
  37. data/lib/winrm/psrp/receive_response_reader.rb +97 -97
  38. data/lib/winrm/psrp/session_capability.xml.erb +7 -7
  39. data/lib/winrm/psrp/uuid.rb +40 -40
  40. data/lib/winrm/shells/base.rb +180 -180
  41. data/lib/winrm/shells/cmd.rb +65 -65
  42. data/lib/winrm/shells/power_shell.rb +202 -202
  43. data/lib/winrm/shells/retryable.rb +45 -45
  44. data/lib/winrm/shells/shell_factory.rb +58 -58
  45. data/lib/winrm/version.rb +7 -7
  46. data/lib/winrm/wsmv/base.rb +59 -59
  47. data/lib/winrm/wsmv/cleanup_command.rb +61 -61
  48. data/lib/winrm/wsmv/close_shell.rb +50 -50
  49. data/lib/winrm/wsmv/command.rb +101 -101
  50. data/lib/winrm/wsmv/command_output.rb +76 -76
  51. data/lib/winrm/wsmv/command_output_decoder.rb +55 -55
  52. data/lib/winrm/wsmv/configuration.rb +46 -46
  53. data/lib/winrm/wsmv/create_pipeline.rb +66 -66
  54. data/lib/winrm/wsmv/create_shell.rb +119 -119
  55. data/lib/winrm/wsmv/header.rb +203 -203
  56. data/lib/winrm/wsmv/init_runspace_pool.rb +95 -95
  57. data/lib/winrm/wsmv/iso8601_duration.rb +60 -60
  58. data/lib/winrm/wsmv/keep_alive.rb +68 -68
  59. data/lib/winrm/wsmv/receive_response_reader.rb +126 -126
  60. data/lib/winrm/wsmv/send_data.rb +68 -68
  61. data/lib/winrm/wsmv/soap.rb +51 -51
  62. data/lib/winrm/wsmv/wql_query.rb +79 -79
  63. data/lib/winrm/wsmv/write_stdin.rb +88 -88
  64. data/tests/integration/auth_timeout_spec.rb +18 -18
  65. data/tests/integration/cmd_spec.rb +131 -131
  66. data/tests/integration/config-example.yml +16 -16
  67. data/tests/integration/issue_59_spec.rb +26 -26
  68. data/tests/integration/powershell_spec.rb +165 -165
  69. data/tests/integration/spec_helper.rb +65 -65
  70. data/tests/integration/transport_spec.rb +99 -99
  71. data/tests/integration/wql_spec.rb +16 -16
  72. data/tests/matchers.rb +60 -60
  73. data/tests/spec/configuration_spec.rb +184 -184
  74. data/tests/spec/connection_spec.rb +39 -39
  75. data/tests/spec/exception_spec.rb +50 -50
  76. data/tests/spec/http/transport_factory_spec.rb +68 -68
  77. data/tests/spec/http/transport_spec.rb +44 -44
  78. data/tests/spec/output_spec.rb +127 -127
  79. data/tests/spec/psrp/fragment_spec.rb +62 -62
  80. data/tests/spec/psrp/message_data/base_spec.rb +13 -13
  81. data/tests/spec/psrp/message_data/error_record_spec.rb +41 -41
  82. data/tests/spec/psrp/message_data/pipeline_host_call_spec.rb +25 -25
  83. data/tests/spec/psrp/message_data/pipeline_output_spec.rb +32 -32
  84. data/tests/spec/psrp/message_data/pipeline_state_spec.rb +40 -40
  85. data/tests/spec/psrp/message_data/runspace_pool_host_call_spec.rb +25 -25
  86. data/tests/spec/psrp/message_data/runspacepool_state_spec.rb +16 -16
  87. data/tests/spec/psrp/message_data/session_capability_spec.rb +30 -30
  88. data/tests/spec/psrp/message_data_spec.rb +35 -35
  89. data/tests/spec/psrp/message_defragmenter_spec.rb +47 -47
  90. data/tests/spec/psrp/message_fragmenter_spec.rb +105 -105
  91. data/tests/spec/psrp/powershell_output_decoder_spec.rb +100 -100
  92. data/tests/spec/psrp/psrp_message_spec.rb +70 -70
  93. data/tests/spec/psrp/recieve_response_reader_spec.rb +172 -172
  94. data/tests/spec/psrp/uuid_spec.rb +28 -28
  95. data/tests/spec/response_handler_spec.rb +61 -61
  96. data/tests/spec/shells/base_spec.rb +202 -202
  97. data/tests/spec/shells/cmd_spec.rb +75 -75
  98. data/tests/spec/shells/powershell_spec.rb +175 -175
  99. data/tests/spec/spec_helper.rb +47 -47
  100. data/tests/spec/stubs/clixml/error_record.xml.erb +84 -84
  101. data/tests/spec/stubs/clixml/pipeline_state.xml.erb +88 -88
  102. data/tests/spec/stubs/responses/get_command_output_response.xml.erb +13 -13
  103. data/tests/spec/stubs/responses/get_command_output_response_not_done.xml.erb +10 -10
  104. data/tests/spec/stubs/responses/get_powershell_keepalive_response.xml.erb +10 -10
  105. data/tests/spec/stubs/responses/get_powershell_output_response.xml.erb +12 -12
  106. data/tests/spec/stubs/responses/get_powershell_output_response_not_done.xml.erb +9 -9
  107. data/tests/spec/stubs/responses/open_shell_v1.xml +19 -19
  108. data/tests/spec/stubs/responses/open_shell_v2.xml +20 -20
  109. data/tests/spec/stubs/responses/soap_fault_v1.xml +36 -36
  110. data/tests/spec/stubs/responses/soap_fault_v2.xml +42 -42
  111. data/tests/spec/stubs/responses/wmi_error_v2.xml +41 -41
  112. data/tests/spec/wsmv/cleanup_command_spec.rb +22 -22
  113. data/tests/spec/wsmv/close_shell_spec.rb +17 -17
  114. data/tests/spec/wsmv/command_output_decoder_spec.rb +37 -37
  115. data/tests/spec/wsmv/command_output_spec.rb +45 -45
  116. data/tests/spec/wsmv/command_spec.rb +19 -19
  117. data/tests/spec/wsmv/configuration_spec.rb +17 -17
  118. data/tests/spec/wsmv/create_pipeline_spec.rb +31 -31
  119. data/tests/spec/wsmv/create_shell_spec.rb +38 -38
  120. data/tests/spec/wsmv/init_runspace_pool_spec.rb +36 -36
  121. data/tests/spec/wsmv/keep_alive_spec.rb +21 -21
  122. data/tests/spec/wsmv/receive_response_reader_spec.rb +123 -123
  123. data/tests/spec/wsmv/send_data_spec.rb +30 -30
  124. data/tests/spec/wsmv/wql_query_spec.rb +13 -13
  125. data/tests/spec/wsmv/write_stdin_spec.rb +22 -22
  126. data/winrm.gemspec +47 -42
  127. metadata +7 -3
@@ -1,68 +1,68 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2016 Matt Wrock <matt@mattwrock.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_relative 'base'
18
-
19
- module WinRM
20
- module WSMV
21
- # Subclasses base, sends additional data to WinRM
22
- class SendData < Base
23
- attr_accessor :shell_id, :command_id, :fragment
24
-
25
- def initialize(session_opts, shell_id, command_id, fragment)
26
- @session_opts = session_opts
27
- @shell_id = shell_id
28
- @command_id = command_id
29
- @fragment = fragment
30
- end
31
-
32
- protected
33
-
34
- def create_header(header)
35
- header << Gyoku.xml(command_headers)
36
- end
37
-
38
- def create_body(body)
39
- body.tag!("#{NS_WIN_SHELL}:Send") do |cl|
40
- cl << Gyoku.xml(command_body)
41
- end
42
- end
43
-
44
- private
45
-
46
- def command_body
47
- {
48
- "#{NS_WIN_SHELL}:Stream" => encode_bytes(fragment.bytes),
49
- :attributes! => {
50
- "#{NS_WIN_SHELL}:Stream" => {
51
- 'Name' => 'stdin',
52
- 'CommandId' => command_id
53
- }
54
- }
55
- }
56
- end
57
-
58
- def command_headers
59
- merge_headers(
60
- shared_headers(@session_opts),
61
- resource_uri_shell(RESOURCE_URI_POWERSHELL),
62
- action_send,
63
- selector_shell_id(shell_id)
64
- )
65
- end
66
- end
67
- end
68
- end
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Copyright 2016 Matt Wrock <matt@mattwrock.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_relative 'base'
18
+
19
+ module WinRM
20
+ module WSMV
21
+ # Subclasses base, sends additional data to WinRM
22
+ class SendData < Base
23
+ attr_accessor :shell_id, :command_id, :fragment
24
+
25
+ def initialize(session_opts, shell_id, command_id, fragment)
26
+ @session_opts = session_opts
27
+ @shell_id = shell_id
28
+ @command_id = command_id
29
+ @fragment = fragment
30
+ end
31
+
32
+ protected
33
+
34
+ def create_header(header)
35
+ header << Gyoku.xml(command_headers)
36
+ end
37
+
38
+ def create_body(body)
39
+ body.tag!("#{NS_WIN_SHELL}:Send") do |cl|
40
+ cl << Gyoku.xml(command_body)
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def command_body
47
+ {
48
+ "#{NS_WIN_SHELL}:Stream" => encode_bytes(fragment.bytes),
49
+ :attributes! => {
50
+ "#{NS_WIN_SHELL}:Stream" => {
51
+ 'Name' => 'stdin',
52
+ 'CommandId' => command_id
53
+ }
54
+ }
55
+ }
56
+ end
57
+
58
+ def command_headers
59
+ merge_headers(
60
+ shared_headers(@session_opts),
61
+ resource_uri_shell(RESOURCE_URI_POWERSHELL),
62
+ action_send,
63
+ selector_shell_id(shell_id)
64
+ )
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,51 +1,51 @@
1
- # -*- encoding: utf-8 -*-
2
- #
3
- # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
- # Copyright 2016 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
- module WinRM
19
- module WSMV
20
- # WSMV SOAP namespaces mixin
21
- module SOAP
22
- NS_SOAP_ENV = 's'.freeze # http://www.w3.org/2003/05/soap-envelope
23
- NS_ADDRESSING = 'a'.freeze # http://schemas.xmlsoap.org/ws/2004/08/addressing
24
- NS_CIMBINDING = 'b'.freeze # http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd
25
- NS_ENUM = 'n'.freeze # http://schemas.xmlsoap.org/ws/2004/09/enumeration
26
- NS_TRANSFER = 'x'.freeze # http://schemas.xmlsoap.org/ws/2004/09/transfer
27
- NS_WSMAN_DMTF = 'w'.freeze # http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
28
- NS_WSMAN_MSFT = 'p'.freeze # http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd
29
- NS_SCHEMA_INST = 'xsi'.freeze # http://www.w3.org/2001/XMLSchema-instance
30
- NS_WIN_SHELL = 'rsp'.freeze # http://schemas.microsoft.com/wbem/wsman/1/windows/shell
31
- NS_WSMAN_FAULT = 'f'.freeze # http://schemas.microsoft.com/wbem/wsman/1/wsmanfault
32
- NS_WSMAN_CONF = 'cfg'.freeze # http://schemas.microsoft.com/wbem/wsman/1/config
33
-
34
- def namespaces
35
- @namespaces ||= {
36
- 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
37
- 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
38
- 'xmlns:env' => 'http://www.w3.org/2003/05/soap-envelope',
39
- "xmlns:#{NS_ADDRESSING}" => 'http://schemas.xmlsoap.org/ws/2004/08/addressing',
40
- "xmlns:#{NS_CIMBINDING}" => 'http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd',
41
- "xmlns:#{NS_ENUM}" => 'http://schemas.xmlsoap.org/ws/2004/09/enumeration',
42
- "xmlns:#{NS_TRANSFER}" => 'http://schemas.xmlsoap.org/ws/2004/09/transfer',
43
- "xmlns:#{NS_WSMAN_DMTF}" => 'http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd',
44
- "xmlns:#{NS_WSMAN_MSFT}" => 'http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd',
45
- "xmlns:#{NS_WIN_SHELL}" => 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell',
46
- "xmlns:#{NS_WSMAN_CONF}" => 'http://schemas.microsoft.com/wbem/wsman/1/config'
47
- }
48
- end
49
- end
50
- end
51
- end
1
+ # -*- encoding: utf-8 -*-
2
+ #
3
+ # Copyright 2010 Dan Wanek <dan.wanek@gmail.com>
4
+ # Copyright 2016 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
+ module WinRM
19
+ module WSMV
20
+ # WSMV SOAP namespaces mixin
21
+ module SOAP
22
+ NS_SOAP_ENV = 's'.freeze # http://www.w3.org/2003/05/soap-envelope
23
+ NS_ADDRESSING = 'a'.freeze # http://schemas.xmlsoap.org/ws/2004/08/addressing
24
+ NS_CIMBINDING = 'b'.freeze # http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd
25
+ NS_ENUM = 'n'.freeze # http://schemas.xmlsoap.org/ws/2004/09/enumeration
26
+ NS_TRANSFER = 'x'.freeze # http://schemas.xmlsoap.org/ws/2004/09/transfer
27
+ NS_WSMAN_DMTF = 'w'.freeze # http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd
28
+ NS_WSMAN_MSFT = 'p'.freeze # http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd
29
+ NS_SCHEMA_INST = 'xsi'.freeze # http://www.w3.org/2001/XMLSchema-instance
30
+ NS_WIN_SHELL = 'rsp'.freeze # http://schemas.microsoft.com/wbem/wsman/1/windows/shell
31
+ NS_WSMAN_FAULT = 'f'.freeze # http://schemas.microsoft.com/wbem/wsman/1/wsmanfault
32
+ NS_WSMAN_CONF = 'cfg'.freeze # http://schemas.microsoft.com/wbem/wsman/1/config
33
+
34
+ def namespaces
35
+ @namespaces ||= {
36
+ 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
37
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
38
+ 'xmlns:env' => 'http://www.w3.org/2003/05/soap-envelope',
39
+ "xmlns:#{NS_ADDRESSING}" => 'http://schemas.xmlsoap.org/ws/2004/08/addressing',
40
+ "xmlns:#{NS_CIMBINDING}" => 'http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd',
41
+ "xmlns:#{NS_ENUM}" => 'http://schemas.xmlsoap.org/ws/2004/09/enumeration',
42
+ "xmlns:#{NS_TRANSFER}" => 'http://schemas.xmlsoap.org/ws/2004/09/transfer',
43
+ "xmlns:#{NS_WSMAN_DMTF}" => 'http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd',
44
+ "xmlns:#{NS_WSMAN_MSFT}" => 'http://schemas.microsoft.com/wbem/wsman/1/wsman.xsd',
45
+ "xmlns:#{NS_WIN_SHELL}" => 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell',
46
+ "xmlns:#{NS_WSMAN_CONF}" => 'http://schemas.microsoft.com/wbem/wsman/1/config'
47
+ }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,79 +1,79 @@
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 'nori'
18
- require_relative 'base'
19
-
20
- module WinRM
21
- module WSMV
22
- # WSMV message to query Windows via WQL
23
- class WqlQuery < Base
24
- def initialize(session_opts, wql)
25
- @session_opts = session_opts
26
- @wql = wql
27
- end
28
-
29
- def process_response(response)
30
- parser = Nori.new(
31
- parser: :rexml,
32
- advanced_typecasting: false,
33
- convert_tags_to: ->(tag) { tag.snakecase.to_sym },
34
- strip_namespaces: true
35
- )
36
- hresp = parser.parse(response.to_s)[:envelope][:body]
37
-
38
- # Normalize items so the type always has an array even if it's just a single item.
39
- items = {}
40
- if hresp[:enumerate_response][:items]
41
- hresp[:enumerate_response][:items].each_pair do |k, v|
42
- items[k] = v.is_a?(Array) ? v : [v]
43
- end
44
- end
45
- items
46
- end
47
-
48
- protected
49
-
50
- def create_header(header)
51
- header << Gyoku.xml(wql_header)
52
- end
53
-
54
- def create_body(body)
55
- body.tag!("#{NS_ENUM}:Enumerate") { |en| en << Gyoku.xml(wql_body) }
56
- end
57
-
58
- private
59
-
60
- def wql_header
61
- merge_headers(shared_headers(@session_opts), resource_uri_wmi, action_enumerate)
62
- end
63
-
64
- def wql_body
65
- {
66
- "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
67
- "#{NS_WSMAN_DMTF}:MaxElements" => '32000',
68
- "#{NS_WSMAN_DMTF}:Filter" => @wql,
69
- "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}",
70
- :attributes! => {
71
- "#{NS_WSMAN_DMTF}:Filter" => {
72
- 'Dialect' => 'http://schemas.microsoft.com/wbem/wsman/1/WQL'
73
- }
74
- }
75
- }
76
- end
77
- end
78
- end
79
- 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 'nori'
18
+ require_relative 'base'
19
+
20
+ module WinRM
21
+ module WSMV
22
+ # WSMV message to query Windows via WQL
23
+ class WqlQuery < Base
24
+ def initialize(session_opts, wql)
25
+ @session_opts = session_opts
26
+ @wql = wql
27
+ end
28
+
29
+ def process_response(response)
30
+ parser = Nori.new(
31
+ parser: :rexml,
32
+ advanced_typecasting: false,
33
+ convert_tags_to: ->(tag) { tag.snakecase.to_sym },
34
+ strip_namespaces: true
35
+ )
36
+ hresp = parser.parse(response.to_s)[:envelope][:body]
37
+
38
+ # Normalize items so the type always has an array even if it's just a single item.
39
+ items = {}
40
+ if hresp[:enumerate_response][:items]
41
+ hresp[:enumerate_response][:items].each_pair do |k, v|
42
+ items[k] = v.is_a?(Array) ? v : [v]
43
+ end
44
+ end
45
+ items
46
+ end
47
+
48
+ protected
49
+
50
+ def create_header(header)
51
+ header << Gyoku.xml(wql_header)
52
+ end
53
+
54
+ def create_body(body)
55
+ body.tag!("#{NS_ENUM}:Enumerate") { |en| en << Gyoku.xml(wql_body) }
56
+ end
57
+
58
+ private
59
+
60
+ def wql_header
61
+ merge_headers(shared_headers(@session_opts), resource_uri_wmi, action_enumerate)
62
+ end
63
+
64
+ def wql_body
65
+ {
66
+ "#{NS_WSMAN_DMTF}:OptimizeEnumeration" => nil,
67
+ "#{NS_WSMAN_DMTF}:MaxElements" => '32000',
68
+ "#{NS_WSMAN_DMTF}:Filter" => @wql,
69
+ "#{NS_WSMAN_MSFT}:SessionId" => "uuid:#{@session_opts[:session_id]}",
70
+ :attributes! => {
71
+ "#{NS_WSMAN_DMTF}:Filter" => {
72
+ 'Dialect' => 'http://schemas.microsoft.com/wbem/wsman/1/WQL'
73
+ }
74
+ }
75
+ }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -1,88 +1,88 @@
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
- require_relative 'base'
19
-
20
- module WinRM
21
- module WSMV
22
- # WSMV message to send stdin to a remote shell
23
- class WriteStdin < Base
24
- def initialize(session_opts, stdin_opts)
25
- validate_opts(session_opts, stdin_opts)
26
- init_ops(session_opts, stdin_opts)
27
- end
28
-
29
- protected
30
-
31
- def create_header(header)
32
- header << Gyoku.xml(stdin_headers)
33
- end
34
-
35
- def create_body(body)
36
- body << Gyoku.xml(stdin_body)
37
- end
38
-
39
- private
40
-
41
- def init_ops(session_opts, stdin_opts)
42
- @session_opts = session_opts
43
- @shell_id = stdin_opts[:shell_id]
44
- @command_id = stdin_opts[:command_id]
45
- @stdin = stdin_opts[:stdin]
46
- @shell_uri = stdin_opts[:shell_uri] || RESOURCE_URI_CMD
47
- end
48
-
49
- def validate_opts(session_opts, stdin_opts)
50
- raise 'session_opts is required' unless session_opts
51
- raise 'stdin_opts[:shell_id] is required' unless stdin_opts[:shell_id]
52
- raise 'stdin_opts[:command_id] is required' unless stdin_opts[:command_id]
53
- raise 'stdin_opts[:stdin] is required' unless stdin_opts[:stdin]
54
- end
55
-
56
- def stdin_headers
57
- merge_headers(shared_headers(@session_opts),
58
- resource_uri_shell(@shell_uri),
59
- action_send,
60
- selector_shell_id(@shell_id))
61
- end
62
-
63
- def action_send
64
- {
65
- "#{NS_ADDRESSING}:Action" =>
66
- 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send',
67
- :attributes! => {
68
- "#{NS_ADDRESSING}:Action" => {
69
- 'mustUnderstand' => true
70
- }
71
- }
72
- }
73
- end
74
-
75
- def stdin_body
76
- {
77
- "#{NS_WIN_SHELL}:Send" => {
78
- "#{NS_WIN_SHELL}:Stream" => {
79
- '@Name' => 'stdin',
80
- '@CommandId' => @command_id,
81
- :content! => Base64.encode64(@stdin)
82
- }
83
- }
84
- }
85
- end
86
- end
87
- end
88
- 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
+ require_relative 'base'
19
+
20
+ module WinRM
21
+ module WSMV
22
+ # WSMV message to send stdin to a remote shell
23
+ class WriteStdin < Base
24
+ def initialize(session_opts, stdin_opts)
25
+ validate_opts(session_opts, stdin_opts)
26
+ init_ops(session_opts, stdin_opts)
27
+ end
28
+
29
+ protected
30
+
31
+ def create_header(header)
32
+ header << Gyoku.xml(stdin_headers)
33
+ end
34
+
35
+ def create_body(body)
36
+ body << Gyoku.xml(stdin_body)
37
+ end
38
+
39
+ private
40
+
41
+ def init_ops(session_opts, stdin_opts)
42
+ @session_opts = session_opts
43
+ @shell_id = stdin_opts[:shell_id]
44
+ @command_id = stdin_opts[:command_id]
45
+ @stdin = stdin_opts[:stdin]
46
+ @shell_uri = stdin_opts[:shell_uri] || RESOURCE_URI_CMD
47
+ end
48
+
49
+ def validate_opts(session_opts, stdin_opts)
50
+ raise 'session_opts is required' unless session_opts
51
+ raise 'stdin_opts[:shell_id] is required' unless stdin_opts[:shell_id]
52
+ raise 'stdin_opts[:command_id] is required' unless stdin_opts[:command_id]
53
+ raise 'stdin_opts[:stdin] is required' unless stdin_opts[:stdin]
54
+ end
55
+
56
+ def stdin_headers
57
+ merge_headers(shared_headers(@session_opts),
58
+ resource_uri_shell(@shell_uri),
59
+ action_send,
60
+ selector_shell_id(@shell_id))
61
+ end
62
+
63
+ def action_send
64
+ {
65
+ "#{NS_ADDRESSING}:Action" =>
66
+ 'http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send',
67
+ :attributes! => {
68
+ "#{NS_ADDRESSING}:Action" => {
69
+ 'mustUnderstand' => true
70
+ }
71
+ }
72
+ }
73
+ end
74
+
75
+ def stdin_body
76
+ {
77
+ "#{NS_WIN_SHELL}:Send" => {
78
+ "#{NS_WIN_SHELL}:Stream" => {
79
+ '@Name' => 'stdin',
80
+ '@CommandId' => @command_id,
81
+ :content! => Base64.encode64(@stdin)
82
+ }
83
+ }
84
+ }
85
+ end
86
+ end
87
+ end
88
+ end