Skip to content

Commit ccd6486

Browse files
committed
(CONT-962) Simplify type of all address fields from Pattern to String[1]
This is done as the regex to accurately assess whether the passed string is a valid IPv4 or IPv6 address is overly complex and too difficult to ensure accuracy. For reference an attempt at creating one is shown below: /^(?:!\s)?(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$
1 parent bab7641 commit ccd6486

File tree

5 files changed

+46
-47
lines changed

5 files changed

+46
-47
lines changed

lib/puppet/provider/firewall/firewall.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def self.process_get(_context, rule_hash, rule, counter)
554554
if !rule_hash[:name]
555555
num = 9000 + counter
556556
rule_hash[:name] = "#{num} #{Digest::SHA256.hexdigest(rule[0])}"
557-
elsif !rule_hash[:name].match(%r{(^\d+(?:\s\w+)+$)})
557+
elsif !rule_hash[:name].match(%r{(^\d+(?:[ \t-]\S+)+$)})
558558
num = 9000 + counter
559559
rule_hash[:name] = "#{num} #{rule_hash[:name]}"
560560
end

lib/puppet/type/firewall.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
DESC
134134
},
135135
name: {
136-
type: 'Pattern[/(^\d+(?:[ \t]\S+)+$)/]',
136+
type: 'Pattern[/(^\d+(?:[ \t-]\S+)+$)/]',
137137
behaviour: :namevar,
138138
desc: <<-DESC
139139
The canonical name of the rule. This name is also used for ordering
@@ -147,7 +147,7 @@
147147
DESC
148148
},
149149
line: {
150-
type: 'Optional[String]',
150+
type: 'Optional[String[1]]',
151151
behaviour: :read_only,
152152
desc: <<-DESC
153153
A read only attribute containing the full rule, used when deleting only.
@@ -200,7 +200,7 @@
200200
DESC
201201
},
202202
source: {
203-
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
203+
type: 'Optional[String[1]]',
204204
desc: <<-DESC
205205
The source address. For example:
206206
@@ -214,7 +214,7 @@
214214
DESC
215215
},
216216
destination: {
217-
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
217+
type: 'Optional[String[1]]',
218218
desc: <<-DESC
219219
The destination address to match. For example:
220220
@@ -624,7 +624,7 @@
624624
DESC
625625
},
626626
ctorigsrc: {
627-
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
627+
type: 'Optional[String[1]]',
628628
desc: <<-DESC
629629
The original source address using the conntrack module. For example:
630630
@@ -640,7 +640,7 @@
640640
ctorigdst: {
641641
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
642642
desc: <<-DESC
643-
The original destination address using the conntrack module. For example:
643+
type: 'Optional[String[1]]',
644644
645645
ctorigdst => '192.168.2.0/24'
646646
@@ -652,7 +652,7 @@
652652
DESC
653653
},
654654
ctreplsrc: {
655-
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
655+
type: 'Optional[String[1]]',
656656
desc: <<-DESC
657657
The reply source address using the conntrack module. For example:
658658
@@ -666,7 +666,7 @@
666666
DESC
667667
},
668668
ctrepldst: {
669-
type: 'Optional[Pattern[/^(?:!\s)?\d+\.\d+\.\d+\.\d+(?:\/\d+)?$/]]',
669+
type: 'Optional[String[1]]',
670670
desc: <<-DESC
671671
The reply destination address using the conntrack module. For example:
672672
@@ -1163,7 +1163,7 @@
11631163
DESC
11641164
},
11651165
todest: {
1166-
type: 'Optional[Pattern[/^\d+\.\d+\.\d+\.\d+(?:-\d+\.\d+\.\d+\.\d+)?(?:\:\d+(?:-\d+(?:\/\d+)?)?)?$/]]',
1166+
type: 'Optional[String[1]]',
11671167
desc: <<-DESC
11681168
When using jump => "DNAT" you can specify the new destination address using this paramter.
11691169
Can specify a single new destination IP address or an inclusive range of IP addresses.
@@ -1172,7 +1172,7 @@
11721172
DESC
11731173
},
11741174
tosource: {
1175-
type: 'Optional[Pattern[/^\d+\.\d+\.\d+\.\d+(?:-\d+\.\d+\.\d+\.\d+)?(?:\:\d+(?:-\d+)?)?$/]]',
1175+
type: 'Optional[String[1]]',
11761176
desc: <<-DESC
11771177
When using jump => "SNAT" you can specify the new source address using this paramter.
11781178
Can specify a single new destination IP address or an inclusive range of IP addresses.

lib/puppet_x/puppetlabs/firewall/utility.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def self.to_hex32(value)
242242
# Converts a given number to its protocol keyword
243243
# https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
244244
def self.proto_number_to_name(value)
245-
return value if %r{^(?:!\s)?([a-z]+)$}.match?(value)
245+
return value if %r{^(?:!\s)?([a-z])}.match?(value)
246246
match = value.to_s.match(%r{^(!\s)?(.*)})
247247
keyword = case match[2]
248248
when '1' then 'icmp'
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
1-
# # frozen_string_literal: true
1+
# frozen_string_literal: true
22

3-
# require 'spec_helper_acceptance'
3+
require 'spec_helper_acceptance'
44

5-
# describe 'firewall - duplicate comments' do
6-
# before(:all) do
7-
# if os[:family] == 'ubuntu' || os[:family] == 'debian'
8-
# update_profile_file
9-
# end
10-
# end
5+
describe 'firewall - duplicate comments' do
6+
before(:all) do
7+
if os[:family] == 'ubuntu' || os[:family] == 'debian'
8+
update_profile_file
9+
end
10+
end
1111

12-
# after(:each) do
13-
# iptables_flush_all_tables
14-
# end
12+
after(:each) do
13+
iptables_flush_all_tables
14+
end
1515

16-
# context 'when a duplicate comment is found' do
17-
# pp = <<-PUPPETCODE
18-
# class { 'firewall': }
19-
# resources { 'firewall':
20-
# purge => true,
21-
# }
16+
context 'when a duplicate comment is found' do
17+
pp = <<-PUPPETCODE
18+
class { 'firewall': }
19+
resources { 'firewall':
20+
purge => true,
21+
}
2222
23-
# firewall { '550 destination':
24-
# proto => tcp,
25-
# dport => '550',
26-
# action => accept,
27-
# destination => '192.168.2.0/24',
28-
# }
29-
# PUPPETCODE
23+
firewall { '550 destination':
24+
proto => tcp,
25+
dport => '550',
26+
jump => accept,
27+
destination => '192.168.2.0/24',
28+
}
29+
PUPPETCODE
3030

31-
# it 'raises an error' do
32-
# run_shell('iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 551 -j ACCEPT -m comment --comment "550 destination"')
33-
# run_shell('iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 552 -j ACCEPT -m comment --comment "550 destination"')
31+
it 'raises an error' do
32+
run_shell('iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 551 -j ACCEPT -m comment --comment "550 destination"')
33+
run_shell('iptables -I INPUT -m state --state NEW -m tcp -p tcp --dport 552 -j ACCEPT -m comment --comment "550 destination"')
3434

35-
# apply_manifest(pp) do |r|
36-
# expect(r.stderr).to include('Duplicate rule found for 550 destination. Skipping update.')
37-
# end
38-
# end
39-
# end
40-
# end
35+
apply_manifest(pp) do |r|
36+
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.')
37+
end
38+
end
39+
end
40+
end

spec/acceptance/resource_cmd_spec.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,9 @@
202202
end
203203
end
204204

205-
# version of iptables that ships with el5 doesn't work with the
206205
# ip6tables provider
207206
# TODO: Test below fails if this file is run seperately. i.e. bundle exec rspec spec/acceptance/resource_cmd_spec.rb
208-
context 'when dport/sport with ip6tables', unless: os[:family] == 'redhat' && os[:release].start_with?('5') do
207+
context 'when dport/sport with ip6tables' do
209208
before :all do
210209
if os['family'] == 'debian'
211210
run_shell('echo "iptables-persistent iptables-persistent/autosave_v4 boolean false" | debconf-set-selections')

0 commit comments

Comments
 (0)