Skip to content

Commit f5c266b

Browse files
Make options for '-m rpfilter' work correctly
Parse the trailing arguments to '-m rpfilter' out of existing rules. Prior behavior included the '--' prefix along with the options themselves when pulling them out of the rule. For ip6tables, the provider could not correctly generate an ip6tables commandline that included '-m rpfilter' at all - its inclusion in the known booleans array precluded its options being expanded or included at all. Additionally: - Using a comma rather than a space as a separator character in the pre-parse munging doesn't require any quotes, nor does it require any new post-parse munging when there is already an existing iterator to handle splitting of comma-separated multiple elements into arrays - '-m rpfilter' on its own is supposed to be valid, and in fact is used in exactly this style of invocation in the examples included in 'man iptables-extensions'; but support for '-m rpfilter' in this module is limited to uses that include one or more modifying arguments. When this is eventually fixed (which I do not have time to do right now), the updated pre-parse munge logic will work with no further alteration - Adjusting the regex used by String#scan to capture the arguments to treat its capture groups differently SIGNIFICANTLY simplifies the logic around substitutions for the pre-parse munge such that no additional branching is required, and the operation is still safe
1 parent 18f575b commit f5c266b

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

lib/puppet/provider/firewall/ip6tables.rb

-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ def self.iptables_save(*args)
243243
:rsource,
244244
:rdest,
245245
:reap,
246-
:rpfilter,
247246
:rttl,
248247
:socket,
249248
:physdev_is_bridged,

lib/puppet/provider/firewall/iptables.rb

+4-12
Original file line numberDiff line numberDiff line change
@@ -581,15 +581,9 @@ def self.rule_to_hash(line, table, counter)
581581
(\s--next)?}x,
582582
'--pol "ipsec\1\2\3\4\5\6\7\8" ')
583583

584-
# rpfilter also takes multiple parameters; use quote trick again
585-
rpfilter_opts = values.scan(%r{-m\srpfilter(\s(--loose)|\s(--validmark)|\s(--accept-local)|\s(--invert))+})
586-
if rpfilter_opts && rpfilter_opts.length == 1 && rpfilter_opts[0]
587-
rpfilter_opts = rpfilter_opts[0][1..-1].reject { |x| x.nil? }
588-
values = values.sub(
589-
%r{-m\srpfilter(\s(--loose)|\s(--validmark)|\s(--accept-local)|\s(--invert))+},
590-
"-m rpfilter \"#{rpfilter_opts.join(' ')}\"",
591-
)
592-
end
584+
# rpfilter can take multiple parameters; if present, strip and comma-join them.
585+
rpfilter_opts = values.scan(%r{-m\srpfilter(?:\s--(loose)|\s--(validmark)|\s--(accept-local)|\s--(invert))+}).flatten.compact
586+
values.sub!(%r{-m\srpfilter(?:\s--(?:loose|validmark|accept-local|invert))+}, "-m rpfilter #{rpfilter_opts.join(',')}")
593587

594588
# on some iptables versions, --connlimit-saddr switch is added after the rule is applied
595589
values = values.gsub(%r{--connlimit-saddr}, '')
@@ -681,16 +675,14 @@ def self.rule_to_hash(line, table, counter)
681675
# POST PARSE CLUDGING
682676
#####################
683677

684-
[:dport, :sport, :port, :state, :ctstate, :ctstatus].each do |prop|
678+
[:dport, :sport, :port, :state, :ctstate, :ctstatus, :rpfilter].each do |prop|
685679
hash[prop] = hash[prop].split(',') unless hash[prop].nil?
686680
end
687681

688682
[:ipset, :dst_type, :src_type].each do |prop|
689683
hash[prop] = hash[prop].split(';') unless hash[prop].nil?
690684
end
691685

692-
hash[:rpfilter] = hash[:rpfilter].split(' ') unless hash[:rpfilter].nil?
693-
694686
## clean up DSCP class to HEX mappings
695687
valid_dscp_classes = {
696688
'0x0a' => 'af11',

0 commit comments

Comments
 (0)