mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-01 16:36:06 +00:00
fix(instances): a new VM cannot take the MAC address of another VM on its host
The create form and API took a MAC address for a new VM's NIC without looking at the host's other VMs, so a VM could be defined with a MAC another one has. Running on one network, neither NIC is reached reliably. A clone already refused this among its own NICs. The create now refuses a MAC a NIC of a VM on the host has, in any form the field takes (case, colons), including a NIC added for a VM's next boot only. The worker checks again before it defines the VM, since another VM may take the address while the job is queued. Turkish and Chinese texts included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
parent
590b8d956d
commit
9fa898a856
6 changed files with 376 additions and 333 deletions
|
|
@ -15,8 +15,8 @@ from django.utils.translation import gettext_noop
|
|||
from django.utils.translation import gettext_noop as _
|
||||
from libvirt import (VIR_CONNECT_LIST_STORAGE_POOLS_ACTIVE, VIR_DOMAIN_JOB_NONE, VIR_DOMAIN_SNAPSHOT_LIST_EXTERNAL,
|
||||
VIR_DOMAIN_SHUTOFF, VIR_DOMAIN_UNDEFINE_KEEP_NVRAM, VIR_DOMAIN_UNDEFINE_MANAGED_SAVE,
|
||||
VIR_DOMAIN_UNDEFINE_NVRAM, VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, VIR_ERR_NO_DOMAIN,
|
||||
VIR_ERR_NO_STORAGE_VOL, libvirtError)
|
||||
VIR_DOMAIN_UNDEFINE_NVRAM, VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA, VIR_DOMAIN_XML_INACTIVE,
|
||||
VIR_ERR_NO_DOMAIN, VIR_ERR_NO_STORAGE_VOL, libvirtError)
|
||||
from lxml import etree
|
||||
from vrtManager import util
|
||||
from vrtManager.connection import NVRAM_DIR, wvmConnect
|
||||
|
|
@ -28,7 +28,7 @@ from vrtManager.storage import FILE_POOL_TYPES
|
|||
from accounts.models import UserInstance, UserSSHKey
|
||||
from appsettings.settings import app_settings
|
||||
from computes.models import Compute
|
||||
from instances import utils
|
||||
from instances import addresses, utils
|
||||
from instances.models import Instance
|
||||
from logs.audit import write
|
||||
from operations import claims
|
||||
|
|
@ -836,6 +836,23 @@ def _check_disk_name(conn, data):
|
|||
raise util.OperationError(_("Image has already exist. Please check volumes or change instance name"))
|
||||
|
||||
|
||||
def _check_macs(conn, data):
|
||||
"""Two NICs of one MAC on a network reach neither reliably."""
|
||||
try:
|
||||
macs = {addresses.parse_mac(mac) for mac in utils.nic_macs(data["mac"], data["networks"])}
|
||||
except ValueError as err:
|
||||
raise util.OperationError(str(err))
|
||||
if macs:
|
||||
used = set()
|
||||
for dom in conn.wvm.listAllDomains():
|
||||
used |= addresses.nic_macs(dom.XMLDesc(0))
|
||||
if dom.isPersistent(): # a NIC added for the next boot only
|
||||
used |= addresses.nic_macs(dom.XMLDesc(VIR_DOMAIN_XML_INACTIVE))
|
||||
if used & macs:
|
||||
raise util.OperationError(_("MAC address %(mac)s is a NIC's of another VM on this host"),
|
||||
mac=", ".join(sorted(used & macs)))
|
||||
|
||||
|
||||
@register
|
||||
class CreateInstance(Kind):
|
||||
name = "instance.create"
|
||||
|
|
@ -884,10 +901,7 @@ class CreateInstance(Kind):
|
|||
pool = conn.get_storage(_seed_pool(data))
|
||||
if util.get_xml_path(pool.XMLDesc(0), "/pool/@type") not in FILE_POOL_TYPES:
|
||||
raise util.OperationError(_("The cloud-init seed needs a directory storage pool for the disk"))
|
||||
try:
|
||||
utils.nic_macs(data["mac"], data["networks"])
|
||||
except ValueError as err:
|
||||
raise util.OperationError(str(err))
|
||||
_check_macs(conn, data)
|
||||
if data["firmware"] and data["firmware"] not in conn.get_firmwares(data["arch"], data["machine"]):
|
||||
raise util.OperationError(_("This host offers no firmware %(firmware)s"), firmware=data["firmware"])
|
||||
if data["tpm"] and not conn.get_tpm_model(data["arch"], data["machine"]):
|
||||
|
|
@ -910,6 +924,7 @@ class CreateInstance(Kind):
|
|||
if op.params["name"] in conn.get_instances():
|
||||
raise util.OperationError(_("A virtual machine with this name already exists"))
|
||||
_check_disk_name(conn, op.params)
|
||||
_check_macs(conn, op.params)
|
||||
|
||||
def run(self, op, ctx):
|
||||
data = op.params
|
||||
|
|
|
|||
|
|
@ -187,6 +187,23 @@ class CreateJobTestCase(TransactionTestCase):
|
|||
with self.assertRaises(PermissionDenied):
|
||||
CreateInstance().precheck(op, None)
|
||||
|
||||
def test_a_mac_of_another_vm_on_the_host_is_refused(self):
|
||||
url = f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/"
|
||||
nic = "<domain><devices><interface><mac address='%s'/></interface></devices></domain>"
|
||||
other = MagicMock()
|
||||
other.isPersistent.return_value = 1
|
||||
# its NIC is added for its next boot only
|
||||
other.XMLDesc.side_effect = lambda flags: nic % "52:54:00:aa:bb:cc" if flags else "<domain/>"
|
||||
self.conn.wvm.listAllDomains.return_value = [other]
|
||||
response = self.client.post(url, {**BODY, "mac": "52:54:00:AA:BB:CC"}, content_type="application/json")
|
||||
self.assertContains(response, "MAC address 52:54:00:aa:bb:cc is a NIC's of another VM", status_code=400)
|
||||
response = self.client.post(url, {**BODY, "mac": "52:54:00:aa:bb:cd"}, content_type="application/json")
|
||||
self.assertEqual(response.status_code, 202)
|
||||
# another VM took it while the job was queued
|
||||
other.XMLDesc.side_effect = lambda flags: nic % "52:54:00:aa:bb:cd"
|
||||
with self.assertRaisesMessage(libvirt.libvirtError, "MAC address 52:54:00:aa:bb:cd is a NIC's of another VM"):
|
||||
CreateInstance().precheck(Operation.objects.get(), None)
|
||||
|
||||
def test_the_api_takes_a_body_without_a_mac_or_a_network_filter(self):
|
||||
body = {k: v for k, v in BODY.items() if k not in ("mac", "nwfilter")}
|
||||
response = self.client.post(f"/api/v1/computes/{self.compute.pk}/instances/create/x86_64/q35/", body,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue