winrm-elevated 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -1,46 +1,58 @@
1
- # encoding: UTF-8
2
- require 'winrm'
3
- require 'winrm-elevated'
4
- require_relative 'matchers'
5
-
6
- # Creates a WinRM connection for integration tests
7
- module ConnectionHelper
8
- def winrm_connection
9
- WinRM::WinRMWebService.new(
10
- winrm_config['endpoint'],
11
- winrm_config['auth_type'].to_sym,
12
- user: username,
13
- pass: password,
14
- basic_auth_only: true)
15
- end
16
-
17
- def elevated_runner
18
- @elevated_runner ||= WinRM::Elevated::Runner.new(winrm_connection.create_executor)
19
- end
20
-
21
- def winrm_config
22
- unless @winrm_config
23
- path = File.expand_path("#{File.dirname(__FILE__)}/config.yml")
24
- unless File.exist?(path)
25
- path = File.expand_path("#{File.dirname(__FILE__)}/config-example.yml")
26
- end
27
- @winrm_config = YAML.load(File.read(path))
28
- @winrm_config['endpoint'] = ENV['winrm_endpoint'] if ENV['winrm_endpoint']
29
- @winrm_config['options']['user'] = ENV['winrm_user'] if ENV['winrm_user']
30
- @winrm_config['options']['pass'] = ENV['winrm_pass'] if ENV['winrm_pass']
31
- end
32
- @winrm_config
33
- end
34
-
35
- def username
36
- winrm_config['options']['user']
37
- end
38
-
39
- def password
40
- winrm_config['options']['pass']
41
- end
42
- end
43
-
44
- RSpec.configure do |config|
45
- config.include(ConnectionHelper)
46
- end
1
+ # encoding: UTF-8
2
+ require 'winrm'
3
+ require 'winrm-elevated'
4
+ require_relative 'matchers'
5
+
6
+ # Creates a WinRM connection for integration tests
7
+ module ConnectionHelper
8
+ def winrm_connection
9
+ WinRM::Connection.new(winrm_config)
10
+ end
11
+
12
+ def elevated_shell
13
+ @elevated_shell ||= winrm_connection.shell(:elevated)
14
+ end
15
+
16
+ def winrm_config
17
+ unless @winrm_config
18
+ path = File.expand_path("#{File.dirname(__FILE__)}/config.yml")
19
+ unless File.exist?(path)
20
+ path = File.expand_path("#{File.dirname(__FILE__)}/config-example.yml")
21
+ end
22
+ @winrm_config = symbolize_keys(YAML.load(File.read(path)))
23
+ @winrm_config[:endpoint] = ENV['winrm_endpoint'] if ENV['winrm_endpoint']
24
+ @winrm_config[:user] = ENV['winrm_user'] if ENV['winrm_user']
25
+ @winrm_config[:password] = ENV['winrm_password'] if ENV['winrm_password']
26
+ end
27
+ @winrm_config
28
+ end
29
+
30
+ def username
31
+ winrm_config[:user]
32
+ end
33
+
34
+ def password
35
+ winrm_config[:password]
36
+ end
37
+
38
+ # rubocop:disable Metrics/MethodLength
39
+ def symbolize_keys(hash)
40
+ hash.each_with_object({}) do |(key, value), result|
41
+ new_key = case key
42
+ when String then key.to_sym
43
+ else key
44
+ end
45
+ new_value = case value
46
+ when Hash then symbolize_keys(value)
47
+ else value
48
+ end
49
+ result[new_key] = new_value
50
+ result
51
+ end
52
+ end
53
+ # rubocop:enable Metrics/MethodLength
54
+ end
55
+
56
+ RSpec.configure do |config|
57
+ config.include(ConnectionHelper)
58
+ end
@@ -1,33 +1,33 @@
1
- # encoding: UTF-8
2
- require 'date'
3
-
4
- version = File.read(File.expand_path('../VERSION', __FILE__)).strip
5
-
6
- Gem::Specification.new do |s|
7
- s.platform = Gem::Platform::RUBY
8
- s.name = 'winrm-elevated'
9
- s.version = version
10
- s.date = Date.today.to_s
11
-
12
- s.author = ['Shawn Neal']
13
- s.email = ['sneal@sneal.net']
14
- s.homepage = 'https://github.com/WinRb/winrm-elevated'
15
-
16
- s.summary = 'Ruby library for running commands as elevated'
17
- s.description = <<-EOF
18
- Ruby library for running commands via WinRM as elevated through a scheduled task
19
- EOF
20
- s.license = 'Apache-2.0'
21
-
22
- s.files = `git ls-files`.split(/\n/)
23
- s.require_path = 'lib'
24
- s.rdoc_options = %w(-x test/ -x examples/)
25
- s.extra_rdoc_files = %w(README.md LICENSE)
26
-
27
- s.required_ruby_version = '>= 1.9.0'
28
- s.add_runtime_dependency 'winrm', '~> 1.5'
29
- s.add_runtime_dependency 'winrm-fs', '~> 0.4.2'
30
- s.add_development_dependency 'rspec', '~> 3.2'
31
- s.add_development_dependency 'rake', '~> 10.3'
32
- s.add_development_dependency 'rubocop', '~> 0.28'
33
- end
1
+ # encoding: UTF-8
2
+ require 'date'
3
+
4
+ version = File.read(File.expand_path('../VERSION', __FILE__)).strip
5
+
6
+ Gem::Specification.new do |s|
7
+ s.platform = Gem::Platform::RUBY
8
+ s.name = 'winrm-elevated'
9
+ s.version = version
10
+ s.date = Date.today.to_s
11
+
12
+ s.author = ['Shawn Neal']
13
+ s.email = ['sneal@sneal.net']
14
+ s.homepage = 'https://github.com/WinRb/winrm-elevated'
15
+
16
+ s.summary = 'Ruby library for running commands as elevated'
17
+ s.description = <<-EOF
18
+ Ruby library for running commands via WinRM as elevated through a scheduled task
19
+ EOF
20
+ s.license = 'Apache-2.0'
21
+
22
+ s.files = `git ls-files`.split(/\n/)
23
+ s.require_path = 'lib'
24
+ s.rdoc_options = %w(-x test/ -x examples/)
25
+ s.extra_rdoc_files = %w(README.md LICENSE)
26
+
27
+ s.required_ruby_version = '>= 1.9.0'
28
+ s.add_runtime_dependency 'winrm', '~> 2.0'
29
+ s.add_runtime_dependency 'winrm-fs', '~> 1.0'
30
+ s.add_development_dependency 'rspec', '~> 3.2'
31
+ s.add_development_dependency 'rake', '~> 10.3'
32
+ s.add_development_dependency 'rubocop', '~> 0.28'
33
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winrm-elevated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shawn Neal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-20 00:00:00.000000000 Z
11
+ date: 2016-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: winrm
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '2.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: winrm-fs
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 0.4.2
33
+ version: '1.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 0.4.2
40
+ version: '1.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -80,8 +80,8 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.28'
83
- description: |2
84
- Ruby library for running commands via WinRM as elevated through a scheduled task
83
+ description: " Ruby library for running commands via WinRM as elevated through
84
+ a scheduled task\n"
85
85
  email:
