Skip to content

Commit

Permalink
Merge ddfea97 into ae7d17f
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Feb 14, 2018
2 parents ae7d17f + ddfea97 commit 80ed695
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package/yast2-firewall.spec
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ BuildRequires: yast2 >= 4.0.45
BuildRequires: rubygem(%rb_default_ruby_abi:yast-rake)
BuildRequires: rubygem(%rb_default_ruby_abi:rspec)

# Firewalld read?
Requires: yast2 >= 4.0.45
# Firewalld - extended API
Requires: yast2 >= 4.0.49

# ButtonBox widget
Conflicts: yast2-ycp-ui-bindings < 2.17.3
Expand Down
66 changes: 64 additions & 2 deletions src/lib/y2firewall/clients/auto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module Clients
# Does not do any changes to the configuration.
class Auto < ::Installation::AutoClient
include Yast::Logger

Yast.import "HTML"

class << self
# @return [Boolean] whether the AutoYaST configuration has been
# modified or not
Expand All @@ -58,9 +61,19 @@ def initialize
#
# @return [String]
def summary
return "" if !firewalld.installed?
return Yast::HTML.Para(_("Firewalld is not available")) if !firewalld.installed?

firewalld.read if !firewalld.read?

# general overview
summary = general_summary

# per zone details
firewalld.zones.each do |zone|
summary << zone_summary(zone)
end

firewalld.api.list_all_zones.join("\n")
summary
end

# Import the firewall configuration
Expand Down Expand Up @@ -198,6 +211,55 @@ def imported
def imported?
!!self.class.imported
end

# Creates a piece for summary for zone detail
#
# See has_many (@see Y2Firewall::Firewalld::Relations#has_many) in
# Y2Firewall::Firewalld::Zone for known detail / relations
#
# @param [String] relation is name of relation (used as a caption for generated blob)
# @param [Array<String>] names details to be formated
# @return [<String>] A string formated using Yast::HTML methods
def zone_detail_summary(relation, names)
return "" if names.nil? || names.empty?

Yast::HTML.Bold("#{relation.capitalize}:") + Yast::HTML.List(names)
end

# Creates a summary for the given zone
#
# @param [Firewalld::Zone] zone object defining a zone
# @return [String] HTML formated zone description
def zone_summary(zone)
raise ArgumentError, "zone parameter has to be defined" if zone.nil?

desc = zone.relations.map do |relation|
zone_detail_summary(relation, zone.send(relation))
end.delete_if(&:empty?)
return "" if desc.empty?

summary = Yast::HTML.Heading(zone.name)
summary << Yast::HTML.List(desc)
end

# Creates a general summary for firewalld
#
# @return [String] HTML formated firewall description
# rubocop:disable Metrics/AbcSize
def general_summary
html = Yast::HTML
running = " " + (firewalld.running? ? _("yes") : _("no"))
enabled = " " + (firewalld.enabled? ? _("yes") : _("no"))

summary = html.Bold(_("Running:")) + running + html.Newline
summary << html.Bold(_("Enabled:")) + enabled + html.Newline
summary << html.Bold(_("Default zone:")) + " " + firewalld.default_zone + html.Newline
summary << html.Bold(_("Defined zones:"))
summary << html.List(firewalld.zones.map(&:name))

html.Para(summary)
end
# rubocop:enable Metrics/AbcSize
end
end
end
58 changes: 54 additions & 4 deletions test/lib/y2firewall/clients/auto_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,61 @@
allow(subject).to receive(:importer).and_return(importer)
end

describe "#summary" do
it "returns the summary of all the configured zones" do
expect(firewalld.api).to receive(:list_all_zones).and_return(["zone1", "zone2"])
describe "#general_summary" do
it "creates a text which includes firewalld overview" do
expect(firewalld).to receive(:running?).and_return(true)
expect(firewalld).to receive(:enabled?).and_return(true)
expect(firewalld).to receive(:default_zone).and_return("public")

summary = subject.send(:general_summary)

expect(summary).to match(/Running/)
expect(summary).to match(/Enabled/)
expect(summary).to match(/Default zone/)
expect(summary).to match(/Defined zones/)
end
end

describe "#zone_summary" do
RELATIONS = {
interfaces: ["eth0", "eth1"],
services: ["ssh", "ftp"],
protocols: ["udp", "tcp"],
ports: ["80"]
}.freeze

def define_zone(name: nil, relation: nil, values: nil)
return nil if name.nil? || name.empty?
return nil if !Y2Firewall::Firewalld::Zone.instance_methods(false).include?(relation)

zone = Y2Firewall::Firewalld::Zone.new(name: name)
expect(zone).to receive(relation).and_return(values)

zone
end

it "empty zone returns empty description" do
summary = subject.send(:zone_summary, Y2Firewall::Firewalld::Zone.new(name: "test_zone"))

expect(subject.summary).to eq("zone1\nzone2")
expect(summary).to be_empty
end

RELATIONS.each_pair do |relation, values|
it "mentions #{relation} if any is assigned to the non-empty zone" do
zone = define_zone(name: "test_zone", relation: relation, values: values)
summary = subject.send(:zone_summary, zone)

values.each do |value|
expect(summary).to match(/#{value}/)
end
end
end
end

describe "#summary" do
it "reports when firewalld is not available" do
expect(firewalld).to receive(:installed?).and_return(false)
expect(subject.summary).to match(/not available/)
end
end

Expand Down

0 comments on commit 80ed695

Please sign in to comment.