1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-11 12:46:49 +00:00

feat(instances): a running VM gets its root password and SSH key through its guest agent

The Access tab's Root Password and SSH Keys worked only on a shut-off
VM, through gstfsd on the compute. A running VM whose QEMU guest agent
is connected now gets them through the agent: guest-set-user-password
with the SHA-512 crypt hash, and guest-ssh-add-authorized-keys adding
the key to root's keys. A shut-off VM still goes through gstfsd; any
other VM is refused with "Shut the VM down, or start its QEMU guest
agent, and try again", and the buttons are disabled.

Each is one agent command with a 10 s timeout. libvirt's own calls for
them (setUserPassword, authorizedSSHKeysSet) wait for a hung agent
indefinitely, holding a worker and the VM's lock, and the domain's
agent timeout would change every other agent call of the VM too.

The agent's or libvirt's error is shown as is through error_text, so
users who do not see the hosts get no connection details, and the
attempt is logged as failed. The README names the SELinux boolean a
RHEL-family guest needs before its agent may add SSH keys.
This commit is contained in:
catborise 2026-10-25 15:30:13 +03:00
parent 85b5c769e9
commit 75fcc6f6c0
7 changed files with 176 additions and 16 deletions

View file

@ -863,6 +863,11 @@ def gstfsd_request(hostname, data):
return reply
# root password and SSH key: gstfsd changes a shut-off VM's disk, the QEMU
# guest agent a running VM
AGENT_OR_SHUT_OFF = _("Shut the VM down, or start its QEMU guest agent, and try again")
@require_POST
@serialize_instance_mutation
def set_root_pass(request, pk):
@ -875,17 +880,20 @@ def set_root_pass(request, pk):
# gstfsd opens the domain by name: send the host's current one.
data = {"action": "password", "passwd": passwd_hash, "vname": instance.proxy.instance.name()}
if instance.proxy.get_status() == 5:
status = instance.proxy.get_status()
if status == 5 or (status == 1 and instance.guest_agent_ready):
try:
with audit_vm(request, instance, "reset root password"):
gstfsd_request(instance.compute.hostname, data)
except util.OperationError as err:
messages.error(request, err)
if status == 5:
gstfsd_request(instance.compute.hostname, data)
else:
instance.proxy.set_user_password("root", passwd_hash)
except (util.OperationError, libvirtError) as err:
messages.error(request, error_text(request, err))
else:
messages.success(request, _("Reset root password"))
else:
msg = _("Please shutdown down your instance and then try again")
messages.error(request, msg)
messages.error(request, AGENT_OR_SHUT_OFF)
return get_safe_redirect(
request, default=reverse("instances:instance", args=[instance.id]) + "#access"
)
@ -910,19 +918,22 @@ def add_public_key(request, pk):
"vname": instance.proxy.instance.name(),
}
if instance.proxy.get_status() == 5:
status = instance.proxy.get_status()
if status == 5 or (status == 1 and instance.guest_agent_ready):
try:
with audit_vm(request, instance, f"install SSH key {publickey.keyname}"):
gstfsd_request(instance.compute.hostname, data)
except util.OperationError as err:
messages.error(request, err)
if status == 5:
gstfsd_request(instance.compute.hostname, data)
else:
instance.proxy.add_authorized_key("root", publickey.keypublic)
except (util.OperationError, libvirtError) as err:
messages.error(request, error_text(request, err))
else:
messages.success(request, _("Installed new SSH public key %(keyname)s") % {
"keyname": publickey.keyname
})
else:
msg = _("Please shutdown down your instance and then try again")
messages.error(request, msg)
messages.error(request, AGENT_OR_SHUT_OFF)
return get_safe_redirect(
request, default=reverse("instances:instance", args=[instance.id]) + "#access"
)