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

Add the parameter protocols to the firewalld_zone resource type #357

Merged
merged 1 commit into from
Oct 30, 2023
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ firewalld::zones:
* `target`: Specify the target of the zone.
* `interfaces`: An array of interfaces for this zone
* `sources`: An array of sources for the zone
* `protocols`: An array of protocols for the zone
* `icmp_blocks`: An array of ICMP blocks for the zone
* `masquerade`: If set to `true` or `false` specifies whether or not
to add masquerading to the zone
Expand Down
4 changes: 4 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,10 @@ Valid values: `true`, `false`

Can be set to true or false, specifies whether to add or remove masquerading from the zone

##### `protocols`

Specify the protocols for the zone

##### `purge_ports`

Valid values: `false`, `true`
Expand Down
18 changes: 18 additions & 0 deletions lib/puppet/provider/firewalld_zone/firewall_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def create

self.target = (@resource[:target]) if @resource[:target]
self.sources = (@resource[:sources]) if @resource[:sources]
self.protocols = (@resource[:protocols]) if @resource[:protocols]
self.interfaces = @resource[:interfaces]
self.icmp_blocks = (@resource[:icmp_blocks]) if @resource[:icmp_blocks]
self.icmp_block_inversion = (@resource[:icmp_block_inversion]) if @resource[:icmp_block_inversion]
Expand Down Expand Up @@ -82,6 +83,23 @@ def sources=(new_sources)
end
end

def protocols
execute_firewall_cmd(['--list-protocols']).chomp.split.sort || []
end

def protocols=(new_protocols)
new_protocols ||= []
cur_protocols = protocols
(new_protocols - cur_protocols).each do |p|
debug("Adding protocol '#{p}' to zone #{@resource[:name]}")
execute_firewall_cmd(['--add-protocol', p])
end
(cur_protocols - new_protocols).each do |p|
debug("Removing protocol '#{p}' from zone #{@resource[:name]}")
execute_firewall_cmd(['--remove-protocol', p])
end
end

def masquerade
if execute_firewall_cmd(['--query-masquerade'], @resource[:name], true, false).chomp == 'yes'
:true
Expand Down
20 changes: 20 additions & 0 deletions lib/puppet/type/firewalld_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ def should_to_s(value = [])
end
end

newproperty(:protocols, array_matching: :all) do
desc 'Specify the protocols for the zone'

def insync?(is)
case should
when String then should.lines.sort == is.sort
when Array then should.sort == is.sort
else raise Puppet::Error, 'parameter protocols must be a string or array of strings!'
end
end

def is_to_s(value = [])
"[#{value.join(', ')}]"
end

def should_to_s(value = [])
"[#{value.join(', ')}]"
end
end

newproperty(:icmp_blocks, array_matching: :all) do
desc "Specify the icmp-blocks for the zone. Can be a single string specifying one icmp type,
or an array of strings specifying multiple icmp types. Any blocks not specified here will be removed
Expand Down
2 changes: 2 additions & 0 deletions spec/unit/puppet/provider/firewalld_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
resource.expects(:[]).with(:name).returns('white').at_least_once
resource.expects(:[]).with(:target).returns(nil).at_least_once
resource.expects(:[]).with(:sources).returns(nil).at_least_once
resource.expects(:[]).with(:protocols).returns(nil).at_least_once
resource.expects(:[]).with(:interfaces).returns(['eth0']).at_least_once
resource.expects(:[]).with(:icmp_blocks).returns(nil).at_least_once
resource.expects(:[]).with(:icmp_block_inversion).returns(false).at_least_once
Expand All @@ -49,6 +50,7 @@
resource.expects(:[]).with(:name).returns('white').at_least_once
resource.expects(:[]).with(:target).returns(nil).at_least_once
resource.expects(:[]).with(:sources).returns(nil).at_least_once
resource.expects(:[]).with(:protocols).returns(nil).at_least_once
resource.expects(:[]).with(:interfaces).returns(['eth0']).at_least_once
resource.expects(:[]).with(:icmp_blocks).returns(nil).at_least_once
resource.expects(:[]).with(:icmp_block_inversion).returns(false).at_least_once
Expand Down
19 changes: 18 additions & 1 deletion spec/unit/puppet/type/firewalld_zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
end
end

%i[target icmp_blocks icmp_block_inversion sources purge_rich_rules purge_services purge_ports].each do |param|
%i[target icmp_blocks icmp_block_inversion sources protocols purge_rich_rules purge_services purge_ports].each do |param|
it "has a #{param} parameter" do
expect(described_class.attrtype(param)).to eq(:property)
end
Expand Down Expand Up @@ -143,6 +143,7 @@
interfaces: ['eth0'],
icmp_blocks: %w[redirect router-advertisment],
icmp_block_inversion: true,
protocols: %w[icmp igmp],
sources: ['192.168.2.2', '10.72.1.100']
)
end
Expand Down Expand Up @@ -186,6 +187,10 @@
provider.expects(:execute_firewall_cmd).with(['--add-source', '192.168.2.2'])
provider.expects(:execute_firewall_cmd).with(['--add-source', '10.72.1.100'])

provider.expects(:protocols).returns([])
provider.expects(:execute_firewall_cmd).with(['--add-protocol', 'icmp'])
provider.expects(:execute_firewall_cmd).with(['--add-protocol', 'igmp'])

provider.expects(:interfaces).returns([])
provider.expects(:execute_firewall_cmd).with(['--add-interface', 'eth0'])
provider.create
Expand Down Expand Up @@ -240,6 +245,18 @@
expect(provider.get_icmp_types).to eq(%w[echo-reply echo-request])
end

it 'gets protocols' do
provider.expects(:execute_firewall_cmd).with(['--list-protocols']).returns('val val')
expect(provider.protocols).to eq(%w[val val])
end

it 'sets protocols' do
provider.expects(:protocols).returns(['valx'])
provider.expects(:execute_firewall_cmd).with(['--add-protocol', 'valy'])
provider.expects(:execute_firewall_cmd).with(['--remove-protocol', 'valx'])
provider.protocols = ['valy']
end

it 'gets icmp_blocks' do
provider.expects(:execute_firewall_cmd).with(['--list-icmp-blocks'], 'restricted').returns('redirect router-advertisement')
expect(provider.icmp_blocks).to eq(%w[redirect router-advertisement])
Expand Down