Skip to content

Commit

Permalink
Add the parameter protocols to the firewalld_zone resource type
Browse files Browse the repository at this point in the history
Rebased off of : #327
  • Loading branch information
jcpunk committed Oct 30, 2023
1 parent e69f796 commit 0d484c7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
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 || []

Check failure on line 87 in lib/puppet/provider/firewalld_zone/firewall_cmd.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/RedundantArgument: Argument ' ' is redundant because it is implied by default.
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]}")

Check failure on line 98 in lib/puppet/provider/firewalld_zone/firewall_cmd.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Layout/IndentationWidth: Use 2 (not 0) spaces for indentation. (https://rubystyle.guide#spaces-indentation)
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 = []) # rubocop:disable Style/PredicateName

Check failure on line 128 in lib/puppet/type/firewalld_zone.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Lint/RedundantCopDisableDirective: Unnecessary disabling of `Naming/PredicateName`.
'[' + value.join(', ') + ']'

Check failure on line 129 in lib/puppet/type/firewalld_zone.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringConcatenation: Prefer string interpolation to string concatenation. (https://rubystyle.guide#string-interpolation)
end

def should_to_s(value = [])
'[' + value.join(', ') + ']'

Check failure on line 133 in lib/puppet/type/firewalld_zone.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/StringConcatenation: Prefer string interpolation to string concatenation. (https://rubystyle.guide#string-interpolation)
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: ['icmp', 'igmp'],

Check failure on line 146 in spec/unit/puppet/type/firewalld_zone_spec.rb

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

Style/WordArray: Use `%w` or `%W` for an array of words. (https://rubystyle.guide#percent-w)
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

0 comments on commit 0d484c7

Please sign in to comment.