Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 556bfc3

Browse files
authoredMay 22, 2024
Merge pull request #1206 from 2fa/fix-empty-builtin-chains-creation
Fix "creation" of empty built-in firewall chains
2 parents aef9a1e + bb9c1fe commit 556bfc3

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed
 

‎lib/puppet/provider/firewallchain/firewallchain.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class Puppet::Provider::Firewallchain::Firewallchain
3030
$chain_delete_command = '-X'
3131
# Command to set chain policy, works on inbuilt chains only
3232
$chain_policy_command = '-P'
33+
# Command to list specific table so it will generate necessary output for iptables-save
34+
# The retrieval of in-built chains may get confused by `iptables-save` tendency to not return table information
35+
# for tables that have not yet been interacted with.
36+
$table_list_command = '-L'
3337
# Check if the given chain name references a built in one
3438
$built_in_regex = %r{^(?:INPUT|OUTPUT|FORWARD|PREROUTING|POSTROUTING)$}
3539

@@ -94,7 +98,12 @@ def set(context, changes)
9498

9599
def create(context, name, should)
96100
context.notice("Creating Chain '#{name}' with #{should.inspect}")
97-
Puppet::Provider.execute([$base_command[should[:protocol]], should[:table], $chain_create_command, should[:chain]].join(' '))
101+
# If a built-in chain is not present we assume that corresponding table has not been interacted with
102+
if $built_in_regex.match(should[:chain])
103+
Puppet::Provider.execute([$base_command[should[:protocol]], should[:table], $table_list_command].join(' '))
104+
else
105+
Puppet::Provider.execute([$base_command[should[:protocol]], should[:table], $chain_create_command, should[:chain]].join(' '))
106+
end
98107
PuppetX::Firewall::Utility.persist_iptables(context, name, should[:protocol])
99108
end
100109

@@ -150,10 +159,7 @@ def self.process_input(is, should)
150159
should[:name] = should[:title] if should[:name].nil?
151160
should[:chain], should[:table], should[:protocol] = should[:name].split(':')
152161

153-
# If an in-built chain, always treat it as being present and ensure it is assigned a policy
154-
# The retrieval of in-built chains may get confused by `iptables-save` tendency to not return table information
155-
# for tables that have not yet been interacted with.
156-
is[:ensure] = 'present' if $built_in_regex.match(is[:chain])
162+
# If an in-built chain, ensure it is assigned a policy
157163
is[:policy] = 'accept' if $built_in_regex.match(is[:chain]) && is[:policy].nil?
158164
# For the same reason assign it the default policy as an intended state if it does not have one
159165
should[:policy] = 'accept' if $built_in_regex.match(should[:chain]) && should[:policy].nil?

‎spec/unit/puppet/provider/firewallchain/firewallchain_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
should: { name: 'INPUT:filter:IPv4', ensure: 'present' }
294294
},
295295
output: {
296-
is: { title: 'INPUT:filter:IPv4', name: 'INPUT:filter:IPv4', chain: 'INPUT', table: 'filter', protocol: 'IPv4', purge: false, ignore_foreign: false, ensure: 'present', policy: 'accept' },
296+
is: { title: 'INPUT:filter:IPv4', name: 'INPUT:filter:IPv4', chain: 'INPUT', table: 'filter', protocol: 'IPv4', purge: false, ignore_foreign: false, policy: 'accept' },
297297
should: { name: 'INPUT:filter:IPv4', chain: 'INPUT', table: 'filter', protocol: 'IPv4', ensure: 'present', policy: 'accept' }
298298
}
299299
},

0 commit comments

Comments
 (0)
Please sign in to comment.