From 3832af5ac4beaaea7ec784772619bae2e0b8fe23 Mon Sep 17 00:00:00 2001 From: Fangge Jin Date: Thu, 12 Dec 2024 11:14:37 +0800 Subject: [PATCH 1/2] set-user-password: Fix the wrong logic of set encrypted password 1. Escape "$" in the encrypted password If "$" is not escaped in the virsh command parameter, libvirt will parse it to wrong value and set wrong password in guest. 2. Fail the test case if it can't login guest with new password Previsouly, the code only logged the message and doesn't fail the case. Signed-off-by: Fangge Jin --- libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py b/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py index 48d14a6360..94794fbfcd 100644 --- a/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py +++ b/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py @@ -76,19 +76,21 @@ def run(test, params, env): ret = process.run(cmd, shell=True) libvirt.check_exit_status(ret) en_passwd = str(ret.stdout_text.strip()) - passwd = en_passwd + passwd = en_passwd.replace('$', r'\$') ret = virsh.set_user_password(vm_name, set_user_name, passwd, encrypted=encrypted, option=option, debug=True) libvirt.check_exit_status(ret) # Login with new password + logging.debug("Trying to log in with new password") try: session = remote.wait_for_login("ssh", vm_ip, "22", set_user_name, new_passwd, r"[\#\$]\s*$", timeout=30) session.close() except remote.LoginAuthenticationError as e: logging.debug(e) + test.fail("Failed to login with new password") # Login with old password try: From 8e62241ef758a9e27617a567df289d72a5c350c0 Mon Sep 17 00:00:00 2001 From: Fangge Jin Date: Thu, 12 Dec 2024 11:46:12 +0800 Subject: [PATCH 2/2] set-user-password: Refine the code logic 1. Remove the code of logging in guest with old password as it is meaningless 2. Move the restore vm logic to finally block Signed-off-by: Fangge Jin --- .../domain/virsh_set_user_password.py | 34 ++++--------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py b/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py index 94794fbfcd..96921ec494 100644 --- a/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py +++ b/libvirt/tests/src/virsh_cmd/domain/virsh_set_user_password.py @@ -92,26 +92,13 @@ def run(test, params, env): logging.debug(e) test.fail("Failed to login with new password") - # Login with old password - try: - session = remote.wait_for_login("ssh", vm_ip, "22", set_user_name, ori_passwd, - r"[\#\$]\s*$", timeout=10) - session.close() - except remote.LoginAuthenticationError: - logging.debug("Login with old password failed as expected.") - - # Change the password back in VM - ret = virsh.set_user_password(vm_name, set_user_name, ori_passwd, False, - option=option, debug=True) - libvirt.check_exit_status(ret) - - # Login with the original password - try: - session = remote.wait_for_login("ssh", vm_ip, "22", set_user_name, ori_passwd, - r"[\#\$]\s*$", timeout=30) - session.close() - except remote.LoginAuthenticationError as e: - logging.debug(e) + finally: + # Recover VM + if vm.is_alive(): + # always restore root password in case previously case execution is broken + if status_error != "yes": + virsh.set_user_password(vm_name, set_user_name, ori_passwd, False, + option=option, debug=True) if start_ga: # Stop guest agent in vm @@ -126,12 +113,5 @@ def run(test, params, env): test.error("Deleting user '%s' got failed: '%s'" % (set_user_name, output)) session.close() - finally: - # Recover VM - if vm.is_alive(): - # always restore root password in case previously case execution is broken - if status_error != "yes": - virsh.set_user_password(vm_name, set_user_name, ori_passwd, False, - option=option, debug=True) vm.destroy(gracefully=False) vmxml_bak.sync()