From 7b2f1fdbbf1ec94d84592aa13ef305126d878095 Mon Sep 17 00:00:00 2001 From: Shim Shtein Date: Sun, 1 Dec 2024 06:50:03 -0500 Subject: [PATCH] Fixes #38050 - Add ip6 to search conditions for host_finder --- .../unattended_installation/host_finder.rb | 10 ++++++++-- test/controllers/unattended_controller_test.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/services/foreman/unattended_installation/host_finder.rb b/app/services/foreman/unattended_installation/host_finder.rb index 718bd13716b..03f2f006972 100644 --- a/app/services/foreman/unattended_installation/host_finder.rb +++ b/app/services/foreman/unattended_installation/host_finder.rb @@ -54,8 +54,14 @@ def find_host_by_ip_or_mac mac_list = query_params[:mac_list] - query = mac_list.empty? ? { :nics => { :ip => ip } } : ["lower(nics.mac) IN (?)", mac_list] - hosts = Host.joins(:provision_interface).where(query).order(:created_at) + # create base query + hosts = Host.joins(:provision_interface).order(:created_at) + # filter the records either by mac or by the IP address + if mac_list.empty? + hosts = hosts.where(:nics => { :ip => ip }).or(hosts.where(:nics => { :ip6 => ip })) + else + hosts = hosts.where("lower(nics.mac) IN (?)", mac_list) + end Rails.logger.warn("Multiple hosts found with #{ip} or #{mac_list}, picking up the most recent") if hosts.count > 1 diff --git a/test/controllers/unattended_controller_test.rb b/test/controllers/unattended_controller_test.rb index ec04f7cdde1..f2a9e5f8f8e 100644 --- a/test/controllers/unattended_controller_test.rb +++ b/test/controllers/unattended_controller_test.rb @@ -75,6 +75,23 @@ class UnattendedControllerTest < ActionController::TestCase assert_response :success end + test "should get a kickstart when pure IPv6 address is used" do + ptable = Ptable.find_by(name: 'default') + @ipv6_host = FactoryBot.create(:host, :managed, :with_ipv6, :build => true, + :operatingsystem => operatingsystems(:redhat), + :ptable => ptable, + :medium => media(:one), + :architecture => architectures(:x86_64), + :organization => @org, + :location => @loc + ) + @request.env["HTTP_X_FORWARDED_FOR"] = @ipv6_host.ip6 + @request.env["REMOTE_ADDR"] = "::1" + get :host_template, params: { :kind => 'provision' } + assert_response :success + end + + test "should set @static when requested" do Setting[:safemode_render] = false @request.env["HTTP_X_RHN_PROVISIONING_MAC_0"] = "eth0 #{@rh_host.mac}"