Skip to content

Commit 8260822

Browse files
committed
Fix ifconfig parse logic failing on some tunnels
1 parent eca23ba commit 8260822

File tree

3 files changed

+57
-30
lines changed

3 files changed

+57
-30
lines changed

lib/facter/resolvers/networking.rb

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,35 @@ def extract_dhcp(interface_name, raw_data, parsed_interface_data)
8080
end
8181

8282
def extract_ip_data(raw_data, parsed_interface_data)
83-
ip = extract_values(raw_data, /inet (\S+)/)
84-
mask = extract_values(raw_data, /netmask (\S+)/).map { |val| val.hex.to_s(2).count('1') }
83+
inets = extract_values(raw_data, /inet (\S+).+netmask (\S+)/, :extract_ip4_data)
84+
bindings = create_bindings(inets)
85+
parsed_interface_data[:bindings] = bindings unless bindings.empty?
8586

86-
ip6 = extract_values(raw_data, /inet6 (\S+)/).map { |val| val.gsub(/%.+/, '') }
87-
mask6 = extract_values(raw_data, /prefixlen (\S+)/)
88-
89-
parsed_interface_data[:bindings] = create_bindings(ip, mask) unless ip.empty?
90-
parsed_interface_data[:bindings6] = create_bindings(ip6, mask6) unless ip6.empty?
87+
inets = extract_values(raw_data, /inet6 (\S+).+prefixlen (\S+)/, :extract_ip6_data)
88+
bindings = create_bindings(inets)
89+
parsed_interface_data[:bindings6] = bindings unless bindings.empty?
9190
end
9291

93-
def extract_values(data, regex)
92+
def extract_values(data, regex, ip_func)
9493
results = []
95-
data.scan(regex).flatten.each do |val|
96-
results << val
94+
data.scan(regex).flatten.each_slice(2) do |val|
95+
results << method(ip_func).call(val)
9796
end
9897
results
9998
end
10099

101-
def create_bindings(ips, masks)
100+
def extract_ip4_data(inet)
101+
[inet[0], inet[1].hex.to_s(2).count('1')]
102+
end
103+
104+
def extract_ip6_data(inet)
105+
[inet[0].gsub(/%.+/, ''), inet[1]]
106+
end
107+
108+
def create_bindings(inets)
102109
bindings = []
103-
ips.zip(masks).each do |ip, mask|
104-
bindings << Facter::Util::Resolvers::Networking.build_binding(ip, mask)
110+
inets.each do |inet|
111+
bindings << Facter::Util::Resolvers::Networking.build_binding(inet[0], inet[1])
105112
end
106113
bindings
107114
end

spec/facter/resolvers/networking_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
describe Facter::Resolvers::Networking do
44
subject(:networking) { Facter::Resolvers::Networking }
55

6+
let(:log_spy) { instance_spy(Facter::Log) }
7+
68
describe '#resolve' do
79
before do
810
allow(Facter::Util::Resolvers::Networking::PrimaryInterface)
@@ -37,7 +39,7 @@
3739
end
3840

3941
it 'detects all interfaces' do
40-
expected = %w[lo0 gif0 stf0 en0 en0.1 en1 en2 bridge0 p2p0 awdl0 llw0 utun0 utun1 utun2 utun3 ib0 ib1]
42+
expected = %w[lo0 gif0 stf0 en0 en0.1 en1 en2 bridge0 p2p0 awdl0 llw0 utun0 utun1 utun2 utun3 utun4 utun5 ib0 ib1]
4143
expect(networking.resolve(:interfaces).keys).to match_array(expected)
4244
end
4345

@@ -135,6 +137,19 @@
135137
expect(networking.resolve(:interfaces)['utun3']).to include(expected)
136138
end
137139

140+
it 'checks interface utun4' do
141+
expected = { bindings: [{ address: '192.0.2.100', netmask: '255.255.255.255', network: '192.0.2.100' }] }
142+
expect(networking.resolve(:interfaces)['utun4']).to include(expected)
143+
end
144+
145+
it 'checks interface utun5' do
146+
expected = { bindings6: [
147+
{ address: '2001:db8::1', netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
148+
network: '2001:db8::1', scope6: 'global' }
149+
] }
150+
expect(networking.resolve(:interfaces)['utun5']).to include(expected)
151+
end
152+
138153
it 'checks interface ib0 has the expected mac' do
139154
expected = { mac: '80:00:02:08:FA:81:00:00:00:00:00:00:00:00:00:00:00:00:00:00' }
140155
expect(networking.resolve(:interfaces)['ib0']).to include(expected)

