-
Notifications
You must be signed in to change notification settings - Fork 898
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
Fix MiqEnvironment.local_ip_address to not prefer loopback #20992
Fix MiqEnvironment.local_ip_address to not prefer loopback #20992
Conversation
Sorry, guess I goofed it before. 👍🏻 |
I'm not sure but I think there might have been a misunderstanding of what Addrinfo considers a "private" address. It looks like it was intended to say |
Not sure if we want to use |
28f85f3
to
e746ef8
Compare
`#local_ip_address` currently prefers loopback addresses since it filters out "private" addresses and not "loopback" addresses. IMO we should prefer public, then private, then loopback.
e746ef8
to
88593d3
Compare
# prefer private addresses, then take whatever we can get | ||
local_addr = ipv4_addrs.detect { |ip| !ip.ipv4_loopback? && !ip.ipv4_private? } | ||
local_addr ||= ipv4_addrs.detect { |ip| !ip.ipv4_loopback? } | ||
local_addr ||= ipv4_addrs.first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For funsies, I think a sort would work too?
local_addr = ipv4_addrs.sort_by { |ip| ip.ipv4_loopback? ? 2 : (ip.ipv4_private? ? 1 : 0) }.first
Only thing I'm not sure about is if there are multiple private ips, I'm not sure if this is deterministic
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah good point about the return order from Socket
not being guaranteed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright now we're sorting by the ip_address and added a spec test with multiple private_ips returned by Socket.ip_address_list
in different orders.
@@ -2,14 +2,14 @@ | |||
|
|||
RSpec.describe "Server Environment Management" do | |||
let(:mac_address) { 'a:1:b:2:c:3:d:4' } | |||
let(:hostname) { Socket.gethostname } | |||
let(:loopback) { '127.0.0.1' } | |||
let(:hostname) { Socket.gethostbyname(Socket.gethostname).first } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not related to the local_ip_address
method but the hostname was also failing for me because this was looking for Socket.gethostname
which returns in my case desktop
but Socket.gethostbyname(Socket.gethostname).first
returns the fully qualified name which in my case is something like desktop.a.b.example.com
so even when the IP matched:
1) Server Environment Management .get_network_information when in non-production mode
Failure/Error: expect(MiqServer.get_network_information).to eq([ip_address, hostname, mac_address])
expected: ["10.2.4.101", "desktop", "a:1:b:2:c:3:d:4"]
got: ["10.2.4.101", "desktop.a.b.example", "a:1:b:2:c:3:d:4"]
let(:hostname) { Socket.gethostname } | ||
let(:loopback) { '127.0.0.1' } | ||
let(:hostname) { Socket.gethostbyname(Socket.gethostname).first } | ||
let(:ip_address) { Socket.ip_address_list.select(&:ipv4?).sort_by(&:ip_address).detect(&:ipv4_private?)&.ip_address } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking it might be better to mock this like we mock the MAC Address so we're not dealing with weird travis vs local env issues
@@ -1,15 +1,21 @@ | |||
require 'socket' | |||
|
|||
RSpec.describe "Server Environment Management" do | |||
let(:mac_address) { 'a:1:b:2:c:3:d:4' } | |||
let(:hostname) { Socket.gethostname } | |||
let(:loopback) { '127.0.0.1' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These were only used in this context so I just moved them down
Checked commits agrare/manageiq@88593d3~...16f8263 with ruby 2.6.3, rubocop 0.82.0, haml-lint 0.35.0, and yamllint |
Backported to
|
…dress Fix MiqEnvironment.local_ip_address to not prefer loopback (cherry picked from commit fd276ba)
#local_ip_address
currently prefers loopback addresses since it filters out "private" addresses and not "loopback" addresses.IMO we should prefer public, then private, then loopback.
https://github.com/ManageIQ/manageiq/pull/20341/files#r566198984
#20812 (comment)
#20986