Skip to content

Commit

Permalink
rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-vaughan committed Mar 28, 2020
1 parent eb6c81f commit 0843da2
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 156 deletions.
42 changes: 20 additions & 22 deletions lib/puppet/provider/firewalld_custom_service/firewall_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create

service_file = Tempfile.new(['puppet_firewalld_custom_service', '.xml'])
begin
service_file.puts(generate_service_xml())
service_file.puts(generate_service_xml)
service_file.flush
service_file.close

Expand All @@ -41,18 +41,18 @@ def description
end

def ports
execute_firewall_cmd(['--service', @resource[:name], '--get-ports'], nil).strip.split(/\s+/).map{ |entry|
execute_firewall_cmd(['--service', @resource[:name], '--get-ports'], nil).strip.split(%r{\s+}).map do |entry|
port, proto = entry.strip.split('/')
{'port' => port, 'protocol' => proto}
}
{ 'port' => port, 'protocol' => proto }
end
end

def protocols
execute_firewall_cmd(['--service', @resource[:name], '--get-protocols'], nil).strip.split(/\s+/)
execute_firewall_cmd(['--service', @resource[:name], '--get-protocols'], nil).strip.split(%r{\s+})
end

def modules
execute_firewall_cmd(['--service', @resource[:name], '--get-modules'], nil).strip.split(/\s+/)
execute_firewall_cmd(['--service', @resource[:name], '--get-modules'], nil).strip.split(%r{\s+})
end

def ipv4_destination
Expand All @@ -64,10 +64,8 @@ def ipv6_destination
end

def flush
unless @property_hash[:destroyed] || @property_hash[:created]
delete_service(false)
create
end
# Rubocop, you crazy
(delete_service(false) && create) unless @property_hash[:destroyed] || @property_hash[:created]
end

private
Expand All @@ -78,7 +76,7 @@ def flush
# Reload the firewall after deleting the service
#
# @return [nil]
def delete_service(reload=true)
def delete_service(reload = true)
debug("Removing custom service from firewalld: #{@resource[:name]}")

execute_firewall_cmd(['--delete-service', @resource[:name]], nil)
Expand All @@ -96,10 +94,10 @@ def delete_service(reload=true)
def destinations
return @destinations if @destinations

@destinations = execute_firewall_cmd(['--service', @resource[:name], '--get-destinations'], nil).strip.split(/\s+/)
@destinations = Hash[@destinations.map{|x| x.split(':',2)}]
@destinations = execute_firewall_cmd(['--service', @resource[:name], '--get-destinations'], nil).strip.split(%r{\s+})
@destinations = Hash[@destinations.map { |x| x.split(':', 2) }]

return @destinations
@destinations
end

# Generate the full version of the new XML service file
Expand All @@ -113,25 +111,25 @@ def generate_service_xml
xml_content << " <description>#{@resource[:description]}</description>" if @resource[:description]

Array(@resource[:ports]).each do |entry|
xml_content << %Q{ <port port="#{entry['port']}" protocol="#{entry['protocol']}"/>} if entry['port']
xml_content << %( <port port="#{entry['port']}" protocol="#{entry['protocol']}"/>) if entry['port']
end

protocols = Array(@resource[:protocols]) + Array(@resource[:ports]).select{|x| x['port'].nil?}.map{|x| x['protocol']}
protocols = Array(@resource[:protocols]) + Array(@resource[:ports]).select { |x| x['port'].nil? }.map { |x| x['protocol'] }

protocols.each do |protocol|
xml_content << %Q{ <protocol value="#{protocol}"/>}
xml_content << %( <protocol value="#{protocol}"/>)
end

Array(@resource[:modules]).each do |mod|
xml_content << %Q{ <module name="#{mod}"/>}
xml_content << %( <module name="#{mod}"/>)
end

