Class: Y2Firewall::Firewalld::Zone

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

Overview

Class to work with Firewalld zones

Constant Summary collapse

KNOWN_ZONES =

Map of known zone names and description

{
  "block"    => N_("Block Zone"),
  "dmz"      => N_("Demilitarized Zone"),
  "drop"     => N_("Drop Zone"),
  "external" => N_("External Zone"),
  "home"     => N_("Home Zone"),
  "internal" => N_("Internal Zone"),
  "public"   => N_("Public Zone"),
  "trusted"  => N_("Trusted Zone"),
  "work"     => N_("Work Zone")
}.freeze

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: nil) ⇒ Zone

Constructor

If a :name is given it is used as the zone name. Otherwise, the default zone name will be used as fallback.



64
65
66
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 64

def initialize(name: nil)
  @name = name || api.default_zone
end

Instance Attribute Details

#masqueradeBoolean Also known as: masquerade?



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

def masquerade
  @masquerade
end

#nameString (readonly)



48
49
50
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 48

def name
  @name
end

Class Method Details

.known_zonesObject



68
69
70
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 68

def self.known_zones
  KNOWN_ZONES
end

Instance Method Details

#add_interface!(interface) ⇒ Object

Override relation method to be more defensive. An interface can only belong to one zone and the change method remove it before add.



146
147
148
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 146

def add_interface!(interface)
  api.change_interface(name, interface)
end

#add_source!(source) ⇒ Object

Override relation method to be more defensive. A source can only belong to one zone and the change method remove it before add.



154
155
156
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 154

def add_source!(source)
  api.change_source(name, source)
end

#apply_changes!Object

Apply all the changes in firewalld but do not reload it



91
92
93
94
95
96
97
98
99
100
101
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 91

def apply_changes!
  return true unless modified?

  apply_relations_changes!
  if modified?(:masquerade)
    masquerade? ? api.add_masquerade(name) : api.remove_masquerade(name)
  end
  untouched!

  true
end

#exportHash

Dump a hash with the zone configuration



130
131
132
133
134
135
136
137
138
139
140
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 130

def export
  {
    "name"       => name,
    "interfaces" => interfaces,
    "services"   => services,
    "ports"      => ports,
    "protocols"  => protocols,
    "sources"    => sources,
    "masquerade" => masquerade
  }
end

#full_nameString

Known full name of the known zones. Usefull when the API is not accessible or when make sense to not call it directly to obtain the full name.



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

def full_name
  self.class.known_zones[name]
end

#readObject

Read and modify the state of the object with the current firewalld configuration for this zone.



110
111
112
113
114
115
116
117
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 110

def read
  return unless firewalld.installed?
  read_relations
  @masquerade = api.masquerade_enabled?(name)
  untouched!

  true
end

#reload!Object

Convenience method wich reload changes applied to firewalld



104
105
106
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 104

def reload!
  api.reload
end

#service_open?(service) ⇒ Boolean

Return whether a service is present in the list of services or not



123
124
125
# File 'library/network/src/lib/y2firewall/firewalld/zone.rb', line 123

def service_open?(service)
  services.include?(service)
end