Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
134 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
library/network/test/y2firewall/helpers/interfaces_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |