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_many

Constructor Details

#initialize(name:) ⇒ Service

Constructor

Parameters:

  • name (String)

    zone name



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

def initialize(name:)
  @name = name
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description



57
58
59
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 57

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#shortObject (readonly)

Returns the value of attribute short



55
56
57
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 55

def short
  @short
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>)

    tcp ports to be opened by the service

  • udp_ports (Array<String>)

    udp ports to be opened by the service

Returns:

  • (Boolean)

    true if modified; false otherwise



76
77
78
79
80
81
82
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 76

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



119
120
121
122
123
124
125
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 119

def apply_changes!
  return false if !supported? || !modified?
  apply_protocols_changes!
  apply_ports_changes!
  untouched!
  true
end

#create!Object

Create the service in firewalld



92
93
94
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 92

def create!
  api.add_service(name)
end

#readBoolean

Read the firewalld configuration initializing the object accordingly

Returns:

  • (Boolean)

    true if read



106
107
108
109
110
111
112
113
114
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 106

def read
  return false unless supported?
  @short        = api.service_short(name)
  @description  = api.service_description(name)
  @protocols    = current_protocols
  @ports        = current_ports
  untouched!
  true
end

#supported?Boolean

Return whether the service is available in firewalld or not

Returns:

  • (Boolean)

    true if defined; false otherwise



99
100
101
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 99

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



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

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



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

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