Skip to content

Commit 75f745e

Browse files
committed
Merge pull request #80 from dcarley/10274-zero_prefixlen_addresses
(#10274) Nullify addresses with zero prefixlen
2 parents 809c036 + 2721826 commit 75f745e

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

lib/puppet/util/firewall.rb

+22-2
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,31 @@ def string_to_port(value)
6969
end
7070
end
7171

72+
# Takes an address and returns it in CIDR notation.
73+
#
74+
# If the address is:
75+
#
76+
# - A hostname:
77+
# It will be resolved
78+
# - An IPv4 address:
79+
# It will be qualified with a /32 CIDR notation
80+
# - An IPv6 address:
81+
# It will be qualified with a /128 CIDR notation
82+
# - An IP address with a CIDR notation:
83+
# It will be normalised
84+
# - An IP address with a dotted-quad netmask:
85+
# It will be converted to CIDR notation
86+
# - Any address with a resulting prefix length of zero:
87+
# It will return nil which is equivilent to not specifying an address
88+
#
7289
def host_to_ip(value)
7390
begin
74-
Puppet::Util::IPCidr.new(value).cidr
91+
value = Puppet::Util::IPCidr.new(value)
7592
rescue
76-
Puppet::Util::IPCidr.new(Resolv.getaddress(value)).cidr
93+
value = Puppet::Util::IPCidr.new(Resolv.getaddress(value))
7794
end
95+
96+
return nil if value.prefixlen == 0
97+
value.cidr
7898
end
7999
end

spec/fixtures/iptables/conversion_hash.rb

+18
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,24 @@
274274
:args => ["-t", :filter, "-p", :tcp, "-m", "comment", "--comment",
275275
"100 no action"],
276276
},
277+
'zero_prefixlen_ipv4' => {
278+
:params => {
279+
:name => '100 zero prefix length ipv4',
280+
:table => 'filter',
281+
:source => '0.0.0.0/0',
282+
:destination => '0.0.0.0/0',
283+
},
284+
:args => ['-t', :filter, '-p', :tcp, '-m', 'comment', '--comment', '100 zero prefix length ipv4'],
285+
},
286+
'zero_prefixlen_ipv6' => {
287+
:params => {
288+
:name => '100 zero prefix length ipv6',
289+
:table => 'filter',
290+
:source => '::/0',
291+
:destination => '::/0',
292+
},
293+
:args => ['-t', :filter, '-p', :tcp, '-m', 'comment', '--comment', '100 zero prefix length ipv6'],
294+
},
277295
'sport_range_1' => {
278296
:params => {
279297
:name => "100 sport range",

spec/unit/puppet/type/firewall_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
@resource[addr] = '127.0.0.1'
117117
@resource[addr].should == '127.0.0.1/32'
118118
end
119+
['0.0.0.0/0', '::/0'].each do |prefix|
120+
it "should be nil for zero prefix length address #{prefix}" do
121+
@resource[addr] = prefix
122+
@resource[addr].should == nil
123+
end
124+
end
119125
end
120126
end
121127

spec/unit/puppet/util/firewall_spec.rb

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
specify { subject.host_to_ip('96.126.112.51/32').should == '96.126.112.51/32' }
1919
specify { subject.host_to_ip('2001:db8:85a3:0:0:8a2e:370:7334').should == '2001:db8:85a3::8a2e:370:7334/128' }
2020
specify { subject.host_to_ip('2001:db8:1234::/48').should == '2001:db8:1234::/48' }
21+
specify { subject.host_to_ip('0.0.0.0/0').should == nil }
22+
specify { subject.host_to_ip('::/0').should == nil }
2123
end
2224

2325
describe '#icmp_name_to_number' do

spec/unit/puppet/util/ipcidr_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
specify { subject.netmask.should == '255.255.255.0' }
2626
end
2727

28+
describe 'ipv4 open range with cidr' do
29+
before { @ipcidr = Puppet::Util::IPCidr.new('0.0.0.0/0') }
30+
subject { @ipcidr }
31+
specify { subject.cidr.should == '0.0.0.0/0' }
32+
specify { subject.prefixlen.should == 0 }
33+
specify { subject.netmask.should == '0.0.0.0' }
34+
end
35+
2836
describe 'ipv6 address' do
2937
before { @ipaddr = Puppet::Util::IPCidr.new('2001:db8:85a3:0:0:8a2e:370:7334') }
3038
subject { @ipaddr }
@@ -48,4 +56,12 @@
4856
specify { subject.prefixlen.should == 48 }
4957
specify { subject.netmask.should == 'ffff:ffff:ffff:0000:0000:0000:0000:0000' }
5058
end
59+
60+
describe 'ipv6 open range with cidr' do
61+
before { @ipaddr = Puppet::Util::IPCidr.new('::/0') }
62+
subject { @ipaddr }
63+
specify { subject.cidr.should == '::/0' }
64+
specify { subject.prefixlen.should == 0 }
65+
specify { subject.netmask.should == '0000:0000:0000:0000:0000:0000:0000:0000' }
66+
end
5167
end

0 commit comments

Comments
 (0)