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 a95ec40f69 fix(virtsecrets): a secret in use is not deleted, and a password is stored as typed
- A secret is in use when a VM names it in a disk, a host device or its
  vTPM, or a pool in its source, by UUID or by its usage within the usage
  type, as they run and as saved. The vTPM, host devices and pools' saved
  definitions were not checked before.
- A Ceph key is taken in base64, as `ceph auth get-key` prints it; any other
  value is a password or passphrase stored as typed, spaces included.
- A new secret takes its value at once; a value refused leaves no secret.
- TLS and vTPM secrets can be created; the usage has no length limit.
- Every POST redirects; the list is read once and only with its users; a
  secret, VM or pool gone meanwhile is left out.
- domain_trees() is shared by the in-use checks of secrets, network filters,
  networks and pools; the storage page's Ceph secret picker reads the same
  listing.
2026-10-29 10:10:43 +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": "", "usage_type": name, "usage": value},
)
self.assertEqual(response.status_code, 302)
self.assertEqual(self.conn.secretLookupByUsage(usage_type, value).usageID(), value)