1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 19:26:48 +00:00
webvirtcloud/virtsecrets/tests/test_live_secrets.py
catborise 66151d42b1 fix(virtsecrets): a secret's usage value goes into its XML as text
create_secret wrote the usage value into the secret XML with an f-string,
unchecked: a quote, "<" or "&" broke the XML or added to it. The page's
pattern ([a-z0-9. ]+) was the only check and refused real values: a
volume secret's path ("/") and an iSCSI IQN ("-", ":"). The XML is now
built with lxml, so the value is text whatever it holds, and the input
keeps only required and the form's 100 characters; libvirt reports a
value it does not take.

A live test creates a volume, an iSCSI and a Ceph secret with real
values through the page.
2026-10-19 16:43:06 +03:00

52 lines
2.2 KiB
Python

"""Live check that the secrets page creates a secret for each usage type
with a real usage value: a volume path, an iSCSI IQN and a Ceph name. Runs
only with TEST_LIBVIRT_HOST set."""
import os
import unittest
import libvirt
from computes.models import Compute
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from instances.tests import livetest
from vrtManager.connection import connection_manager
P = livetest.PREFIX
USAGES = {
libvirt.VIR_SECRET_USAGE_TYPE_VOLUME: ("volume", f"/var/lib/libvirt/{P}secrets/vm-1.qcow2"),
libvirt.VIR_SECRET_USAGE_TYPE_ISCSI: ("iscsi", f"iqn.2026-10.test.wvc:{P}secret"),
libvirt.VIR_SECRET_USAGE_TYPE_CEPH: ("ceph", f"client.{P}secret secret"),
}
@unittest.skipUnless(livetest.enabled(), "Set TEST_LIBVIRT_HOST to run the live libvirt tests")
class LiveSecretsTestCase(TestCase):
def setUp(self):
args = dict(
hostname=os.environ["TEST_LIBVIRT_HOST"],
login=os.environ.get("TEST_LIBVIRT_LOGIN", ""),
password=os.environ.get("TEST_LIBVIRT_PASSWORD", ""),
type=int(os.environ.get("TEST_LIBVIRT_TYPE", 4)),
)
self.conn = connection_manager.get_connection(*args.values())
self.remove_secrets()
self.addCleanup(self.remove_secrets)
self.compute = Compute.objects.create(name="live-secrets", **args)
self.client.force_login(get_user_model().objects.create_superuser("live-secrets", "ls@example.com", "x"))
def remove_secrets(self):
for secret in self.conn.listAllSecrets():
if (secret.usageType(), secret.usageID()) in {(t, v) for t, (_, v) in USAGES.items()}:
secret.undefine()
def test_each_usage_type_takes_a_real_value(self):
for usage_type, (name, value) in USAGES.items():
with self.subTest(name):
response = self.client.post(
reverse("virtsecrets", args=[self.compute.id]),
{"create": "", "ephemeral": "no", "private": "no", "usage_type": name, "data": value},
)
self.assertEqual(response.status_code, 302)
self.assertEqual(self.conn.secretLookupByUsage(usage_type, value).usageID(), value)