Skip to content

Commit 2b94c3c

Browse files
committed
(CAT-376) Clean up code
- Small improvement to how rule is passed
1 parent 5d94d77 commit 2b94c3c

File tree

2 files changed

+68
-68
lines changed

2 files changed

+68
-68
lines changed

lib/puppet/provider/firewall/firewall.rb

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,12 @@ def self.get_rules(context, basic, protocols = ['IPv4', 'IPv6'])
489489
table_name = table[0].scan($table_name_regex)[0][0]
490490
table[0].scan($rules_regex).each do |rule|
491491
raw_rules = if basic
492-
Puppet::Provider::Firewall::Firewall.rule_to_name(context, rule, table_name, protocol)
492+
Puppet::Provider::Firewall::Firewall.rule_to_name(context, rule[0], table_name, protocol)
493493
else
494-
Puppet::Provider::Firewall::Firewall.rule_to_hash(context, rule, table_name, protocol)
494+
Puppet::Provider::Firewall::Firewall.rule_to_hash(context, rule[0], table_name, protocol)
495495
end
496496
# Process the returned values so that it is correct for our purposes
497-
rules << Puppet::Provider::Firewall::Firewall.process_get(context, raw_rules, rule, counter)
497+
rules << Puppet::Provider::Firewall::Firewall.process_get(context, raw_rules, rule[0], counter)
498498
counter += 1
499499
end
500500
end
@@ -512,12 +512,12 @@ def self.rule_to_name(_context, rule, table_name, protocol)
512512
rule_hash[:protocol] = protocol
513513

514514
name_regex = Regexp.new("#{$resource_map[:name]}\\s(?:\"([^\"]*)|([^\"\\s]*))")
515-
name_value = rule[0].scan(name_regex)[0]
515+
name_value = rule.scan(name_regex)[0]
516516
# Combine the returned values and remove and trailing or leading whitespace
517517
rule_hash[:name] = [name_value[0], name_value[1]].join(' ').strip if name_value
518518

519519
chain_regex = Regexp.new("#{$resource_map[:chain]}\\s(\\S+)")
520-
rule_hash[:chain] = rule[0].scan(chain_regex)[0][0]
520+
rule_hash[:chain] = rule.scan(chain_regex)[0][0]
521521

522522
rule_hash
523523
end
@@ -530,12 +530,12 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
530530
rule_hash[:ensure] = 'present'
531531
rule_hash[:table] = table_name
532532
rule_hash[:protocol] = protocol
533-
rule_hash[:line] = rule[0]
533+
rule_hash[:line] = rule
534534
# Add the ensure parameter first
535535
$resource_map.each do |key, value|
536536
if $known_booleans.include?(key)
537537
# check for flag with regex, add a space/line end to ensure accuracy with the more simplistic flags; i.e. `-f`, `--random`
538-
rule_hash[key] = if rule[0].match(Regexp.new("#{value}(\\s|$)"))
538+
rule_hash[key] = if rule.match(Regexp.new("#{value}(\\s|$)"))
539539
true
540540
else
541541
false
@@ -548,57 +548,57 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
548548
# When :name/:string/:string_hex/:bytecode, return everything inside the double quote pair following the key value
549549
# When only a single word comment is returned no quotes are given, so we must check for this as well
550550
# First find if flag is present, add a space to ensure accuracy with the more simplistic flags; i.e. `-i`
551-
if rule[0].match(Regexp.new("#{value}\\s"))
551+
if rule.match(Regexp.new("#{value}\\s"))
552552
value_regex = Regexp.new("(?:(!\\s))?#{value}\\s(?:\"([^\"]*)|([^\"\\s]*))")
553-
key_value = rule[0].scan(value_regex)[0]
553+
key_value = rule.scan(value_regex)[0]
554554
# Combine the returned values and remove and trailing or leading whitespace
555555
key_value[1] = [key_value[0], key_value[1], key_value[2]].join('')
556556
rule_hash[key] = key_value[1] if key_value[1]
557557
end
558558
when :sport, :dport
559559
split_value_regex = value[0].split(%r{ })
560560
negated_multi_regex = [split_value_regex[0], split_value_regex[1], '!', split_value_regex[2]].join(' ')
561-
if rule[0].match(value[0])
561+
if rule.match(value[0])
562562
# First check against the multiport value, if found split and return as an array
563563
value_regex = Regexp.new("#{value[0]}\\s(\\S+)")
564-
key_value = rule[0].scan(value_regex)[0]
564+
key_value = rule.scan(value_regex)[0]
565565
rule_hash[key] = key_value[0].split(%r{,})
566-
elsif rule[0].match(negated_multi_regex)
566+
elsif rule.match(negated_multi_regex)
567567
# Next check against a negated multiport value, if found split and return as an array with the first value negated
568568
value_regex = Regexp.new("#{negated_multi_regex}\\s(\\S+)")
569-
key_value = rule[0].scan(value_regex)[0]
569+
key_value = rule.scan(value_regex)[0]
570570

