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

fix(console): Reconnect after a migration opens the console on the VM's new compute

A console page's link names the compute the VM ran on. After a migration
moved the VM, the page's Reconnect asked for it there and got "User does
not have permission to access console or host/instance not exist". The
console page now sends the user to the VM's new compute when that is the
only VM with the UUID the user may open; others still get the same 403.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 18:58:05 +03:00
parent 1488baea7a
commit 450ffbba30
2 changed files with 27 additions and 1 deletions

View file

@ -183,6 +183,26 @@ class ConsoleViewsTestCase(TestCase):
self.assertNotContains(response, "vm-alpha", status_code=403)
self.assertNotIn("token", response.context)
def test_a_migrated_vms_console_link_leads_to_its_new_compute(self):
# the page's Reconnect after a migration, which moved the VM's row
other = Compute.objects.create(name="other", hostname="192.0.2.7", login="root", password="", type=2)
Instance.objects.filter(pk=self.instance.pk).update(compute=other)
self.client.force_login(self.alice)
response = self.client.get(self.url)
self.assertRedirects(response, reverse("console", args=[other.id, self.instance.uuid]),
fetch_redirect_response=False)
# who may not open it learns nothing
self.client.force_login(self.bob)
self.assertEqual(self.client.get(self.url).status_code, 403)
def test_a_uuid_on_two_other_computes_is_not_guessed(self):
Instance.objects.filter(pk=self.instance.pk).delete()
for name, hostname in (("c2", "192.0.2.8"), ("c3", "192.0.2.9")):
compute = Compute.objects.create(name=name, hostname=hostname, login="root", password="", type=2)
Instance.objects.create(compute=compute, name="vm-alpha", uuid=self.instance.uuid)
self.client.force_login(self.admin_user)
self.assertEqual(self.client.get(self.url).status_code, 403)
@override_settings(SERIAL_CONSOLE_ENABLED=False)
@patch("console.views.wvmInstance")
def test_a_console_that_cannot_open_gets_no_console_client(self, mock_wvm):

View file

@ -8,7 +8,7 @@ from pathlib import Path
from appsettings.settings import app_settings
from console import tokens
from django.conf import settings
from django.shortcuts import render
from django.shortcuts import redirect, render
from django.utils.translation import gettext_lazy as _
from instances.models import Instance
from instances.utils import can_open_console
@ -42,6 +42,12 @@ def console(request, compute_id, uuid):
"""The console page of the VM with this UUID on this compute. A console
that cannot open gets a page that says why, and no console client."""
instance = Instance.objects.filter(compute_id=compute_id, uuid=uuid).first()
if instance is None:
# migrated since the page was opened: a reconnect finds the VM on its
# new compute, when it is the only one with that UUID the user may open
moved = [vm for vm in Instance.objects.filter(uuid=uuid) if can_open_console(request.user, vm)]
if len(moved) == 1:
return redirect("console", moved[0].compute_id, uuid)
# Same answer for "no such VM" and "not allowed", so the response
# does not reveal whether a VM exists.
if instance is None or not can_open_console(request.user, instance):