Skip to content

Commit

Permalink
Added firewall_zone widget
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 5, 2019
1 parent 35d3310 commit 3fc267c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 38 deletions.
1 change: 0 additions & 1 deletion src/data/network/sysconfig_defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@ BONDING_MODULE_OPTS: mode=active-backup miimon=100
TUNNEL_SET_OWNER: ''
TUNNEL_SET_GROUP: ''
BRIDGE_PORTS: ''
ZONE: ''
47 changes: 10 additions & 37 deletions src/include/network/lan/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#
require "ui/text_helpers"
require "y2firewall/helpers/interfaces"
require "y2network/widgets/firewall_zone"

module Yast
module NetworkLanAddressInclude
Expand Down Expand Up @@ -132,14 +133,7 @@ def initialize_network_lan_address(include_target)
"opt" => [:hstretch],
"help" => _("<p>TODO kind of vague!</p>")
},
"FWZONE" => {
"widget" => :combobox,
# Combo Box label
"label" => _("Assign Interface to Firewall &Zone"),
"opt" => [:hstretch],
"help" => Ops.get_string(@help, "fwzone", ""),
"init" => fun_ref(method(:InitFwZone), "void (string)")
},
"FWZONE" => firewall_zone.cwm_definition,
"MANDATORY" => {
"widget" => :checkbox,
# check box label
Expand Down Expand Up @@ -1046,31 +1040,6 @@ def ValidateBootproto(_key, _event)
true
end

# Initialize value of firewall zone widget
# (disables it when firewalld is not installed)
# @param _key [String] id of the widget
def InitFwZone(_key)
if firewalld.installed?
UI.ChangeWidget(
Id("FWZONE"),
:Value,
current_zone
)
else
UI.ChangeWidget(Id("FWZONE"), :Enabled, false)
end

nil
end

def current_zone
ifcfg_zone = NetworkInterfaces.Current["ZONE"]
return ifcfg_zone if ifcfg_zone
zone = interface_zone(LanItems.device)
return if zone.nil?
zone.name
end

# @param [Array<String>] types network card types
# @return their descriptions for CWM
def BuildTypesListCWM(types)
Expand Down Expand Up @@ -1288,8 +1257,6 @@ def AddressDialog
]
)

wd["FWZONE"]["items"] = firewall_zones

if LanItems.GetCurrentType == "ib"
wd["IPOIB_MODE"] = ipoib_mode_widget
wd["MTU"]["items"] = ipoib_mtu_items
Expand All @@ -1298,6 +1265,7 @@ def AddressDialog
end

@settings["IFCFG"] = LanItems.device if LanItems.operation != :add
firewall_zone.value = @settings["FWZONE"]

functions = {
"init" => fun_ref(method(:InitAddrWidget), "void (string)"),
Expand Down Expand Up @@ -1366,8 +1334,8 @@ def AddressDialog
if ret != :back && ret != :abort
# general tab
LanItems.startmode = Ops.get_string(@settings, "STARTMODE", "")
LanItems.firewall_zone = @settings.fetch("FWZONE", "")
LanItems.mtu = Ops.get_string(@settings, "MTU", "")
LanItems.firewall_zone = firewall_zone.value

# address tab
bootproto = @settings.fetch("BOOTPROTO", "")
Expand Down Expand Up @@ -1435,8 +1403,8 @@ def initialize_address_settings
"STARTMODE" => LanItems.startmode,
"IFPLUGD_PRIORITY" => LanItems.ifplugd_priority,
# problems when renaming the interface?
"FWZONE" => current_zone,
"MTU" => LanItems.mtu,
"FWZONE" => LanItems.firewall_zone,
# address tab:
"BOOTPROTO" => LanItems.bootproto,
"IPADDR" => LanItems.ipaddr,
Expand Down Expand Up @@ -1558,13 +1526,18 @@ def initial_hostname(ipaddr)
String.FirstChunk(Ops.get(host_list, 0, ""), " \t")
end

def firewall_zone
@fw_zone ||= Y2Network::Widgets::FirewallZone.new
end

# Return a list of items for ComboBox with all the known firewalld zones
# and also an empty string option for the default zone.
#
# @return [Array <Array <String, String>>] list of names an description of
# known zones
def firewall_zones
zones = [["", _("Automatically Assigned Zone")]]

if firewalld.installed?
firewalld.zones.each { |z| zones << [z.name, z.short] }
else
Expand Down
99 changes: 99 additions & 0 deletions src/lib/y2network/widgets/firewall_zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
require "cwm"
require "y2firewall/firewalld"
require "y2firewall/helpers/interfaces"

module Y2Network
module Widgets
class FirewallZone < ::CWM::CustomWidget
include Y2Firewall::Helpers::Interfaces

def initialize
textdomain "network"
@value = nil
end

def label
_("Assign Interface to Firewall &Zone")
end

def init
Yast::UI.ChangeWidget(Id(:zones), :Items, firewall_zones)
self.value = @value
enable_zones(managed?)
end

def contents
VBox(
Left(manage_widget),
Left(zones_widget)
)
end

def handle(event)
enable_zones(managed?) if event["ID"] == :manage_zone

nil
end

def value=(name)
@value = name
Yast::UI.ChangeWidget(Id(:manage_zone), :Value, !!name)
return if name.nil?
select_zone(name)
end

def value
return @value unless Yast::UI.WidgetExists(Id(:manage_zone))

managed? ? zone : nil
end

def store
@value = value
end

private

def manage_widget
Yast::UI.CheckBox(Id(:manage_zone), Opt(:notify), _("Manage interface ZONE"))
end

def managed?
Yast::UI.QueryWidget(Id(:manage_zone), :Value)
end

def zones_widget
ComboBox(Id(:zones), Opt(:notify, :hstretch), _("ZONE"))
end

def select_zone(zone)
Yast::UI.ChangeWidget(Id(:zones), :Value, zone)
end

def zone
Yast::UI.QueryWidget(Id(:zones), :Value)
end

def enable_zones(value)
Yast::UI.ChangeWidget(Id(:zones), :Enabled, value)
end

# Return a list of items for ComboBox with all the known firewalld zones
# and also an empty string option for the default zone.
#
# @return [Array <Array <String, String>>] list of names an description of
# known zones
def firewall_zones
zones = [["", _("Default")]]

if firewalld.installed?
firewalld.zones.each { |z| zones << [z.name, z.short] }
else
zones = [["", _("Firewall is not installed.")]]
end

zones.map { |z| Item(Id(z[0]), z[1]) }
end
end
end
end
2 changes: 2 additions & 0 deletions src/modules/LanItems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def main
@wl_key = []
@wl_default_key = 0
@wl_nick = ""
@firewall_zone = nil

# FIXME: We should unify bridge_ports and bond_slaves variables

Expand Down Expand Up @@ -1715,6 +1716,7 @@ def SetDeviceVars(devmap, defaults)
@prefix = d["PREFIXLEN"]
@remoteip = d["REMOTE_IPADDR"]
@netmask = d["NETMASK"]
@firewall_zone = d["ZONE"]
@set_default_route = case d["DHCLIENT_SET_DEFAULT_ROUTE"]
when "yes" then true
when "no" then false
Expand Down

0 comments on commit 3fc267c

Please sign in to comment.