Skip to content

Commit

Permalink
Merge pull request #1197 from yast/merge-sle-15-sp3
Browse files Browse the repository at this point in the history
Merge SLE-15-SP3 into master
  • Loading branch information
imobachgs committed Apr 7, 2021
2 parents 827f07d + 3bfe293 commit 6981d43
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 67 deletions.
13 changes: 12 additions & 1 deletion package/yast2-network.changes
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
-------------------------------------------------------------------
Wed Apr 7 11:27:26 UTC 2021 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>

- Write DNS servers to NetworkManager connection files when using
a static configuration (bsc#1181701).
- 4.4.0

-------------------------------------------------------------------
Tue Apr 6 08:54:07 UTC 2021 - Michal Filka <mfilka@suse.com>

- Drop isCurrentDHCP and isCurrentHotplug methods from LanItems.

-------------------------------------------------------------------
Mon Apr 5 15:21:47 UTC 2021 - David Diaz <dgonzalez@suse.com>

- Do not show the "Abort" button when the inst_lan client is called
from another installation step (bsc#1183586).
- 4.4.0

-------------------------------------------------------------------
Mon Mar 29 11:52:08 UTC 2021 - Imobach Gonzalez Sosa <igonzalezsosa@suse.com>
Expand Down
42 changes: 41 additions & 1 deletion src/lib/cfa/nm_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# find current contact information at www.suse.com.

require "cfa/base_model"
require "pathname"
require "y2network/connection_config/wireless"

Yast.import "WFM"

module CFA
# Class to handle NetworkManager connection configuration files
Expand All @@ -32,14 +36,43 @@ class NmConnection < BaseModel
"bond", "bridge", "connection", "ethernet", "ipv4", "ipv6", "vlan", "wifi", "wifi_security"
].freeze

# @return [String] File path
attr_reader :file_path

class << self
# Returns the file corresponding to a connection
#
# @param conn [ConnectionConfig::Base] Connection configuration
# @return [NmConnection]
def for(conn)
path = SYSTEM_CONNECTIONS_PATH.join(file_basename_for(conn)).sub_ext(FILE_EXT)
new(path)
end

private

SYSTEM_CONNECTIONS_PATH = Pathname.new("/etc/NetworkManager/system-connections").freeze
FILE_EXT = ".nmconnection".freeze

# Returns the file base name for the given connection
#
# @param conn [ConnectionConfig::Base]
# @return [String]
def file_basename_for(conn)
return conn.essid.to_s if conn.is_a?(Y2Network::ConnectionConfig::Wireless) && conn.essid

conn.name
end
end

# Constructor
#
# @param path [String] File path
# @param file_handler [.read, .write] Object to read/write the file.
def initialize(path, file_handler: nil)
# FIXME: The Networkmanager lense writes the values surrounded by double
# quotes which is not valid
super(AugeasParser.new("Puppet.lns"), path, file_handler: file_handler)
super(AugeasParser.new("Desktop.lns"), path, file_handler: file_handler)
end

# Returns the augeas tree for the given section
Expand Down Expand Up @@ -80,6 +113,13 @@ def add_collection(section, name, values)
end
end

# Determines whether the file exist
#
# @return [Boolean] true if the file exist, false otherwise
def exist?
::File.exist?(::File.join(Yast::WFM.scr_root, file_path))
end

KNOWN_SECTIONS.each { |s| define_method(s) { section_for(s) } }
end
end
8 changes: 8 additions & 0 deletions src/lib/y2network/connection_configs_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ def by_ids(*ids)
select { |c| ids.include?(c.id) }
end

# Returns connections with a given bootproto
#
# @param bootprotos [Array<BootProtocol>] Boot protocols
# @return [Array<ConnectionConfig::Base>] Connection configs with the given boot protocol
def by_bootproto(*bootprotos)
select { |c| bootprotos.include?(c.bootproto) }
end

# Adds or updates a connection configuration
#
# @note It uses the name to do the matching.
Expand Down
34 changes: 34 additions & 0 deletions src/lib/y2network/network_manager/config_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ def write_connections(config, _old_config)
end
end

# Updates the DNS configuration
#
# In case a connection has a static configuration, the DNS nameservers are added
# to the configuration file (see bsc#1181701).
#
# @param config [Y2Network::Config] Current config object
# @param old_config [Y2Network::Config,nil] Config object with original configuration
def write_dns(config, old_config)
static = config.connections.by_bootproto(Y2Network::BootProtocol::STATIC)
return super if static.empty? || config.dns.nameservers.empty?

ipv4_ns, ipv6_ns = config.dns.nameservers.partition(&:ipv4?)
ipv4_dns = ipv4_ns.map(&:to_s).join(";")
ipv6_dns = ipv6_ns.map(&:to_s).join(";")
static.each do |conn|
add_dns_to_conn(conn, ipv4_dns, ipv6_dns)
end
end

# Finds routes for a given connection
#
# @param conn [ConnectionConfig::Base] Connection configuration
Expand All @@ -51,6 +70,21 @@ def write_connections(config, _old_config)
def routes_for(conn, routes)
routes.select { |r| r.interface&.name == conn.name }
end

# Add the DNS settings to the nmconnection file corresponding to the give conn
#
# @param conn [Connectionconfig::Base] Connection configuration
# @param ipv4_dns [String] Value for the 'dns' key in the ipv4 section
# @param ipv6_dns [String] Value for the 'dns' key in the ipv6 section
def add_dns_to_conn(conn, ipv4_dns, ipv6_dns)
file = CFA::NmConnection.for(conn)
return unless file.exist?

file.load
file.ipv4["dns"] = ipv4_dns unless ipv4_dns.empty?
file.ipv6["dns"] = ipv6_dns unless ipv6_dns.empty?
file.save
end
end
end
end
22 changes: 6 additions & 16 deletions src/lib/y2network/network_manager/connection_config_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ module NetworkManager
class ConnectionConfigWriter
include Yast::Logger

SYSTEM_CONNECTIONS_PATH = Pathname.new("/etc/NetworkManager/system-connections").freeze
FILE_EXT = ".nmconnection".freeze

# @param conn [ConnectionConfig::Base] Connection configuration to be
# written
# @param old_conn [ConnectionConfig::Base] Original connection
Expand All @@ -39,12 +36,15 @@ class ConnectionConfigWriter
def write(conn, old_conn = nil, opts = {})
return if conn == old_conn

path = SYSTEM_CONNECTIONS_PATH.join(file_basename_for(conn)).sub_ext(FILE_EXT)
file = CFA::NmConnection.new(path)
file = CFA::NmConnection.for(conn)
handler_class = find_handler_class(conn.type)
return nil if handler_class.nil?

ensure_permissions(path) unless ::File.exist?(path)
if file.exist?
file.load
else
ensure_permissions(file.file_path)
end

handler_class.new(file).write(conn, opts)
file.save
Expand Down Expand Up @@ -74,16 +74,6 @@ def find_handler_class(type)
"Connection handler could not be loaded: #{e.message}"
nil
end

# Returns the file base name for the given connection
#
# @param conn [ConnectionConfig::Base]
# @return [String]
def file_basename_for(conn)
return conn.essid.to_s if conn.is_a?(ConnectionConfig::Wireless)

conn.name
end
end
end
end
59 changes: 59 additions & 0 deletions test/cfa/nm_connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_relative "../test_helper"
require "cfa/nm_connection"
require "cfa/memory_file"
require "y2network/connection_config/ethernet"

describe CFA::NmConnection do
def file_path(filename)
Expand All @@ -29,6 +30,46 @@ def file_path(filename)
subject { described_class.new(conn_file) }
let(:conn_file) { file_path("some_wifi.nmconnection") }

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

it "uses the interface as path basename" do
file = described_class.for(conn)
expect(file.file_path.basename.to_s).to eq("eth0.nmconnection")
end

context "when a wireless connection is given" do
let(:essid) { "MY_WIRELESS" }

let(:conn) do
Y2Network::ConnectionConfig::Wireless.new.tap do |wlo1|
wlo1.name = "wlo1"
wlo1.interface = "wlo1"
wlo1.essid = essid
end
end

it "uses the ESSID as path basename" do
file = described_class.for(conn)
expect(file.file_path.basename.to_s).to eq("MY_WIRELESS.nmconnection")
end

context "and the ESSID is not set" do
let(:essid) { nil }

it "uses the interface as path basename" do
file = described_class.for(conn)
expect(file.file_path.basename.to_s).to eq("wlo1.nmconnection")
end
end
end
end

describe "#connection" do
before { subject.load }

Expand All @@ -44,4 +85,22 @@ def file_path(filename)
end
end
end

describe "#exist?" do
context "when the file exists" do
let(:conn_file) { file_path("some_wifi.nmconnection") }

it "returns true" do
expect(subject.exist?).to eq(true)
end
end

context "when the file does not exist" do
let(:conn_file) { file_path("missing.nmconnection") }

it "returns false" do
expect(subject.exist?).to eq(false)
end
end
end
end
3 changes: 1 addition & 2 deletions test/cmdline_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

require "y2network/interface_config_builder"

require_relative "test_helper"
require "y2network/interface_config_builder"

class DummyClass < Yast::Module
def initialize
Expand Down
2 changes: 2 additions & 0 deletions test/support/config_writer_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
require "y2network/connection_configs_collection"
require "y2network/interfaces_collection"
require "y2network/hostname"
require "y2network/boot_protocol"

RSpec.shared_examples "ConfigWriter" do
subject(:writer) { described_class.new }
Expand All @@ -50,6 +51,7 @@
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
conn.bootproto = Y2Network::BootProtocol::DHCP
end
end

Expand Down
48 changes: 46 additions & 2 deletions test/y2network/network_manager/config_writer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
require "y2network/connection_configs_collection"
require "y2network/interface"
require "y2network/interfaces_collection"
require "y2network/boot_protocol"
require "cfa/nm_connection"

describe Y2Network::NetworkManager::ConfigWriter do
subject(:writer) { described_class.new }
Expand All @@ -42,18 +44,32 @@
let(:config) do
old_config.copy.tap do |cfg|
cfg.add_or_update_connection_config(eth0_conn)
cfg.dns = dns
end
end

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

let(:nameserver1) { IPAddr.new("192.168.1.1") }
let(:nameserver2) { IPAddr.new("10.0.0.1") }
let(:nameserver3) { IPAddr.new("2000:e1dd:f002:0120:0000:0000:0000:0001") }
let(:nameserver4) { IPAddr.new("2000:e1dd:f002:0120:0000:0000:0000:0002") }

let(:dns) do
Y2Network::DNS.new(
nameservers: [
nameserver1, nameserver2, nameserver3, nameserver4
]
)
end

let(:ip) { Y2Network::ConnectionConfig::IPConfig.new(address: IPAddr.new("192.168.122.2")) }
let(:eth0) { Y2Network::Interface.new("eth0") }

Expand All @@ -64,7 +80,6 @@
before do
allow(Y2Network::NetworkManager::ConnectionConfigWriter).to receive(:new)
.and_return(conn_config_writer)
allow(writer).to receive(:write_dns)
allow(writer).to receive(:write_drivers)
allow(writer).to receive(:write_hostname)
allow(writer).to receive(:write_routing)
Expand All @@ -74,5 +89,34 @@
expect(conn_config_writer).to receive(:write).with(eth0_conn, nil, routes: [], parent: nil)
writer.write(config)
end

context "writes DNS configuration" do
context "when a connection has static configuration" do
let(:eth0_conn) do
Y2Network::ConnectionConfig::Ethernet.new.tap do |conn|
conn.interface = "eth0"
conn.name = "eth0"
conn.bootproto = Y2Network::BootProtocol::STATIC
conn.ip = ip
end
end

let(:file) do
CFA::NmConnection.new(File.join(DATA_PATH, "some_wifi.nmconnection"))
end

before do
allow(CFA::NmConnection).to receive(:for).and_return(file)
allow(file).to receive(:save)
end

it "includes DNS configuration in the configuration file" do
writer.write(config)
expect(file.ipv4["dns"]).to eq("#{nameserver1};#{nameserver2}")
expect(file.ipv6["dns"]).to eq("#{nameserver3};#{nameserver4}")
end
end
end

end
end

0 comments on commit 6981d43

Please sign in to comment.