Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change the default zone #85

Merged
merged 4 commits into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/lib/y2firewall/dialogs/change_zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def contents

private

# @return [Array<Yast::Term>] List of buttons to display
def buttons
[ok_button, cancel_button]
end

# Returns a combobox to select the zone
#
# @note The widget is 'memoized'.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/y2firewall/widgets/change_zone_button.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def initialize(interface)
@interface = interface
end

# @see seeAbstractWidget
# @macro seeAbstractWidget
def label
_("Change Zone")
end

# @see seeAbstractWidget
# @macro seeAbstractWidget
def handle
return nil unless interface
result = Dialogs::ChangeZone.run(interface)
Expand Down
83 changes: 83 additions & 0 deletions src/lib/y2firewall/widgets/default_zone_button.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "cwm"
require "y2firewall/firewalld"

module Y2Firewall
module Widgets
# This button sets a zone as the default one
class DefaultZoneButton < CWM::PushButton
# @!attribute [r] zone
# @return [Y2Firewall::Firewalld::Zone] Zone to set as 'default'
attr_reader :zone

# Constructor
#
# @param zone [Y2Firewall::Firewalld::Zone] Zone to set as 'default'
def initialize(zone)
textdomain "firewall"
@zone = zone
end

# @macro seeAbstractWidget
def label
_("Set As Default")
end

# Sets the zone to act on
#
# @note If the given zone is the default one then the button is disabled.
#
# @param zone [Y2Firewall::Firewalld::Zone] Zone to set as 'default'
def zone=(zone)
@zone = zone
enable_or_disable
end

# @macro seeAbstractWidget
def handle
firewall.default_zone = zone.name
:redraw
end

private

# Enables or disables the button depending whether the zone is the default one or not
def enable_or_disable
if firewall.default_zone == zone.name
disable
else
enable
end
end

# Return the current `Y2Firewall::Firewalld` instance
#
# This is just a convenience method.
#
# @return [Y2Firewall::Firewalld]
def firewall
Y2Firewall::Firewalld.instance
end
end
end
end
14 changes: 14 additions & 0 deletions src/lib/y2firewall/widgets/interfaces_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,24 @@ def handle(event)
nil
end

# Returns the selected interface
#
# @return [Y2Firewall::Firewall::Interface] Interface
def selected_interface
interfaces.find { |i| i.id == value }
end

# @macro seeAbstractMethod
def help
_(
"<p>Assign your network devices into firewall zones by selecting the device\n" \
"in the table and clicking <b>Change Zone</b>.</p>\n\n" \
"<p>If you assign an interface to the <b>default</b> zone, the device\n" \
"will be assigned to the firewall's default zone. Visit the <b>Zones</b>\n" \
"section to find out (and eventually change) which zone is the default one.</p>"
)
end

private

# @return [Y2Firewalld::Widgets::ChangeZoneButton] Button to change the assigned zone
Expand Down
8 changes: 7 additions & 1 deletion src/lib/y2firewall/widgets/pages/zones.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require "cwm/page"
require "y2firewall/firewalld"
require "y2firewall/widgets/zones_table"
require "y2firewall/widgets/default_zone_button"

module Y2Firewall
module Widgets
Expand All @@ -47,12 +48,17 @@ def contents
return @contents if @contents
@contents = VBox(
Left(Heading(_("Zones"))),
ZonesTable.new(firewall.zones)
ZonesTable.new(firewall.zones, default_zone_button),
firewall.zones.empty? ? Empty() : default_zone_button
)
end

private

def default_zone_button
@default_zone_button ||= DefaultZoneButton.new(firewall.zones.first)
end

def firewall
Y2Firewall::Firewalld.instance
end
Expand Down
58 changes: 55 additions & 3 deletions src/lib/y2firewall/widgets/zones_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,74 @@ class ZonesTable < ::CWM::Table
# Constructor
#
# @param zones [Array<Y2Firewall::Firewalld::Zone>] Zones
def initialize(zones)
# @param default_zone_button [Y2Firewall::Widgets::DefaultZoneButton] Button to change
# the default zone
def initialize(zones, default_zone_button)
textdomain "firewall"
@zones = zones
@default_zone_button = default_zone_button
end

# @macro seeAbstractWidget
def opt
[:notify, :immediate]
end

# @macro seeAbstractWidget
def init
zone = Y2Firewall::UIState.instance.row_id
self.value = zone if zone && zones.map(&:name).include?(zone.to_s)
default_zone_button.zone = selected_zone
end

