Skip to content

Commit

Permalink
Relocate some readers methods
Browse files Browse the repository at this point in the history
Some methods in Y2Users::Linux::BaseReaders make no sense there since
they are not compatible with Y2Users::Linux::LocalReader. Thus, they
have been placed in Y2User::Linux::Reader until they allow reading from
a specific location.
  • Loading branch information
dgdavid committed Oct 29, 2021
1 parent be5dc8c commit 2c57e9a
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 160 deletions.
71 changes: 0 additions & 71 deletions src/lib/y2users/linux/base_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# find current contact information at www.suse.com.

require "yast"
require "yast2/execute"
require "abstract_method"
require "y2users/config"
require "y2users/login_config"
Expand All @@ -29,7 +28,6 @@
require "users/ssh_authorized_keyring"

Yast.import "Autologin"
Yast.import "MailAliases"

module Y2Users
module Linux
Expand All @@ -43,12 +41,7 @@ class BaseReader
def read
Config.new.tap do |config|
read_elements(config)
read_root_aliases(config)
read_passwords(config)
read_home_permissions(config)
read_authorized_keys(config)
read_useradd_config(config)
read_login(config)
end
end

Expand Down Expand Up @@ -89,18 +82,6 @@ def read_groups
# @return [String] loaded groups from the system
abstract_method :load_groups

# Set root aliases (i.e., users receiving system mails)
#
# @see Yast::MailAliases#GetRootAlias
#
# @param config [Config]
def read_root_aliases(config)
Yast::MailAliases.GetRootAlias.split(", ").each do |name|
user = config.users.by_name(name)
user.receive_system_mail = true if user
end
end

# Parses the content retrieved by {#load_passwords} and sets user passwords
#
# @see Parsers::Shadow#parse
Expand All @@ -123,58 +104,6 @@ def read_passwords(config)
# @!method load_passwords
# @return [String] loaded passwords from the system
abstract_method :load_passwords

# Command for reading home directory permissions
STAT = "/usr/bin/stat".freeze
private_constant :STAT

# Reads home permissions
#
# @return [Array<Y2Users::User>]
def read_home_permissions(config)
config.users.reject(&:system?).each do |user|
next unless user.home && Dir.exist?(user.home.path)

user.home.permissions = Yast::Execute.locally!(
STAT, "--printf", "%a", user.home.path,
stdout: :capture
)
end
end

# Reads users authorized keys
#
# @see Yast::Users::SSHAuthorizedKeyring#read_keys
# @return [Array<Y2Users::User>]
def read_authorized_keys(config)
config.users.each do |user|
next unless user.home

user.authorized_keys = Yast::Users::SSHAuthorizedKeyring.new(user.home.path).read_keys
end
end

# Reads the configuration for useradd
#
# @param config [Config]
def read_useradd_config(config)
config.useradd = UseraddConfigReader.new.read
end

# Reads the login information
#
# @param config [Config]
def read_login(config)
Yast::Autologin.Read

return unless Yast::Autologin.used

login = LoginConfig.new
login.autologin_user = config.users.by_name(Yast::Autologin.user)
login.passwordless = Yast::Autologin.pw_less

config.login = login
end
end
end
end
84 changes: 83 additions & 1 deletion src/lib/y2users/linux/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,93 @@
require "yast2/execute"
require "y2users/linux/base_reader"

Yast.import "MailAliases"

module Y2Users
module Linux
# Reads users configuration from the system using `getent` command.
class Reader < BaseReader
private # rubocop:disable Layout/IndentationWidth
# @see BaseReader#read
# @note some of these #read_* methods could be moved to the
# {BaseReader#read} once they allow reading from a specific location
# (i.e., compatible with {LocalReader} too)
def read
config = super

read_root_aliases(config)
read_home_permissions(config)
read_authorized_keys(config)
read_useradd_config(config)
read_login(config)

config
end

private

# Set root aliases (i.e., users receiving system mails)
#
# @see Yast::MailAliases#GetRootAlias
#
# @param config [Config]
def read_root_aliases(config)
Yast::MailAliases.GetRootAlias.split(", ").each do |name|
user = config.users.by_name(name)
user.receive_system_mail = true if user
end
end

# Command for reading home directory permissions
STAT = "/usr/bin/stat".freeze
private_constant :STAT

# Reads home permissions
#
# @return [Array<Y2Users::User>]
def read_home_permissions(config)
config.users.reject(&:system?).each do |user|
next unless user.home && Dir.exist?(user.home.path)

user.home.permissions = Yast::Execute.locally!(
STAT, "--printf", "%a", user.home.path,
stdout: :capture
)
end
end

# Reads users authorized keys
#
# @see Yast::Users::SSHAuthorizedKeyring#read_keys
# @return [Array<Y2Users::User>]
def read_authorized_keys(config)
config.users.each do |user|
next unless user.home

user.authorized_keys = Yast::Users::SSHAuthorizedKeyring.new(user.home.path).read_keys
end
end

# Reads the configuration for useradd
#
# @param config [Config]
def read_useradd_config(config)
config.useradd = UseraddConfigReader.new.read
end

