Skip to content

Commit

Permalink
Added firewall interfaces helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 12, 2018
1 parent a798970 commit 6135826
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 52 deletions.
96 changes: 96 additions & 0 deletions library/network/src/lib/y2firewall/helpers/interfaces.rb
@@ -0,0 +1,96 @@
# 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 or 3 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 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
module Helpers
# Set of helpers methods for operating with NetworkInterfaces and firewalld
# zones.
module Interfaces
def self.included(_base)
Yast.import "NetworkInterfaces"
end

# Return an instance of Y2Firewall::Firewalld
#
# @return [Y2Firewall::Firewalld] a firewalld instance
def firewalld
Y2Firewall::Firewalld.instance
end

# Return the name of interfaces which belongs to the default zone
#
# @return [Array<String>] default zone interface names
def default_interfaces
known_interfaces.select { |i| i["zone"].to_s.empty? }.map { |i| i["id"] }
end

# Return the zone name for a given interface from the firewalld instance
# instead of from the API.
#
# @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
end

# Convenience method to return the default zone object
#
# @return [Y2Firewall::Firewalld::Zone] default zone
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 [Array<Hash<String,String>>] known interfaces "id", "name" and "zone"
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
end
end
end
end
54 changes: 2 additions & 52 deletions library/network/src/modules/CWMFirewallInterfaces.rb
Expand Up @@ -40,6 +40,7 @@

require "yast"
require "y2firewall/firewalld"
require "y2firewall/helpers/interfaces"

module Yast
# This class provide a set of methods to define a widget for handling with
Expand All @@ -66,6 +67,7 @@ module Yast
#
# Y2Firewall::Firewalld.instance.write
class CWMFirewallInterfacesClass < Module
include Y2Firewall::Helpers::Interfaces
include Yast::Logger

# [Array<String>] List of all interfaces relevant for firewall settings
Expand Down Expand Up @@ -999,58 +1001,6 @@ def firewall_status_label(status)
end
end

# Convenience method to return the default zone object
#
# @return [Y2Firewall::Firewalld::Zone] default zone
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 [Array<Hash<String,String>>] known interfaces "id", "name" and "zone"
def known_interfaces
return @known_interfaces if @known_interfaces

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

@known_interfaces = interfaces.map do |interface|
{
"id" => interface,
"name" => NetworkInterfaces.GetValue(interface, "NAME"),
"zone" => interface_zone(interface)
}
end
end

# Return the name of interfaces which belongs to the default zone
#
# @return [Array<String>] default zone interface names
def default_interfaces
known_interfaces.select { |i| i["zone"].to_s.empty? }.map { |i| i["id"] }
end

# Return the zone name for a given interface from the firewalld instance
# instead of from the API.
#
# @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
end

def zone_services(services)
services_status = {}

Expand Down
36 changes: 36 additions & 0 deletions library/network/test/y2firewall/helpers/interfaces_test.rb
@@ -0,0 +1,36 @@
#!/usr/bin/env rspec

require_relative "../../test_helper"
require "y2firewall/helpers/interfaces"

class DummyClass
include Y2Firewall::Helpers::Interfaces
end

describe Y2Firewall::Helpers::Interfaces do
subject { DummyClass.new }

before do
allow(Yast::NetworkInterfaces).to receive("List").and_return(["eth0", "eth1"])
allow(Yast::NetworkInterfaces).to receive("GetValue").with("eth0", "NAME").and_return("Intel I217-LM")
allow(Yast::NetworkInterfaces).to receive("GetValue").with("eth1", "NAME").and_return("Intel I217-LM")
end

describe "#interface_zone" do
pending
it "returns the zone name of the given interface" do
end
end

describe "#known_interfaces" do
pending
it "returns a hash with the 'id', 'name' and zone of the current interfaces" do
end
end

describe "#default_interfaces" do
pending
it "returns all the interface names that does not belong to any zone" do
end
end
end

0 comments on commit 6135826

Please sign in to comment.