Skip to content

Commit

Permalink
Do not change the interface at all.
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 8, 2019
1 parent 399fc2c commit 3183d7f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 116 deletions.
4 changes: 2 additions & 2 deletions src/include/network/lan/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ def AddressDialog
@settings["IFCFG"] = LanItems.device if LanItems.operation != :add
firewall_zone = Y2Network::Widgets::FirewallZone.new(LanItems.device)
wd["FWZONE"] = firewall_zone.cwm_definition
firewall_zone.value = @settings["FWZONE"]
firewall_zone.value = @settings["FWZONE"] if firewalld.installed?

functions = {
"init" => fun_ref(method(:InitAddrWidget), "void (string)"),
Expand Down Expand Up @@ -1336,7 +1336,7 @@ def AddressDialog
# general tab
LanItems.startmode = Ops.get_string(@settings, "STARTMODE", "")
LanItems.mtu = Ops.get_string(@settings, "MTU", "")
LanItems.firewall_zone = firewall_zone.store_zone
LanItems.firewall_zone = firewall_zone.store_permanent if firewalld.installed?

# address tab
bootproto = @settings.fetch("BOOTPROTO", "")
Expand Down
92 changes: 37 additions & 55 deletions src/lib/y2network/widgets/firewall_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,13 @@ def init

populate_select(firewall_zones)
self.value = @value
enable_zones(managed?)
end

# @see CWM::AbstractWidget
def contents
return Label(_("Firewall is not installed.")) unless installed?

VBox(
Left(manage_widget),
Left(zones_widget),
Left(permanent_config_widget)
)
end

# @see CWM::AbstractWidget
# @param event [Hash]
def handle(event)
enable_zones(managed?) if event["ID"] == :manage_zone

nil
Left(zones_widget)
end

# Stores the given name and when it is enabled to configure the
Expand All @@ -84,17 +71,16 @@ def handle(event)
def value=(name)
@value = name
return unless installed?
manage_zone!(!!name) && select_zone(name)
select_zone(name)
end

# It returns the current ZONE selection or nil in case of not enabled
# the management through the ifcfg files.
#
# @return [String, nil] current zone or nil when not managed
def value
return @value unless Yast::UI.WidgetExists(Id(:manage_zone))

managed? ? selected_zone : nil
return @value unless Yast::UI.WidgetExists(Id(:zones))
selected_zone
end

# Stores the current value
Expand All @@ -109,7 +95,7 @@ def store
# enabled to be managed through the ifcfg files
#
# @return [String, nil] the current zone selection
def store_zone
def store_permanent
return @value unless installed?

@interface.zone = @value if zone_changed?
Expand All @@ -121,12 +107,8 @@ def help
help_text =
_("<p><b><big>FIREWALL ZONE</big></b></p>" \
"<p>A network zone defines the level of trust for network connections. " \
"The <b>ZONE</b> can be set by yast-firewall, by different firewalld " \
"utilities or via the ifcfg file.</p>" \
"<p>When enabled in yast-network, it sets the <b>ZONE</b> which this " \
"interface belongs to modifying also the firewalld permanent " \
"configuration</p>")

"The selected ZONE will be added to the ifcfg as well as the firewalld " \
"permanent configuration.")
help_text << zones_help if installed?
help_text
end
Expand All @@ -140,54 +122,44 @@ def zone_changed?
@value && (current_zone.to_s != @value)
end

# @return [String]
def default_label
_("DEFAULT")
_("Assign to the default ZONE")
end

def permanent_config_widget
label = current_zone ? current_zone : default_label

VBox(
VSpacing(1),
Label(_("Current ZONE (permanent config): %s") % label),
Label(_("Default ZONE (permanent config): %s") % firewalld.default_zone)
)
# @return [String]
def no_zone_label
_("Do not assign ZONE")
end

# Current {Y2Firewall::Firewalld::Interface} name
# @return [String, nil]
def current_zone
return unless @interface.zone
@interface.zone.name
end

def manage_widget
CheckBox(Id(:manage_zone), Opt(:notify), _("Define Ifcfg ZONE"))
end

def manage_zone!(value)
Yast::UI.ChangeWidget(Id(:manage_zone), :Value, value)
value
end

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

# @return [Yast::Term] zones select list
def zones_widget
ComboBox(Id(:zones), Opt(:notify, :hstretch), _("ZONE"))
ComboBox(Id(:zones), Opt(:notify, :hstretch), label)
end

# Convenince method to select an specific zone from the zones list
#
# @param zone [String]
def select_zone(zone)
Yast::UI.ChangeWidget(Id(:zones), :Value, zone)
end

# Convenince method which returns the selected zone from the zones list
#
# @return [String, nil]
def selected_zone
Yast::UI.QueryWidget(Id(:zones), :Value)
end

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

# @param zones [Array <Array <String, String>>] list of available zones
# names
def populate_select(zones)
items = zones.map { |z| Item(Id(z[0]), z[1]) }
Yast::UI.ChangeWidget(Id(:zones), :Items, items)
Expand All @@ -197,24 +169,34 @@ def populate_select(zones)
# and also an empty string option for the default zone.
#
# @return [Array <Array <String, String>>] list of names an description of
# known zones
# available zones
def firewall_zones
zones = [["", default_label]]
firewalld.zones.each { |z| zones << [z.name, z.short] }
zones = [[nil, no_zone_label], ["", default_label]]
firewalld.zones.each { |z| zones << [z.name, z.name] }
zones
end

# Convenience method to check whether firewalld is installed or not
#
# @return [Boolean] whether firewalld is installed or not
def installed?
@installed ||= firewalld.installed?
end

# Help text with the description of the available zones
#
# @return [String] zones help description
def zones_help
description = firewalld.zones.map { |z| zone_description(z) }
return "" if description.empty?

_("<p>Find below the available zones description: <ul>%s</ul></p>") % description.join
end

# Return the description of the given zone as a HTML list entry
#
# @param zone [Y2Firewall::Firewalld::Zone]
# @return [String] zone description
def zone_description(zone)
"<li><b>#{zone.short}: </b>" \
"#{zone.description} " \
Expand Down
69 changes: 10 additions & 59 deletions test/lib/y2network/widgets/firewall_zone_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
let(:subject) { described_class.new("eth0") }
let(:firewalld) { Y2Firewall::Firewalld.instance }
let(:firewall_zones) { [["", "Default"], ["custom", "custom"]] }
let(:managed?) { true }
let(:installed?) { true }

before do
allow(firewalld).to receive(:installed?).and_return(installed?)
allow(subject).to receive(:firewall_zones).and_return(firewall_zones)
allow(subject).to receive(:managed?).and_return(managed?)
end

include_examples "CWM::CustomWidget"
Expand All @@ -31,37 +29,19 @@
expect(subject).to receive(:select_zone).with("custom")
subject.init
end

it "enables / disables the selection of zone" do
allow(subject).to receive(:managed?).and_return(false, true)
expect(subject).to receive(:enable_zones).with(false)
subject.init
expect(subject).to receive(:enable_zones).with(true)
subject.init
end
end

describe "#value" do
before do
allow(Yast::UI).to receive(:WidgetExists).and_return(true)
end

context "when the checkbox is not checked" do
let(:managed?) { false }

it "returns nil" do
expect(subject.value).to eql(nil)
end
it "returns the selected element" do
expect(subject).to receive(:selected_zone).and_return("")
expect(subject.value).to eql("")
end

context "when the checkbox is checked" do
it "returns the selected element" do
expect(subject).to receive(:selected_zone).and_return("")
expect(subject.value).to eql("")
end
end

context "when the checkbox widget does not exist" do
context "when the select zone widget does not exist" do
before do
allow(Yast::UI).to receive(:WidgetExists).and_return(false)
end
Expand All @@ -80,15 +60,14 @@
end

it "caches the current value" do
expect(subject).to receive(:managed?).and_return(true)
expect(subject).to receive(:selected_zone).and_return("external")
subject.store
expect(subject).to_not receive(:managed?)
expect(subject).to_not receive(:selected_zone?)
expect(subject.store).to eql("external")
end
end

describe "#store_zone" do
describe "#store_permanent" do
before do
subject.value = "custom"
end
Expand All @@ -97,7 +76,7 @@
let(:installed?) { false }

it "returns the cached value" do
expect(subject.store_zone).to eql("custom")
expect(subject.store_permanent).to eql("custom")
end
end

Expand All @@ -106,53 +85,25 @@
let(:managed?) { false }

it "returns the cached value" do
expect(subject.store_zone).to eql("custom")
expect(subject.store_permanent).to eql("custom")
end
end

context "and the cached value is not equal to the firewalld interface zone" do
it "modifies the interface permanent ZONE" do
allow(subject).to receive(:current_zone).and_return("external")
expect_any_instance_of(Y2Firewall::Firewalld::Interface).to receive(:zone=).with("custom")
subject.store_zone
subject.store_permanent
end
end

context "and the cached value is the same than the firewalld interface zone" do
it "does not touch the interface permanent ZONE" do
allow(subject).to receive(:current_zone).and_return("custom")
expect_any_instance_of(Y2Firewall::Firewalld::Interface).to_not receive(:zone=)
subject.store_zone
subject.store_permanent
end
end
end
end

describe "#handle" do
it "returns nil" do
expect(subject.handle("ID" => "fake_event")).to eql(nil)
end

context "when the checkbox is checked" do
before do
allow(subject).to receive(:managed?).and_return(true)
end

it "enables the zone list selection" do
expect(subject).to receive(:enable_zones).with(true)
subject.handle("ID" => :manage_zone)
end
end

context "then the checkbox is unchecked" do
before do
allow(subject).to receive(:managed?).and_return(false)
end

it "disables the zone list selection when not checked" do
expect(subject).to receive(:enable_zones).with(false)
subject.handle("ID" => :manage_zone)
end
end
end
end

0 comments on commit 3183d7f

Please sign in to comment.