Class: Y2Firewall::Firewalld::Service

Inherits:
Object
  • Object
show all
Extended by:
Relations, Yast::I18n
Includes:
Yast::I18n
Defined in:
library/network/src/lib/y2firewall/firewalld/service.rb

Overview

Class to work with Firewalld services

@example

ha = firewalld.find_service("high-availability") ha.ports # => ["2224/tcp", "3121/tcp", "5403/tcp", "5404/udp", "5405/udp", "21064/tcp"]

ha.tcp_ports #=> ["2224", "3121", "5403", "21064"] ha.udp_ports #=> ["5404", "5405"]

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relations

enable_modifications_cache, has_attributes, has_many

Constructor Details

#initialize(name:) ⇒ Service

Constructor

Parameters:

  • name (String)

    zone name



82
83
84
85
86
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 82

def initialize(name:)
  @name = name
  @ports = []
  @protocols = []
end

Instance Attribute Details

#nameString (readonly)

Returns service name.

Returns:

  • (String)

    service name



51
52
53
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 51

def name
  @name
end

Class Method Details

.modify_ports(name:, tcp_ports: [], udp_ports: []) ⇒ Boolean

Convenience method for setting the tcp and udp ports of a given service. If the service is found, it modify the ports according to the given parameters applying the changes at the end.

Examples:

Y2Firewall::Firewalld::Service.modify_ports("apach", tcp_ports:
["80", "8080"]) #=> Y2Firewall::Firewalld::Service::NotFound

Y2Firewall::Firewalld::Service.modify_ports("apache2", tcp_ports:
["80", "8080"]) #=> true

Parameters:

  • name (String)

    service name

  • tcp_ports (Array<String>) (defaults to: [])

    tcp ports to be opened by the service

  • udp_ports (Array<String>) (defaults to: [])

    udp ports to be opened by the service

Returns:

  • (Boolean)

    true if modified; false otherwise



71
72
73
74
75
76
77
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 71

def self.modify_ports(name:, tcp_ports: [], udp_ports: [])
  return false unless Firewalld.instance.installed?

  service = Firewalld.instance.find_service(name)
  service.ports = tcp_ports.map { |p| "#{p}/tcp" } + udp_ports.map { |p| "#{p}/udp" }
  service.apply_changes!
end

Instance Method Details

#apply_changes!Boolean

Apply the changes done since read in firewalld

Returns:

  • (Boolean)

    true if applied; false otherwise



115
116
117
118
119
120
121
122
123
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 115

def apply_changes!
  return true if !modified?
  return false if !supported?

  apply_attributes_changes!
  apply_relations_changes!
  untouched!
  true
end

#create!Object

Create the service in firewalld



89
90
91
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 89

def create!
  api.create_service(name)
end

#readBoolean

Read the firewalld configuration initializing the object accordingly

Returns:

  • (Boolean)

    true if read



103
104
105
106
107
108
109
110
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 103

def read
  return false unless supported?

  read_attributes
  read_relations
  untouched!
  true
end

#supported?Boolean

Return whether the service is available in firewalld or not

Returns:

  • (Boolean)

    true if defined; false otherwise



96
97
98
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 96

def supported?
  api.service_supported?(name)
end

#tcp_portsArray<String>

Convenience method to select only the service tcp ports

Returns:

  • (Array<String>)

    array with the service tcp ports



128
129
130
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 128

def tcp_ports
  ports.select { |p| p.include?("tcp") }.map { |p| p.sub("/tcp", "") }
end

#udp_portsArray<String>

Convenience method to select only the service udp ports

Returns:

  • (Array<String>)

    array with the service udp ports



135
136
137
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 135

def udp_ports
  ports.select { |p| p.include?("udp") }.map { |p| p.sub("/udp", "") }
end