Skip to content

Commit 1a55b2f

Browse files
Merge pull request #1059 from cmusik/main
add support for using rpfilter in rules
2 parents 96c74ee + 039f788 commit 1a55b2f

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

lib/puppet/provider/firewall/iptables.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,17 @@ def self.rule_to_hash(line, table, counter)
534534
(\s--tunnel-src\s\S+)?
535535
(\s--next)?}x,
536536
'--pol "ipsec\1\2\3\4\5\6\7\8" ')
537+
538+
# rpfilter also takes multiple parameters; use quote trick again
539+
rpfilter_opts = values.scan(%r{-m\srpfilter(\s(--loose)|\s(--validmark)|\s(--accept-local)|\s(--invert))+})
540+
if rpfilter_opts && rpfilter_opts.length == 1 && rpfilter_opts[0]
541+
rpfilter_opts = rpfilter_opts[0][1..-1].reject { |x| x.nil? }
542+
values = values.sub(
543+
%r{-m\srpfilter(\s(--loose)|\s(--validmark)|\s(--accept-local)|\s(--invert))+},
544+
"-m rpfilter \"#{rpfilter_opts.join(' ')}\"",
545+
)
546+
end
547+
537548
# on some iptables versions, --connlimit-saddr switch is added after the rule is applied
538549
values = values.gsub(%r{--connlimit-saddr}, '')
539550

@@ -632,6 +643,8 @@ def self.rule_to_hash(line, table, counter)
632643
hash[prop] = hash[prop].split(';') unless hash[prop].nil?
633644
end
634645

646+
hash[:rpfilter] = hash[:rpfilter].split(' ') unless hash[:rpfilter].nil?
647+
635648
## clean up DSCP class to HEX mappings
636649
valid_dscp_classes = {
637650
'0x0a' => 'af11',
@@ -918,6 +931,8 @@ def general_args
918931
one, two = resource_value.split(' ')
919932
args << one
920933
args << two
934+
elsif res == :rpfilter
935+
args << resource_value
921936
elsif resource_value.is_a?(Array)
922937
args << resource_value.join(',')
923938
elsif !resource_value.nil?

lib/puppet/type/firewall.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ def insync?(is)
17051705
newvalues(:true, :false)
17061706
end
17071707

1708-
newproperty(:rpfilter, required_features: :rpfilter) do
1708+
newproperty(:rpfilter, required_features: :rpfilter, array_matching: :all) do
17091709
desc <<-PUPPETCODE
17101710
Enable the rpfilter module.
17111711
PUPPETCODE
@@ -1714,6 +1714,10 @@ def insync?(is)
17141714
munge do |value|
17151715
_value = '--' + value
17161716
end
1717+
1718+
def insync?(is)
1719+
is.to_set == should.to_set
1720+
end
17171721
end
17181722

17191723
newproperty(:socket, required_features: :socket) do

spec/acceptance/firewall_attributes_happy_path_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ class { '::firewall': }
331331
physdev_is_bridged => true,
332332
}
333333
firewall { '900 - set rpfilter':
334+
table => 'raw',
335+
chain => 'PREROUTING',
336+
action => 'accept',
337+
rpfilter => [ 'invert', 'validmark', 'loose', 'accept-local' ],
338+
}
339+
firewall { '901 - set rpfilter':
334340
table => 'raw',
335341
chain => 'PREROUTING',
336342
action => 'accept',
@@ -421,6 +427,12 @@ class { '::firewall': }
421427
it 'toports is set' do
422428
expect(result.stdout).to match(%r{-A PREROUTING -p icmp -m comment --comment "574 - toports" -j REDIRECT --to-ports 2222})
423429
end
430+
it 'rpfilter is set' do
431+
expect(result.stdout).to match(%r{-A PREROUTING -p tcp -m rpfilter --loose --validmark --accept-local --invert -m comment --comment "900 - set rpfilter" -j ACCEPT})
432+
end
433+
it 'single rpfilter is set' do
434+
expect(result.stdout).to match(%r{-A PREROUTING -p tcp -m rpfilter --invert -m comment --comment "901 - set rpfilter" -j ACCEPT})
435+
end
424436
it 'limit is set' do
425437
expect(result.stdout).to match(%r{-A INPUT -p tcp -m multiport --dports 572 -m limit --limit 500\/sec -m comment --comment "572 - limit" -j ACCEPT})
426438
end

0 commit comments

Comments
 (0)