Skip to content

Commit

Permalink
Merge 61510a8 into de4d78b
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Sep 19, 2018
2 parents de4d78b + 61510a8 commit 6ca53ed
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 49 deletions.
1 change: 1 addition & 0 deletions library/network/src/Makefile.am
Expand Up @@ -42,6 +42,7 @@ yfwlib_DATA = \
yfwdlibdir = @ylibdir@/y2firewall/firewalld
yfwdlib_DATA = \
lib/y2firewall/firewalld/api.rb \
lib/y2firewall/firewalld/interface.rb \
lib/y2firewall/firewalld/relations.rb \
lib/y2firewall/firewalld/service.rb \
lib/y2firewall/firewalld/zone.rb \
Expand Down
81 changes: 81 additions & 0 deletions library/network/src/lib/y2firewall/firewalld/interface.rb
@@ -0,0 +1,81 @@
# encoding: utf-8

# ------------------------------------------------------------------------------
# Copyright (c) 2018 SUSE LLC
#
#
# 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.
#
# To contact SUSE about this file by physical or electronic mail, you may find
# current contact information at www.suse.com.
# ------------------------------------------------------------------------------

require "yast"
require "y2firewall/firewalld"

module Y2Firewall
class Firewalld
# Class to work with firewalld interfaces
class Interface
# @return [Symbol]
attr_accessor :id

# Constructor
#
# @param name [String] interface name
def initialize(name)
Yast.import "NetworkInterfaces"

@id = name.to_sym
end

# Return an array with all the known or configured interfaces
#
# @return [Array<Y2Firewall::Firewalld::Interface>] known interfaces
def self.known
Yast.import "NetworkInterfaces"
interfaces = Yast::NetworkInterfaces.List("").reject { |i| i == "lo" }

interfaces.map { |i| new(i) }
end

# @return [String] interface name
def name
id.to_s
end

# @return [String] device name
def device_name
Yast::NetworkInterfaces.GetValue(name, "NAME")
end

# Return the zone name for a given interface from the firewalld instance
# instead of from the API.
#
# @return [String, nil] zone name whether belongs to some or nil if not
def zone
zone = fw.zones.find { |z| z.interfaces.include?(id.to_s) }

zone ? zone.name : nil
end

private

# Return an instance of Y2Firewall::Firewalld
#
# @return [Y2Firewall::Firewalld] a firewalld instance
def fw
Y2Firewall::Firewalld.instance
end
end
end
end
33 changes: 7 additions & 26 deletions library/network/src/lib/y2firewall/helpers/interfaces.rb
Expand Up @@ -22,7 +22,7 @@
#
# ***************************************************************************
require "yast"
require "y2firewall/firewalld"
require "y2firewall/firewalld/interface"

module Y2Firewall
module Helpers
Expand All @@ -44,7 +44,7 @@ def firewalld
#
# @return [Array<String>] default zone interface names
def default_interfaces
known_interfaces.select { |i| i["zone"].to_s.empty? }.map { |i| i["id"] }
known_interfaces.reject(&:zone).map(&:name)
end

# Return the zone name for a given interface from the firewalld instance
Expand All @@ -53,9 +53,7 @@ def default_interfaces
# @param name [String] interface name
# @return [String, nil] zone name whether belongs to some or nil if not
def interface_zone(name)
zone = firewalld.zones.find { |z| z.interfaces.include?(name) }

zone ? zone.name : nil
Y2Firewall::Firewalld::Interface.new(name).zone
end

# Convenience method to return the default zone object
Expand All @@ -65,31 +63,14 @@ def default_zone
@default_zone ||= firewalld.find_zone(firewalld.default_zone)
end

# Return a hash of all the known interfaces with their "id", "name" and
# "zone".
#
# @example
# CWMFirewallInterfaces.known_interfaces #=>
# [
# { "id" => "eth0", "name" => "Intel Ethernet Connection I217-LM", "zone" => "external"},
# { "id" => "eth1", "name" => "Intel Ethernet Connection I217-LM", "zone" => "public"},
# { "id" => "eth2", "name" => "Intel Ethernet Connection I217-LM", "zone" => nil},
# { "id" => "eth3", "name" => "Intel Ethernet Connection I217-LM", "zone" => nil},
# ]
# Return an array with all the known (sysconfig configured) firewalld
# interfaces.
#
# @return [Array<Hash<String,String>>] known interfaces "id", "name" and "zone"
# @return [Array<Y2Firewall::Firewalld::Interface>] known interfaces
def known_interfaces
return @known_interfaces if @known_interfaces

interfaces = Yast::NetworkInterfaces.List("").reject { |i| i == "lo" }

@known_interfaces = interfaces.map do |interface|
{
"id" => interface,
"name" => Yast::NetworkInterfaces.GetValue(interface, "NAME"),
"zone" => interface_zone(interface)
}
end
@known_interfaces = Y2Firewall::Firewalld::Interface.known
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions library/network/src/modules/CWMFirewallInterfaces.rb
Expand Up @@ -250,19 +250,19 @@ def StoreAllowedInterfaces(services)

