2024-09-19 16:35:26 +00:00
|
|
|
import json
|
2024-10-22 08:57:06 +00:00
|
|
|
import uuid
|
|
|
|
import logging
|
2024-10-15 09:05:43 +00:00
|
|
|
|
2024-10-15 14:58:51 +00:00
|
|
|
from uuid import uuid4
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-10 12:03:08 +00:00
|
|
|
from django.urls import reverse_lazy
|
2024-10-15 14:58:51 +00:00
|
|
|
from django.http import JsonResponse
|
2024-09-19 16:35:26 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2024-10-10 12:03:08 +00:00
|
|
|
from django_tables2 import SingleTableView
|
|
|
|
from django.views.generic.edit import (
|
|
|
|
CreateView,
|
|
|
|
DeleteView,
|
|
|
|
UpdateView,
|
|
|
|
)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-15 14:58:51 +00:00
|
|
|
from utils.save_snapshots import move_json, save_in_disk
|
2024-09-19 16:35:26 +00:00
|
|
|
from dashboard.mixins import DashboardView
|
|
|
|
from evidence.models import Annotation
|
|
|
|
from evidence.parse import Build
|
|
|
|
from api.models import Token
|
|
|
|
from api.tables import TokensTable
|
|
|
|
|
|
|
|
|
2024-10-22 08:57:06 +00:00
|
|
|
logger = logging.getLogger('django')
|
|
|
|
|
|
|
|
|
2024-09-19 16:35:26 +00:00
|
|
|
@csrf_exempt
|
|
|
|
def NewSnapshot(request):
|
|
|
|
# Accept only posts
|
|
|
|
if request.method != 'POST':
|
|
|
|
return JsonResponse({'error': 'Invalid request method'}, status=400)
|
|
|
|
|
|
|
|
# Authentication
|
2024-09-27 12:23:25 +00:00
|
|
|
auth_header = request.headers.get('Authorization')
|
|
|
|
if not auth_header or not auth_header.startswith('Bearer '):
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception("Invalid or missing token {}".format(auth_header))
|
2024-09-27 12:23:25 +00:00
|
|
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
2024-10-22 08:09:53 +00:00
|
|
|
|
|
|
|
token = auth_header.split(' ')[1].strip("'").strip('"')
|
2024-10-22 08:57:06 +00:00
|
|
|
try:
|
|
|
|
uuid.UUID(token)
|
|
|
|
except Exception:
|
|
|
|
logger.exception("Invalid token {}".format(token))
|
|
|
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
|
|
|
|
2024-09-27 12:23:25 +00:00
|
|
|
tk = Token.objects.filter(token=token).first()
|
2024-10-22 08:57:06 +00:00
|
|
|
|
2024-09-27 12:23:25 +00:00
|
|
|
if not tk:
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception("Invalid or missing token {}".format(token))
|
2024-09-27 12:23:25 +00:00
|
|
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
|
|
|
# Validation snapshot
|
|
|
|
try:
|
|
|
|
data = json.loads(request.body)
|
|
|
|
except json.JSONDecodeError:
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception("Invalid Snapshot of user {}".format(tk.owner))
|
|
|
|
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
|
|
|
# try:
|
|
|
|
# Build(data, None, check=True)
|
|
|
|
# except Exception:
|
|
|
|
# return JsonResponse({'error': 'Invalid Snapshot'}, status=400)
|
|
|
|
|
|
|
|
exist_annotation = Annotation.objects.filter(
|
|
|
|
uuid=data['uuid']
|
|
|
|
).first()
|
|
|
|
|
|
|
|
if exist_annotation:
|
2024-10-15 09:05:43 +00:00
|
|
|
txt = "error: the snapshot {} exist".format(data['uuid'])
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception(txt)
|
2024-10-15 09:05:43 +00:00
|
|
|
return JsonResponse({'status': txt}, status=500)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
|
|
|
# Process snapshot
|
2024-10-15 09:05:43 +00:00
|
|
|
path_name = save_in_disk(data, tk.owner.institution.name)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
|
|
|
try:
|
2024-10-14 10:22:28 +00:00
|
|
|
Build(data, tk.owner)
|
2024-10-17 17:08:00 +00:00
|
|
|
except Exception as err:
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception(err)
|
2024-10-17 17:08:00 +00:00
|
|
|
return JsonResponse({'status': f"fail: {err}"}, status=500)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-14 15:43:32 +00:00
|
|
|
annotation = Annotation.objects.filter(
|
|
|
|
uuid=data['uuid'],
|
|
|
|
type=Annotation.Type.SYSTEM,
|
2024-10-14 18:08:50 +00:00
|
|
|
# TODO this is hardcoded, it should select the user preferred algorithm
|
2024-10-14 15:43:32 +00:00
|
|
|
key="hidalgo1",
|
|
|
|
owner=tk.owner.institution
|
|
|
|
).first()
|
|
|
|
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-14 15:43:32 +00:00
|
|
|
if not annotation:
|
2024-10-22 08:57:06 +00:00
|
|
|
logger.exception("Error: No annotation for uuid: {}".format(data["uuid"]))
|
2024-10-18 13:24:42 +00:00
|
|
|
return JsonResponse({'status': 'fail'}, status=500)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-15 11:09:34 +00:00
|
|
|
url_args = reverse_lazy("device:details", args=(annotation.value,))
|
|
|
|
url = request.build_absolute_uri(url_args)
|
|
|
|
|
2024-10-14 15:43:32 +00:00
|
|
|
response = {
|
|
|
|
"status": "success",
|
2024-10-14 15:54:58 +00:00
|
|
|
"dhid": annotation.value[:6].upper(),
|
2024-10-14 15:43:32 +00:00
|
|
|
"url": url,
|
2024-10-15 11:09:34 +00:00
|
|
|
# TODO replace with public_url when available
|
2024-10-14 15:43:32 +00:00
|
|
|
"public_url": url
|
|
|
|
}
|
2024-10-15 09:05:43 +00:00
|
|
|
move_json(path_name, tk.owner.institution.name)
|
|
|
|
|
2024-10-14 15:43:32 +00:00
|
|
|
return JsonResponse(response, status=200)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TokenView(DashboardView, SingleTableView):
|
|
|
|
template_name = "token.html"
|
|
|
|
title = _("Credential management")
|
|
|
|
section = "Credential"
|
|
|
|
subtitle = _('Managament Tokens')
|
|
|
|
icon = 'bi bi-key'
|
|
|
|
model = Token
|
|
|
|
table_class = TokensTable
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
"""
|
|
|
|
Override the get_queryset method to filter events based on the user type.
|
|
|
|
"""
|
|
|
|
return Token.objects.filter().order_by("-id")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context.update({
|
2024-10-10 12:03:08 +00:00
|
|
|
'tokens': Token.objects.all(),
|
2024-09-19 16:35:26 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
|
class TokenDeleteView(DashboardView, DeleteView):
|
|
|
|
model = Token
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
self.pk = kwargs['pk']
|
2024-09-20 12:30:31 +00:00
|
|
|
self.object = get_object_or_404(self.model, pk=self.pk, owner=self.request.user)
|
2024-09-19 16:35:26 +00:00
|
|
|
self.object.delete()
|
|
|
|
|
|
|
|
return redirect('api:tokens')
|
|
|
|
|
|
|
|
|
2024-10-10 12:03:08 +00:00
|
|
|
class TokenNewView(DashboardView, CreateView):
|
|
|
|
template_name = "new_token.html"
|
|
|
|
title = _("Credential management")
|
|
|
|
section = "Credential"
|
|
|
|
subtitle = _('New Tokens')
|
|
|
|
icon = 'bi bi-key'
|
|
|
|
model = Token
|
|
|
|
success_url = reverse_lazy('api:tokens')
|
|
|
|
fields = (
|
|
|
|
"tag",
|
|
|
|
)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-10 12:03:08 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.owner = self.request.user
|
|
|
|
form.instance.token = uuid4()
|
|
|
|
return super().form_valid(form)
|
2024-09-19 16:35:26 +00:00
|
|
|
|
2024-10-10 12:03:08 +00:00
|
|
|
|
|
|
|
class EditTokenView(DashboardView, UpdateView):
|
|
|
|
template_name = "new_token.html"
|
|
|
|
title = _("Credential management")
|
|
|
|
section = "Credential"
|
|
|
|
subtitle = _('New Tokens')
|
|
|
|
icon = 'bi bi-key'
|
|
|
|
model = Token
|
|
|
|
success_url = reverse_lazy('api:tokens')
|
|
|
|
fields = (
|
|
|
|
"tag",
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
self.object = get_object_or_404(
|
|
|
|
self.model,
|
|
|
|
owner=self.request.user,
|
|
|
|
pk=pk,
|
|
|
|
)
|
|
|
|
kwargs = super().get_form_kwargs()
|
|
|
|
return kwargs
|