Skip to content

Commit 1305aaf

Browse files
committed
(CAT-1260) Addition of tests for firewall provider private get methods
- Slight reorganization of providers - Corrections made to certain regex - Clarification of returned errors
1 parent 9f4b8aa commit 1305aaf

File tree

5 files changed

+447
-24
lines changed

5 files changed

+447
-24
lines changed

lib/puppet/provider/firewall/firewall.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,6 @@ def insync?(context, _name, property_name, is_hash, should_hash)
473473
###### GET ######
474474

475475
# Retrieve the rules
476-
# Placed in utility as the code is reused when determining order
477476
# Optional values lets you return a simplified set of data, used for determining order when adding/updating/deleting rules,
478477
# while also allowing for the protocols used to retrieve the rules to be limited.
479478
# @api.private
@@ -594,7 +593,7 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
594593
split_regex = value.split(%r{ })
595594
if rule[0].match(Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s"))
596595
# The exact information retrieved changes dependeing on the key
597-
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+)\\s(--limit-iface-(?:in|out))?") if [:src_type, :dst_type].include?(key)
596+
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+)\\s?(--limit-iface-(?:in|out))?") if [:src_type, :dst_type].include?(key)
598597
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+\\s\\S+)") if [:ipset].include?(key)
599598
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+)") if [:match_mark, :mss, :connmark].include?(key)
600599
# Since multiple values can be recovered, we must loop through each instance
@@ -635,11 +634,11 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
635634
rule_hash[key] = key_value[0]
636635
end
637636
when :recent
638-
if rule[0].match(Regexp.new("#{value}\\s--"))
637+
if rule[0].match(Regexp.new("#{value}\\s"))
639638
value_regex = Regexp.new("#{value}\\s(!\\s)?--(\\S+)")
640639
key_value = rule[0].scan(value_regex)[0]
641640
# If it has, combine the retrieved '!' with the actual value to make one string
642-
key_value[1] = [key_value[0], key_value[1]].join(' ') unless key_value[0].nil?
641+
key_value[1] = [key_value[0], key_value[1]].join('') unless key_value[0].nil?
643642
rule_hash[key] = key_value[1] if key_value
644643
end
645644
when :rpfilter
@@ -709,7 +708,7 @@ def self.validate_get(_context, rules)
709708
rules.each do |rule|
710709
names << rule[:name]
711710
end
712-
raise 'Duplicate names have been found within your Firewalls. This will prevent the module from working correctly and must be manually resolved.' if names.length != names.uniq.length
711+
raise ArgumentError, 'Duplicate names have been found within your Firewalls. This prevents the module from working correctly and must be manually resolved.' if names.length != names.uniq.length
713712
# Verify that the current order of the retrieved puppet rules is correct
714713
end
715714

@@ -718,7 +717,7 @@ def self.validate_get(_context, rules)
718717
def self.process_get(_context, rule_hash, rule, counter)
719718
# Puppet-firewall requires that all rules have structured comments (resource names) and will fail if a
720719
# rule in iptables does not have a matching comment.
721-
if !rule_hash[:name]
720+
if !rule_hash.key?(:name)
722721
num = 9000 + counter
723722
rule_hash[:name] = "#{num} #{Digest::SHA256.hexdigest(rule[0])}"
724723
elsif !rule_hash[:name].match(%r{(^\d+(?:[ \t-]\S+)+$)})

lib/puppet/provider/firewallchain/firewallchain.rb

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
# Implementation for the firewallchain type using the Resource API.
66
class Puppet::Provider::Firewallchain::Firewallchain
7+
###### GLOBAL VARIABLES ######
8+
79
# Command to list all chains and rules
810
$list_command = {
911
'IPv4' => 'iptables-save',
@@ -31,6 +33,8 @@ class Puppet::Provider::Firewallchain::Firewallchain
3133
# Check if the given chain name references a built in one
3234
$built_in_regex = %r{^(?:INPUT|OUTPUT|FORWARD|PREROUTING|POSTROUTING)$}
3335

36+
###### PUBLIC METHODS ######
37+
3438
# Raw data is retrieved via `iptables-save` and then regexed to retrieve the different Chains and whether they have a set Policy
3539
def get(_context)
3640
# Create empty return array
@@ -120,6 +124,23 @@ def delete(context, name, is)
120124
PuppetX::Firewall::Utility.persist_iptables(context, name, is[:protocol])
121125
end
122126

127+
# Custom insync method
128+
def insync?(context, _name, property_name, _is_hash, _should_hash)
129+
context.debug("Checking whether '#{property_name}' is out of sync")
130+
131+
case property_name
132+
when :purge, :ignore, :ignore_foreign
133+
# Suppres any update notifications for the purge/ignore(_foreign) values
134+
# They are used in the generate method which is ran prior to this point and have no
135+
# bearing on it's actual state.
136+
true
137+
else
138+
nil
139+
end
140+
end
141+
142+
###### PRIVATE METHODS ######
143+
123144
# Process the information so that it can be correctly applied
124145
# @api.private
125146
def self.process_input(is, should)
@@ -203,19 +224,4 @@ def generate(_context, title, _is, should)
203224

204225
rules_resources
205226
end
206-
207-
# Custom insync method
208-
def insync?(context, _name, property_name, _is_hash, _should_hash)
209-
context.debug("Checking whether '#{property_name}' is out of sync")
210-
211-
case property_name
212-
when :purge, :ignore, :ignore_foreign
213-
# Suppres any update notifications for the purge/ignore(_foreign) values
214-
# They are used in the generate method which is ran prior to this point and have no
215-
# bearing on it's actual state.
216-
true
217-
else
218-
nil
219-
end
220-
end
221227
end

lib/puppet/type/firewall.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,9 @@
942942
desc: <<-DESC
943943
Matches against the specified ipset list.
944944
Requires ipset kernel module. Will accept a single element or an array.
945-
The value is the name of the blacklist, followed by a space, and then
945+
The value is the name of the denylist, followed by a space, and then
946946
'src' and/or 'dst' separated by a comma.
947-
For example: 'blacklist src,dst'
947+
For example: 'denylist src,dst'
948948
To negate simply place a space seperated `!` at the beginning of a value.
949949
Values can de negated independently.
950950
DESC

spec/acceptance/firewall_duplicate_comment_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class { 'firewall': }
3131
run_shell('iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 552 -j ACCEPT -m comment --comment "550 destination"')
3232

3333
apply_manifest(pp) do |r|
34-
expect(r.stderr).to include('Duplicate names have been found within your Firewalls. This will prevent the module from working correctly and must be manually resolved.')
34+
expect(r.stderr).to include('Duplicate names have been found within your Firewalls. This prevents the module from working correctly and must be manually resolved.')
3535
end
3636
end
3737
end

0 commit comments

Comments
 (0)