spec/fixtures/ifconfig_mac

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
22
options=1203<RXCSUM,TXCSUM,TXSTATUS,SW_TIMESTAMP>
3-
inet 127.0.0.1 netmask 0xff000000
4-
inet6 ::1 prefixlen 128
5-
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
3+
inet 127.0.0.1 netmask 0xff000000
4+
inet6 ::1 prefixlen 128
5+
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
66
nd6 options=201<PERFORMNUD,DAD>
77
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
88
stf0: flags=0<> mtu 1280
99
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
1010
options=400<CHANNEL_IO>
11-
ether 64:5a:ed:ea:5c:81
11+
ether 64:5a:ed:ea:5c:81
1212
inet 192.168.143.212 netmask 0xffffff00 broadcast 192.168.143.255
1313
media: autoselect
1414
status: active
@@ -23,17 +23,17 @@ en0.1: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
2323
nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
2424
en1: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
2525
options=460<TSO4,TSO6,CHANNEL_IO>
26-
ether 82:17:0e:93:9d:00
26+
ether 82:17:0e:93:9d:00
2727
media: autoselect <full-duplex>
2828
status: inactive
2929
en2: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
3030
options=460<TSO4,TSO6,CHANNEL_IO>
31-
ether 82:17:0e:93:9d:01
31+
ether 82:17:0e:93:9d:01
3232
media: autoselect <full-duplex>
3333
status: inactive
3434
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
3535
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
36-
ether 82:17:0e:93:9d:00
36+
ether 82:17:0e:93:9d:00
3737
Configuration:
3838
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
3939
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
@@ -47,33 +47,39 @@ bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
4747
status: inactive
4848
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
4949
options=400<CHANNEL_IO>
50-
ether 06:5a:ed:ea:5c:81
50+
ether 06:5a:ed:ea:5c:81
5151
media: autoselect
5252
status: inactive
5353
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
5454
options=400<CHANNEL_IO>
55-
ether 2e:ba:e4:83:4b:b7
56-
inet6 fe80::2cba:e4ff:fe83:4bb7%awdl0 prefixlen 64 scopeid 0x9
55+
ether 2e:ba:e4:83:4b:b7
56+
inet6 fe80::2cba:e4ff:fe83:4bb7%awdl0 prefixlen 64 scopeid 0x9
5757
nd6 options=201<PERFORMNUD,DAD>
5858
media: autoselect
5959
status: active
6060
llw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
6161
options=400<CHANNEL_IO>
62-
ether 2e:ba:e4:83:4b:b7
63-
inet6 fe80::2cba:e4ff:fe83:4bb7%llw0 prefixlen 64 scopeid 0xa
62+
ether 2e:ba:e4:83:4b:b7
63+
inet6 fe80::2cba:e4ff:fe83:4bb7%llw0 prefixlen 64 scopeid 0xa
6464
nd6 options=201<PERFORMNUD,DAD>
6565
media: aumm,l.j hn toselect
6666
status: active
6767
utun0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
68-
inet6 fe80::ba29:a797:6aa:9eb%utun0 prefixlen 64 scopeid 0xb
68+
inet6 fe80::ba29:a797:6aa:9eb%utun0 prefixlen 64 scopeid 0xb
6969
nd6 options=201<PERFORMNUD,DAD>
7070
utun1: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 2000
71-
inet6 fe80::64c6:1885:bec0:c316%utun1 prefixlen 64 scopeid 0xc
71+
inet6 fe80::64c6:1885:bec0:c316%utun1 prefixlen 64 scopeid 0xc
7272
nd6 options=201<PERFORMNUD,DAD>
7373
utun2: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
74-
inet 10.16.132.213 --> 10.16.132.213 netmask 0xfffffe00
74+
inet 10.16.132.213 --> 10.16.132.213 netmask 0xfffffe00
7575
utun3: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1500
7676
inet6 2001:db8:cafe::132:213 --> 2001:db8:cafe::132:213 prefixlen 128
77+
utun4: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
78+
tunnel inet 192.0.2.1 --> 192.0.2.2
79+
inet 192.0.2.100 --> 192.0.2.101 netmask 0xffffffff
80+
utun5: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1380
81+
tunnel inet 192.0.2.1 --> 192.0.2.2
82+
inet6 2001:db8::1 --> 2001:db8::2 prefixlen 128
7783
ib0: flags=4099<UP,BROADCAST,MULTICAST> mtu 4092
7884
infiniband 80:00:02:08:FA:81:00:00:00:00:00:00:00:00:00:00:00:00:00:00 txqueuelen 256 (InfiniBand)
7985
RX packets 0 bytes 0 (0.0 B)
@@ -87,4 +93,3 @@ ib1: flags=4099<UP,BROADCAST,MULTICAST> mtu 4092
8793
RX errors 0 dropped 0 overruns 0 frame 0
8894
TX packets 0 bytes 0 (0.0 B)
8995
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
90-

0 commit comments

Comments
 (0)