Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DDF based verify_credentials #198

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class ManageIQ::Providers::AnsibleTower::AutomationManager < ManageIQ::Providers::ExternalAutomationManager
include ProcessTasksMixin

class << self
delegate :params_for_create,
:verify_credentials,
:to => ManageIQ::Providers::AnsibleTower::Provider
end

delegate :authentications,
:authentication_check,
:authentication_status,
Expand Down
99 changes: 85 additions & 14 deletions app/models/manageiq/providers/ansible_tower/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,88 @@ class ManageIQ::Providers::AnsibleTower::Provider < ::Provider
validates :name, :presence => true, :uniqueness => true
validates :url, :presence => true

PARAMS_FOR_CREATE = {
:title => "Configure Ansible Tower",
:fields => [
{
:component => "text-field",
:name => "endpoints.default.base_url",
:label => "URL",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "text-field",
:name => "endpoints.default.username",
:label => "Username",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "text-field",
:name => "endpoints.default.password",
:label => "Password",
:type => "password",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "checkbox",
:name => "endpoints.default.verify_ssl",
:label => "Verify SSL"
}
]
}.freeze

def self.params_for_create
PARAMS_FOR_CREATE
end

# Verify Credentials
# args:
# {
# "endpoints" => {
# "default" => {
# "base_url" => "",
# "username" => "",
# "password" => "",
# "verify_ssl" => ""
# }
# }
# }
def self.verify_credentials(args)
default_endpoint = args.dig("endpoints", "default")

base_url, username, password, verify_ssl = default_endpoint&.values_at(
"base_url", "username", "password", "verify_ssl"
)
base_url = adjust_url(base_url)
verify_ssl = verify_ssl ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE

!!verify_connection(raw_connect(base_url, username, password, verify_ssl))
agrare marked this conversation as resolved.
Show resolved Hide resolved
end

def self.default_api_path
"/api/v1".freeze
end

def self.adjust_url(url)
url = "https://#{url}" unless url =~ %r{\Ahttps?:\/\/} # HACK: URI can't properly parse a URL with no scheme
URI(url).tap do |adjusted_url|
adjusted_url.path = default_api_path if adjusted_url.path.blank?
end
end

def self.verify_connection(connection)
require 'ansible_tower_client'
begin
connection.api.verify_credentials ||
raise(MiqException::MiqInvalidCredentialsError, _("Username or password is not valid"))
rescue AnsibleTowerClient::ClientError => err
raise MiqException::MiqCommunicationsError, err.message, err.backtrace
end
end

def self.raw_connect(base_url, username, password, verify_ssl)
require 'ansible_tower_client'
AnsibleTowerClient.logger = $ansible_tower_log
Expand Down Expand Up @@ -38,28 +120,17 @@ def connect(options = {})
end

def verify_credentials(auth_type = nil, options = {})
require 'ansible_tower_client'
begin
with_provider_connection(options.merge(:auth_type => auth_type)) { |c| c.api.verify_credentials } ||
raise(MiqException::MiqInvalidCredentialsError, _("Username or password is not valid"))
rescue AnsibleTowerClient::ClientError => err
raise MiqException::MiqCommunicationsError, err.message, err.backtrace
with_provider_connection(options.merge(:auth_type => auth_type)) do |c|
self.class.verify_connection(c)
end
end

def url=(new_url)
new_url = "https://#{new_url}" unless new_url =~ %r{\Ahttps?:\/\/} # HACK: URI can't properly parse a URL with no scheme
uri = URI(new_url)
uri.path = default_api_path if uri.path.blank?
default_endpoint.url = uri.to_s
default_endpoint.url = self.class.adjust_url(new_url).to_s
end

private

def default_api_path
"/api/v1".freeze
end

def ensure_managers
build_automation_manager unless automation_manager
automation_manager.name = _("%{name} Automation Manager") % {:name => name}
Expand Down