Skip to content

Commit

Permalink
Merge pull request #4129 from 6-dehan/KVMAUTOMA-2587-support-ssh-windows
Browse files Browse the repository at this point in the history
qemu_guest_agent: Add ssh-key injection support for windows
  • Loading branch information
vivianQizhu authored Dec 16, 2024
2 parents f96cee8 + dabb8f2 commit d4cb7fe
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 19 deletions.
38 changes: 30 additions & 8 deletions qemu/tests/cfg/qemu_guest_agent.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -549,30 +549,52 @@
input_dev_type_input2 = mouse
input_dev_type_input3 = tablet
- gagent_ssh_public_key_injection:
only Linux
no RHEL.7 RHEL.8 RHEL.9.0 RHEL.9.1
gagent_check_type = ssh_public_key_injection
set_sebool = "setsebool virt_qemu_ga_read_nonsecurity_files on ; setsebool virt_qemu_ga_manage_ssh on"
cmd_clean_keys = rm -rf ~/.ssh/*
ssh_keygen_cmd = "ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa"
cmd_get_hostkey = "cat ~/.ssh/id_rsa.pub"
add_line_at_end = "echo >> ${guest_homepath}/.ssh/authorized_keys"
cmd_get_guestkey = "cat ${guest_homepath}/.ssh/authorized_keys"
cmd_del_key_file = "rm -rf ${guest_homepath}/.ssh/authorized_keys"
cmd_install_sshpass = "dnf -y install sshpass"
# Please set user password
# guest_user_passwd =
variants:
- root:
only Linux
guest_user = "root"
guest_homepath = /${guest_user}
test_login_guest = ssh ${guest_user}@%s -o stricthostkeychecking=no ls ${guest_homepath}
test_login_guest = ssh ${guest_user}@%s -o StrictHostKeyChecking=no ls ${guest_homepath}
output_check_str = 'anaconda-ks.cfg'
- non_root_user:
only Linux
guest_user = "fedora"
guest_user_passwd = "redhat"
guest_homepath = "/home/${guest_user}"
cmd_add_user_set_passwd = useradd ${guest_user} && echo ${guest_user_passwd} | passwd --stdin ${guest_user}
cmd_add_user_set_passwd = "useradd ${guest_user} && echo %s | passwd --stdin ${guest_user}"
cmd_remove_user = userdel -rf ${guest_user}
test_login_guest = ssh ${guest_user}@%s -o stricthostkeychecking=no ls '/home'
test_login_guest = ssh ${guest_user}@%s -o StrictHostKeyChecking=no ls '/home'
output_check_str = '${guest_user}'
add_line_at_end = "echo >> ${guest_homepath}/.ssh/authorized_keys"
cmd_get_guestkey = "cat ${guest_homepath}/.ssh/authorized_keys"
cmd_del_key_file = "rm -rf ${guest_homepath}/.ssh/authorized_keys"
- administrator:
only Windows
guest_user = "Administrator"
guest_homepath = "C:\Users\${guest_user}"
cmd_get_guestkey = "powershell.exe Get-Content C:\ProgramData\ssh\administrators_authorized_keys"
- non_admin_user:
only Windows
guest_user = "nonadminuser"
guest_homepath = "C:\Users\${guest_user}"
guest_sshdir = "${guest_homepath}\.ssh"
cmd_add_user_set_passwd = "powershell.exe $securePassword = ConvertTo-SecureString -String %s -AsPlainText -Force;New-LocalUser -Name ${guest_user} -Password $securePassword -FullName 'New User' -Description 'Standard non-admin user';Add-LocalGroupMember -Group 'Users' -Member ${guest_user}"
cmd_remove_user = "powershell.exe Remove-LocalUser -Name ${guest_user}"
cmd_get_guestkey = "powershell.exe Get-Content ${guest_homepath}\.ssh\authorized_keys"
Windows:
install_config_openssh = "powershell.exe Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force; WIN_UTILS:\Install_config_OpenSSH.ps1"
# Please fill out guest_user_passwd before running test
cmd_sshpass = 'sshpass -p %s ssh ${guest_user}@%s -o StrictHostKeyChecking=no dir "${guest_homepath}"'
test_login_guest = ssh ${guest_user}@%s -o StrictHostKeyChecking=no dir "${guest_homepath}"
output_check_str = "Downloads"
- check_get_cpustats:
only Linux
no RHEL.7 RHEL.8 RHEL.9.1 RHEL.9.0
Expand Down
39 changes: 28 additions & 11 deletions qemu/tests/qemu_guest_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1417,10 +1417,10 @@ def ssh_key_test(operation, guest_name, *keys, **kwargs):
op_func(guest_name, *keys, **kwargs)
keys_ga = self.gagent.ssh_get_authorized_keys(guest_name)

add_line_at_end = params["add_line_at_end"]
if os_type == "linux":
add_line_at_end = params["add_line_at_end"]
session.cmd(add_line_at_end)
cmd_guest_keys = params["cmd_get_guestkey"]

session.cmd(add_line_at_end)
keys_guest = session.cmd_output(cmd_guest_keys).strip()
_value_compared_ga_guest(keys_ga, keys_guest, operation)
return keys_ga, keys_guest
Expand All @@ -1433,15 +1433,25 @@ def _prepared_n_restore_env(prepare=True):
"""

if prepare:
output = session.cmd_output("getenforce")
if str(output) == "Permissive":
session.cmd("setenforce 1")
session.cmd(params["set_sebool"])
if guest_user != "root":
session.cmd(params["cmd_add_user_set_passwd"])
cmd_install_sshpass = self.params["cmd_install_sshpass"]
status = process.system(cmd_install_sshpass, shell=True)
if status:
test.error("sshpass install failed, Please install it.")
if os_type == "linux":
if session.cmd_output("getenforce") == "Permissive":
session.cmd("setenforce 1")
session.cmd(params["set_sebool"])
else:
install_config_openssh_cmd = utils_misc.set_winutils_letter(
session, self.params["install_config_openssh"]
)
session.cmd(install_config_openssh_cmd, timeout=720)
if guest_user not in ["root", "Administrator"]:
session.cmd(params["cmd_add_user_set_passwd"] % guest_user_passwd)
else:
session.cmd(params["cmd_del_key_file"])
if guest_user != "root":
if os_type == "linux":
session.cmd(params["cmd_del_key_file"])
if guest_user not in ["root", "Administrator"]:
session.cmd(params["cmd_remove_user"])

def _generate_host_keys():
Expand Down Expand Up @@ -1500,11 +1510,18 @@ def _value_compared_ga_guest(return_value_ga, return_value_guest, status):
mac_addr = self.vm.get_mac_address()
os_type = self.params["os_type"]
guest_user = self.params["guest_user"]
guest_user_passwd = self.params["guest_user_passwd"]
output_check_str = self.params["output_check_str"]
guest_ip_ipv4 = utils_net.get_guest_ip_addr(session, mac_addr, os_type)
_prepared_n_restore_env()

error_context.context("Check the basic function ", LOG_JOB.info)
if os_type == "windows":
cmd_sshpass = params["cmd_sshpass"] % (
guest_user_passwd,
guest_ip_ipv4,
)
process.system(cmd_sshpass, shell=True)
host_key1 = _generate_host_keys()
ssh_key_test("add", guest_user, host_key1, reset=False)
_login_guest_test(guest_ip_ipv4)
Expand Down

0 comments on commit d4cb7fe

Please sign in to comment.