2020-05-27 12:24:06 +00:00
|
|
|
from secrets.forms import AddSecret
|
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.shortcuts import get_object_or_404, render
|
2020-01-24 07:09:46 +00:00
|
|
|
from django.urls import reverse
|
2020-05-27 12:24:06 +00:00
|
|
|
from libvirt import libvirtError
|
|
|
|
|
|
|
|
from admin.decorators import superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
from computes.models import Compute
|
|
|
|
from vrtManager.secrets import wvmSecrets
|
2015-02-27 08:53:51 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-02-27 09:28:22 +00:00
|
|
|
def secrets(request, compute_id):
|
|
|
|
"""
|
|
|
|
:param request:
|
2019-10-30 08:05:50 +00:00
|
|
|
:param compute_id:
|
2015-02-27 09:28:22 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
secrets_all = []
|
2015-02-27 09:51:33 +00:00
|
|
|
error_messages = []
|
2015-04-02 12:39:40 +00:00
|
|
|
compute = get_object_or_404(Compute, pk=compute_id)
|
2015-02-27 09:28:22 +00:00
|
|
|
try:
|
2020-05-27 12:24:06 +00:00
|
|
|
conn = wvmSecrets(compute.hostname, compute.login, compute.password, compute.type)
|
2015-02-27 09:28:22 +00:00
|
|
|
secrets = conn.get_secrets()
|
2020-03-16 13:59:45 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
for uuid in secrets:
|
|
|
|
secrt = conn.get_secret(uuid)
|
|
|
|
try:
|
|
|
|
secret_value = conn.get_secret_value(uuid)
|
2015-02-27 09:51:33 +00:00
|
|
|
except libvirtError as lib_err:
|
|
|
|
secret_value = None
|
2020-05-27 12:24:06 +00:00
|
|
|
secrets_all.append({
|
|
|
|
'usage': secrt.usageID(),
|
|
|
|
'uuid': secrt.UUIDString(),
|
|
|
|
'usageType': secrt.usageType(),
|
|
|
|
'value': secret_value
|
|
|
|
})
|
2015-02-27 09:28:22 +00:00
|
|
|
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())
|
2015-03-10 14:24:10 +00:00
|
|
|
else:
|
|
|
|
for msg_err in form.errors.values():
|
|
|
|
error_messages.append(msg_err.as_text())
|
2015-02-27 09:28:22 +00:00
|
|
|
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', '')
|
|
|
|
conn.set_secret_value(uuid, value)
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
except libvirtError as err:
|
2015-02-27 09:51:33 +00:00
|
|
|
error_messages.append(err)
|
2015-02-27 09:28:22 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'secrets.html', locals())
|