Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support specifying priority on rich rules #296

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/puppet/provider/firewalld_rich_rule/firewall_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def key_val_opt(opt, resource_param = opt)
quote_keyval(opt, @resource[resource_param.to_s])
end

def eval_priority
return [] unless (priority = @resource[:priority])
quote_keyval('priority', priority)
end

def eval_source
args = []
return [] unless (addr = @resource[:source])
Expand Down Expand Up @@ -112,6 +117,7 @@ def build_rich_rule
rule = ['rule']
rule << [
key_val_opt('family'),
eval_priority,
eval_source,
eval_dest,
eval_element,
Expand Down
9 changes: 9 additions & 0 deletions lib/puppet/type/firewalld_rich_rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
munge(&:to_s)
end

newparam(:priority) do
desc 'Rule priority, it can be in the range of -32768 to 32767'
ananace marked this conversation as resolved.
Show resolved Hide resolved
munge(&:to_s)

validate do |value|
raise Puppet::Error, 'Priority must be between -32768 and 32767' unless value.to_i.to_s == value.to_s && (-32768..32767).include?(value.to_i)
end
end

newparam(:source) do
desc 'Specify source address, this can be a string of the IP address or a hash containing other options'
munge do |value|
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/puppet/provider/firewalld_rich_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
describe 'when creating' do
context 'with basic parameters' do
it 'builds the rich rule' do
resource.expects(:[]).with(:priority).returns(nil)
resource.expects(:[]).with(:source).returns('192.168.1.2/32').at_least_once
resource.expects(:[]).with(:service).returns('ssh').at_least_once
resource.expects(:[]).with('family').returns('ipv4').at_least_once
Expand All @@ -45,6 +46,7 @@
end
context 'with reject type' do
it 'builds the rich rule' do
resource.expects(:[]).with(:priority).returns(nil)
resource.expects(:[]).with(:source).returns(nil).at_least_once
resource.expects(:[]).with(:service).returns('ssh').at_least_once
resource.expects(:[]).with('family').returns('ipv4').at_least_once
Expand All @@ -62,5 +64,25 @@
expect(provider.build_rich_rule).to eq('rule family="ipv4" destination address="192.168.0.1/32" service name="ssh" reject type="icmp-admin-prohibited"')
end
end
context 'with priority' do
it 'builds the rich rule' do
resource.expects(:[]).with(:priority).returns(1200)
resource.expects(:[]).with(:source).returns(nil).at_least_once
resource.expects(:[]).with(:service).returns('ssh').at_least_once
resource.expects(:[]).with('family').returns('ipv4').at_least_once
resource.expects(:[]).with(:dest).returns('address' => '192.168.0.1/32')
resource.expects(:[]).with(:port).returns(nil)
resource.expects(:[]).with(:protocol).returns(nil)
resource.expects(:[]).with(:icmp_block).returns(nil)
resource.expects(:[]).with(:icmp_type).returns(nil)
resource.expects(:[]).with(:masquerade).returns(nil)
resource.expects(:[]).with(:forward_port).returns(nil)
resource.expects(:[]).with(:log).returns(nil)
resource.expects(:[]).with(:audit).returns(nil)
resource.expects(:[]).with(:raw_rule).returns(nil)
resource.expects(:[]).with(:action).returns(action: 'reject', type: 'icmp-admin-prohibited')
expect(provider.build_rich_rule).to eq('rule family="ipv4" priority="1200" destination address="192.168.0.1/32" service name="ssh" reject type="icmp-admin-prohibited"')
end
end
end
end
35 changes: 35 additions & 0 deletions spec/unit/puppet/type/firewalld_rich_rule_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,41 @@
end
end

describe 'priority validation' do
it 'raises an error if invalid priority' do
expect do
described_class.new(
title: 'SSH from barny',
priority: 'none'
)
end.to raise_error(%r{Priority must be between -32768 and 32767})
end
it 'raises an error if too low priority' do
expect do
described_class.new(
title: 'SSH from barny',
priority: -32769
)
end.to raise_error(%r{Priority must be between -32768 and 32767})
end
it 'raises an error if too high priority' do
expect do
described_class.new(
title: 'SSH from barny',
priority: 32768
)
end.to raise_error(%r{Priority must be between -32768 and 32767})
end
it 'does not raises an error if priority is valid' do
expect do
described_class.new(
title: 'SSH from barny',
priority: 10
)
end.not_to raise_error()
end
end

## Many more scenarios needed!
#
describe 'provider' do
Expand Down