destination = []
destination << %Q{ipv4="#{@resource[:ipv4_destination]}"} if @resource[:ipv4_destination]
destination << %Q{ipv6="#{@resource[:ipv6_destination]}"} if @resource[:ipv6_destination]
xml_content << %Q{ <destination #{destination.join(' ')}/>} unless destination.empty?
destination << %(ipv4="#{@resource[:ipv4_destination]}") if @resource[:ipv4_destination]
destination << %(ipv6="#{@resource[:ipv6_destination]}") if @resource[:ipv6_destination]
xml_content << %( <destination #{destination.join(' ')}/>) unless destination.empty?
xml_content << '</service>'

return xml_content.join("\n")
xml_content.join("\n")
end
end
44 changes: 22 additions & 22 deletions lib/puppet/type/firewalld_custom_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,40 @@
newparam(:name, namevar: :true) do
desc 'The "short" name of the resource'

newvalues(/.+/)
newvalues(%r{.+})

munge do |value|
value = value.gsub(/[^\w-]/, '_')
value.gsub(%r{[^\w-]}, '_')
end
end

newproperty(:description) do
desc 'The long description of the service'

newvalues(/.+/)
newvalues(%r{.+})
end

newproperty(:ports, :array_matching => :all) do
newproperty(:ports, array_matching: :all) do
desc 'An Array of allowed port/protocol Hashes or Strings of the form `port/protocol`'

munge do |value|
if value.is_a?(Hash)
value = value
else
port,protocol = value.split('/')
port, protocol = value.split('/')

# Just a protocol is valid
if port && !protocol
value = {'protocol' => port}
value = { 'protocol' => port }
else
# Handle the legacy format from the module
port.gsub!(':','-')
port.tr!(':', '-')

value = {'port' => port, 'protocol' => protocol}
value = { 'port' => port, 'protocol' => protocol }
end
end

value
value
end

validate do |value|
Expand All @@ -70,41 +70,41 @@
raise Puppet::ParseError, "Ports must match #{port_regexp}" unless port_regexp.match?(value['port'].to_s)
end

allowed_protocols = ['tcp','udp','sctp','dccp']
allowed_protocols = %w[tcp udp sctp dccp]
unless allowed_protocols.include?(value['protocol'])
raise Puppet::ParseError, "The protocol must be one of '#{allowed_protocols.join(', ')}'. Got '#{value['protocol']}'"
end
end
end

def insync?(is)
is.select{|x| !x['port'].nil?}.sort_by{|x| x['port'].to_s} ==
@should.select{|x| !x['port'].nil?}.sort_by{|x| x['port'].to_s}
is.reject { |x| x['port'].nil? }.sort_by { |x| x['port'].to_s } ==
@should.reject { |x| x['port'].nil? }.sort_by { |x| x['port'].to_s }
end
end

newproperty(:protocols, :array_matching => :all) do
newproperty(:protocols, array_matching: :all) do
desc 'Protocols allowed by the service as defined in /etc/protocols'

newvalues(/^[^\s#]+$/)
newvalues(%r{^[^\s#]+$})

def insync?(is)
protocols = (
Array(should) +
@resource[:ports].select{|x| x['port'].nil?}.map{|x| x['protocol']}
).uniq
protocols = (
Array(should) +
@resource[:ports].select { |x| x['port'].nil? }.map { |x| x['protocol'] }
).uniq

protocols.sort == Array(is).sort
protocols.sort == Array(is).sort
end
end

newproperty(:modules, :array_matching => :all) do
newproperty(:modules, array_matching: :all) do
desc 'The list of netfilter modules to add to the service'

newvalues(/^[\w-]+$/)
newvalues(%r{^[\w-]+$})

munge do |value|
value = value.split('nf_conntrack_').last
value.split('nf_conntrack_').last
end

def insync?(is)
Expand Down
4 changes: 1 addition & 3 deletions lib/puppet/type/firewalld_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
end

autorequire(:firewalld_custom_service) do
if self[:service]
self[:service].gsub(/[^\w-]/, '_')
end
self[:service].gsub(%r{[^\w-]}, '_') if self[:service]
end
end
30 changes: 12 additions & 18 deletions spec/defines/custom_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@
end

it do
is_expected.to contain_firewalld_custom_service(params[:short]).with({
description: params[:description],
ports: params[:port],
modules: params[:module],
ipv4_destination: params[:destination]['ipv4'],
ipv6_destination: params[:destination]['ipv6'],
})
is_expected.to contain_firewalld_custom_service(params[:short]).with(description: params[:description],
ports: params[:port],
modules: params[:module],
ipv4_destination: params[:destination]['ipv4'],
ipv6_destination: params[:destination]['ipv6'])
end
end

Expand Down Expand Up @@ -103,13 +101,11 @@
end

it do
is_expected.to contain_firewalld_custom_service(params[:short]).with({
description: params[:description],
ports: params[:port],
modules: params[:module],
ipv4_destination: params[:destination]['ipv4'],
ipv6_destination: params[:destination]['ipv6'],
})
is_expected.to contain_firewalld_custom_service(params[:short]).with(description: params[:description],
ports: params[:port],
modules: params[:module],
ipv4_destination: params[:destination]['ipv4'],
ipv6_destination: params[:destination]['ipv6'])
end
end

Expand All @@ -132,10 +128,8 @@
end

it do
is_expected.to contain_firewalld_custom_service(params[:short]).with({
description: params[:description],
ports: params[:port],
})
is_expected.to contain_firewalld_custom_service(params[:short]).with(description: params[:description],
ports: params[:port])
end
end
end
Loading

0 comments on commit 0843da2

Please sign in to comment.