Skip to content
This repository was archived by the owner on Jul 25, 2023. It is now read-only.

Commit

Permalink
Fix exception handing for credential validation on raw_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
tumido committed Nov 7, 2017
1 parent cf0e30b commit a02871b
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions app/models/manageiq/providers/microsoft/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def self.raw_connect(connect_params, validate = false)
connect = WinRM::Connection.new(connect_params)
return connect unless validate

connect.shell(:powershell).run('hostname')
connection_rescue_block do
connect.shell(:powershell).run('hostname')
end
end

def self.auth_url(hostname, port = nil)
Expand All @@ -56,8 +58,21 @@ def self.build_connect_params(options)
connect_params
end

def self.connection_rescue_block(realm = nil)
require 'winrm'
require 'gssapi' # A winrm dependency
yield
rescue WinRM::WinRMHTTPTransportError => e # Error 401
raise MiqException::MiqHostError, "Check credentials and WinRM configuration settings. " \
"Remote error message: #{e.message}"
rescue GSSAPI::GssApiError
raise MiqException::MiqHostError, "Unable to reach any KDC in realm #{realm}"
rescue => e
raise MiqException::MiqHostError, "Unable to connect: #{e.message}"
end

def connect(options = {})
raise "no credentials defined" if self.missing_credentials?(options[:auth_type])
raise "no credentials defined" if missing_credentials?(options[:auth_type])

hostname = options[:hostname] || self.hostname
options[:endpoint] = self.class.auth_url(hostname, port)
Expand All @@ -71,20 +86,10 @@ def connect(options = {})
end

def verify_credentials(_auth_type = nil, options = {})
require 'winrm'
require 'gssapi' # A winrm dependency

raise MiqException::MiqHostError, "No credentials defined" if self.missing_credentials?(options[:auth_type])
raise MiqException::MiqHostError, "No credentials defined" if missing_credentials?(options[:auth_type])

begin
self.class.connection_rescue_block do
run_dos_command("hostname")
rescue WinRM::WinRMHTTPTransportError => e # Error 401
raise MiqException::MiqHostError, "Check credentials and WinRM configuration settings. " \
"Remote error message: #{e.message}"
rescue GSSAPI::GssApiError
raise MiqException::MiqHostError, "Unable to reach any KDC in realm #{realm}"
rescue => e
raise MiqException::MiqHostError, "Unable to connect: #{e.message}"
end

true
Expand Down

0 comments on commit a02871b

Please sign in to comment.