Skip to content

Commit

Permalink
updating rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
mwrock committed Apr 18, 2016
1 parent d210532 commit 0d7b11d
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 130 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AllCops:
Exclude:
- 'appveyor.yml'
- 'Vagrantfile'
- 'scripts/**/*'
- 'lib/winrm/winrm_service.rb'
- 'lib/winrm/http/transport.rb'
Expand All @@ -17,6 +18,9 @@ Metrics/LineLength:
Metrics/MethodLength:
Max: 20

ModuleLength:
Max: 250

ClassLength:
Max: 250

Expand Down
7 changes: 0 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# encoding: UTF-8
source 'https://rubygems.org'
gemspec

group :development do
# Use a tighter specification until rubocop issues are fixed.
# Then remove this
gem 'rubocop', '~> 0.28.0'
gem 'pry', '~> 0.10'
end
68 changes: 36 additions & 32 deletions lib/winrm/connection_opts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,41 @@ class ConnectionOpts < Hash
DEFAULT_OPERATION_TIMEOUT = 60
DEFAULT_RECEIVE_TIMEOUT = DEFAULT_OPERATION_TIMEOUT + 10
DEFAULT_MAX_ENV_SIZE = 153600
DEFAULT_LOCALE = 'en-US'
DEFAULT_LOCALE = 'en-US'.freeze
DEFAULT_RETRY_DELAY = 10
DEFAULT_RETRY_LIMIT = 3
DEFAULT_MAX_COMMANDS = 1480 # TODO: interrogate remote OS version

def self.create_with_defaults(overrides)
config = default.merge(overrides)
config = ensure_receive_timeout_is_greater_than_operation_timeout(config)
config.validate
config
class << self
def create_with_defaults(overrides)
config = default.merge(overrides)
config = ensure_receive_timeout_is_greater_than_operation_timeout(config)
config.validate
config
end

private

def ensure_receive_timeout_is_greater_than_operation_timeout(config)
if config[:receive_timeout] < config[:operation_timeout]
config[:receive_timeout] = config[:operation_timeout] + 10
end
config
end

def default
config = ConnectionOpts.new
config[:session_id] = SecureRandom.uuid.to_s.upcase
config[:transport] = :negotiate
config[:locale] = DEFAULT_LOCALE
config[:max_envelope_size] = DEFAULT_MAX_ENV_SIZE
config[:max_commands] = DEFAULT_MAX_COMMANDS
config[:operation_timeout] = DEFAULT_OPERATION_TIMEOUT
config[:receive_timeout] = DEFAULT_RECEIVE_TIMEOUT
config[:retry_delay] = DEFAULT_RETRY_DELAY
config[:retry_limit] = DEFAULT_RETRY_LIMIT
config
end
end

def validate
Expand All @@ -42,9 +67,9 @@ def validate
private

def validate_required_fields
fail 'endpoint is a required option' unless self[:endpoint]
fail 'user is a required option' unless self[:user]
fail 'password is a required option' unless self[:password]
raise 'endpoint is a required option' unless self[:endpoint]
raise 'user is a required option' unless self[:user]
raise 'password is a required option' unless self[:password]
end

def validate_data_types
Expand All @@ -58,29 +83,8 @@ def validate_data_types

def validate_fixnum(key, min = 0)
value = self[key]
fail "#{key} must be a Fixnum" unless value && value.is_a?(Fixnum)
fail "#{key} must be greater than #{min}" unless value > min
end

def self.ensure_receive_timeout_is_greater_than_operation_timeout(config)
if config[:receive_timeout] < config[:operation_timeout]
config[:receive_timeout] = config[:operation_timeout] + 10
end
config
end

def self.default
config = ConnectionOpts.new
config[:session_id] = SecureRandom.uuid.to_s.upcase
config[:transport] = :negotiate
config[:locale] = DEFAULT_LOCALE
config[:max_envelope_size] = DEFAULT_MAX_ENV_SIZE
config[:max_commands] = DEFAULT_MAX_COMMANDS
config[:operation_timeout] = DEFAULT_OPERATION_TIMEOUT
config[:receive_timeout] = DEFAULT_RECEIVE_TIMEOUT
config[:retry_delay] = DEFAULT_RETRY_DELAY
config[:retry_limit] = DEFAULT_RETRY_LIMIT
config
raise "#{key} must be a Fixnum" unless value && value.is_a?(Fixnum)
raise "#{key} must be greater than #{min}" unless value > min
end
end
end
8 changes: 4 additions & 4 deletions lib/winrm/http/response_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def raise_if_error
end

def raise_if_auth_error
fail WinRMAuthorizationError if @status_code == 401
raise WinRMAuthorizationError if @status_code == 401
end

