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

Fix MiqEnvironment.local_ip_address to not prefer loopback #20992

Merged
merged 4 commits into from
Feb 1, 2021

Conversation

agrare
Copy link
Member

@agrare agrare commented Jan 28, 2021

#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

@djberg96
Copy link
Contributor

Sorry, guess I goofed it before.

👍🏻

@agrare
Copy link
Member Author

agrare commented Jan 28, 2021

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 !127.0 which would actually be !ipv4_loopback? since the common 192.168, 172.16, and 10. addresses are "private"

@agrare
Copy link
Member Author

agrare commented Jan 28, 2021

Not sure if we want to use hostname -i for the spec test on this one, will investigate alternatives

@agrare agrare force-pushed the fix_miq_environment_local_ip_address branch from 28f85f3 to e746ef8 Compare January 28, 2021 18:37
@agrare agrare added the core label Jan 28, 2021
`#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.
@agrare agrare force-pushed the fix_miq_environment_local_ip_address branch from e746ef8 to 88593d3 Compare January 28, 2021 19:21
# 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
Copy link
Member

@Fryguy Fryguy Jan 28, 2021

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

Copy link
Member Author

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

Copy link
Member Author

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 }
Copy link
Member Author

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 }
Copy link
Member Author

@agrare agrare Jan 29, 2021

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' }
Copy link
Member Author

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

@miq-bot
Copy link
Member

miq-bot commented Jan 29, 2021

Checked commits agrare/manageiq@88593d3~...16f8263 with ruby 2.6.3, rubocop 0.82.0, haml-lint 0.35.0, and yamllint
3 files checked, 0 offenses detected
Everything looks fine. 🍰

@Fryguy Fryguy merged commit fd276ba into ManageIQ:master Feb 1, 2021
@agrare agrare deleted the fix_miq_environment_local_ip_address branch February 1, 2021 22:07
@Fryguy
Copy link
Member

Fryguy commented Nov 10, 2021

Backported to kasparov in commit 9855433.

commit 98554337d27d58d21a383f4fdd0fe2f666478f7e
Author: Jason Frey <fryguy9@gmail.com>
Date:   Mon Feb 1 17:03:39 2021 -0500

    Merge pull request #20992 from agrare/fix_miq_environment_local_ip_address
    
    Fix MiqEnvironment.local_ip_address to not prefer loopback
    
    (cherry picked from commit fd276bac67283bf050e68ecc40cf95b31c5dd16b)

Fryguy added a commit that referenced this pull request Nov 10, 2021
…dress

Fix MiqEnvironment.local_ip_address to not prefer loopback

(cherry picked from commit fd276ba)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants