Skip to content

Commit

Permalink
Fix: bootstrap: check is_nologin more robustly (bsc#1228251)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasyang2022 committed Sep 12, 2024
1 parent 28abc7c commit ec915e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
22 changes: 13 additions & 9 deletions crmsh/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#
import codecs
import os
import pwd
import subprocess
import sys
import re
Expand Down Expand Up @@ -976,15 +977,18 @@ def is_nologin(user, remote=None):
"""
Check if user's shell is nologin
"""
passwd_file = "/etc/passwd"
pattern = f"{user}:.*:/.*/nologin"
if remote:
cmd = f"cat {passwd_file}|grep {pattern}"
rc, _, _ = sh.cluster_shell().get_rc_stdout_stderr_without_input(remote, cmd)
return rc == 0
else:
with open(passwd_file) as f:
return re.search(pattern, f.read()) is not None
rc, error = sh.cluster_shell().get_rc_and_error(
remote, None,
"set -e\n"
f"shell=$(getent passwd '{user}' | awk -F: '{{ print $NF }}')\n"
'[ -n "${shell}" ] && [ -f "${shell}" ] && [ -x "${shell}" ] || exit 1\n'
'case $(basename "$shell") in\n'
' nologin) exit 1 ;;\n'
' false) exit 1 ;;\n'
'esac\n'
'"${shell}" < /dev/null &>/dev/null\n'
)
return 0 != rc


def change_user_shell(user, remote=None):
Expand Down
7 changes: 0 additions & 7 deletions test/unittests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,6 @@ def test_key_files(self, mock_gethome):
self.assertEqual(bootstrap.key_files("root"), expected_res)
mock_gethome.assert_called_once_with("root")

@mock.patch('builtins.open')
def test_is_nologin(self, mock_open_file):
data = "hacluster:x:90:90:heartbeat processes:/var/lib/heartbeat/cores/hacluster:/sbin/nologin"
mock_open_file.return_value = mock.mock_open(read_data=data).return_value
assert bootstrap.is_nologin("hacluster") is not None
mock_open_file.assert_called_once_with("/etc/passwd")

@mock.patch('crmsh.bootstrap.confirm')
@mock.patch('logging.Logger.info')
@mock.patch('crmsh.bootstrap.is_nologin')
Expand Down

0 comments on commit ec915e2

Please sign in to comment.