def raise_if_wsman_fault
Expand All @@ -66,7 +66,7 @@ def raise_if_wsman_fault
fault = REXML::XPath.first(
soap_errors,
"//#{WinRM::WSMV::SOAP::NS_WSMAN_FAULT}:WSManFault")
fail WinRMWSManFault.new(fault.to_s, fault.attributes['Code']) unless fault.nil?
raise WinRMWSManFault.new(fault.to_s, fault.attributes['Code']) unless fault.nil?
end

def raise_if_wmi_error
Expand All @@ -83,11 +83,11 @@ def raise_if_wmi_error
error_code = REXML::XPath.first(
error,
"//#{WinRM::WSMV::SOAP::NS_WSMAN_MSFT}:error_Code").text
fail WinRMWMIError.new(error.to_s, error_code)
raise WinRMWMIError.new(error.to_s, error_code)
end

def raise_transport_error
fail WinRMHTTPTransportError.new('Bad HTTP response returned from server', @status_code)
raise WinRMHTTPTransportError.new('Bad HTTP response returned from server', @status_code)
end
end
end
10 changes: 5 additions & 5 deletions lib/winrm/psrp/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Message
information_record: 0x00041011,
pipeline_host_call: 0x00041100,
pipeline_host_response: 0x00041101
}
}.freeze

# Creates a new PSRP message instance
# @param message_parts [Hash]
Expand All @@ -88,11 +88,11 @@ class Message
def initialize(message_parts)
message_parts.merge!(default_parts) { |_key, v1, _v2| v1 }

fail 'runspace_pool_id cannot be nil' unless message_parts[:runspace_pool_id]
raise 'runspace_pool_id cannot be nil' unless message_parts[:runspace_pool_id]
unless MESSAGE_TYPES.values.include?(message_parts[:message_type])
fail 'invalid message type'
raise 'invalid message type'
end
fail 'data cannot be nil' unless message_parts[:data]
raise 'data cannot be nil' unless message_parts[:data]

@data = message_parts[:data]
@destination = message_parts[:destination]
Expand All @@ -113,7 +113,7 @@ def initialize(message_parts)
# @return [Array<Byte>] Unencoded raw byte array of the PSRP message.
def bytes
if data_bytes.length > BLOB_MAX_LEN
fail "data cannot be greater than #{BLOB_MAX_LEN} bytes"
raise "data cannot be greater than #{BLOB_MAX_LEN} bytes"
end

