Skip to content

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 6, 2017
1 parent c506e6d commit 7d2fbb1
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions app/models/manageiq/providers/google/manager_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,25 @@ module ManageIQ::Providers::Google::ManagerMixin
extend ActiveSupport::Concern

def verify_credentials(auth_type = nil, options = {})
begin
options[:auth_type] = auth_type

connection = connect(options)

# Not all errors will cause Fog to raise an exception,
# for example an error in the google_project id will
# succeed to connect but the first API call will raise
# an exception, so make a simple call to the API to
# confirm everything is working
connection.regions.all
rescue => err
raise MiqException::MiqInvalidCredentialsError, err.message
end
options[:auth_type] = auth_type
connect(options, true)

true
end

def connect(options = {})
def connect(options = {}, validate = false)
raise MiqException::MiqHostError, "No credentials defined" if missing_credentials?(options[:auth_type])

auth_token = authentication_token(options[:auth_type])
self.class.raw_connect(project, auth_token, options, options[:proxy_uri] || http_proxy_uri)
self.class.raw_connect(project, auth_token, options, options[:proxy_uri] || http_proxy_uri, validate)
end

def gce
@gce ||= connect(:service => "compute")
end

module ClassMethods
def raw_connect(google_project, google_json_key, options, proxy_uri = nil)
def raw_connect(google_project, google_json_key, options, proxy_uri = nil, validate = false)
require 'fog/google'

config = {
Expand All @@ -46,17 +34,30 @@ def raw_connect(google_project, google_json_key, options, proxy_uri = nil)
}
}

case options[:service]
# specify Compute as the default
when 'compute', nil
::Fog::Compute.new(config)
when 'pubsub'
::Fog::Google::Pubsub.new(config.except(:provider))
when 'monitoring'
::Fog::Google::Monitoring.new(config.except(:provider))
else
raise ArgumentError, "Unknown service: #{options[:service]}"
begin
case options[:service]
# specify Compute as the default
when 'compute', nil
connection = ::Fog::Compute.new(config)
when 'pubsub'
connection = ::Fog::Google::Pubsub.new(config.except(:provider))
when 'monitoring'
connection = ::Fog::Google::Monitoring.new(config.except(:provider))
else
raise ArgumentError, "Unknown service: #{options[:service]}"
end

# Not all errors will cause Fog to raise an exception,
# for example an error in the google_project id will
# succeed to connect but the first API call will raise
# an exception, so make a simple call to the API to
# confirm everything is working
connection.regions.all if validate
rescue => err
raise MiqException::MiqInvalidCredentialsError, err.message
end

connection
end
end
end

0 comments on commit 7d2fbb1

Please sign in to comment.