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

Fixes #38051 - Fix built request to accept ipv6 hosts #10392

Merged
merged 1 commit into from
Jan 16, 2025
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
17 changes: 16 additions & 1 deletion app/controllers/unattended_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,28 @@ def set_content_type
# the form save)
def update_ip
ip = request.remote_ip
logger.debug "Built notice from #{ip}, current host ip is #{@host.ip}, updating" if @host.ip != ip
address = IPAddr.new(ip)

# @host has been changed even if the save fails, so we have to change it back
if address.ipv4?
update_ip4(ip)
elsif address.ipv6? && !address.link_local?
update_ip6(ip)
end
end

def update_ip4(ip)
logger.debug "Built notice from #{ip}, current host ip is #{@host.ip}, updating" if @host.ip != ip
old_ip = @host.ip
@host.ip = old_ip unless @host.update({'ip' => ip})
end

def update_ip6(ip)
logger.debug "Built notice from #{ip}, current host ip is #{@host.ip6}, updating" if @host.ip6 != ip
old_ip = @host.ip6
@host.ip6 = old_ip unless @host.update({'ip6' => ip})
end

def ipxe_request?
%w[iPXE gPXE].include?(params[:kind])
end
Expand Down
10 changes: 10 additions & 0 deletions test/controllers/unattended_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ class UnattendedControllerTest < ActionController::TestCase
refute nic.build
end

test "should accept built notifications over ipv6 and store the address" do
nic6 = '2001:db8::1234'
Setting[:update_ip_from_built_request] = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this revert the change after the test? We have a helper for SETTINGS:

foreman/test/test_helper.rb

Lines 204 to 214 in 9b8f759

def with_temporary_settings(**kwargs)
old_settings = SETTINGS.dup
begin
SETTINGS.update(kwargs)
yield
ensure
SETTINGS.replace(old_settings)
end
end
end

But I'm not sure we need to a similar thing for Setting.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not, but from looking at the code, we don't revert any of the settings. I suppose if the test relies on a specific setting, it should be explicitly set in the setup block.

@request.env["REMOTE_ADDR"] = nic6
post :built, params: { mac: @ub_host.primary_interface.mac }
assert_response :created
@ub_host.reload
assert_equal nic6, @ub_host.primary_interface.ip6
end

test "should accept failed notifications" do
@request.env["REMOTE_ADDR"] = @ub_host.ip
post :failed
Expand Down
Loading