[
Expand Down
20 changes: 11 additions & 9 deletions lib/winrm/shells/cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ module Shells
class Cmd
include Retryable

class << self
def finalize(connection_opts, transport, shell_id)
proc { Cmd.close_shell(connection_opts, transport, shell_id) }
end

def close_shell(connection_opts, transport, shell_id)
msg = WinRM::WSMV::CloseShell.new(connection_opts, shell_id: shell_id)
transport.send_request(msg.build)
end
end

# Create a new Cmd shell
# @param connection_opts [ConnectionOpts] The WinRM connection options
# @param transport [HttpTransport] The WinRM SOAP transport
Expand Down Expand Up @@ -110,15 +121,6 @@ def add_finalizer
def remove_finalizer
ObjectSpace.undefine_finalizer(self)
end

def self.close_shell(connection_opts, transport, shell_id)
msg = WinRM::WSMV::CloseShell.new(connection_opts, shell_id: shell_id)
transport.send_request(msg.build)
end

def self.finalize(connection_opts, transport, shell_id)
proc { Cmd.close_shell(connection_opts, transport, shell_id) }
end
end
end
end
28 changes: 15 additions & 13 deletions lib/winrm/shells/power_shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ module Shells
class PowerShell
include Retryable

class << self
def finalize(connection_opts, transport, shell_id)
proc { PowerShell.close_shell(connection_opts, transport, shell_id) }
end

def close_shell(connection_opts, transport, shell_id)
msg = WinRM::WSMV::CloseShell.new(
connection_opts,
shell_id: shell_id,
shell_uri: WinRM::WSMV::Header::RESOURCE_URI_POWERSHELL
)
transport.send_request(msg.build)
end
end

# Create a new PowerShell shell
# @param connection_opts [ConnectionOpts] The WinRM connection options
# @param transport [HttpTransport] The WinRM SOAP transport
Expand Down Expand Up @@ -93,15 +108,6 @@ def cleanup_command(command_id)
@transport.send_request(cleanup_msg.build)
end

def self.close_shell(connection_opts, transport, shell_id)
msg = WinRM::WSMV::CloseShell.new(
connection_opts,
shell_id: shell_id,
shell_uri: WinRM::WSMV::Header::RESOURCE_URI_POWERSHELL
)
transport.send_request(msg.build)
end

def open
close
retryable(@connection_opts[:retry_limit], @connection_opts[:retry_delay]) do
Expand All @@ -124,10 +130,6 @@ def add_finalizer
def remove_finalizer
ObjectSpace.undefine_finalizer(self)
end

def self.finalize(connection_opts, transport, shell_id)
proc { PowerShell.close_shell(connection_opts, transport, shell_id) }
end
end
end
end
9 changes: 3 additions & 6 deletions lib/winrm/shells/retryable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ module Retryable
def retryable(retries, delay)
yield
rescue *RETRYABLE_EXCEPTIONS.call
if (retries -= 1) > 0
sleep(delay)
retry
else
raise
end
raise unless (retries -= 1) > 0
sleep(delay)
retry
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/winrm/shells/shell_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create_shell(shell_type)
when :powershell
return WinRM::Shells::PowerShell.new(@connection_opts, @transport, @logger)
else
fail "#{shell_type} is not a valid WinRM shell type. " \
raise "#{shell_type} is not a valid WinRM shell type. " \
'Expected either :cmd or :powershell.'
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/winrm/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# WinRM module
module WinRM
# The version of the WinRM library
VERSION = '2.0.0.dev'
VERSION = '2.0.0.dev'.freeze
end
4 changes: 2 additions & 2 deletions lib/winrm/wsmv/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def build
protected

def create_header
fail 'create_header must be implemented'
raise 'create_header must be implemented'
end

def create_body
fail 'create_body must be implemented'
raise 'create_body must be implemented'
end

def encode_bytes(bytes)
Expand Down
8 changes: 4 additions & 4 deletions lib/winrm/wsmv/cleanup_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module WSMV
# WSMV message to execute a command inside a remote shell
class CleanupCommand < Base
def initialize(session_opts, opts)
fail 'opts[:shell_id] is required' unless opts[:shell_id]
fail 'opts[:command_id] is required' unless opts[:command_id]
raise 'opts[:shell_id] is required' unless opts[:shell_id]
raise 'opts[:command_id] is required' unless opts[:command_id]
@session_opts = session_opts
@shell_id = opts[:shell_id]
@command_id = opts[:command_id]
Expand All @@ -36,8 +36,8 @@ def create_header(header)
end

def create_body(body)
body.tag!("#{NS_WIN_SHELL}:Signal", 'CommandId' => @command_id) do
|cl| cl << Gyoku.xml(cleanup_body)
body.tag!("#{NS_WIN_SHELL}:Signal", 'CommandId' => @command_id) do |cl|
cl << Gyoku.xml(cleanup_body)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/winrm/wsmv/close_shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module WSMV
# WSMV message to close a remote shell
class CloseShell < Base
def initialize(session_opts, shell_opts)
fail 'shell_opts[:shell_id] is required' unless shell_opts[:shell_id]
raise 'shell_opts[:shell_id] is required' unless shell_opts[:shell_id]
@session_opts = session_opts
@shell_id = shell_opts[:shell_id]
@shell_uri = shell_opts[:shell_uri] || RESOURCE_URI_CMD
Expand Down
8 changes: 4 additions & 4 deletions lib/winrm/wsmv/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def init_ops(session_opts, cmd_opts)
end

def validate_opts(session_opts, cmd_opts)
fail 'session_opts is required' unless session_opts
fail 'cmd_opts[:shell_id] is required' unless cmd_opts[:shell_id]
fail 'cmd_opts[:command] is required' unless cmd_opts[:command]
raise 'session_opts is required' unless session_opts
raise 'cmd_opts[:shell_id] is required' unless cmd_opts[:shell_id]
raise 'cmd_opts[:command] is required' unless cmd_opts[:command]
end

def issue69_unescape_single_quotes(xml)
escaped_cmd = /<#{NS_WIN_SHELL}:Command>(.+)<\/#{NS_WIN_SHELL}:Command>/m.match(xml)[1]
escaped_cmd = %r{<#{NS_WIN_SHELL}:Command>(.+)<\/#{NS_WIN_SHELL}:Command>}m.match(xml)[1]
xml[escaped_cmd] = escaped_cmd.gsub(/&#39;/, "'")
xml
end
Expand Down
4 changes: 2 additions & 2 deletions lib/winrm/wsmv/command_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ module WSMV
# WSMV message to get output from a remote shell
class CommandOutput < Base
def initialize(session_opts, command_out_opts)
fail 'command_out_opts[:shell_id] is required' unless command_out_opts[:shell_id]
fail 'command_out_opts[:command_id] is required' unless command_out_opts[:command_id]
raise 'command_out_opts[:shell_id] is required' unless command_out_opts[:shell_id]
raise 'command_out_opts[:command_id] is required' unless command_out_opts[:command_id]
@session_opts = session_opts
@shell_id = command_out_opts[:shell_id]
@command_id = command_out_opts[:command_id]
Expand Down
Loading

0 comments on commit 0d7b11d

Please sign in to comment.