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

Openstack infra ssh keypair validation fixes #13445

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
7 changes: 5 additions & 2 deletions app/models/manageiq/providers/openstack/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,12 @@ def required_credential_fields(type)
end

def verify_ssh_keypair_credentials(_options)
hosts.sort_by(&:ems_cluster_id)
# Select one powered-on host in each cluster to verify
# ssh credentials against
hosts.select(&:ems_cluster_id)
.sort_by(&:ems_cluster_id)
.slice_when { |i, j| i.ems_cluster_id != j.ems_cluster_id }
.map { |c| c.find { |h| h.power_state == 'on' } }
.map { |c| c.find { |h| h.power_state == 'on' } }.compact
.all? { |h| h.verify_credentials('ssh_keypair') }
end
private :verify_ssh_keypair_credentials
Expand Down
1 change: 1 addition & 0 deletions spec/factories/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
vmm_vendor "unknown"
ems_ref "openstack-perf-host"
ems_ref_obj "openstack-perf-host-nova-instance"
association :ems_cluster, factory: :ems_cluster_openstack
end

factory :host_openstack_infra_compute, :parent => :host_openstack_infra,
Expand Down
30 changes: 30 additions & 0 deletions spec/models/manageiq/providers/openstack/infra_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@
end
end

context "verifying SSH keypair credentials" do
it "verifies Openstack SSH credentials successfully when all hosts report that the credentials are valid" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(true)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end

it "fails to verify Openstack SSH credentials when any hosts report that the credentials are invalid" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
host = FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_falsey
end

it "disregards powered off hosts when verifying Openstack SSH credentials" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "off")
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end

it "disregards host with no ems_cluster" do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
FactoryGirl.create(:host_openstack_infra, :ext_management_system => @ems, :state => "on", :ems_cluster => nil)
allow_any_instance_of(ManageIQ::Providers::Openstack::InfraManager::Host).to receive(:verify_credentials).and_return(false)
expect(@ems.send(:verify_ssh_keypair_credentials, nil)).to be_truthy
end
end

context "validation" do
before :each do
@ems = FactoryGirl.create(:ems_openstack_infra_with_authentication)
Expand Down