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

fix(networks): the XML editor does not overwrite a change made meanwhile

The network and filter XML editors defined what they sent as a whole, so
a page opened before another admin's change (a QoS limit, a filter rule)
saved over it and the change was gone without a word. The editor now
sends a digest of the definition it showed, and a definition changed
since is refused with a reload hint.
This commit is contained in:
catborise 2026-11-12 13:02:20 +03:00
parent 534782fdda
commit d00f7d8841
16 changed files with 151 additions and 98 deletions

View file

@ -485,9 +485,12 @@ class wvmNetwork(wvmConnect):
limit.getparent().remove(limit)
self._define(tree)
def edit_network(self, new_xml):
def edit_network(self, new_xml, seen):
"""Defines this network anew from new_xml: of the same name and, if it
names one, the same UUID, not another network."""
names one, the same UUID, not another network; seen is the digest of
the saved definition the editor showed."""
if seen != util.digest(self.saved_xml):
raise util.OperationError(gettext_noop("Network %(net)s was changed meanwhile; reload the page"), net=self.net.name())
tree = util.parse_xml(new_xml)
if tree.tag != "network" or tree.findtext("name") != self.net.name():
raise util.OperationError(gettext_noop("The XML must define network %(net)s"), net=self.net.name())

View file

@ -161,9 +161,12 @@ class wvmNWFilter(wvmNWFilters):
self._define(tree)
return elements[index]
def edit(self, xml):
def edit(self, xml, seen):
"""Define the filter anew from the XML entered: the same filter, of
its name and UUID."""
its name and UUID; seen is the digest of the XML (get_xml) the editor
showed."""
if seen != util.digest(self.get_xml()):
raise util.OperationError(gettext_noop("Filter %(name)s was changed meanwhile; reload the page"), name=self.get_name())
tree = util.parse_xml(xml)
if tree.tag != "filter" or tree.get("name") != self.get_name():
raise util.OperationError(gettext_noop("The XML must define filter %(name)s"), name=self.get_name())

View file

@ -15,7 +15,7 @@ from lxml import etree
from vrtManager.network import network_size, wvmNetwork, wvmNetworks
from vrtManager.tests.driver import without_inactive_flag
from vrtManager.tests.fakes import DUID, lease
from vrtManager.util import OperationError
from vrtManager.util import OperationError, digest
NODE = """<node>
<network><name>lab</name><bridge name='virbr9'/>
@ -155,12 +155,13 @@ class NetworkChangeTests(SimpleTestCase):
xml = ('<!DOCTYPE network [<!ENTITY name SYSTEM "file:///etc/hostname">]>'
"<network><name>&name;</name></network>")
with self.assertRaisesRegex(OperationError, "may not declare a DTD"):
net.edit_network(xml)
net.edit_network(xml, digest(net.saved_xml))
def test_the_xml_editor_takes_an_encoding_declaration(self):
self.conn.networkLookupByName("lab").destroy()
net = self.network()
net.edit_network('<?xml version="1.0" encoding="UTF-8"?>\n' + net.saved_xml.replace("virbr9", "virbr6"))
net.edit_network('<?xml version="1.0" encoding="UTF-8"?>\n' + net.saved_xml.replace("virbr9", "virbr6"),
digest(net.saved_xml))
self.assertEqual(self.saved().xpath("bridge/@name"), ["virbr6"])
def test_a_fixed_address_is_of_the_subnet_and_its_own(self):
@ -180,10 +181,21 @@ class NetworkChangeTests(SimpleTestCase):
(xml.replace(net.net.UUIDString(), "11111111-2222-3333-4444-555555555555"), "keep the network's UUID"),
("<network>", "not valid")):
with self.subTest(why=why), self.assertRaisesRegex(OperationError, why):
net.edit_network(other)
net.edit_network(xml.replace("virbr9", "virbr7"))
net.edit_network(other, digest(xml))
net.edit_network(xml.replace("virbr9", "virbr7"), digest(xml))
self.assertEqual(self.saved().xpath("bridge/@name"), ["virbr7"])
def test_the_xml_editor_does_not_undo_a_change_made_meanwhile(self):
# another admin set a limit after this editor showed the definition
self.conn.networkLookupByName("lab").destroy()
net = self.network()
shown = net.saved_xml
net.set_qos("inbound", 100, 200, 300)
with self.assertRaisesRegex(OperationError, "Network lab was changed meanwhile"):
net.edit_network(shown.replace("virbr9", "virbr7"), digest(shown))
self.assertEqual((self.saved().xpath("bridge/@name"), self.saved().xpath("bandwidth/inbound/@average")),
(["virbr9"], ["100"]))
def test_a_network_that_does_not_start_is_undefined(self):
gateway, mask, dhcp = network_size("192.0.2.0/24", True)
with patch.object(libvirt.virNetwork, "create", side_effect=libvirt.libvirtError("in use by virbr0")):

View file

@ -10,7 +10,7 @@ import libvirt
from django.test import SimpleTestCase
from lxml import etree
from vrtManager.nwfilters import wvmNWFilter, wvmNWFilters
from vrtManager.util import OperationError
from vrtManager.util import OperationError, digest
UUID = "6d1f7a10-0000-4000-8000-00000000c0de"
WEB = (f"<filter name='web' chain='root'><uuid>{UUID}</uuid>"
@ -132,9 +132,12 @@ class IdentityTests(SimpleTestCase):
(WEB.replace(f"<uuid>{UUID}</uuid>", ""), "keep the filter's UUID"),
(WEB.replace(UUID, "11111111-2222-3333-4444-555555555555"), "keep the filter's UUID")):
with self.subTest(why=why), self.assertRaisesRegex(OperationError, why):
conn.edit(xml)
conn.edit(xml, digest(conn.get_xml()))
conn.wvm.nwfilterDefineXML.assert_not_called()
conn.edit(WEB.replace("priority='500'", "priority='400'"))
# an editor that showed the filter as it was before another change
with self.assertRaisesRegex(OperationError, "Filter web was changed meanwhile"):
conn.edit(WEB, digest(WEB.replace("priority='500'", "priority='300'")))
conn.edit(WEB.replace("priority='500'", "priority='400'"), digest(conn.get_xml()))
conn.wvm.nwfilterDefineXML.assert_called_once()

View file

@ -1,4 +1,5 @@
from django.utils.translation import gettext_noop
import hashlib
import ipaddress
import secrets
import re
@ -30,6 +31,12 @@ def render(text, values, translate):
return text % {key: shown(value) for key, value in values.items()} if values else text
def digest(text):
"""A fingerprint of a definition: a page sends back the one of the
definition it showed, and a change made meanwhile is not overwritten."""
return hashlib.sha256(text.encode()).hexdigest()
class OperationError(libvirt.libvirtError):
"""An error with WebVirtCloud's own message (see message()). Its args are
the English message, for logs and the audit log; str() is the message in