1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/virtsecrets/views.py
catborise e79817e7db fix(ui): a block or a job's cancel asks first; every delete and save says it is done
- Block in the Users list asks first: the user is signed out and their
  API tokens are deleted, which unblocking does not bring back. Block
  and Unblock then say what they did.
- Cancel on a job's page and in the job panel asks first, naming the
  job; the button says Cancel the job, apart from the dialog's Cancel.
- Deleting a user or a group, removing a host from the panel or an
  owner from a VM, and deleting an interface, a network filter or a
  secret say so, as the other deletes do.
- Renaming a tag and saving a VM's title and description say so.
2026-11-21 21:02:20 +03:00

67 lines
3.4 KiB
Python

from admin.decorators import superuser_only
from computes.models import Compute
from django.contrib import messages
from django.utils.translation import gettext as _
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.views.decorators.debug import sensitive_post_parameters
from instances.models import Instance
from libvirt import libvirtError
from logs.audit import audit
from vrtManager.virtsecrets import wvmSecrets
from virtsecrets.forms import USAGE_TEXTS, AddSecret
@sensitive_post_parameters()
@superuser_only
def secrets(request, compute_id):
"""The host's secrets with the VMs and pools that use them; one created,
given a value or deleted, after which the list is read again. A value is
never shown or logged."""
compute = get_object_or_404(Compute, pk=compute_id)
secrets_all = None # not read: the page shows no list
try:
conn = wvmSecrets(compute.hostname, compute.login, compute.password, compute.type)
if request.method == "POST":
uuid = request.POST.get("uuid", "")
if "create" in request.POST:
form = AddSecret(request.POST)
if form.is_valid():
data = form.cleaned_data
with audit(request, compute, f"Secret {data['usage_type']} {data['usage']}", "add"):
conn.create_secret(data["usage_type"], data["usage"], data["value"],
data["ephemeral"], data["private"])
else:
for msg_err in form.errors.values():
messages.error(request, msg_err.as_text())
elif "set_value" in request.POST:
with audit(request, compute, f"Secret {uuid}", "set value"):
conn.set_secret_value(uuid, request.POST.get("value", ""))
elif "delete" in request.POST:
with audit(request, compute, f"Secret {uuid}", "delete"):
conn.delete_secret(uuid)
messages.success(request, _("Secret %(uuid)s deleted") % {"uuid": uuid})
return HttpResponseRedirect(request.get_full_path())
# shown only with their users: a list without them would offer to delete secrets in use
secrets = conn.get_secrets_info()
users = conn.get_users(secrets)
vm_ids = dict(Instance.objects.filter(compute=compute).values_list("name", "id"))
labels = dict(AddSecret.base_fields["usage_type"].choices)
for secret in secrets:
used = users.get(secret["uuid"], {"vms": [], "pools": []})
secret["vms"] = [{"name": name, "id": vm_ids.get(name)} for name in used["vms"]]
secret["pools"] = used["pools"]
secret["type_label"] = labels.get(secret["usage_type"], secret["usage_type"])
secrets_all = secrets
except libvirtError as err: # OperationError too: a secret in use, a Ceph key not base64
messages.error(request, err)
if request.method == "POST": # a host not reached or a secret gone as well
return HttpResponseRedirect(request.get_full_path())
usage_choices = AddSecret.base_fields["usage_type"].choices
return render(request, "secrets.html", {"secrets_all": secrets_all, "compute": compute,
"usage_choices": usage_choices, "usage_texts": USAGE_TEXTS})