From 2685964715a07285598e9d4b675ecf4e1f2fb000 Mon Sep 17 00:00:00 2001 From: Saurabh Pandit Date: Thu, 21 Nov 2024 17:10:37 +0530 Subject: [PATCH] (MODULE-11463): Fix rule parsing when iptables chains with '-A' in the name --- lib/puppet/provider/firewall/firewall.rb | 9 +- .../firewall/firewall_output_parsing_spec.rb | 354 ++++++++++++++++++ 2 files changed, 360 insertions(+), 3 deletions(-) create mode 100644 spec/unit/puppet/provider/firewall/firewall_output_parsing_spec.rb diff --git a/lib/puppet/provider/firewall/firewall.rb b/lib/puppet/provider/firewall/firewall.rb index 503a1b57e..61bfc1acc 100644 --- a/lib/puppet/provider/firewall/firewall.rb +++ b/lib/puppet/provider/firewall/firewall.rb @@ -19,7 +19,7 @@ class Puppet::Provider::Firewall::Firewall # Regex used to retrieve table name $table_name_regex = %r{^\*(nat|mangle|filter|raw|rawpost|broute|security)} # Regex used to retrieve Rules - $rules_regex = %r{(-A.*)\n} + $rules_regex = %r{^(-A.*)\n} # Base command $base_command = { 'IPv4' => 'iptables -t', @@ -466,6 +466,9 @@ def self.get_rules(context, basic, protocols = ['IPv4', 'IPv6']) iptables_list.scan($table_regex).each do |table| table_name = table[0].scan($table_name_regex)[0][0] table[0].scan($rules_regex).each do |rule| + # iptables-save escapes ' symbol in it's output for some reason which leads to an incorrect command + # We need to manually replace \' to ' + rule[0].gsub!("\\'", "'") raw_rules = if basic Puppet::Provider::Firewall::Firewall.rule_to_name(context, rule[0], table_name, protocol) else @@ -489,7 +492,7 @@ def self.rule_to_name(_context, rule, table_name, protocol) rule_hash[:table] = table_name rule_hash[:protocol] = protocol - name_regex = Regexp.new("#{$resource_map[:name]}\\s(?:\"([^\"]*)|([^\"\\s]*))") + name_regex = Regexp.new("#{$resource_map[:name]}\\s+(?:\"(.+?(?