[Pkg-puppet-devel] [facter] 09/18: (FACT-380) Use the interface of the default route for network facts
Stig Sandbeck Mathisen
ssm at debian.org
Wed Jan 27 21:12:52 UTC 2016
This is an automated email from the git hooks/post-receive script.
ssm pushed a commit to branch master
in repository facter.
commit ec73549be0c26aab40a8968330726b369ab080f7
Author: Branan Riley <branan at puppetlabs.com>
Date: Thu Jan 7 12:15:22 2016 -0800
(FACT-380) Use the interface of the default route for network facts
Previously, Facter would use whichever non-loopback device came first
in the ifconfig output for the networking facts. This is not always
what most people would consider the "ipaddress of a node", since
things like virtual network devices can show up in this list and might
sort before physical interfaces.
This changes the code to check the routing table for the default
interface, and uses the values from that interface.
---
lib/facter/ipaddress.rb | 19 +++++------------
lib/facter/ipaddress6.rb | 5 +++--
lib/facter/macaddress.rb | 11 +++-------
lib/facter/netmask.rb | 10 ++++++++-
lib/facter/util/ip.rb | 17 +++++++++++++++
lib/facter/util/netmask.rb | 6 ------
.../net_route_all_with_multiple_interfaces | 2 ++
.../fixtures/ifconfig/net_route_net_tools_1.60.txt | 3 +++
.../ipaddress/net_route_multiple_127_addresses.txt | 3 +++
.../unit/ipaddress/net_route_net_tools_1.60.txt | 3 +++
.../ipaddress/net_route_non_english_locale.txt | 3 +++
.../unit/ipaddress/net_route_ubuntu_1204.txt | 3 +++
...arwin_10_8_5.txt => ifconfig_darwin_10_8_5.txt} | 0
.../netmask/net_route_multiple_127_addresses.txt | 3 +++
.../unit/netmask/net_route_net_tools_1.60.txt | 3 +++
.../unit/netmask/net_route_non_english_locale.txt | 3 +++
.../unit/netmask/net_route_ubuntu_1204.txt | 3 +++
spec/unit/ipaddress6_spec.rb | 20 ++++++++++++++----
spec/unit/ipaddress_spec.rb | 24 +++++++++++-----------
spec/unit/macaddress_spec.rb | 7 ++++++-
spec/unit/netmask_spec.rb | 21 +++++++++++++++----
21 files changed, 117 insertions(+), 52 deletions(-)
diff --git a/lib/facter/ipaddress.rb b/lib/facter/ipaddress.rb
index 6179a4d..96b10db 100644
--- a/lib/facter/ipaddress.rb
+++ b/lib/facter/ipaddress.rb
@@ -3,7 +3,9 @@
# Purpose: Return the main IP address for a host.
#
# Resolution:
-# On the Unixes does an ifconfig, and returns the first non 127.0.0.0/8
+# On Linux and AIX, it examines the routing table and uses the IP address
+# of the default interface.
+# On other Unixes does an ifconfig, and returns the first non 127.0.0.0/8
# subnetted IP it finds.
# On Windows, it attempts to use the socket library and resolve the machine's
# hostname via DNS.
@@ -27,19 +29,8 @@ require 'facter/util/ip'
Facter.add(:ipaddress) do
confine :kernel => :linux
setcode do
- ip = nil
- output = Facter::Util::IP.exec_ifconfig(["2>/dev/null"])
- if output
- regexp = /inet (?:addr:)?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
- output.split("\n").each do |line|
- match = regexp.match(line)
- if match and not /^127\./.match(match[1])
- ip = match[1]
- break
- end
- end
- end
- ip
+ iface = Facter::Util::IP.linux_default_iface
+ Facter.value("ipaddress_#{iface}")
end
end
diff --git a/lib/facter/ipaddress6.rb b/lib/facter/ipaddress6.rb
index 128f3cd..68a2771 100644
--- a/lib/facter/ipaddress6.rb
+++ b/lib/facter/ipaddress6.rb
@@ -37,11 +37,12 @@ def get_address_after_token(output, token, return_first=false)
ip
end
+
Facter.add(:ipaddress6) do
confine :kernel => :linux
setcode do
- output = Facter::Util::IP.exec_ifconfig(["2>/dev/null"])
- get_address_after_token(output, 'inet6(?: addr:)?')
+ iface = Facter::Util::IP.linux_default_iface
+ Facter.value("ipaddress6_#{iface}")
end
end
diff --git a/lib/facter/macaddress.rb b/lib/facter/macaddress.rb
index c2cbe65..a950435 100644
--- a/lib/facter/macaddress.rb
+++ b/lib/facter/macaddress.rb
@@ -12,15 +12,10 @@ require 'facter/util/macaddress'
require 'facter/util/ip'
Facter.add(:macaddress) do
- confine :kernel => 'Linux'
+ confine :kernel => :linux
setcode do
- ether = []
- output = Facter::Util::IP.exec_ifconfig(["-a","2>/dev/null"])
-
- String(output).each_line do |s|
- ether.push($1) if s =~ /(?:ether|HWaddr) ((\w{1,2}:){5,}\w{1,2})/
- end
- Facter::Util::Macaddress.standardize(ether[0])
+ iface = Facter::Util::IP.linux_default_iface
+ Facter.value("macaddress_#{iface}")
end
end
diff --git a/lib/facter/netmask.rb b/lib/facter/netmask.rb
index 7156714..185d23e 100644
--- a/lib/facter/netmask.rb
+++ b/lib/facter/netmask.rb
@@ -16,8 +16,16 @@
#
require 'facter/util/netmask'
+Facter.add(:netmask) do
+ confine :kernel => :linux
+ setcode do
+ iface = Facter::Util::IP.linux_default_iface
+ Facter.value("netmask_#{iface}")
+ end
+end
+
Facter.add("netmask") do
- confine :kernel => [ :sunos, :linux, :freebsd, :openbsd, :netbsd, :darwin, :"gnu/kfreebsd", :dragonfly, :AIX ]
+ confine :kernel => [ :sunos, :freebsd, :openbsd, :netbsd, :darwin, :"gnu/kfreebsd", :dragonfly, :AIX ]
setcode do
Facter::NetMask.get_netmask
end
diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb
index 62df8a4..887c8d2 100644
--- a/lib/facter/util/ip.rb
+++ b/lib/facter/util/ip.rb
@@ -337,4 +337,21 @@ module Facter::Util::IP
network = ip.mask(subnet.to_s).to_s
end
end
+
+ def self.read_proc_net_route
+ File.read("/proc/net/route")
+ end
+
+ def self.linux_default_iface
+ routes = read_proc_net_route
+ iface = nil
+ routes.split("\n").each do |line|
+ parts = line.split
+ if parts[1] == "00000000"
+ iface = alphafy(parts[0])
+ break
+ end
+ end
+ iface
+ end
end
diff --git a/lib/facter/util/netmask.rb b/lib/facter/util/netmask.rb
index 4c4cf01..e06ca52 100644
--- a/lib/facter/util/netmask.rb
+++ b/lib/facter/util/netmask.rb
@@ -5,12 +5,6 @@ module Facter::NetMask
ops = nil
case Facter.value(:kernel)
- when 'Linux'
- ops = {
- :ifconfig_opts => ['2>/dev/null'],
- :regex => %r{#{Facter.value(:ipaddress)}.*?(?:Mask:|netmask)\s*(#{ipregex})}x,
- :munge => nil,
- }
when 'SunOS'
ops = {
:ifconfig_opts => ['-a'],
diff --git a/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces b/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces
new file mode 100644
index 0000000..bd427d2
--- /dev/null
+++ b/spec/fixtures/ifconfig/net_route_all_with_multiple_interfaces
@@ -0,0 +1,2 @@
+eth0 00000000 01D1FC83 0003 0 0 100 00000000 0 0 0
+eth0 01D1FC83 00000000 0001 0 0 100 00FFFFFF 0 0 0
diff --git a/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt b/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt
new file mode 100644
index 0000000..15ae788
--- /dev/null
+++ b/spec/fixtures/ifconfig/net_route_net_tools_1.60.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
+em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt b/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt
new file mode 100644
index 0000000..ed3edb3
--- /dev/null
+++ b/spec/fixtures/unit/ipaddress/net_route_multiple_127_addresses.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+venet0:1 00000000 1400DE0A 0003 0 0 100 00000000 0 0 0
+venet0:1 1400DE0A 1400DE0A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt b/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt
new file mode 100644
index 0000000..15ae788
--- /dev/null
+++ b/spec/fixtures/unit/ipaddress/net_route_net_tools_1.60.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
+em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt b/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt
new file mode 100644
index 0000000..0a6508a
--- /dev/null
+++ b/spec/fixtures/unit/ipaddress/net_route_non_english_locale.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+wlan0 00000000 5301A8C0 0003 0 0 100 00000000 0 0 0
+wlan0 5301A8C0 5301A8C0 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt b/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt
new file mode 100644
index 0000000..007e3d4
--- /dev/null
+++ b/spec/fixtures/unit/ipaddress/net_route_ubuntu_1204.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+eth0 00000000 6E50570A 0003 0 0 100 00000000 0 0 0
+eth0 6E50570A 6E50570A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/darwin_10_8_5.txt b/spec/fixtures/unit/netmask/ifconfig_darwin_10_8_5.txt
similarity index 100%
rename from spec/fixtures/unit/netmask/darwin_10_8_5.txt
rename to spec/fixtures/unit/netmask/ifconfig_darwin_10_8_5.txt
diff --git a/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt b/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt
new file mode 100644
index 0000000..ed3edb3
--- /dev/null
+++ b/spec/fixtures/unit/netmask/net_route_multiple_127_addresses.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+venet0:1 00000000 1400DE0A 0003 0 0 100 00000000 0 0 0
+venet0:1 1400DE0A 1400DE0A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt b/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt
new file mode 100644
index 0000000..15ae788
--- /dev/null
+++ b/spec/fixtures/unit/netmask/net_route_net_tools_1.60.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+em1 00000000 99D1FC83 0003 0 0 100 00000000 0 0 0
+em1 99D1FC83 99D1FC83 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_non_english_locale.txt b/spec/fixtures/unit/netmask/net_route_non_english_locale.txt
new file mode 100644
index 0000000..0a6508a
--- /dev/null
+++ b/spec/fixtures/unit/netmask/net_route_non_english_locale.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+wlan0 00000000 5301A8C0 0003 0 0 100 00000000 0 0 0
+wlan0 5301A8C0 5301A8C0 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt b/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt
new file mode 100644
index 0000000..007e3d4
--- /dev/null
+++ b/spec/fixtures/unit/netmask/net_route_ubuntu_1204.txt
@@ -0,0 +1,3 @@
+Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
+eth0 00000000 6E50570A 0003 0 0 100 00000000 0 0 0
+eth0 6E50570A 6E50570A 0007 0 0 100 FFFFFFFF 0 0 0
diff --git a/spec/unit/ipaddress6_spec.rb b/spec/unit/ipaddress6_spec.rb
index 96650fe..b95d4f8 100755
--- a/spec/unit/ipaddress6_spec.rb
+++ b/spec/unit/ipaddress6_spec.rb
@@ -26,8 +26,11 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 information for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
+ Facter::Util::IP.stubs(:exec_ifconfig).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces'))
+ routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
end
@@ -35,8 +38,11 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 information for Linux with recent net-tools" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
+ Facter::Util::IP.stubs(:exec_ifconfig).
returns(ifconfig_fixture('ifconfig_net_tools_1.60.txt'))
+ routes = ifconfig_fixture('net_route_net_tools_1.60.txt')
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:febe:2201"
end
@@ -44,8 +50,11 @@ describe "The IPv6 address fact" do
it "should return ipaddress6 with fe80 in any other octet than the first for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
+ Facter::Util::IP.stubs(:exec_ifconfig).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces_and_fe80'))
+ routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should == "2610:10:20:209:212:3fff:fe80:2201"
end
@@ -53,8 +62,11 @@ describe "The IPv6 address fact" do
it "should not return ipaddress6 link-local address for Linux" do
Facter::Core::Execution.stubs(:exec).with('uname -s').returns('Linux')
Facter::Util::IP.stubs(:get_ifconfig).returns("/sbin/ifconfig")
- Facter::Util::IP.stubs(:exec_ifconfig).with(["2>/dev/null"]).
+ Facter::Util::IP.stubs(:exec_ifconfig).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces_and_no_public_ipv6'))
+ routes = ifconfig_fixture('net_route_all_with_multiple_interfaces')
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
Facter.value(:ipaddress6).should be_false
end
diff --git a/spec/unit/ipaddress_spec.rb b/spec/unit/ipaddress_spec.rb
index 78b0c96..a1387f6 100755
--- a/spec/unit/ipaddress_spec.rb
+++ b/spec/unit/ipaddress_spec.rb
@@ -4,10 +4,6 @@ require 'spec_helper'
require 'facter/util/ip'
describe "ipaddress fact" do
- before do
- Facter.collection.internal_loader.load(:ipaddress)
- end
-
context 'using `ifconfig`' do
before :each do
Facter.fact(:hostname).stubs(:value)
@@ -18,25 +14,29 @@ describe "ipaddress fact" do
Facter.fact(:kernel).stubs(:value).returns("Linux")
end
+ after :each do
+ Facter.collection.flush
+ end
+
def expect_ifconfig_parse(address, fixture)
- Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read(fixture))
+ Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read("ifconfig_#{fixture}"))
+ routes = my_fixture_read("net_route_#{fixture}")
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
+ Facter.collection.internal_loader.load(:ipaddress)
Facter.fact(:ipaddress).value.should == address
end
it "parses correctly on Ubuntu 12.04" do
- expect_ifconfig_parse "10.87.80.110", "ifconfig_ubuntu_1204.txt"
+ expect_ifconfig_parse "10.87.80.110", "ubuntu_1204.txt"
end
it "parses correctly on Fedora 17" do
- expect_ifconfig_parse "131.252.209.153", "ifconfig_net_tools_1.60.txt"
- end
-
- it "parses a real address over multiple loopback addresses" do
- expect_ifconfig_parse "10.0.222.20", "ifconfig_multiple_127_addresses.txt"
+ expect_ifconfig_parse "131.252.209.153", "net_tools_1.60.txt"
end
it "parses nothing with a non-english locale" do
- expect_ifconfig_parse nil, "ifconfig_non_english_locale.txt"
+ expect_ifconfig_parse nil, "non_english_locale.txt"
end
end
end
diff --git a/spec/unit/macaddress_spec.rb b/spec/unit/macaddress_spec.rb
index ffc4e06..b3ac6a2 100755
--- a/spec/unit/macaddress_spec.rb
+++ b/spec/unit/macaddress_spec.rb
@@ -23,8 +23,11 @@ describe "macaddress fact" do
end
it "should return the macaddress of the first interface" do
- Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
+ Facter::Util::IP.stubs(:exec_ifconfig).
returns(ifconfig_fixture('linux_ifconfig_all_with_multiple_interfaces'))
+ routes = ifconfig_fixture("net_route_all_with_multiple_interfaces")
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ Facter.collection.internal_loader.load(:interfaces)
Facter.value(:macaddress).should == "00:12:3f:be:22:01"
end
@@ -32,6 +35,7 @@ describe "macaddress fact" do
it "should return nil when no macaddress can be found" do
Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_no_mac'))
+ Facter::Util::IP.stubs(:read_proc_net_route).returns("")
proc { Facter.value(:macaddress) }.should_not raise_error
Facter.value(:macaddress).should be_nil
@@ -41,6 +45,7 @@ describe "macaddress fact" do
it "should return nil when no interface has a real macaddress" do
Facter::Util::IP.stubs(:exec_ifconfig).with(["-a","2>/dev/null"]).
returns(ifconfig_fixture('linux_ifconfig_venet'))
+ Facter::Util::IP.stubs(:read_proc_net_route).returns("")
proc { Facter.value(:macaddress) }.should_not raise_error
Facter.value(:macaddress).should be_nil
diff --git a/spec/unit/netmask_spec.rb b/spec/unit/netmask_spec.rb
index 226fa11..ead885c 100755
--- a/spec/unit/netmask_spec.rb
+++ b/spec/unit/netmask_spec.rb
@@ -6,8 +6,21 @@ require 'facter/util/ip'
shared_examples_for "netmask from ifconfig output" do |platform, address, fixture|
it "correctly on #{platform}" do
- Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read(fixture))
+ Facter::Util::IP.stubs(:exec_ifconfig).returns(my_fixture_read("ifconfig_#{fixture}"))
Facter.collection.internal_loader.load(:netmask)
+ begin
+ routes = my_fixture_read("net_route_#{fixture}")
+ Facter::Util::IP.stubs(:read_proc_net_route).returns(routes)
+ rescue RuntimeError
+ # We want to try to load proc/net/route fixtures, but skip if
+ # they don't exist for non-linux platforms. Ideally we'd get an
+ # IOError here, but the fixture machinery here is dumb and
+ # converts this to a RuntimeError. Hopefully anything that would
+ # error here would also cause the actual test to fail, so I'm
+ # not going to worry too hard.
+ end
+ Facter.collection.internal_loader.load(:interfaces)
+ Facter.collection.internal_loader.load(:ipaddress)
Facter.fact(:netmask).value.should eq(address)
end
@@ -21,10 +34,10 @@ describe "The netmask fact" do
example_behavior_for "netmask from ifconfig output",
"Archlinux (net-tools 1.60)", "255.255.255.0",
- "ifconfig_net_tools_1.60.txt"
+ "net_tools_1.60.txt"
example_behavior_for "netmask from ifconfig output",
"Ubuntu 12.04", "255.255.255.255",
- "ifconfig_ubuntu_1204.txt"
+ "ubuntu_1204.txt"
end
context "on AIX" do
@@ -34,7 +47,7 @@ describe "The netmask fact" do
example_behavior_for "netmask from ifconfig output",
"AIX 7", "255.255.255.0",
- "ifconfig_aix_7.txt"
+ "aix_7.txt"
end
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-puppet/facter.git
More information about the Pkg-puppet-devel
mailing list