# Reads the login information
#
# @param config [Config]
def read_login(config)
Yast::Autologin.Read

return unless Yast::Autologin.used

login = LoginConfig.new
login.autologin_user = config.users.by_name(Yast::Autologin.user)
login.passwordless = Yast::Autologin.pw_less

config.login = login
end

# Loads entries from `passwd` database
#
Expand Down
45 changes: 7 additions & 38 deletions test/lib/y2users/linux/base_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,29 @@
require "y2users/linux/base_reader"

describe Y2Users::Linux::BaseReader do
around do |example|
# Let's use test/fixtures/home as src root for reading authorized keys from there
change_scr_root(FIXTURES_PATH.join("home")) { example.run }
end

describe "#read" do
let(:passwd_content) { File.read(File.join(FIXTURES_PATH, "/root2/etc/passwd")) }
let(:group_content) { File.read(File.join(FIXTURES_PATH, "/root2/etc/group")) }
let(:shadow_content) { File.read(File.join(FIXTURES_PATH, "/root2/etc/shadow")) }
let(:passwd_content) { File.read(File.join(FIXTURES_PATH, "/root/etc/passwd")) }
let(:group_content) { File.read(File.join(FIXTURES_PATH, "/root/etc/group")) }
let(:shadow_content) { File.read(File.join(FIXTURES_PATH, "/root/etc/shadow")) }
let(:root_home) { FIXTURES_PATH.join("home", "root").to_s }
let(:expected_root_auth_keys) { authorized_keys_from(root_home) }

before do
allow(subject.log).to receive(:warn)

# mock Yast::Execute calls and provide file content from fixture
allow(subject).to receive(:load_users).and_return(passwd_content)
allow(subject).to receive(:load_groups).and_return(group_content)
allow(subject).to receive(:load_passwords).and_return(shadow_content)

allow(subject.log).to receive(:warn)
allow(Yast::MailAliases).to receive(:GetRootAlias).and_return("games, unknown, news")

# mocks to check reading of home permissions
allow(Dir).to receive(:exist?)
allow(Dir).to receive(:exist?).with("/home/a_user").and_return(true)
allow(Yast::Execute).to receive(:locally!)
allow(Yast::Execute).to receive(:locally!)
.with("/usr/bin/stat", any_args, "/home/a_user", stdout: :capture)
.and_return("700")
end

it "generates a config with read data" do
config = subject.read

expect(config).to be_a(Y2Users::Config)

expect(config.users.size).to eq 19
expect(config.groups.size).to eq 7
expect(config.users.size).to eq 18
expect(config.groups.size).to eq 37

root_user = config.users.root
expect(root_user.uid).to eq "0"
Expand All @@ -70,7 +56,6 @@
expect(root_user.primary_group.name).to eq "root"
expect(root_user.password.value.encrypted?).to eq true
expect(root_user.password.value.content).to match(/^\$6\$pL/)
expect(root_user.authorized_keys).to eq(expected_root_auth_keys)
end

it "logs warning if password found for not existing user" do
Expand All @@ -80,21 +65,5 @@

subject.read
end

it "sets root aliases" do
config = subject.read

root_aliases = config.users.select(&:receive_system_mail?)

expect(root_aliases.size).to eq 2
expect(root_aliases.map(&:name)).to contain_exactly("games", "news")
end

it "sets home permissions" do
config = subject.read

user = config.users.by_name("a_user")
expect(user.home.permissions).to eq("700")
end
end
end
21 changes: 0 additions & 21 deletions test/lib/y2users/linux/local_reader_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,10 @@
before do
allow(Yast::Execute).to receive(:on_target!).with(/useradd/, "-D", anything)
.and_return(useradd_default_values)

allow(Yast::ShadowConfig).to receive(:fetch)
allow(Yast::ShadowConfig).to receive(:fetch).with(:umask).and_return("044")
end

describe "#read" do
context "when all expected files are present" do
let(:root_home) { FIXTURES_PATH.join("home", "root").to_s }
let(:expected_root_auth_keys) { authorized_keys_from(root_home) }

it "generates a config with read data" do
config = subject.read

Expand All @@ -61,14 +55,6 @@
expect(root_user.primary_group.name).to eq "root"
expect(root_user.password.value.encrypted?).to eq true
expect(root_user.password.value.content).to match(/^\$6\$pL/)
expect(root_user.authorized_keys).to eq(expected_root_auth_keys)

useradd = config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.create_mail_spool).to eq true
expect(useradd.umask).to eq "044"

expect(config.login?).to eq(false)
end
Expand Down Expand Up @@ -97,13 +83,6 @@
expect(config.users.size).to eq 0
expect(config.groups.size).to eq 0

useradd = config.useradd
expect(useradd.group).to eq "100"
expect(useradd.expiration).to eq ""
expect(useradd.inactivity_period).to eq(-1)
expect(useradd.create_mail_spool).to eq true
expect(useradd.umask).to eq "044"

expect(config.login?).to eq(false)
end
end
Expand Down
Loading

0 comments on commit 2c57e9a

Please sign in to comment.