Skip to content

Commit 2e0e2b5

Browse files
committed
test_session_record_pipe_io_stdin: remove sshpass
Removing reliance on sshpass by adding code in setup to generate an ssh key and use it in the test code.
1 parent 661833d commit 2e0e2b5

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

lib/tlitest/test_tlog_rec_session.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import inspect
66
from tempfile import mkdtemp
7-
from subprocess import Popen, PIPE, STDOUT
7+
from subprocess import Popen, PIPE, STDOUT, run
88
import pytest
99

1010
from misc import check_recording, mklogfile, mkcfgfile, \
@@ -22,6 +22,25 @@ def utempter_enabled():
2222
return 'libutempter.so' in stdout_data
2323

2424

25+
@pytest.fixture
26+
def gen_ssh_key(request):
27+
def _del_ssh_key(user):
28+
# Remove ssh key to ensure no tests affected later
29+
run(f'sed -i "/stdin_test/d" ~{user}/.ssh/authorized_keys', shell=True)
30+
run(f'rm -rf ~/.ssh/id_rsa_{user}*', shell=True)
31+
32+
def _gen_ssh_key(user):
33+
# Generate SSH Key for test
34+
run(f'rm -rf ~/.ssh/id_rsa_{user}*', shell=True)
35+
run(f'ssh-keygen -t rsa -b 2048 -N "" -C stdin_test -f ~/.ssh/id_rsa_{user}', shell=True)
36+
run(f"mkdir -p ~{user}/.ssh", shell=True)
37+
run(f"cat ~/.ssh/id_rsa_{user}.pub >> ~{user}/.ssh/authorized_keys", shell=True)
38+
run(f"chown -R {user}:{user} ~{user}/.ssh", shell=True)
39+
request.addfinalizer(lambda: _del_ssh_key(user))
40+
41+
return _gen_ssh_key
42+
43+
2544
class TestTlogRecSession:
2645
""" Test tlog-rec-session functionality """
2746
user = 'tlitestlocaluser2'
@@ -217,10 +236,14 @@ def test_session_record_pipe_io_stdin(self):
217236
"""
218237
text_in_stdio = 'print("hello world")\n'
219238
text_out = "hello world"
220-
p = Popen(['sshpass', '-p', 'Secret123', 'ssh', '-o',
221-
'StrictHostKeyChecking=no',
222-
'tlitestlocaluser2@localhost', 'python3'],
223-
stdout=PIPE, stdin=PIPE, stderr=PIPE, encoding='utf8')
239+
240+
sessionclass = TlogRecSessionConfig(writer="syslog")
241+
sessionclass.generate_config(SYSTEM_TLOG_REC_SESSION_CONF)
242+
243+
p = Popen(['ssh', '-i', f'~/.ssh/id_rsa_{self.user}',
244+
'-o', 'StrictHostKeyChecking=no',
245+
f'{self.user}@localhost', 'python3'],
246+
stdout=PIPE, stdin=PIPE, stderr=PIPE, encoding='utf8')
224247
stdout_data = p.communicate(input=text_in_stdio)[0]
225248
assert text_out in stdout_data
226249

src/tlitest/tlitest-setup

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ python3-pytest
88
python3-pexpect
99
python3-systemd
1010
tcsh
11-
sshpass
1211
"
1312

1413
[[ -z "${CONTAINER_ENV}" ]] && PKGS+="tlog"
@@ -61,3 +60,20 @@ echo "%wheel ALL=(ALL) NOPASSWD: ALL" > \
6160
usermod tlitestlocaladmin1 -aG wheel,systemd-journal
6261
usermod tlitestlocaluser1 -aG systemd-journal
6362
usermod tlitestlocaluser2 -s /usr/bin/tlog-rec-session
63+
64+
# some environments disable password authentication
65+
# tlog tests need this to run currently
66+
echo "Adding sshd config to enable password authentication"
67+
cat > /etc/ssh/sshd_config.d/00-tlog-override.conf <<EOF
68+
PasswordAuthentication yes
69+
EOF
70+
chmod 600 /etc/ssh/sshd_config.d/00-tlog-override.conf
71+
systemctl restart sshd
72+
73+
# Generate ssh key for testing
74+
user="tlitestlocaluser2"
75+
rm -rf ~/.ssh/id_rsa_${user}*
76+
ssh-keygen -t rsa -b 2048 -N "" -C stdin_test -f ~/.ssh/id_rsa_${user}
77+
mkdir -p ~${user}/.ssh
78+
cat ~/.ssh/id_rsa_${user}.pub >> ~${user}/.ssh/authorized_keys
79+
chown -R ${user}:${user} ~${user}/.ssh

src/tlitest/tlitest-teardown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,14 @@ if [ -f /etc/sudoers.d/01_wheel_nopass_tlitest ]; then
4848
echo "Found test sudoers file...removing"
4949
rm /etc/sudoers.d/01_wheel_nopass_tlitest
5050
fi
51+
52+
if [ -f /etc/ssh/sshd_config.d/00-tlog-override.conf ]; then
53+
echo "Found sshd config override for password authentication...removing"
54+
rm /etc/ssh/sshd_config.d/00-tlog-override.conf
55+
systemctl restart sshd
56+
fi
57+
58+
user="tlitestlocaluser2"
59+
if [ -f ~/.ssh/id_rsa_${user} ]; then
60+
rm -rf ~/.ssh/id_rsa_${user}*
61+
fi

0 commit comments

Comments
 (0)