Skip to content

Commit aa97e7b

Browse files
authored
Merge pull request #1238 from span786/CAT-2088-match-marks-with-mask
(CAT-2088): Add ability to set match_mark|connmark as a mark/mask value
2 parents 8b222cb + 4da76ff commit aa97e7b

File tree

7 files changed

+27
-42
lines changed

7 files changed

+27
-42
lines changed

REFERENCE.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,10 @@ Data type: `Optional[Integer]`
364364

365365
##### `connmark`
366366

367-
Data type: `Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+$/]]`
367+
Data type: `Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+(?:\/[a-fA-F0-9x]+)?$/]]`
368368
_*this data type contains a regex that may not be accurately reflected in generated documentation_
369369

370-
Match the Netfilter mark value associated with the packet, accepts a mark.
370+
Match the Netfilter mark value associated with the packet. Accepts either of mark/mask or mark.
371371
This value will be converted to hex if it is not already.
372372
This value can be negated by adding a space seperated `!` to the beginning.
373373

@@ -1039,10 +1039,10 @@ _*this data type contains a regex that may not be accurately reflected in genera
10391039

10401040
##### `match_mark`
10411041

1042-
Data type: `Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+$/]]`
1042+
Data type: `Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+(?:\/[a-fA-F0-9x]+)?$/]]`
10431043
_*this data type contains a regex that may not be accurately reflected in generated documentation_
10441044

1045-
Match the Netfilter mark value associated with the packet, accepts a mark.
1045+
Match the Netfilter mark value associated with the packet. Accepts either of mark/mask or mark.
10461046
This value will be converted to hex if it is not already.
10471047
This value can be negated by adding a space seperated `!` to the beginning.
10481048

lib/puppet/provider/firewall/firewall.rb

+3-8
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,11 @@ def insync?(context, _name, property_name, is_hash, should_hash)
394394
is = PuppetX::Firewall::Utility.log_level_name_to_number(is_hash[property_name])
395395
should = PuppetX::Firewall::Utility.log_level_name_to_number(should_hash[property_name])
396396
is == should
397-
when :set_mark
397+
when :set_mark, :match_mark, :connmark
398398
# Ensure that the values are compared to eachother in hexidecimal format
399399
is = PuppetX::Firewall::Utility.mark_mask_to_hex(is_hash[property_name])
400400
should = PuppetX::Firewall::Utility.mark_mask_to_hex(should_hash[property_name])
401401
is == should
402-
when :match_mark, :connmark
403-
# Ensure that the values are compared to eachother in hexidecimal format
404-
is = PuppetX::Firewall::Utility.mark_to_hex(is_hash[property_name])
405-
should = PuppetX::Firewall::Utility.mark_to_hex(should_hash[property_name])
406-
is == should
407402
when :time_start, :time_stop
408403
# Ensure the values are compared in full `00:00:00` format
409404
is = is_hash[property_name]
@@ -893,8 +888,8 @@ def self.process_input(should)
893888

894889
# `set_mark`, `match_mark` and `connmark` must be applied in hexidecimal format
895890
should[:set_mark] = PuppetX::Firewall::Utility.mark_mask_to_hex(should[:set_mark]) if should[:set_mark]
896-
should[:match_mark] = PuppetX::Firewall::Utility.mark_to_hex(should[:match_mark]) if should[:match_mark]
897-
should[:connmark] = PuppetX::Firewall::Utility.mark_to_hex(should[:connmark]) if should[:connmark]
891+
should[:match_mark] = PuppetX::Firewall::Utility.mark_mask_to_hex(should[:match_mark]) if should[:match_mark]
892+
should[:connmark] = PuppetX::Firewall::Utility.mark_mask_to_hex(should[:connmark]) if should[:connmark]
898893

899894
# `time_start` and `time_stop` must be applied in full HH:MM:SS format
900895
time = [:time_start, :time_stop]

lib/puppet/type/firewall.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -1278,9 +1278,9 @@
12781278
DESC
12791279
},
12801280
match_mark: {
1281-
type: 'Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+$/]]',
1281+
type: 'Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+(?:\/[a-fA-F0-9x]+)?$/]]',
12821282
desc: <<-DESC
1283-
Match the Netfilter mark value associated with the packet, accepts a mark.
1283+
Match the Netfilter mark value associated with the packet. Accepts either of mark/mask or mark.
12841284
This value will be converted to hex if it is not already.
12851285
This value can be negated by adding a space seperated `!` to the beginning.
12861286
DESC
@@ -1313,9 +1313,9 @@
13131313
DESC
13141314
},
13151315
connmark: {
1316-
type: 'Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+$/]]',
1316+
type: 'Optional[Pattern[/^(?:!\s)?[a-fA-F0-9x]+(?:\/[a-fA-F0-9x]+)?$/]]',
13171317
desc: <<-DESC
1318-
Match the Netfilter mark value associated with the packet, accepts a mark.
1318+
Match the Netfilter mark value associated with the packet. Accepts either of mark/mask or mark.
13191319
This value will be converted to hex if it is not already.
13201320
This value can be negated by adding a space seperated `!` to the beginning.
13211321
DESC

lib/puppet_x/puppetlabs/firewall/utility.rb

+8-17
Original file line numberDiff line numberDiff line change
@@ -216,25 +216,16 @@ def self.to_hex32(value)
216216

217217
# Accepts a valid mark or mark/mask and returns them in the valid
218218
# hexidecimal format.
219-
# USed for set_mark
219+
# Used for set_mark, match_mark, connmark
220220
def self.mark_mask_to_hex(value)
221-
match = value.to_s.match(%r{([a-fA-F0-9x]+)/?([a-fA-F0-9x]+)?})
222-
mark = PuppetX::Firewall::Utility.to_hex32(match[1])
223-
return "#{mark}/0xffffffff" if match[2].nil?
221+
match = value.to_s.match(%r{^(!\s)?([a-fA-F0-9x]+)\/?([a-fA-F0-9x]+)?})
222+
negation = '! '
223+
negation = '' if match[1].nil?
224+
mark = PuppetX::Firewall::Utility.to_hex32(match[2])
225+
return "#{negation}#{mark}/0xffffffff" if match[3].nil?
224226

225-
mask = PuppetX::Firewall::Utility.to_hex32(match[2])
226-
"#{mark}/#{mask}"
227-
end
228-
229-
# Accepts a valid mark and returns them in the valid hexidecimal format.
230-
# Accounts for negation.
231-
# Used for match_mark / connmark
232-
def self.mark_to_hex(value)
233-
match = value.to_s.match(%r{^(!\s)?([a-fA-F0-9x]+)})
234-
mask = PuppetX::Firewall::Utility.to_hex32(match[2])
235-
return mask if match[1].nil?
236-
237-
"! #{mask}"
227+
mask = PuppetX::Firewall::Utility.to_hex32(match[3])
228+
"#{negation}#{mark}/#{mask}"
238229
end
239230

240231
# Converts a given number to its protocol keyword

spec/acceptance/firewall_attributes_exceptions_spec.rb

+6
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,11 @@ class { '::firewall': }
11631163
match_mark => '0x1',
11641164
jump => reject,
11651165
}
1166+
firewall { '504 match_mark - test with mask':
1167+
proto => 'all',
1168+
match_mark => '0x1/0x2000',
1169+
jump => reject,
1170+
}
11661171
PUPPETCODE
11671172
it 'applies' do
11681173
apply_manifest(pp1, catch_failures: true)
@@ -1171,6 +1176,7 @@ class { '::firewall': }
11711176
it 'contains the rule' do
11721177
run_shell('iptables-save') do |r|
11731178
expect(r.stdout).to match(%r{-A INPUT -m mark --mark 0x1 -m comment --comment "503 match_mark - test" -j REJECT --reject-with icmp-port-unreachable})
1179+
expect(r.stdout).to match(%r{-A INPUT -m mark --mark 0x1/0x2000 -m comment --comment "504 match_mark - test with mask" -j REJECT --reject-with icmp-port-unreachable})
11741180
end
11751181
end
11761182
end

spec/unit/puppet/provider/firewall/firewall_private_set_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@
302302
result: { log_level: '1' }
303303
},
304304
{
305-
process: '`set_mark`, `match_mark` and `connmark` must be put through mark_mask_to_hex/mark_to_hex',
305+
process: '`set_mark`, `match_mark` and `connmark` must be put through mark_mask_to_hex',
306306
should: { set_mark: '42', match_mark: '42', connmark: '42' },
307-
result: { set_mark: '0x2a/0xffffffff', match_mark: '0x2a', connmark: '0x2a' }
307+
result: { set_mark: '0x2a/0xffffffff', match_mark: '0x2a/0xffffffff', connmark: '0x2a/0xffffffff' }
308308
},
309309
{
310310
process: '`time_start` and `time_stop` must be applied in full HH:MM:SS format',

spec/unit/puppet_x/puppetlabs/firewall/utility_spec.rb

-7
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,6 @@
192192
it { expect(utility.mark_mask_to_hex('4294967295/42')).to eql '0xffffffff/0x2a' }
193193
end
194194

195-
describe '#mark_to_hex' do
196-
it { expect(utility.mark_to_hex('0')).to eql '0x0' }
197-
it { expect(utility.mark_to_hex('! 0x32')).to eql '! 0x32' }
198-
it { expect(utility.mark_to_hex('42')).to eql '0x2a' }
199-
it { expect(utility.mark_to_hex('! 4294967295')).to eql '! 0xffffffff' }
200-
end
201-
202195
describe '#proto_number_to_name' do
203196
it { expect(utility.proto_number_to_name('1')).to eql 'icmp' }
204197
it { expect(utility.proto_number_to_name('2')).to eql 'igmp' }

0 commit comments

Comments
 (0)