Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom shell when creating system users #906

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions charmhelpers/core/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def init_is_systemd(service_name=None):
return os.path.isdir(SYSTEMD_SYSTEM)


def adduser(username, password=None, shell='/bin/bash',
def adduser(username, password=None, shell=None,
system_user=False, primary_group=None,
secondary_groups=None, uid=None, home_dir=None):
"""Add a user to the system.
Expand Down Expand Up @@ -389,11 +389,14 @@ def adduser(username, password=None, shell='/bin/bash',
if home_dir:
cmd.extend(['--home', str(home_dir)])
if system_user or password is None:
cmd.append('--system')
cmd.extend([
'--system',
'--shell', shell if shell else '/usr/sbin/nologin'
])
else:
cmd.extend([
'--create-home',
'--shell', shell,
'--shell', shell if shell else '/bin/bash',
'--password', password,
])
if not primary_group:
Expand Down
28 changes: 28 additions & 0 deletions tests/core/test_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ def test_adduser_with_groups(self, log, check_call, getpwnam, getgrnam):
@patch.object(host, 'log')
def test_adds_a_systemuser(self, log, check_call, getpwnam):
username = 'johndoe'
shell = '/usr/sbin/nologin'
existing_user_pwnam = KeyError('user not found')
new_user_pwnam = 'some user pwnam'

Expand All @@ -766,6 +767,7 @@ def test_adds_a_systemuser(self, log, check_call, getpwnam):
check_call.assert_called_with([
'useradd',
'--system',
'--shell', shell,
username
])
getpwnam.assert_called_with(username)
Expand All @@ -775,6 +777,7 @@ def test_adds_a_systemuser(self, log, check_call, getpwnam):
@patch.object(host, 'log')
def test_adds_a_systemuser_with_home_dir(self, log, check_call, getpwnam):
username = 'johndoe'
shell = '/usr/sbin/nologin'
existing_user_pwnam = KeyError('user not found')
new_user_pwnam = 'some user pwnam'

Expand All @@ -789,6 +792,29 @@ def test_adds_a_systemuser_with_home_dir(self, log, check_call, getpwnam):
'--home',
'/var/lib/johndoe',
'--system',
'--shell', shell,
username
])
getpwnam.assert_called_with(username)

@patch('pwd.getpwnam')
@patch('subprocess.check_call')
@patch.object(host, 'log')
def test_adds_a_systemuser_with_different_shell(self, log, check_call, getpwnam):
username = 'johndoe'
shell = '/bin/bash'
existing_user_pwnam = KeyError('user not found')
new_user_pwnam = 'some user pwnam'

getpwnam.side_effect = [existing_user_pwnam, new_user_pwnam]

result = host.adduser(username, system_user=True, shell=shell)

self.assertEqual(result, new_user_pwnam)
check_call.assert_called_with([
'useradd',
'--system',
'--shell', shell,
username
])
getpwnam.assert_called_with(username)
Expand All @@ -801,6 +827,7 @@ def test_adds_a_systemuser_with_home_dir(self, log, check_call, getpwnam):
def test_add_user_uid(self, log, check_call, getgrnam, getpwuid, getpwnam):
user_name = 'james'
user_id = 1111
shell = '/usr/sbin/nologin'
uid_key_error = KeyError('user not found')
getpwuid.side_effect = uid_key_error
host.adduser(user_name, uid=user_id)
Expand All @@ -810,6 +837,7 @@ def test_add_user_uid(self, log, check_call, getgrnam, getpwuid, getpwnam):
'--uid',
str(user_id),
'--system',
'--shell', shell,
'-g',
user_name,
user_name
Expand Down
Loading