86
86
  - sneal@sneal.net
87
87
  executables: []
@@ -102,8 +102,8 @@ files:
102
102
  - appveyor.yml
103
103
  - changelog.md
104
104
  - lib/winrm-elevated.rb
105
- - lib/winrm-elevated/runner.rb
106
105
  - lib/winrm-elevated/scripts/elevated_shell.ps1
106
+ - lib/winrm/shells/elevated.rb
107
107
  - spec/config-example.yml
108
108
  - spec/matchers.rb
109
109
  - spec/powershell_elevated_spec.rb
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  version: '0'
134
134
  requirements: []
135
135
  rubyforge_project:
136
- rubygems_version: 2.5.2
136
+ rubygems_version: 2.6.6
137
137
  signing_key:
138
138
  specification_version: 4
139
139
  summary: Ruby library for running commands as elevated
@@ -1,83 +0,0 @@
1
- # encoding: UTF-8
2
- #
3
- # Copyright 2015 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 'winrm'
18
- require 'winrm-fs'
19
- require 'securerandom'
20
-
21
- module WinRM
22
- module Elevated
23
- # Runs PowerShell commands elevated via a scheduled task
24
- class Runner
25
- # Creates a new Elevated Runner instance
26
- # @param [CommandExecutor] a winrm CommandExecutor
27
- def initialize(executor)
28
- @executor = executor
29
- @winrm_file_transporter = WinRM::FS::Core::FileTransporter.new(executor)
30
- @elevated_shell_path = 'c:/windows/temp/winrm-elevated-shell-' + SecureRandom.uuid + '.ps1'
31
- @uploaded = nil
32
- end
33
-
34
- # Run a command or PowerShell script elevated without any of the
35
- # restrictions that WinRM puts in place.
36
- #
37
- # @param [String] The command or PS script to wrap in a scheduled task
38
- # @param [String] The admin user name to execute the scheduled task as
39
- # @param [String] The admin user password
40
- #
41
- # @return [Hash] :stdout and :stderr
42
- def powershell_elevated(script, username, password, &block)
43
- # if an IO object is passed read it, otherwise assume the contents of the file were passed
44
- script_text = script.respond_to?(:read) ? script.read : script
45
-
46
- upload_elevated_shell_wrapper_script
47
- wrapped_script = wrap_in_scheduled_task(script_text, username, password)
48
- @executor.run_cmd(wrapped_script, &block)
49
- end
50
-
51
- private
52
-
53
- def upload_elevated_shell_wrapper_script
54
- return if @uploaded
55
- with_temp_file do |temp_file|
56
- @winrm_file_transporter.upload(temp_file, @elevated_shell_path)
57
- @uploaded = true
58
- end
59
- end
60
-
61
- def with_temp_file
62
- file = Tempfile.new(['winrm-elevated-shell', 'ps1'])
63
- file.write(elevated_shell_script_content)
64
- file.fsync
65
- file.close
66
- yield file.path
67
- ensure
68
- file.close
69
- file.unlink
70
- end
71
-
72
- def elevated_shell_script_content
73
- IO.read(File.expand_path('../scripts/elevated_shell.ps1', __FILE__))
74
- end
75
-
76
- def wrap_in_scheduled_task(script_text, username, password)
77
- ps_script = WinRM::PowershellScript.new(script_text)
78
- "powershell -executionpolicy bypass -file \"#{@elevated_shell_path}\" " \
79
- "-username \"#{username}\" -password \"#{password}\" -encoded_command \"#{ps_script.encoded}\""
80
- end
81
- end
82
- end
83
- end