mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
fix(networks): a fixed address has a host name, a host address and is changed as shown
dnsmasq skips a fixed address whose name is not a host name ("bad DHCP
host name"): the VM got no fixed address while the page said it was set.
A name now has only letters, digits, '.', '_' and '-', as the dialog
already asks.
An IPv4 fixed address or DHCP range end may no longer be the subnet's
network or broadcast address.
The edit dialog sends the entry's MAC or DUID and name as the page showed
them; an entry another admin changed meanwhile is refused instead of being
changed back.
This commit is contained in:
parent
e264104e85
commit
a78d92e7aa
9 changed files with 202 additions and 142 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from django.utils.translation import gettext_noop
|
||||
import copy
|
||||
import ipaddress
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from functools import cached_property
|
||||
|
||||
|
|
@ -356,6 +357,9 @@ class wvmNetwork(wvmConnect):
|
|||
raise util.OperationError(outside, address=address, subnet=ip["subnet"])
|
||||
if address == ipaddress.ip_address(ip["address"]):
|
||||
raise util.OperationError(gettext_noop("%(address)s is the host's own address on the network"), address=address)
|
||||
subnet = ip["subnet"]
|
||||
if subnet.version == 4 and subnet.prefixlen < 31 and address in (subnet.network_address, subnet.broadcast_address):
|
||||
raise util.OperationError(gettext_noop("%(address)s is not a host address of %(subnet)s"), address=address, subnet=subnet)
|
||||
return address
|
||||
|
||||
def modify_dhcp_range(self, index, range_start, range_end):
|
||||
|
|
@ -385,14 +389,17 @@ class wvmNetwork(wvmConnect):
|
|||
self.update(VIR_NETWORK_UPDATE_COMMAND_DELETE, VIR_NETWORK_SECTION_IP_DHCP_HOST,
|
||||
etree.tostring(host, with_tail=False).decode(), index)
|
||||
|
||||
def modify_fixed_address(self, index, original, name, address, mac_duid):
|
||||
def modify_fixed_address(self, index, original, name, address, mac_duid, seen=None):
|
||||
"""Adds a fixed address, or changes the one that had the address
|
||||
original, keeping what the page does not show of it (a client id, a
|
||||
lease time). The address must be of the subnet, not the host's, and
|
||||
neither it, the MAC (IPv4) or DUID (IPv6) nor the name another entry's.
|
||||
A change deletes the entry and adds it anew, put back if that fails:
|
||||
libvirt's own change finds an entry by any of its MAC, address or name,
|
||||
another entry's too, and an IPv6 one never by its DUID. Returns False
|
||||
another entry's too, and an IPv6 one never by its DUID. seen: the
|
||||
entry's MAC or DUID and name as the page showed them, refused when
|
||||
another change has made them different meanwhile. A new name must be
|
||||
a host name: dnsmasq drops an entry with another one. Returns False
|
||||
when nothing changes."""
|
||||
_tree, ip, info = self._ip(index)
|
||||
address = self._checked_address(info, address, "fixed address")
|
||||
|
|
@ -401,8 +408,11 @@ class wvmNetwork(wvmConnect):
|
|||
old = None
|
||||
if original:
|
||||
old = next((h for h in hosts if h.get("ip") == original), None)
|
||||
if old is None:
|
||||
raise util.OperationError(gettext_noop("The fixed address %(original)s is gone meanwhile; reload the page"), original=original)
|
||||
if old is None or (seen is not None and seen != ((old.get("mac") or old.get("id") or "").lower(), old.get("name", ""))):
|
||||
raise util.OperationError(gettext_noop("The fixed address %(original)s has changed meanwhile; reload the page"), original=original)
|
||||
# a new name must be one dnsmasq takes; one set outside the panel stays as it is
|
||||
if name and (old is None or name != old.get("name")) and not re.fullmatch(r"[A-Za-z0-9_.-]+", name):
|
||||
raise util.OperationError(gettext_noop("The name %(name)s may have only letters, digits, '.', '_' and '-'"), name=name)
|
||||
for other in hosts:
|
||||
if other is old:
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -111,10 +111,34 @@ class NetworkChangeTests(SimpleTestCase):
|
|||
self.assertEqual([dict(h.attrib) for h in saved.xpath("ip/dhcp/host")], [{"id": "00:01:00:01:aa:bb", "ip": "2001:db8:5::11"}])
|
||||
|
||||
def test_an_entry_gone_meanwhile_is_not_added_anew(self):
|
||||
with self.assertRaisesRegex(OperationError, "is gone meanwhile"):
|
||||
with self.assertRaisesRegex(OperationError, "has changed meanwhile"):
|
||||
self.network().modify_fixed_address(1, "192.0.2.99", "x", "192.0.2.40", "")
|
||||
self.assertEqual(self.saved().xpath("ip[2]/dhcp/host/@ip"), ["192.0.2.30", "192.0.2.31"])
|
||||
|
||||
def test_an_entry_changed_meanwhile_is_not_changed_back(self):
|
||||
# another admin gave 192.0.2.31 a name after this page showed it without one
|
||||
net = self.network()
|
||||
with self.assertRaisesRegex(OperationError, "192.0.2.31 has changed meanwhile"):
|
||||
net.modify_fixed_address(1, "192.0.2.31", "db", "192.0.2.31", "52:54:00:00:00:32", ("52:54:00:00:00:31", "web"))
|
||||
net.modify_fixed_address(1, "192.0.2.31", "db", "192.0.2.31", "52:54:00:00:00:32", ("52:54:00:00:00:31", ""))
|
||||
self.assertEqual(self.saved().xpath("ip[2]/dhcp/host[@ip='192.0.2.31']/@mac"), ["52:54:00:00:00:32"])
|
||||
|
||||
def test_a_name_dnsmasq_would_drop_is_refused(self):
|
||||
# dnsmasq skips an entry whose name is not a host name: the VM would get no fixed address
|
||||
with self.assertRaisesRegex(OperationError, "may have only letters"):
|
||||
self.network().modify_fixed_address(1, "", "vm one", "192.0.2.40", "52:54:00:00:00:40")
|
||||
self.network().modify_fixed_address(1, "", "vm_1.lab", "192.0.2.40", "52:54:00:00:00:40")
|
||||
self.assertEqual(self.saved().xpath("ip[2]/dhcp/host[@ip='192.0.2.40']/@name"), ["vm_1.lab"])
|
||||
|
||||
def test_a_name_set_outside_the_panel_stays_editable(self):
|
||||
# dnsmasq may take an internationalized name the panel would not give
|
||||
conn = self.conn.networkLookupByName("lab")
|
||||
conn.destroy()
|
||||
self.conn.networkDefineXML(conn.XMLDesc(0).replace(
|
||||
"<host name='paul'", "<host mac='52:54:00:00:00:41' name='vm.münchen' ip='192.0.2.41'/><host name='paul'"))
|
||||
self.network().modify_fixed_address(1, "192.0.2.41", "vm.münchen", "192.0.2.42", "52:54:00:00:00:41")
|
||||
self.assertEqual(self.saved().xpath("ip[2]/dhcp/host[@mac='52:54:00:00:00:41']/@ip"), ["192.0.2.42"])
|
||||
|
||||
def test_a_running_network_saved_with_other_addresses_is_not_changed_live(self):
|
||||
# parentIndex would name another <ip> of the running network
|
||||
net = self.network()
|
||||
|
|
@ -142,7 +166,8 @@ class NetworkChangeTests(SimpleTestCase):
|
|||
def test_a_fixed_address_is_of_the_subnet_and_its_own(self):
|
||||
net = self.network()
|
||||
for address, why in (("198.51.100.5", "is not in 192.0.2.0/24"), ("192.0.2.1", "the host's own address"),
|
||||
("192.0.2.31", "another entry's fixed address"), ("x", "is not an IP address")):
|
||||
("192.0.2.31", "another entry's fixed address"), ("x", "is not an IP address"),
|
||||
("192.0.2.0", "not a host address of 192.0.2.0/24"), ("192.0.2.255", "not a host address")):
|
||||
with self.subTest(address=address), self.assertRaisesRegex(OperationError, why):
|
||||
net.modify_fixed_address(1, "192.0.2.30", "paul", address, "")
|
||||
self.assertIs(net.modify_fixed_address(1, "192.0.2.30", "paul", "192.0.2.30", ""), False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue