Skip to content

Commit

Permalink
Merge 47fd26e into 24157b3
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed Feb 24, 2020
2 parents 24157b3 + 47fd26e commit 8596d97
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 41 deletions.
7 changes: 7 additions & 0 deletions package/yast2-network.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Fri Feb 21 09:53:25 UTC 2020 - Knut Anderssen <kanderssen@suse.com>

- Do not crash when checking if a given 'host' is local or not
(bsc#1163305)
- 4.2.56

-------------------------------------------------------------------
Wed Feb 19 11:52:25 UTC 2020 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

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


Name: yast2-network
Version: 4.2.55
Version: 4.2.56
Release: 0
Summary: YaST2 - Network Configuration
License: GPL-2.0-only
Expand Down
2 changes: 1 addition & 1 deletion src/lib/y2network/connection_configs_collection.rb
Expand Up @@ -37,7 +37,7 @@ class ConnectionConfigsCollection
alias_method :to_a, :connection_configs

def_delegators :@connection_configs, :each, :find, :push, :<<, :reject!, :map, :flat_map,
:any?, :size, :first, :empty?
:any?, :size, :first, :empty?, :each_with_object

# Constructor
#
Expand Down
51 changes: 22 additions & 29 deletions src/modules/DNS.rb
Expand Up @@ -80,10 +80,10 @@ def main
Yast.import "UI"
textdomain "network"

Yast.import "Lan"
Yast.import "Arch"
Yast.import "Hostname"
Yast.import "IP"
Yast.import "NetworkInterfaces"
Yast.import "ProductFeatures"
Yast.import "Progress"
Yast.import "Service"
Expand Down Expand Up @@ -223,16 +223,14 @@ def Write(netconfig_update: true)
# NOTE: used in yast2-nis-server, yast2-samba-server, yast2-dhcp-server
def IsHostLocal(check_host)
Read()
NetworkInterfaces.Read
dhcp_data = {}

if Ops.greater_than(
Builtins.size(NetworkInterfaces.Locate("BOOTPROTO", "dhcp")),
0
) || dhcp_hostname
dhcp_data = dhcp_data()
Builtins.y2milestone("Got DHCP-configured data: %1", dhcp_data)
current_dhcp_data = {}
connections = Yast::Lan.yast_config.connections

if connections.any?(:dhcp?) || dhcp_hostname
current_dhcp_data = dhcp_data
log.info("Got DHCP-configured data: #{current_dhcp_data.inspect}")
end

# FIXME: May not work properly in following situations:
# - multiple addresses per interface
# - aliases in /etc/hosts
Expand All @@ -241,29 +239,24 @@ def IsHostLocal(check_host)
# loopback interface or localhost hostname
return true if ["127.0.0.1", "::1", "localhost", "localhost.localdomain"].include?(check_host)

ip_addresses = connections.each_with_object([]) do |conn, res|
conn.all_ips.each_with_object(res) do |ip, ips|
address = ip&.address&.address.to_s
ips << address if !address.empty? && !ips.include?(address)
end
end

# IPv4 address
if IP.Check4(check_host)
if Ops.greater_than(
Builtins.size(NetworkInterfaces.Locate("IPADDR", check_host)),
0
) ||
Ops.get(dhcp_data, "ip", "") == check_host
return true
end
return true if ip_addresses.include?(check_host) || current_dhcp_data["ip"] == check_host
# IPv6 address
elsif IP.Check6(check_host)
Builtins.y2debug(
"TODO make it similar to IPv4 after other code adapted to IPv6"
)
# short hostname
elsif Builtins.findfirstof(check_host, ".").nil?
if Builtins.tolower(check_host) == Builtins.tolower(@hostname) ||
Ops.get(dhcp_data, "hostname_short", "") == check_host
return true
end
elsif Builtins.tolower(check_host) ==
Builtins.tolower(Ops.add(Ops.add(@hostname, "."), @domain)) ||
Ops.get(dhcp_data, "hostname_fq", "") == check_host
log.debug("TODO make it similar to IPv4 after other code adapted to IPv6")
# short hostname or FQDN
elsif check_host.downcase == hostname.to_s.downcase ||
current_dhcp_data["hostname_short"] == check_host ||
current_dhcp_data["hostname_fq"] == check_host

return true
end
false
Expand Down
53 changes: 43 additions & 10 deletions test/dns_test.rb
Expand Up @@ -32,8 +32,12 @@
Yast.import "Lan"

describe Yast::DNS do
let(:logger) { double(info: true, debug: true) }
let(:lan_config) do
Y2Network::Config.new(dns: dns_config, hostname: hostname_config, source: :sysconfig)
Y2Network::Config.new(
dns: dns_config, hostname: hostname_config, source: :sysconfig,
connections: connections, interfaces: interfaces
)
end
let(:dns_config) do
Y2Network::DNS.new
Expand All @@ -42,9 +46,30 @@
Y2Network::Hostname.new(static: "install", dhcp_hostname: true)
end

let(:eth0) { Y2Network::PhysicalInterface.new("eth0") }
let(:interfaces) { Y2Network::InterfacesCollection.new([eth0]) }

let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
conn.ip = ip
end
end

let(:ip) do
Y2Network::ConnectionConfig::IPConfig.new(
Y2Network::IPAddress.from_string("192.168.122.33/24"),
id: "", broadcast: Y2Network::IPAddress.from_string("192.168.122.255")
)
end

let(:connections) { Y2Network::ConnectionConfigsCollection.new([eth0_conn]) }

subject { Yast::DNS }

before do
allow(described_class).to receive(:log).and_return(logger)
allow(Yast::Lan).to receive(:Read)
allow(Yast::Lan).to receive(:yast_config).and_return(lan_config)
end
Expand Down Expand Up @@ -117,14 +142,12 @@
end

describe ".IsHostLocal" do
let(:ip) { "10.111.66.75" }
let(:dhcp_ip) { "10.111.66.75" }
let(:hostname_short) { "test" }
let(:hostname_fq) { "test.test.de" }
let(:output) do
{ "ip" => ip, "hostname_short" => hostname_short, "hostname_fq" => hostname_fq }
end
let(:ipv4) { false }
let(:ipv6) { false }
let(:stdout) { double }

before do
Expand All @@ -133,10 +156,8 @@
.with(/eth[0-9]/)
.and_return(Y2Network::InterfaceType::ETHERNET)
allow(subject).to receive(:Read)
allow(Yast::IP).to receive(:Check4).and_return(ipv4)
allow(Yast::IP).to receive(:Check6).and_return(ipv6)
allow(Yast::Execute).to receive(:stdout).and_return(stdout)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -i").and_return(ip)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -i").and_return(dhcp_ip)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname").and_return(hostname_short)
allow(stdout).to receive(:on_target!).with("/usr/bin/hostname -f").and_return(hostname_fq)

Expand All @@ -158,16 +179,28 @@
end

context "for IPv4" do
let(:ipv4) { true }

it "returns true when the ip of local machine is given" do
expect(subject.IsHostLocal(ip)).to eq(true)
expect(subject.IsHostLocal(dhcp_ip)).to eq(true)
expect(subject.IsHostLocal("192.168.122.33")).to eq(true)
end

it "returns false when the ip of local machine is not given" do
expect(subject.IsHostLocal("1.2.3.4")).to eq(false)
end
end

context "for IPv6" do
let(:ip6) { "2001:db8:1234:ffff:ffff:ffff:ffff:fff1" }

it "logs that the implementation is still pending" do
expect(logger).to receive(:debug).with(/^TODO/)
subject.IsHostLocal(ip6)
end

it "returns false" do
expect(subject.IsHostLocal(ip6)).to eq(false)
end
end
end

describe ".Read" do
Expand Down

0 comments on commit 8596d97

Please sign in to comment.