# @see CWM::Table#header
def header
[
_("Name"),
_("Interfaces")
_("Interfaces"),
_("Default")
Copy link
Contributor

@teclator teclator Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about having an extra column for it, but probably that is something for Ken, I would maybe prefer something like public (default)

Copy link
Contributor Author

@imobachgs imobachgs Sep 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have something similar in the repository list, but it is a slightly different case. But we might append a "(default)" string if preferred.

]
end

# @see CWM::Table#items
def items
zones.map { |z| [z.name.to_sym, z.name, z.interfaces.join(", ")] }
zones.map do |zone|
[
zone.name.to_sym,
zone.name,
zone.interfaces.join(", "),
zone.name == firewall.default_zone ? Yast::UI.Glyph(:CheckMark) : ""
]
end
end

# @macro seeAbstractWidget
def handle(event)
return nil unless my_event?(event) && event["EventReason"] == "SelectionChanged"
UIState.instance.select_row(value)
default_zone_button.zone = selected_zone
nil
end

# Returns the selected interface
#
# @return [Y2Firewall::Firewall::Interface] Interface
def selected_zone
zones.find { |z| z.name == value.to_s }
end

private

# @return [Y2Firewalld::Widgets::DefaultZoneButton] Button to set a zone as 'default'
attr_reader :default_zone_button

# Return the current `Y2Firewall::Firewalld` instance
#
# This is just a convenience method.
#
# @return [Y2Firewall::Firewalld]
def firewall
Y2Firewall::Firewalld.instance
end
end
end
Expand Down
85 changes: 85 additions & 0 deletions test/lib/y2firewall/widgets/default_zone_button_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#!/usr/bin/env rspec
# encoding: utf-8

# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require_relative "../../../test_helper"

require "cwm/rspec"
require "y2firewall/widgets/default_zone_button"

describe Y2Firewall::Widgets::DefaultZoneButton do
include_examples "CWM::PushButton"

subject(:widget) { described_class.new(zone) }

let(:zone) do
instance_double(
Y2Firewall::Firewalld::Zone, name: "public", interfaces: [], remove_interface: nil
)
end

describe "#zone=" do
before do
allow(Y2Firewall::Firewalld.instance).to receive(:default_zone).and_return(zone.name)
end

let(:other_zone) do
instance_double(Y2Firewall::Firewalld::Zone, name: "dmz")
end

it "sets the given zone as the one to act on" do
expect { widget.zone = other_zone }.to change { widget.zone }.to other_zone
end

context "when the given zone is not the default one" do
let(:default_zone) { other_zone }

it "enables the button" do
expect(widget).to receive(:enable)
widget.zone = other_zone
end
end

context "when the given zone is the default one" do
it "disables the button" do
expect(widget).to receive(:disable)
widget.zone = zone
end
end
end

describe "#handle" do
before do
allow(Y2Firewall::Firewalld.instance).to receive(:default_zone=)
end

it "sets the current zone as the default one" do
expect(Y2Firewall::Firewalld.instance).to receive(:default_zone=).with(zone.name)
widget.handle
end

it "returns :redraw in order to redraw the interface" do
expect(widget.handle).to eq(:redraw)
end
end

describe "#zone"
end
32 changes: 30 additions & 2 deletions test/lib/y2firewall/widgets/zones_table_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,38 @@
require_relative "../../../test_helper.rb"
require "cwm/rspec"
require "y2firewall/widgets/zones_table"
require "y2firewall/widgets/default_zone_button"
require "y2firewall/firewalld/interface"

describe Y2Firewall::Widgets::ZonesTable do
let(:z1) { double("fake zone", name: "zoe", interfaces: []) }
subject { described_class.new([z1]) }
subject(:widget) { described_class.new([public_zone, dmz_zone], default_zone_button) }

let(:default_zone_button) do
instance_double(Y2Firewall::Widgets::DefaultZoneButton).as_null_object
end

let(:public_zone) do
instance_double(Y2Firewall::Firewalld::Zone, name: "public", interfaces: ["eth0", "eth1"])
end

let(:dmz_zone) do
instance_double(Y2Firewall::Firewalld::Zone, name: "dmz", interfaces: [])
end

before do
allow(Y2Firewall::Firewalld.instance).to receive(:default_zone).and_return(public_zone.name)
end

include_examples "CWM::Table"

describe "#items" do
it "returns the list of zones" do
expect(widget.items).to eq(
[
[:public, "public", "eth0, eth1", Yast::UI.Glyph(:CheckMark)],
[:dmz, "dmz", "", ""]
]
)
end
end
end