Skip to content

Commit 625853e

Browse files
authoredMay 7, 2025
Merge pull request #1212 from 2fa/port-ranges-with-hyphens
Port ranges as string with hyphen as range indicator should work
2 parents 61b3430 + c848f93 commit 625853e

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed
 

‎lib/puppet/provider/firewall/firewall.rb

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,28 +418,32 @@ def insync?(context, _name, property_name, is_hash, should_hash)
418418
when :dport, :sport, :state, :ctstate, :ctstatus
419419
is = is_hash[property_name]
420420
should = should_hash[property_name]
421+
ports = [:dport, :sport]
422+
423+
if is.is_a?(Array) && should.is_a?(Array)
424+
# Ensure values are sorted
425+
# Ensure any negation includes only the first value
426+
is_negated = true if %r{^!\s}.match?(is[0].to_s)
427+
is.each_with_index do |_value, _index|
428+
is = is.map { |value| value.to_s.tr('! ', '') }.sort
429+
end
430+
is[0] = ['!', is[0]].join(' ') if is_negated
421431

422-
# Unique logic is only needed when both values are arrays
423-
return nil unless is.is_a?(Array) && should.is_a?(Array)
424-
425-
# Ensure values are sorted
426-
# Ensure any negation includes only the first value
427-
is_negated = true if %r{^!\s}.match?(is[0].to_s)
428-
is.each_with_index do |_value, _index|
429-
is = is.map { |value| value.to_s.tr('! ', '') }.sort
430-
end
431-
is[0] = ['!', is[0]].join(' ') if is_negated
432+
should_negated = true if %r{^!\s}.match?(should[0].to_s)
433+
should.each_with_index do |_value, _index|
434+
should = should.map { |value| value.to_s.tr('! ', '') }.sort
435+
# Port range can be passed as `-` but will always be set/returned as `:`
436+
should = should.map { |value| value.to_s.tr('-', ':') }.sort if ports.include?(property_name)
437+
end
438+
should[0] = ['!', should[0]].join(' ') if should_negated
432439

433-
should_negated = true if %r{^!\s}.match?(should[0].to_s)
434-
should.each_with_index do |_value, _index|
435-
should = should.map { |value| value.to_s.tr('! ', '') }.sort
440+
is == should
441+
elsif is.is_a?(String) && should.is_a?(String)
436442
# Port range can be passed as `-` but will always be set/returned as `:`
437-
ports = [:dport, :sport]
438-
should = should.map { |value| value.to_s.tr('-', ':') }.sort if ports.include?(property_name)
439-
end
440-
should[0] = ['!', should[0]].join(' ') if should_negated
443+
should = should.tr('-', ':') if ports.include?(property_name)
441444

442-
is == should
445+
is == should
446+
end
443447
when :string_hex
444448
# Compare the values with any whitespace removed
445449
is = is_hash[property_name].to_s.gsub(%r{\s+}, '')

‎spec/acceptance/firewall_attributes_exceptions_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ class { '::firewall': }
6868
end
6969
end
7070
end
71+
72+
context 'when port range as a string' do
73+
pp23 = <<-PUPPETCODE
74+
class { '::firewall': }
75+
firewall { '562 - test port range':
76+
proto => tcp,
77+
dport => '561-570',
78+
jump => accept,
79+
}
80+
PUPPETCODE
81+
it 'applies' do
82+
idempotent_apply(pp23)
83+
apply_manifest(pp23, catch_changes: true)
84+
end
85+
end
7186
end
7287

7388
describe 'ensure' do
@@ -652,6 +667,21 @@ class { '::firewall': }
652667
end
653668
end
654669
end
670+
671+
context 'when port range as a string' do
672+
pp20 = <<-PUPPETCODE
673+
class { '::firewall': }
674+
firewall { '561 - test port range':
675+
proto => tcp,
676+
sport => '561-570',
677+
jump => accept,
678+
}
679+
PUPPETCODE
680+
it 'applies' do
681+
idempotent_apply(pp20)
682+
apply_manifest(pp20, catch_changes: true)
683+
end
684+
end
655685
end
656686

657687
describe 'source' do

‎spec/unit/puppet/provider/firewall/firewall_public_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
{ is_hash: { mac_source: '! 0A:1B:3C:4D:5E:6F' }, should_hash: { mac_source: '0A:1B:3C:4D:5E:6F' }, result: false },
152152
] },
153153
{ testing: 'state/ctstate/ctstatus', property_name: :state, comparisons: [
154-
{ is_hash: { state: 'NEW' }, should_hash: { state: 'NEW' }, result: nil },
154+
{ is_hash: { state: 'NEW' }, should_hash: { state: 'NEW' }, result: true },
155155
{ is_hash: { state: ['NEW'] }, should_hash: { state: 'NEW' }, result: nil },
156156
{ is_hash: { state: 'NEW' }, should_hash: { state: ['NEW', 'INVALID'] }, result: nil },
157157
{ is_hash: { state: ['INVALID', 'NEW'] }, should_hash: { state: ['NEW', 'INVALID'] }, result: true },
@@ -201,7 +201,7 @@
201201
{ is_hash: { jump: 'accept' }, should_hash: { jump: 'drop' }, result: false },
202202
] },
203203
{ testing: 'dport/sport', property_name: :dport, comparisons: [
204-
{ is_hash: { dport: '50' }, should_hash: { dport: '50' }, result: nil },
204+
{ is_hash: { dport: '50' }, should_hash: { dport: '50' }, result: true },
205205
{ is_hash: { dport: ['50:60'] }, should_hash: { dport: '50-60' }, result: nil },
206206
{ is_hash: { dport: ['50:60'] }, should_hash: { dport: ['50-60'] }, result: true },
207207
{ is_hash: { dport: ['! 50:60', '90'] }, should_hash: { dport: ['! 90', '! 50-60'] }, result: true },

0 commit comments

Comments
 (0)
Please sign in to comment.