mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 01:16:10 +00:00
fix(instances): a Windows VM's root password and SSH key are refused in words
On a running Windows VM with its guest agent connected, the Access tab's root password and SSH key were sent to the agent, which has no root account, takes no crypt(3) hash and has no SSH key command: the owner got the agent's raw error. The guest's OS is now asked first, and a Windows guest is refused with a message.
This commit is contained in:
parent
87f7100fc9
commit
3412c63417
7 changed files with 496 additions and 424 deletions
|
|
@ -57,7 +57,7 @@ from webvirtcloud.middleware import error_text, same_origin_referer
|
|||
from vrtManager import util
|
||||
from vrtManager.connection import CONNECT_TIMEOUT
|
||||
from vrtManager.create import cpu_choices, wvmCreate
|
||||
from vrtManager.instance import USAGE_MAX_WINDOW, wvmInstances
|
||||
from vrtManager.instance import USAGE_MAX_WINDOW, VIR_ERR_AGENT_COMMAND_TIMEOUT, wvmInstances
|
||||
from vrtManager.interface import wvmInterface
|
||||
from vrtManager.nodedevs import (check_hostdevs, device_names, hostdev_problems, hostdev_users, managed_pci,
|
||||
node_devices, pci_problems, usb_unmatched)
|
||||
|
|
@ -1075,11 +1075,6 @@ def gstfsd_request(compute, data):
|
|||
AGENT_OR_SHUT_OFF = gettext_lazy("Shut the VM down, or start its QEMU guest agent, and try again")
|
||||
|
||||
|
||||
# libvirt's value; bindings built against older libvirt (Ubuntu 24.04's) lack
|
||||
# the name, and the code comes from the host's libvirtd
|
||||
VIR_ERR_AGENT_COMMAND_TIMEOUT = 112
|
||||
|
||||
|
||||
def access_error(request, err):
|
||||
"""The error of a root password or SSH key change. A command that timed
|
||||
out was sent to the agent, which still runs it when it answers again."""
|
||||
|
|
|
|||
Binary file not shown.
File diff suppressed because it is too large
Load diff
Binary file not shown.
File diff suppressed because it is too large
Load diff
|
|
@ -97,6 +97,10 @@ USAGE_MAX_WINDOW = 30
|
|||
USAGE_STATS = VIR_DOMAIN_STATS_CPU_TOTAL | VIR_DOMAIN_STATS_BLOCK | VIR_DOMAIN_STATS_INTERFACE
|
||||
# seconds the guest agent has to set a password or add a key
|
||||
AGENT_TIMEOUT = 10
|
||||
# libvirt's values; bindings built against older libvirt (Ubuntu 24.04's) lack
|
||||
# the names, and the code comes from the host's libvirtd
|
||||
VIR_ERR_AGENT_COMMAND_TIMEOUT = 112
|
||||
VIR_ERR_AGENT_COMMAND_FAILED = 113 # the agent answered with an error: a command it lacks or has disabled
|
||||
# A running VM is cloned with a push backup that keeps the VM alive when the
|
||||
# guest shuts down during the copy: libvirt 11.10.
|
||||
LIVE_CLONE_MIN_LIBVIRT = 11_010_000
|
||||
|
|
@ -3253,15 +3257,31 @@ class wvmInstance(wvmConnect):
|
|||
|
||||
return util.get_xml_path(self._XMLDesc(PERSISTENT_XML if config else 0), func=_get_agent)
|
||||
|
||||
def check_not_windows(self):
|
||||
"""Windows has no root account, and its agent takes no crypt(3) hash
|
||||
and no SSH keys: refused in words, not with the agent's error."""
|
||||
try:
|
||||
osinfo = agent_command(self.instance, "guest-get-osinfo")
|
||||
except libvirtError as err:
|
||||
if err.get_error_code() == VIR_ERR_AGENT_COMMAND_FAILED:
|
||||
return # an agent without the command, or with it disabled: not known to be Windows
|
||||
if err.get_error_code() == VIR_ERR_AGENT_COMMAND_TIMEOUT: # not the change's timeout, which may still run
|
||||
raise util.OperationError(gettext_noop("The guest agent did not answer in time; nothing was changed")) from err
|
||||
raise
|
||||
if osinfo.get("id") == "mswindows":
|
||||
raise util.OperationError(gettext_noop("The VM runs Windows: its guest agent sets no root password or SSH key"))
|
||||
|
||||
def set_user_password(self, user, crypted):
|
||||
"""Set a user's password in the running guest through its agent;
|
||||
crypted is a crypt(3) hash."""
|
||||
self.check_not_windows()
|
||||
password = base64.b64encode(crypted.encode()).decode()
|
||||
agent_command(self.instance, "guest-set-user-password", {"username": user, "password": password, "crypted": True})
|
||||
|
||||
def add_authorized_key(self, user, key):
|
||||
"""Add an SSH public key to the user's authorized keys in the running
|
||||
guest through its agent."""
|
||||
self.check_not_windows()
|
||||
agent_command(self.instance, "guest-ssh-add-authorized-keys", {"username": user, "keys": [key], "reset": False})
|
||||
|
||||
def is_agent_ready(self):
|
||||
|
|
|
|||
|
|
@ -14,6 +14,12 @@ from vrtManager import instance as module
|
|||
from vrtManager.instance import wvmInstance
|
||||
|
||||
|
||||
def coded(code, text):
|
||||
err = libvirtError(text)
|
||||
err.get_error_code = lambda: code
|
||||
return err
|
||||
|
||||
|
||||
class AgentAccessTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.vm = wvmInstance.__new__(wvmInstance) # no connection: only these calls
|
||||
|
|
@ -23,10 +29,14 @@ class AgentAccessTestCase(unittest.TestCase):
|
|||
self.addCleanup(patcher.stop)
|
||||
|
||||
def sent(self):
|
||||
(dom, command, timeout, flags), = [c.args for c in self.agent.call_args_list]
|
||||
self.assertIs(dom, self.vm.instance)
|
||||
self.assertEqual((timeout, flags), (10, 0))
|
||||
return json.loads(command)
|
||||
# the guest's OS first, then the change
|
||||
calls = [c.args for c in self.agent.call_args_list]
|
||||
for dom, _, timeout, flags in calls:
|
||||
self.assertIs(dom, self.vm.instance)
|
||||
self.assertEqual((timeout, flags), (10, 0))
|
||||
osinfo, change = [json.loads(command) for _, command, _, _ in calls]
|
||||
self.assertEqual(osinfo, {"execute": "guest-get-osinfo"})
|
||||
return change
|
||||
|
||||
def test_the_password_goes_as_a_hash(self):
|
||||
self.vm.set_user_password("root", "$6$salt$hash")
|
||||
|
|
@ -42,3 +52,32 @@ class AgentAccessTestCase(unittest.TestCase):
|
|||
self.agent.side_effect = libvirtError("guest agent didn't respond to command within '10' seconds")
|
||||
with self.assertRaisesRegex(libvirtError, "within '10' seconds"):
|
||||
self.vm.set_user_password("root", "$6$salt$hash")
|
||||
|
||||
def test_a_windows_guest_is_refused_in_words(self):
|
||||
# its agent has no root account to change and takes no hash or SSH key
|
||||
self.agent.return_value = '{"return":{"id":"mswindows","name":"Microsoft Windows"}}'
|
||||
for change in (lambda: self.vm.set_user_password("root", "$6$salt$hash"),
|
||||
lambda: self.vm.add_authorized_key("root", "ssh-ed25519 AAAA laptop")):
|
||||
with self.assertRaisesRegex(module.util.OperationError, "runs Windows"):
|
||||
change()
|
||||
self.assertEqual([json.loads(c.args[1])["execute"] for c in self.agent.call_args_list],
|
||||
["guest-get-osinfo", "guest-get-osinfo"])
|
||||
|
||||
def test_an_agent_without_osinfo_still_gets_the_change(self):
|
||||
# an agent that lacks guest-get-osinfo or has it disabled answers with an error
|
||||
def answer(dom, command, timeout, flags):
|
||||
if json.loads(command)["execute"] == "guest-get-osinfo":
|
||||
raise coded(module.VIR_ERR_AGENT_COMMAND_FAILED, "Command guest-get-osinfo has been disabled")
|
||||
return '{"return":{}}'
|
||||
|
||||
self.agent.side_effect = answer
|
||||
self.vm.add_authorized_key("root", "ssh-ed25519 AAAA laptop")
|
||||
self.assertEqual(self.sent()["execute"], "guest-ssh-add-authorized-keys")
|
||||
|
||||
def test_an_osinfo_that_times_out_says_nothing_was_changed(self):
|
||||
# not the timeout of the change, which the agent may still run
|
||||
self.agent.side_effect = coded(module.VIR_ERR_AGENT_COMMAND_TIMEOUT, "guest agent command timed out")
|
||||
with self.assertRaisesRegex(module.util.OperationError, "nothing was changed"):
|
||||
self.vm.set_user_password("root", "$6$salt$hash")
|
||||
self.assertEqual(self.agent.call_count, 1)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue