Skip to content

Commit 4069d08

Browse files
committed
(CAT-1260) Unit test coverage for firewallchain Provider
1 parent 8a65310 commit 4069d08

File tree

3 files changed

+363
-64
lines changed

3 files changed

+363
-64
lines changed

lib/puppet/provider/firewallchain/firewallchain.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def set(context, changes)
8989
end
9090

9191
def create(context, name, should)
92-
context.notice("Creating '#{name}' with #{should.inspect}")
92+
context.notice("Creating Chain '#{name}' with #{should.inspect}")
9393
Puppet::Provider.execute([$base_command[should[:protocol]], should[:table], $chain_create_command, should[:chain]].join(' '))
9494
PuppetX::Firewall::Utility.persist_iptables(context, name, should[:protocol])
9595
end
@@ -99,7 +99,7 @@ def update(context, name, should, is)
9999
return if !$built_in_regex.match(should[:chain]) ||
100100
($built_in_regex.match(should[:chain]) && is[:policy] == should[:policy])
101101

102-
context.notice("Updating '#{name}' with #{should.inspect}")
102+
context.notice("Updating Chain '#{name}' with #{should.inspect}")
103103
Puppet::Provider.execute([$base_command[should[:protocol]], should[:table], $chain_policy_command, should[:chain], should[:policy].upcase].join(' '))
104104
PuppetX::Firewall::Utility.persist_iptables(context, name, should[:protocol])
105105
end
@@ -129,10 +129,11 @@ def self.process_input(is, should)
129129
should[:name] = should[:title] if should[:name].nil?
130130
should[:chain], should[:table], should[:protocol] = should[:name].split(':')
131131

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

@@ -145,23 +146,23 @@ def self.verify(_is, should)
145146
# Verify that no incorrect chain names are passed
146147
case should[:table]
147148
when 'filter'
148-
raise ArgumentError, "INPUT, OUTPUT and FORWARD are the only inbuilt chains that can be used in table 'filter'" if %r{^(PREROUTING|POSTROUTING|BROUTING)$}.match?(should[:chain])
149+
raise ArgumentError, 'INPUT, OUTPUT and FORWARD are the only inbuilt chains that can be used in table \'filter\'' if %r{^(PREROUTING|POSTROUTING|BROUTING)$}.match?(should[:chain])
149150
when 'mangle'
150-
raise ArgumentError, "PREROUTING, POSTROUTING, INPUT, FORWARD and OUTPUT are the only inbuilt chains that can be used in table 'mangle'" if %r{^(BROUTING)$}.match?(should[:chain])
151+
raise ArgumentError, 'PREROUTING, POSTROUTING, INPUT, FORWARD and OUTPUT are the only inbuilt chains that can be used in table \'mangle\'' if %r{^(BROUTING)$}.match?(should[:chain])
151152
when 'nat'
152-
raise ArgumentError, "PREROUTING, POSTROUTING, INPUT, and OUTPUT are the only inbuilt chains that can be used in table 'nat'" if %r{^(BROUTING|FORWARD)$}.match?(should[:chain])
153-
raise ArgumentError, "table nat isn't valid in IPv6. You must specify ':IPv4' as the name suffix" if %r{^(IP(v6)?)?$}.match?(should[:protocol])
153+
raise ArgumentError, 'PREROUTING, POSTROUTING, INPUT, and OUTPUT are the only inbuilt chains that can be used in table \'nat\'' if %r{^(BROUTING|FORWARD)$}.match?(should[:chain])
154+
raise ArgumentError, 'table nat isn\'t valid in IPv6. You must specify \':IPv4\' as the name suffix' if %r{^(IP(v6)?)?$}.match?(should[:protocol])
154155
when 'raw'
155156
raise ArgumentError, 'PREROUTING and OUTPUT are the only inbuilt chains in the table \'raw\'' if %r{^(POSTROUTING|BROUTING|INPUT|FORWARD)$}.match?(should[:chain])
156157
when 'broute'
157158
raise ArgumentError, 'BROUTE is only valid with protocol \'ethernet\'' if should[:protocol] != 'ethernet'
158159
raise ArgumentError, 'BROUTING is the only inbuilt chain allowed on on table \'broute\'' if %r{^PREROUTING|POSTROUTING|INPUT|FORWARD|OUTPUT$}.match?(should[:chain])
159160
when 'security'
160-
raise ArgumentError, "INPUT, OUTPUT and FORWARD are the only inbuilt chains that can be used in table 'security'" if %r{^(PREROUTING|POSTROUTING|BROUTING)$}.match?(should[:chain])
161+
raise ArgumentError, 'INPUT, OUTPUT and FORWARD are the only inbuilt chains that can be used in table \'security\'' if %r{^(PREROUTING|POSTROUTING|BROUTING)$}.match?(should[:chain])
161162
end
162163

163164
# Verify that Policy is only passed for the inbuilt chains
164-
raise "`policy` can only be set on Internal Chains. Setting for `#{should[:name]}` is invalid" if !$built_in_regex.match(should[:chain]) && should.key?(:policy)
165+
raise ArgumentError, "'policy' can only be set on Internal Chains. Setting for '#{should[:name]}' is invalid" if !$built_in_regex.match(should[:chain]) && should.key?(:policy)
165166

166167
# Warn that inbuilt chains will be flushed, not deleted
167168
warn "Warning: Inbuilt Chains may not be deleted. Chain `#{should[:name]}` will be flushed and have it's policy reverted to default." if $built_in_regex.match(should[:chain]) &&
@@ -186,6 +187,8 @@ def generate(_context, title, _is, should)
186187
end
187188

188189
# Remove rules which match our ignore filter
190+
# Ensure ignore value is wrapped as an array to simplify the code
191+
should[:ignore] = [should[:ignore]] if should[:ignore].is_a?(String)
189192
rules_resources.delete_if { |resource| should[:ignore].find_index { |ignore| resource.rsapi_current_state[:name].match(ignore) } } if should[:ignore]
190193

191194
# Remove rules that were (presumably) not put in by puppet
@@ -203,7 +206,7 @@ def generate(_context, title, _is, should)
203206

204207
# Custom insync method
205208
def insync?(context, _name, property_name, _is_hash, _should_hash)
206-
context.debug("Checking whether #{property_name} is out of sync")
209+
context.debug("Checking whether '#{property_name}' is out of sync")
207210

208211
case property_name
209212
when :purge, :ignore, :ignore_foreign

0 commit comments

Comments
 (0)