From 447dde404720dd1163230228bfb91b2cfc57db6a Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 28 Jan 2020 10:58:52 -0500 Subject: [PATCH] Add verify_credentials_task to host This adds a way for the UI/API to queue a verify_credentials call for a host rather than calling it directly. --- app/models/host.rb | 25 +++++++++++++++++++++++++ spec/models/host_spec.rb | 20 +++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/models/host.rb b/app/models/host.rb index 2116f582180..95898446cb4 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -737,6 +737,31 @@ def self.batch_update_authentication(host_ids, creds = {}) errors.empty? ? true : errors end + def verify_credentials_task(userid, auth_type = nil, options = {}) + task_opts = { + :action => "Verify Host Credentials", + :userid => userid + } + + queue_opts = { + :args => [auth_type, options], + :class_name => self.class.name, + :instance_id => id, + :method_name => "verify_credentials?", + :queue_name => queue_name_for_ems_operations, + :role => "ems_operations", + :zone => my_zone + } + + MiqTask.generic_action_with_callback(task_opts, queue_opts) + end + + def verify_credentials?(*args) + # Prevent the connection details, including the password, from being leaked into the logs + # and MiqQueue by only returning true/false + !!verify_credentials(*args) + end + def verify_credentials(auth_type = nil, options = {}) raise MiqException::MiqHostError, _("No credentials defined") if missing_credentials?(auth_type) if auth_type.to_s != 'ipmi' && os_image_name !~ /linux_*/ diff --git a/spec/models/host_spec.rb b/spec/models/host_spec.rb index 8e158c561c1..91650bc9af6 100644 --- a/spec/models/host_spec.rb +++ b/spec/models/host_spec.rb @@ -194,11 +194,29 @@ EvmSpecHelper.local_miq_server @password = "v2:{/OViaBJ0Ug+RSW9n7EFGqw==}" - @host = FactoryBot.create(:host_vmware_esx) + @ems = FactoryBot.create(:ems_vmware) + @host = FactoryBot.create(:host_vmware_esx, :ext_management_system => @ems) @data = {:default => {:userid => "root", :password => @password}} @options = {:save => false} end + context "#verify_credentials_task" do + it "verifies the credentials" do + @host.update_authentication(@data, @options) + @host.save + @host.verify_credentials_task(FactoryBot.create(:user).userid) + + expect(MiqQueue.last).to have_attributes( + :method_name => "verify_credentials?", + :instance_id => @host.id, + :class_name => @host.class.name, + :role => "ems_operations", + :zone => @ems.zone.name, + :queue_name => @ems.queue_name_for_ems_operations, + ) + end + end + context "default credentials" do it "save" do @host.update_authentication(@data, @options)