571571
# Add '!' to the beginning of the first value to show it as negated
572572
split_value = key_value[0].split(%r{,})
573573
split_value[0] = "! #{split_value[0]}"
574574
rule_hash[key] = split_value
575-
elsif rule[0].match(value[1])
575+
elsif rule.match(value[1])
576576
# If no multi value matches, check against the regular value instead
577577
value_regex = Regexp.new("(?:(!)\\s)?#{value[1]}\\s(\\S+)")
578-
key_value = rule[0].scan(value_regex)[0]
578+
key_value = rule.scan(value_regex)[0]
579579
# If it is negated, combine the retrieved '!' with the actual value to make one string
580580
key_value[1] = [key_value[0], key_value[1]].join(' ') unless key_value[0].nil?
581581
rule_hash[key] = key_value[1]
582582
end
583583
when :tcp_flags
584584
# First find if flag is present, add a space to ensure accuracy with the more simplistic flags; i.e. `-i`
585-
if rule[0].match(Regexp.new("#{value}\\s"))
585+
if rule.match(Regexp.new("#{value}\\s"))
586586
value_regex = Regexp.new("(?:(!)\\s)?#{value}\\s(\\S+)\\s(\\S+)")
587-
key_value = rule[0].scan(value_regex)[0]
587+
key_value = rule.scan(value_regex)[0]
588588
# If a negation is found combine it with the first retrieved value, then combine both values
589589
key_value[1] = [key_value[0], key_value[1]].join(' ') unless key_value[0].nil?
590590
rule_hash[key] = [key_value[1], key_value[2]].join(' ')
591591
end
592592
when :src_type, :dst_type, :ipset, :match_mark, :mss, :connmark
593593
split_regex = value.split(%r{ })
594-
if rule[0].match(Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s"))
594+
if rule.match(Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s"))
595595
# The exact information retrieved changes dependeing on the key
596596
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)
597597
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+\\s\\S+)") if [:ipset].include?(key)
598598
value_regex = Regexp.new("#{split_regex[1]}\\s(?:(!)\\s)?#{split_regex[2]}\\s(\\S+)") if [:match_mark, :mss, :connmark].include?(key)
599599
# Since multiple values can be recovered, we must loop through each instance
600600
type_value = []
601-
key_value = rule[0].scan(value_regex)
601+
key_value = rule.scan(value_regex)
602602
key_value.length.times do |i|
603603
type_value.append(key_value[i].join(' ').strip) if key_value[i]
604604
end
@@ -607,9 +607,9 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
607607
rule_hash[key] = type_value if type_value.length > 1
608608
end
609609
when :state, :ctstate, :ctstatus, :month_days, :week_days
610-
if rule[0].match(Regexp.new("#{value}\\s"))
610+
if rule.match(Regexp.new("#{value}\\s"))
611611
value_regex = Regexp.new("(?:(!)\\s)?#{value}\\s(\\S+)")
612-
key_value = rule[0].scan(value_regex)
612+
key_value = rule.scan(value_regex)
613613
split_value = key_value[0][1].split(%r{,})
614614
# If negated add to first value
615615
split_value[0] = [key_value[0][0], split_value[0]].join(' ') unless key_value[0][0].nil?
@@ -628,24 +628,24 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
628628
proto = 1
629629
end
630630

