Skip to content

Commit

Permalink
Fixes #26321 - Only allow hwuuid for VMware hypervisors
Browse files Browse the repository at this point in the history
  • Loading branch information
chris1984 committed May 1, 2024
1 parent aa0fc52 commit 752eac0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ function virt_who_update_listing_mode() {
var blacklist = $('#foreman_virt_who_configure_config_blacklist').parents('div.form-group');
var whitelist_parent = $('#foreman_virt_who_configure_config_filter_host_parents').parents('div.form-group');
var blacklist_parent = $('#foreman_virt_who_configure_config_exclude_host_parents').parents('div.form-group');
var hypervisor_ids = foreman_virt_who_configure_config_hypervisor_id;

// https://projects.theforeman.org/issues/26321
if (hypervisor_type != 'esx') {
$(hypervisor_ids).find('option[value=hwuuid]').remove();
} else if (hypervisor_type == 'esx' && $(hypervisor_ids).find('option[value=hwuuid]').length == 0) {
$(hypervisor_ids).append('<option value='+'hwuuid>'+'hwuuid'+'</option>');
}

// UNLIMITED = 0, WHITELIST = 1, BLACKLIST = 2, see config.rb model for the definition
if (filtering_mode == '0') {
Expand Down
8 changes: 8 additions & 0 deletions app/models/foreman_virt_who_configure/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Config < ApplicationRecord
validates :prism_flavor, :inclusion => {:in => PRISM_FLAVORS.keys, :message => "should be either central or element"}, :if => proc { |c| c.hypervisor_type == 'ahv' }
validate :validates_whitelist_blacklist
validate :validates_debug_settings, :if => proc { |c| c.hypervisor_type == 'ahv' }
validate :validates_hypervisor_id_non_vmware, :if => proc { |c| c.hypervisor_type != 'esx' }
validate :validates_hypervisor_options

def validates_whitelist_blacklist
Expand All @@ -131,6 +132,13 @@ def validates_whitelist_blacklist
end
end

def validates_hypervisor_id_non_vmware
return unless hypervisor_id.present?
if hypervisor_id == 'hwuuid'
errors.add(:hypervisor_id, "hwuuid is only supported for ESX hypervisor type")
end
end

def validates_debug_settings
if ahv_internal_debug && !debug
errors.add(:ahv_internal_debug, "Enable debugging output is required for Enable AHV debug")
Expand Down
21 changes: 21 additions & 0 deletions test/functional/api/v2/configs_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,27 @@ class ForemanVirtWhoConfigure::Api::V2::ConfigsControllerTest < ActionController
assert_equal "Kubeconfig path Invalid option for hypervisor [esx]", response['error']['full_messages'].join('')
end

test "should error when hypervisor type is hwuuid and type is not vmware" do
org = FactoryBot.create(:organization)
post :create, :params => { :foreman_virt_who_configure_config => { :name => 'my new config',
:interval => 240,
:filtering_mode => ForemanVirtWhoConfigure::Config::BLACKLIST,
:blacklist => ' a,b ',
:hypervisor_id => 'hwuuid',
:hypervisor_type => 'libvirt',
:hypervisor_server => "libvirt.example.com",
:hypervisor_username => "root",
:debug => true,
:satellite_url => "foreman.example.com",
:organization_id => org.id }
}, :session => set_session_user

assert_response :unprocessable_entity

response = ActiveSupport::JSON.decode(@response.body)
assert_equal "Hypervisor hwuuid is only supported for ESX hypervisor type", response['error']['full_messages'].join('')
end

test "should create the config" do
org = FactoryBot.create(:organization)
post :create, :params => { :foreman_virt_who_configure_config => { :name => 'my new config',
Expand Down

0 comments on commit 752eac0

Please sign in to comment.