diff --git a/console/tests/test_views.py b/console/tests/test_views.py index c9c9c294..20a9dfde 100644 --- a/console/tests/test_views.py +++ b/console/tests/test_views.py @@ -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): diff --git a/console/views.py b/console/views.py index 3e10bc1a..656e9238 100644 --- a/console/views.py +++ b/console/views.py @@ -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):