631-
if rule[0].match(Regexp.new("#{value[proto]}\\s"))
631+
if rule.match(Regexp.new("#{value[proto]}\\s"))
632632
value_regex = Regexp.new("#{value[proto]}\\s(\\S+)")
633-
key_value = rule[0].scan(value_regex)[0]
633+
key_value = rule.scan(value_regex)[0]
634634
rule_hash[key] = key_value[0]
635635
end
636636
when :recent
637-
if rule[0].match(Regexp.new("#{value}\\s"))
637+
if rule.match(Regexp.new("#{value}\\s"))
638638
value_regex = Regexp.new("#{value}\\s(!\\s)?--(\\S+)")
639-
key_value = rule[0].scan(value_regex)[0]
639+
key_value = rule.scan(value_regex)[0]
640640
# If it has, combine the retrieved '!' with the actual value to make one string
641641
key_value[1] = [key_value[0], key_value[1]].join('') unless key_value[0].nil?
642642
rule_hash[key] = key_value[1] if key_value
643643
end
644644
when :rpfilter
645-
if rule[0].match(Regexp.new("#{value}\\s--"))
645+
if rule.match(Regexp.new("#{value}\\s--"))
646646
# Since the values are their own flags we can simply look for them directly
647647
value_regex = Regexp.new("(?:\s--(invert|validmark|loose|accept-local))")
648-
key_value = rule[0].scan(value_regex)
648+
key_value = rule.scan(value_regex)
649649
return_value = []
650650
key_value.each do |value|
651651
return_value << value[0]
@@ -658,9 +658,9 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
658658
:ctorigsrcport, :ctorigdstport, :ctreplsrcport, :ctrepldstport, :ctexpire, :cgroup, :hop_limit
659659
# Values where negation is prior to the flag
660660
# First find if flag is present, add a space to ensure accuracy with the more simplistic flags; i.e. `-i`
661-
if rule[0].match(Regexp.new("#{value}\\s"))
661+
if rule.match(Regexp.new("#{value}\\s"))
662662
value_regex = Regexp.new("(?:(!)\\s)?#{value}\\s(\\S+)")
663-
key_value = rule[0].scan(value_regex)[0]
663+
key_value = rule.scan(value_regex)[0]
664664
# If it has, combine the retrieved '!' with the actual value to make one string
665665
key_value[1] = [key_value[0], key_value[1]].join(' ') unless key_value[0].nil?
666666
rule_hash[key] = key_value[1] if key_value
@@ -676,9 +676,9 @@ def self.rule_to_hash(_context, rule, table_name, protocol)
676676
# :hashlimit_htable_gcinterval, :zone, :helper, :condition
677677
# Default return, retrieve first complete block following the key value
678678
# First find if flag is present, add a space to ensure accuracy with the more simplistic flags; i.e. `-j`, `--to`
679-
if rule[0].match(Regexp.new("#{value}\\s"))
679+
if rule.match(Regexp.new("#{value}\\s"))
680680
value_regex = Regexp.new("#{value}(?:\\s(!)\\s|\\s{1,2})(\\S+)")
681-
key_value = rule[0].scan(value_regex)[0]
681+
key_value = rule.scan(value_regex)[0]
682682
# If it has, combine the retrieved '!' with the actual value to make one string
683683
key_value[1] = [key_value[0], key_value[1]].join(' ') unless key_value[0].nil?
684684
# If value is meant to return as an integer/float ensure it does
@@ -719,7 +719,7 @@ def self.process_get(_context, rule_hash, rule, counter)
719719
# rule in iptables does not have a matching comment.
720720
if !rule_hash.key?(:name)
721721
num = 9000 + counter
722-
rule_hash[:name] = "#{num} #{Digest::SHA256.hexdigest(rule[0])}"
722+
rule_hash[:name] = "#{num} #{Digest::SHA256.hexdigest(rule)}"
723723
elsif !rule_hash[:name].match(%r{(^\d+(?:[ \t-]\S+)+$)})
724724
num = 9000 + counter
725725
rule_hash[:name] = "#{num} #{rule_hash[:name]}"

0 commit comments

Comments
 (0)