1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(jobs): VM create and volume clone check the user and the name again in the worker

Every other job re-checks its permission and state in precheck before any
effect; these two checked only when queued. A queued create of a user who
is no longer a superuser, or whose VM name a host took meanwhile, ran; so
did a volume clone whose name a volume made outside the panel took.
This commit is contained in:
catborise 2026-11-05 16:00:32 +03:00
parent c1658ace13
commit a33a802368
4 changed files with 49 additions and 0 deletions

View file

@ -792,6 +792,16 @@ class CreateInstance(Kind):
label=f"{compute.name}/{params['name']}",
intent={"compute": compute.pk, "uuid": util.randomUUID(), "name": params["name"]})
def precheck(self, op, ctx):
if op.user is None or not op.user.is_active or not op.user.is_superuser:
raise PermissionDenied
# a VM found on a host since the job was queued may have taken the name
if Instance.objects.filter(name__exact=op.params["name"]).exists():
raise util.OperationError(_("There is an instance with same name. Remove it and try again!"))
c = Compute.objects.get(pk=op.params["compute"])
if op.params["name"] in wvmCreate(c.hostname, c.login, c.password, c.type).get_instances():
raise util.OperationError(_("A virtual machine with this name already exists"))
def run(self, op, ctx):
data = op.params
c = Compute.objects.get(pk=data["compute"])

View file

@ -6,6 +6,7 @@ from unittest.mock import MagicMock, patch
import libvirt
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.test import TransactionTestCase
from appsettings.settings import get_settings
@ -114,3 +115,19 @@ class CreateJobTestCase(TransactionTestCase):
self.assertEqual(CreateInstance().reconcile(op).state, Operation.FAILED)
pool.listVolumes.return_value = ["api-vm.qcow2"]
self.assertEqual(CreateInstance().reconcile(op).state, Operation.UNCERTAIN)
def test_the_worker_checks_the_user_and_the_name_again(self):
response = self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/", BODY,
content_type="application/json")
self.assertEqual(response.status_code, 202)
op = Operation.objects.get()
CreateInstance().precheck(op, None)
# a VM of that name found on a host meanwhile
self.conn.get_instances.return_value = ["api-vm"]
with self.assertRaisesMessage(libvirt.libvirtError, "A virtual machine with this name already exists"):
CreateInstance().precheck(op, None)
# the user is no longer a superuser
op.user.is_superuser = False
op.user.save()
with self.assertRaises(PermissionDenied):
CreateInstance().precheck(op, None)