1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/virtsecrets/views.py
catborise 4ece22230e fix(virtsecrets): a secret's value never reaches the page
The edit dialog of a secret was filled with its current value, so every
readable secret's value was in the secrets page's HTML. A private
secret, or one with no value yet, cannot be read: its field held the
text "None", which is valid base64, and saving the dialog unchanged set
the secret to the three bytes it decodes to. The dialog now only writes
a new value: the field starts empty, is a password field and is
required. The page no longer reads secret values, and get_secret_value,
used only for that, is gone.
2026-10-20 22:37:36 +03:00

82 lines
2.7 KiB
Python

from admin.decorators import superuser_only
from computes.models import Compute
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from libvirt import (
VIR_SECRET_USAGE_TYPE_CEPH,
VIR_SECRET_USAGE_TYPE_ISCSI,
VIR_SECRET_USAGE_TYPE_NONE,
VIR_SECRET_USAGE_TYPE_TLS,
VIR_SECRET_USAGE_TYPE_VOLUME,
libvirtError,
)
from vrtManager.virtsecrets import wvmSecrets
from virtsecrets.forms import AddSecret
@superuser_only
def secrets(request, compute_id):
"""
:param request:
:param compute_id:
:return:
"""
secrets_all = []
compute = get_object_or_404(Compute, pk=compute_id)
secret_usage_types = {
VIR_SECRET_USAGE_TYPE_NONE: "none",
VIR_SECRET_USAGE_TYPE_VOLUME: "volume",
VIR_SECRET_USAGE_TYPE_CEPH: "ceph",
VIR_SECRET_USAGE_TYPE_ISCSI: "iscsi",
VIR_SECRET_USAGE_TYPE_TLS: "tls",
}
try:
conn = wvmSecrets(
compute.hostname, compute.login, compute.password, compute.type
)
secrets = conn.get_secrets()
for uuid in secrets:
secrt = conn.get_secret(uuid)
secrets_all.append(
{
"usage": secrt.usageID(),
"uuid": secrt.UUIDString(),
"usageType": secret_usage_types[secrt.usageType()],
}
)
if request.method == "POST":
if "create" in request.POST:
form = AddSecret(request.POST)
if form.is_valid():
data = form.cleaned_data
conn.create_secret(
data["ephemeral"],
data["private"],
data["usage_type"],
data["data"],
)
return HttpResponseRedirect(request.get_full_path())
else:
for msg_err in form.errors.values():
messages.error(request, msg_err.as_text())
if "delete" in request.POST:
uuid = request.POST.get("uuid", "")
conn.delete_secret(uuid)
return HttpResponseRedirect(request.get_full_path())
if "set_value" in request.POST:
uuid = request.POST.get("uuid", "")
value = request.POST.get("value", "")
try:
conn.set_secret_value(uuid, value)
except Exception as err:
messages.error(request, err)
return HttpResponseRedirect(request.get_full_path())
except libvirtError as err:
messages.error(request, err)
return render(request, "secrets.html", locals())