Skip to content

Commit

Permalink
Merge pull request #1212 from yast/handle-invalid-values
Browse files Browse the repository at this point in the history
Handle invalid values
  • Loading branch information
imobachgs committed Apr 22, 2021
2 parents 1c2af43 + b36ea63 commit 50e9404
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
7 changes: 7 additions & 0 deletions package/yast2-network.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Thu Apr 22 10:15:42 UTC 2021 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- Do not crash when the BOOTPROTO or STARTMODE ar missing or
invalid (bsc#1181295).
- 4.3.66

-------------------------------------------------------------------
Mon Apr 12 12:05:02 UTC 2021 - Knut Anderssen <kanderssen@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.3.65
Version: 4.3.66
Release: 0
Summary: YaST2 - Network Configuration
License: GPL-2.0-only
Expand Down
48 changes: 46 additions & 2 deletions src/lib/y2network/wicked/connection_config_readers/base.rb
Expand Up @@ -32,6 +32,8 @@ module ConnectionConfigReaders
# The derived classes should implement {#update_connection_config} method.
# methods.
class Base
include Yast::Logger

# @return [CFA::InterfaceFile] Interface's configuration file
attr_reader :file

Expand All @@ -47,14 +49,14 @@ def initialize(file)
# @return [Y2Network::ConnectionConfig::Base]
def connection_config
connection_class.new.tap do |conn|
conn.bootproto = BootProtocol.from_name(file.bootproto || "static")
conn.bootproto = find_bootproto
conn.description = file.name
conn.interface = file.interface
conn.ip = all_ips.find { |i| i.id.empty? }
conn.ip_aliases = all_ips.reject { |i| i.id.empty? }
conn.name = file.interface
conn.lladdress = file.lladdr
conn.startmode = Startmode.create(file.startmode || "manual")
conn.startmode = find_startmode
conn.startmode.priority = file.ifplugd_priority if conn.startmode.name == "ifplugd"
conn.ethtool_options = file.ethtool_options
conn.firewall_zone = file.zone
Expand All @@ -70,6 +72,39 @@ def connection_config

private

DEFAULT_BOOTPROTO = BootProtocol::STATIC

# Finds the boot protocol
#
# If it is not defined or it has an unknown value, it returns the
# fallback value (BootProtocol::STATIC).
#
# @return [BootProtocol]
def find_bootproto
bootproto = BootProtocol.from_name(file.bootproto.to_s)
return bootproto if bootproto

report_invalid_value("BOOTPROTO", file.bootproto, DEFAULT_BOOTPROTO.name)
DEFAULT_BOOTPROTO
end

DEFAULT_STARTMODE_NAME = "manual".freeze

# Finds the start mode
#
# If it is not defined or it has an unknown value, it returns the
# fallback value (manual).
#
# @return [Startmode]
def find_startmode
startmode = Startmode.create(file.startmode) if file.startmode
return startmode if startmode

fallback = Startmode.create(DEFAULT_STARTMODE_NAME)
report_invalid_value("STARTMODE", file.startmode, fallback.name)
fallback
end

# Returns the class of the connection configuration
#
# @return [Class]
Expand Down Expand Up @@ -132,6 +167,15 @@ def hostnames(conn)
aliases = Yast::Host.names(conn.ip.address.address.to_s).first
aliases.to_s.split(" ")
end

# It reports an invalid value for a given key.
#
# @param key [#to_s] Key name
# @param value [#to_s] Invalid value
# @param fallback [#to_s] Fallback value
def report_invalid_value(key, value, fallback)
log.warn "Invalid value '#{value}' for '#{key}'. Using '#{fallback}' instead."
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions test/y2network/wicked/connection_config_readers/ethernet_test.rb
Expand Up @@ -103,5 +103,38 @@
it "reads dhclient set hostname value as boolean" do
expect(handler.connection_config.dhclient_set_hostname).to eq true
end

context "when the BOOTPROTO is not valid" do
before do
allow(file).to receive(:bootproto).and_return("something")
end

it "falls back to STATIC" do
eth = handler.connection_config
expect(eth.bootproto).to eq(Y2Network::BootProtocol::STATIC)
end

it "logs the problem" do
expect(handler.log).to receive(:warn).with(/Invalid.*BOOTPROTO.*static/)
handler.connection_config
end
end

context "when the STARTMODE is not valid" do
before do
allow(file).to receive(:startmode).and_return("automatic")
end

it "falls back to MANUAL" do
eth = handler.connection_config
expect(eth.startmode.to_s).to eq("manual")
end

it "logs the problem" do
expect(handler.log).to receive(:warn).with(/Invalid.*STARTMODE.*manual/)
handler.connection_config
end
end

end
end

0 comments on commit 50e9404

Please sign in to comment.