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

fix(appsettings): settings that new VMs and clones cannot use are refused

Disk I/O native was saved with a disk cache other than none or
directsync; libvirt refuses such a disk ("io='native' needs either no
disk cache or directsync cache mode"), so every new VM failed. The pair
is now checked together when either is saved.

A clone name prefix with a space or another character a clone's name
cannot have was saved; every name the clone dialog then offered a user
who is not a superuser was refused, and they could not clone at all.
The prefix now takes letters, digits and '-' only.
This commit is contained in:
catborise 2026-11-12 14:11:29 +03:00
parent d00f7d8841
commit 3bd519c935
6 changed files with 97 additions and 34 deletions

View file

@ -73,6 +73,8 @@ class AppSettingsTestCase(TestCase):
def post(self, key, value):
response = self.client.post(reverse("appsettings"), {key: value})
# not followed: the messages would stay in the cookie for the next post
self.client.cookies.pop("messages", None)
return [str(m) for m in get_messages(response.wsgi_request)]
def test_a_listed_setting_takes_only_its_choices(self):
@ -99,6 +101,30 @@ class AppSettingsTestCase(TestCase):
page = self.client.get(reverse("appsettings")).content.decode()
self.assertIn('name="CLONE_INSTANCE_DEFAULT_PREFIX" value="web-"', page)
def test_the_clone_name_prefix_makes_clone_names(self):
# every name offered to a user who is not a superuser would be refused
for bad in ("bad prefix", "web_"):
with self.subTest(bad=bad):
self.assertTrue(any("letters, digits and '-' only" in m for m in self.post("CLONE_INSTANCE_DEFAULT_PREFIX", bad)))
self.assertEqual(AppSettings.objects.get(key="CLONE_INSTANCE_DEFAULT_PREFIX").value, "instance")
def test_native_disk_io_needs_a_cache_libvirt_takes_with_it(self):
# libvirt: "io='native' needs either no disk cache or directsync cache mode"
self.post("INSTANCE_VOLUME_DEFAULT_CACHE", "writeback")
self.assertTrue(any("needs disk cache none or directsync" in m
for m in self.post("INSTANCE_VOLUME_DEFAULT_IO", "native")))
self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_IO").value, "default")
response = self.client.post(reverse("appsettings"), {"INSTANCE_VOLUME_DEFAULT_CACHE": "none",
"INSTANCE_VOLUME_DEFAULT_IO": "native"})
self.assertEqual(response.status_code, 302)
self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_IO").value, "native")
self.assertTrue(any("needs disk cache" in m for m in self.post("INSTANCE_VOLUME_DEFAULT_CACHE", "unsafe")))
self.assertEqual(AppSettings.objects.get(key="INSTANCE_VOLUME_DEFAULT_CACHE").value, "none")
# a pair saved before does not block another setting
AppSettings.objects.filter(key="INSTANCE_VOLUME_DEFAULT_CACHE").update(value="writeback")
self.post("LOGS_PER_PAGE", "50")
self.assertEqual(AppSettings.objects.get(key="LOGS_PER_PAGE").value, "50")
def page(self):
from lxml import html

View file

@ -1,3 +1,4 @@
import re
from pathlib import Path
from admin.decorators import superuser_only
@ -66,6 +67,18 @@ def refusal(setting, value):
elif setting.choices and value not in setting.choices_as_list():
return _("%(setting)s must be one of %(choices)s") % {
"setting": setting.name, "choices": ", ".join(setting.choices_as_list())}
elif setting.key == "CLONE_INSTANCE_DEFAULT_PREFIX" and not re.fullmatch(r"[a-zA-Z0-9-]+", value):
# a clone's name is the prefix and a number, and takes only these
return _("%(setting)s takes letters, digits and '-' only") % {"setting": setting.name}
return None
def combined_refusal(values):
"""Why settings that work only together cannot be saved as values
(key: value, saved or new), or None."""
# libvirt refuses such a disk: every new VM would fail
if values["INSTANCE_VOLUME_DEFAULT_IO"] == "native" and values["INSTANCE_VOLUME_DEFAULT_CACHE"] not in ("none", "directsync"):
return _("Disk I/O native needs disk cache none or directsync")
return None
@ -98,6 +111,11 @@ def appsettings(request):
key=lambda item: item[0].name)
changed = [(s, value) for s, value in posted if value != request.POST.get(f"was-{s.key}", s.value)]
refusals = [refused for refused in (refusal(s, value) for s, value in changed) if refused]
# checked only when the pair changes: a pair saved before does not block other settings
pair = {"INSTANCE_VOLUME_DEFAULT_IO", "INSTANCE_VOLUME_DEFAULT_CACHE"}
if not refusals and pair & {s.key for s, _v in changed} and (refused := combined_refusal(
{key: s.value for key, s in settings_by_key.items()} | {s.key: value for s, value in changed})):
refusals = [refused]
for refused in refusals:
messages.error(request, refused)
if changed and not refusals: