diff mbox series

[v1] tst_test.sh: Add support for localhost ssh key setup

Message ID 20240918100344.21316-1-wegao@suse.com
State Changes Requested
Headers show
Series [v1] tst_test.sh: Add support for localhost ssh key setup | expand

Commit Message

Wei Gao Sept. 18, 2024, 10:03 a.m. UTC
When RHOST=localhost, ssh@localhost will encounter error since
no correct setup for authorized_keys and known_hosts etc.

Signed-off-by: Wei Gao <wegao@suse.com>
---
 testcases/lib/tst_test.sh               | 32 +++++++++++++++++++++++++
 testcases/network/tcp_cmds/ftp/ftp01.sh | 12 ++++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)

Comments

Martin Doucha Sept. 18, 2024, 11:46 a.m. UTC | #1
Hi,
this kind of configuration should be done by the test runner. LTP should 
not modify global system configuration unless doing so is a key part of 
the test scenario itself.

The test runner can also create an alias for the lftp command so that 
the test can call it using the old name.

On 18. 09. 24 12:03, Wei Gao via ltp wrote:
> When RHOST=localhost, ssh@localhost will encounter error since
> no correct setup for authorized_keys and known_hosts etc.
> 
> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>   testcases/lib/tst_test.sh               | 32 +++++++++++++++++++++++++
>   testcases/network/tcp_cmds/ftp/ftp01.sh | 12 ++++++++--
>   2 files changed, 42 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
> index c19c30b76..6df16bd17 100644
> --- a/testcases/lib/tst_test.sh
> +++ b/testcases/lib/tst_test.sh
> @@ -784,6 +784,34 @@ tst_run()
>   	_tst_do_exit
>   }
>   
> +_tst_setup_localhost_ssh()
> +{
> +	if [ -z "$TST_SSHD_CONFIG" ]; then
> +		TST_SSHD_CONFIG="/etc/ssh/sshd_config"
> +	fi
> +
> +	if [ -z "$TST_SSH_DIR" ]; then
> +		TST_SSH_DIR="/root/.ssh/"
> +	fi
> +
> +	if [ ! -e "$TST_SSHD_CONFIG" ]; then
> +		echo 'PermitRootLogin yes' >$TST_SSHD_CONFIG
> +	elif [ ! `grep "^PermitRootLogin *yes" $TST_SSHD_CONFIG | wc -l` -gt 0 ]; then
> +		echo 'PermitRootLogin yes' >>$TST_SSHD_CONFIG
> +	fi
> +
> +	if [ ! -e "$TST_SSH_DIR/id_rsa" ]; then
> +		ssh-keygen -q -N "" -t rsa -b 4096 -f $TST_SSH_DIR/id_rsa
> +	fi
> +
> +	if [ -e "$TST_SSH_DIR/id_rsa.pub" ]; then
> +		cat $TST_SSH_DIR/id_rsa.pub >> $TST_SSH_DIR/authorized_keys
> +		ssh-keyscan -H localhost >> $TST_SSH_DIR/known_hosts
> +	fi
> +
> +	systemctl restart sshd
> +}
> +
>   _tst_run_iterations()
>   {
>   	local _tst_i=$TST_ITERATIONS
> @@ -910,6 +938,10 @@ if [ -z "$TST_NO_DEFAULT_RUN" ]; then
>   	tst_res TINFO "Running: $(basename $0) $TST_ARGS"
>   	tst_res TINFO "Tested kernel: $(uname -a)"
>   
> +	if [ "$TST_NEEDS_LOCALHOST_SSH" = 1 ]; then
> +		_tst_setup_localhost_ssh
> +	fi
> +
>   	OPTIND=1
>   
>   	while getopts ":hi:$TST_OPTS" _tst_name $TST_ARGS; do
> diff --git a/testcases/network/tcp_cmds/ftp/ftp01.sh b/testcases/network/tcp_cmds/ftp/ftp01.sh
> index 53d1eec53..8ec7f4fca 100755
> --- a/testcases/network/tcp_cmds/ftp/ftp01.sh
> +++ b/testcases/network/tcp_cmds/ftp/ftp01.sh
> @@ -4,13 +4,21 @@
>   # Copyright (c) 2003 Manoj Iyer <manjo@mail.utexas.edu>
>   # Copyright (c) 2001 Robbie Williamson <robbiew@us.ibm.com>
>   
> +TST_SETUP=setup
>   TST_TESTFUNC=do_test
>   TST_CNT=4
>   TST_NEEDS_CMDS='awk ftp'
>   TST_NEEDS_TMPDIR=1
> +TST_NEEDS_LOCALHOST_SSH=1
>   
>   RUSER="${RUSER:-root}"
>   RHOST="${RHOST:-localhost}"
> +FTP_CLIENT_CMD="ftp -nv"
> +
> +setup()
> +{
> +    grep -q 'sles' /etc/os-release && FTP_CLIENT_CMD='lftp'
> +}
>   
>   do_test()
>   {
> @@ -41,7 +49,7 @@ test_get()
>               echo cd $TST_NET_DATAROOT
>               echo get $file
>               echo quit
> -        } | ftp -nv $RHOST
> +        } | $FTP_CLIENT_CMD $RHOST
>   
>           sum1="$(ls -l $file | awk '{print $5}')"
>           sum2="$(ls -l $TST_NET_DATAROOT/$file | awk '{print $5}')"
> @@ -62,7 +70,7 @@ test_put()
>               echo cd $TST_TMPDIR
>               echo put $file
>               echo quit
> -        } | ftp -nv $RHOST
> +        } | $FTP_CLIENT_CMD $RHOST
>   
>           sum1="$(tst_rhost_run -c "sum $TST_TMPDIR/$file" -s | awk '{print $1}')"
>           sum2="$(sum $TST_NET_DATAROOT/$file | awk '{print $1}')"
Wei Gao Sept. 24, 2024, 10:15 a.m. UTC | #2
On Wed, Sep 18, 2024 at 01:46:30PM +0200, Martin Doucha wrote:
> Hi,
> this kind of configuration should be done by the test runner. LTP should not
> modify global system configuration unless doing so is a key part of the test
> scenario itself.
Will remove ssh configuration part to test runner.
> 
> The test runner can also create an alias for the lftp command so that the
> test can call it using the old name.
Create an alias for lftp not enough since current code use parameter "-nv" which
not supported by lftp so following error will popup:

lftp: invalid option -- 'n'
Try `lftp --help' for more information

> 
> On 18. 09. 24 12:03, Wei Gao via ltp wrote:
> > When RHOST=localhost, ssh@localhost will encounter error since
> > no correct setup for authorized_keys and known_hosts etc.
> > 
> > Signed-off-by: Wei Gao <wegao@suse.com>
> > ---
> >   testcases/lib/tst_test.sh               | 32 +++++++++++++++++++++++++
> >   testcases/network/tcp_cmds/ftp/ftp01.sh | 12 ++++++++--
> >   2 files changed, 42 insertions(+), 2 deletions(-)
> > 
> > diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
> > index c19c30b76..6df16bd17 100644
> > --- a/testcases/lib/tst_test.sh
> > +++ b/testcases/lib/tst_test.sh
> > @@ -784,6 +784,34 @@ tst_run()
> >   	_tst_do_exit
> >   }
> > +_tst_setup_localhost_ssh()
> > +{
> > +	if [ -z "$TST_SSHD_CONFIG" ]; then
> > +		TST_SSHD_CONFIG="/etc/ssh/sshd_config"
> > +	fi
> > +
> > +	if [ -z "$TST_SSH_DIR" ]; then
> > +		TST_SSH_DIR="/root/.ssh/"
> > +	fi
> > +
> > +	if [ ! -e "$TST_SSHD_CONFIG" ]; then
> > +		echo 'PermitRootLogin yes' >$TST_SSHD_CONFIG
> > +	elif [ ! `grep "^PermitRootLogin *yes" $TST_SSHD_CONFIG | wc -l` -gt 0 ]; then
> > +		echo 'PermitRootLogin yes' >>$TST_SSHD_CONFIG
> > +	fi
> > +
> > +	if [ ! -e "$TST_SSH_DIR/id_rsa" ]; then
> > +		ssh-keygen -q -N "" -t rsa -b 4096 -f $TST_SSH_DIR/id_rsa
> > +	fi
> > +
> > +	if [ -e "$TST_SSH_DIR/id_rsa.pub" ]; then
> > +		cat $TST_SSH_DIR/id_rsa.pub >> $TST_SSH_DIR/authorized_keys
> > +		ssh-keyscan -H localhost >> $TST_SSH_DIR/known_hosts
> > +	fi
> > +
> > +	systemctl restart sshd
> > +}
> > +
> >   _tst_run_iterations()
> >   {
> >   	local _tst_i=$TST_ITERATIONS
> > @@ -910,6 +938,10 @@ if [ -z "$TST_NO_DEFAULT_RUN" ]; then
> >   	tst_res TINFO "Running: $(basename $0) $TST_ARGS"
> >   	tst_res TINFO "Tested kernel: $(uname -a)"
> > +	if [ "$TST_NEEDS_LOCALHOST_SSH" = 1 ]; then
> > +		_tst_setup_localhost_ssh
> > +	fi
> > +
> >   	OPTIND=1
> >   	while getopts ":hi:$TST_OPTS" _tst_name $TST_ARGS; do
> > diff --git a/testcases/network/tcp_cmds/ftp/ftp01.sh b/testcases/network/tcp_cmds/ftp/ftp01.sh
> > index 53d1eec53..8ec7f4fca 100755
> > --- a/testcases/network/tcp_cmds/ftp/ftp01.sh
> > +++ b/testcases/network/tcp_cmds/ftp/ftp01.sh
> > @@ -4,13 +4,21 @@
> >   # Copyright (c) 2003 Manoj Iyer <manjo@mail.utexas.edu>
> >   # Copyright (c) 2001 Robbie Williamson <robbiew@us.ibm.com>
> > +TST_SETUP=setup
> >   TST_TESTFUNC=do_test
> >   TST_CNT=4
> >   TST_NEEDS_CMDS='awk ftp'
> >   TST_NEEDS_TMPDIR=1
> > +TST_NEEDS_LOCALHOST_SSH=1
> >   RUSER="${RUSER:-root}"
> >   RHOST="${RHOST:-localhost}"
> > +FTP_CLIENT_CMD="ftp -nv"
> > +
> > +setup()
> > +{
> > +    grep -q 'sles' /etc/os-release && FTP_CLIENT_CMD='lftp'
> > +}
> >   do_test()
> >   {
> > @@ -41,7 +49,7 @@ test_get()
> >               echo cd $TST_NET_DATAROOT
> >               echo get $file
> >               echo quit
> > -        } | ftp -nv $RHOST
> > +        } | $FTP_CLIENT_CMD $RHOST
> >           sum1="$(ls -l $file | awk '{print $5}')"
> >           sum2="$(ls -l $TST_NET_DATAROOT/$file | awk '{print $5}')"
> > @@ -62,7 +70,7 @@ test_put()
> >               echo cd $TST_TMPDIR
> >               echo put $file
> >               echo quit
> > -        } | ftp -nv $RHOST
> > +        } | $FTP_CLIENT_CMD $RHOST
> >           sum1="$(tst_rhost_run -c "sum $TST_TMPDIR/$file" -s | awk '{print $1}')"
> >           sum2="$(sum $TST_NET_DATAROOT/$file | awk '{print $1}')"
> 
> -- 
> Martin Doucha   mdoucha@suse.cz
> SW Quality Engineer
> SUSE LINUX, s.r.o.
> CORSO IIa
> Krizikova 148/34
> 186 00 Prague 8
> Czech Republic
>
Martin Doucha Sept. 24, 2024, 12:08 p.m. UTC | #3
On 24. 09. 24 12:15, Wei Gao wrote:
> On Wed, Sep 18, 2024 at 01:46:30PM +0200, Martin Doucha wrote:
>> The test runner can also create an alias for the lftp command so that the
>> test can call it using the old name.
> Create an alias for lftp not enough since current code use parameter "-nv" which
> not supported by lftp so following error will popup:
> 
> lftp: invalid option -- 'n'
> Try `lftp --help' for more information

In that case we can either leave the test as is or detect the available 
ftp client in setup():

if tst_cmd_available ftp; then
	FTP_CMD="ftp -nv"
elif tst_cmd_available lftp; then
	FTP_CMD="lftp -v --norc"
else
	tst_brkm TCONF "No FTP client found"
fi
diff mbox series

Patch

diff --git a/testcases/lib/tst_test.sh b/testcases/lib/tst_test.sh
index c19c30b76..6df16bd17 100644
--- a/testcases/lib/tst_test.sh
+++ b/testcases/lib/tst_test.sh
@@ -784,6 +784,34 @@  tst_run()
 	_tst_do_exit
 }
 
+_tst_setup_localhost_ssh()
+{
+	if [ -z "$TST_SSHD_CONFIG" ]; then
+		TST_SSHD_CONFIG="/etc/ssh/sshd_config"
+	fi
+
+	if [ -z "$TST_SSH_DIR" ]; then
+		TST_SSH_DIR="/root/.ssh/"
+	fi
+
+	if [ ! -e "$TST_SSHD_CONFIG" ]; then
+		echo 'PermitRootLogin yes' >$TST_SSHD_CONFIG
+	elif [ ! `grep "^PermitRootLogin *yes" $TST_SSHD_CONFIG | wc -l` -gt 0 ]; then
+		echo 'PermitRootLogin yes' >>$TST_SSHD_CONFIG
+	fi
+
+	if [ ! -e "$TST_SSH_DIR/id_rsa" ]; then
+		ssh-keygen -q -N "" -t rsa -b 4096 -f $TST_SSH_DIR/id_rsa
+	fi
+
+	if [ -e "$TST_SSH_DIR/id_rsa.pub" ]; then
+		cat $TST_SSH_DIR/id_rsa.pub >> $TST_SSH_DIR/authorized_keys
+		ssh-keyscan -H localhost >> $TST_SSH_DIR/known_hosts
+	fi
+
+	systemctl restart sshd
+}
+
 _tst_run_iterations()
 {
 	local _tst_i=$TST_ITERATIONS
@@ -910,6 +938,10 @@  if [ -z "$TST_NO_DEFAULT_RUN" ]; then
 	tst_res TINFO "Running: $(basename $0) $TST_ARGS"
 	tst_res TINFO "Tested kernel: $(uname -a)"
 
+	if [ "$TST_NEEDS_LOCALHOST_SSH" = 1 ]; then
+		_tst_setup_localhost_ssh
+	fi
+
 	OPTIND=1
 
 	while getopts ":hi:$TST_OPTS" _tst_name $TST_ARGS; do
diff --git a/testcases/network/tcp_cmds/ftp/ftp01.sh b/testcases/network/tcp_cmds/ftp/ftp01.sh
index 53d1eec53..8ec7f4fca 100755
--- a/testcases/network/tcp_cmds/ftp/ftp01.sh
+++ b/testcases/network/tcp_cmds/ftp/ftp01.sh
@@ -4,13 +4,21 @@ 
 # Copyright (c) 2003 Manoj Iyer <manjo@mail.utexas.edu>
 # Copyright (c) 2001 Robbie Williamson <robbiew@us.ibm.com>
 
+TST_SETUP=setup
 TST_TESTFUNC=do_test
 TST_CNT=4
 TST_NEEDS_CMDS='awk ftp'
 TST_NEEDS_TMPDIR=1
+TST_NEEDS_LOCALHOST_SSH=1
 
 RUSER="${RUSER:-root}"
 RHOST="${RHOST:-localhost}"
+FTP_CLIENT_CMD="ftp -nv"
+
+setup()
+{
+    grep -q 'sles' /etc/os-release && FTP_CLIENT_CMD='lftp'
+}
 
 do_test()
 {
@@ -41,7 +49,7 @@  test_get()
             echo cd $TST_NET_DATAROOT
             echo get $file
             echo quit
-        } | ftp -nv $RHOST
+        } | $FTP_CLIENT_CMD $RHOST
 
         sum1="$(ls -l $file | awk '{print $5}')"
         sum2="$(ls -l $TST_NET_DATAROOT/$file | awk '{print $5}')"
@@ -62,7 +70,7 @@  test_put()
             echo cd $TST_TMPDIR
             echo put $file
             echo quit
-        } | ftp -nv $RHOST
+        } | $FTP_CLIENT_CMD $RHOST
 
         sum1="$(tst_rhost_run -c "sum $TST_TMPDIR/$file" -s | awk '{print $1}')"
         sum2="$(sum $TST_NET_DATAROOT/$file | awk '{print $1}')"