zones =
known_interfaces.each_with_object([]) do |known_interface, a|
if allowed_interfaces.include?(known_interface["id"])
zone_name = known_interface["zone"] || default_zone.name
if allowed_interfaces.include?(known_interface.name)
zone_name = known_interface.zone || firewalld.default_zone
a << zone_name
end
end

firewalld.zones.map do |zone|
firewalld.zones.each do |zone|
if zones.include?(zone.name)
services.map do |service|
services.each do |service|
zone.add_service(service) unless zone.services.include?(service)
end
else
services.map do |service|
services.each do |service|
zone.remove_service(service) if zone.services.include?(service)
end
end
Expand Down
17 changes: 8 additions & 9 deletions library/network/test/cwm_firewall_interfaces_test.rb
Expand Up @@ -201,9 +201,9 @@
describe "#Selected2Opened" do
let(:known_interfaces) do
[
{ "id" => "eth0", "name" => "Ethernet 1", "zone" => "external" },
{ "id" => "eth1", "name" => "Ethernet 2", "zone" => "public" },
{ "id" => "eth2", "name" => "Ethernet 3", "zone" => "dmz" }
mock_firewalld_interface(:eth0, "Ethernet 1", "external"),
mock_firewalld_interface(:eth1, "Ethernet 2", "public"),
mock_firewalld_interface(:eth2, "Ethernet 3", "dmz")
]
end

Expand All @@ -230,9 +230,9 @@
describe "#StoreAllowedInterfaces" do
let(:known_interfaces) do
[
{ "id" => "eth0", "name" => "Ethernet 1", "zone" => "external" },
{ "id" => "eth1", "name" => "Ethernet 2", "zone" => "public" },
{ "id" => "eth2", "name" => "Ethernet 3", "zone" => nil }
mock_firewalld_interface(:eth0, "Ethernet 1", "external"),
mock_firewalld_interface(:eth1, "Ethernet 2", "public"),
mock_firewalld_interface(:eth2, "Ethernet 3", "nil")
]
end

Expand All @@ -249,10 +249,9 @@
let(:zones) { [external_zone, public_zone] }

before do
expect(subject).to receive(:known_interfaces).and_return(known_interfaces)
expect(subject).to receive(:allowed_interfaces).and_return(["eth0", "eth1"])
allow(subject).to receive(:known_interfaces).and_return(known_interfaces)
allow(firewalld).to receive(:zones).and_return(zones)
allow(subject).to receive(:default_zone).and_return(public_zone)
allow(firewalld).to receive(:default_zone).and_return("public")
allow(subject).to receive(:configuration_changed).and_return(true)
allow(subject).to receive(:allowed_interfaces).and_return(["eth0", "eth1", "eth2"])
end
Expand Down
5 changes: 5 additions & 0 deletions library/network/test/test_helper.rb
Expand Up @@ -81,3 +81,8 @@ module NetworkStubs
}
}.freeze
end

def mock_firewalld_interface(id, name, zone)
instance_double("Y2Firewall::Firewalld::Interface",
id: id, name: id.to_s, device_name: name, zone: zone)
end
16 changes: 8 additions & 8 deletions library/network/test/y2firewall/helpers/interfaces_test.rb
Expand Up @@ -35,14 +35,14 @@ class DummyClass
end

describe "#known_interfaces" do
it "returns a hash with the 'id', 'name' and zone of the current interfaces" do
expect(subject.known_interfaces)
.to eql(
[
{ "id" => "eth0", "name" => "Intel I217-LM", "zone" => "external" },
{ "id" => "eth1", "name" => "Intel I217-LM", "zone" => nil }
]
)
it "returns an array with the known firewalld interfaces" do
known_interfaces = subject.known_interfaces
expect(known_interfaces.size).to eql(2)
eth0 = known_interfaces.find { |i| i.id == :eth0 }
expect(eth0.name).to eq("eth0")
expect(eth0.zone).to eq("external")
expect(eth0.device_name).to eq("Intel I217-LM")
expect(eth0).to be_a(Y2Firewall::Firewalld::Interface)
end
end

Expand Down
9 changes: 9 additions & 0 deletions package/yast2.changes
@@ -1,3 +1,12 @@
-------------------------------------------------------------------
Wed Sep 19 09:53:09 UTC 2018 - knut.anderssen@suse.com

- Network (Firewall)
- Added Y2Firewall::Firewalld::Interface class.
- Adapted interfaces helpers to work with the new class.
(fate#324662)
- 4.0.95

-------------------------------------------------------------------
Tue Sep 18 08:28:02 UTC 2018 - igonzalezsosa@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2
Version: 4.0.94
Version: 4.0.95
Release: 0
Summary: YaST2 - Main Package
License: GPL-2.0-only
Expand Down

0 comments on commit 6ca53ed

Please sign in to comment.