jumpserver/apps/applications/api.py

176 lines
6.3 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# -*- coding: utf-8 -*-
2017-11-14 01:44:16 +00:00
#
from collections import OrderedDict
2016-12-25 09:44:39 +00:00
import copy
2017-11-22 02:54:59 +00:00
import logging
import os
2017-11-14 01:44:16 +00:00
from rest_framework import viewsets, serializers
2016-10-17 09:28:07 +00:00
from rest_framework.views import APIView, Response
from rest_framework.permissions import AllowAny
2017-02-11 11:49:15 +00:00
from django.shortcuts import get_object_or_404
2017-11-14 01:44:16 +00:00
from django.utils import timezone
2017-11-22 02:54:59 +00:00
from django.conf import settings
2016-10-16 14:12:13 +00:00
2017-11-14 01:44:16 +00:00
from .models import Terminal, TerminalStatus, TerminalSession, TerminalTask
from .serializers import TerminalSerializer, TerminalStatusSerializer, \
TerminalSessionSerializer, TerminalTaskSerializer
2017-03-31 03:25:25 +00:00
from .hands import IsSuperUserOrAppUser, IsAppUser, ProxyLog, \
IsSuperUserOrAppUserOrUserReadonly
2016-12-25 09:44:39 +00:00
from common.utils import get_object_or_none
2017-11-22 02:54:59 +00:00
logger = logging.getLogger(__file__)
2016-10-16 14:12:13 +00:00
2017-10-31 03:34:20 +00:00
class TerminalViewSet(viewsets.ModelViewSet):
2017-11-14 01:44:16 +00:00
queryset = Terminal.objects.filter(is_deleted=False)
serializer_class = TerminalSerializer
2017-10-31 03:34:20 +00:00
permission_classes = (IsSuperUserOrAppUserOrUserReadonly,)
def create(self, request, *args, **kwargs):
2017-10-31 03:34:20 +00:00
name = request.data.get('name')
remote_ip = request.META.get('REMOTE_ADDR')
x_real_ip = request.META.get('X-Real-IP')
remote_addr = x_real_ip or remote_ip
terminal = get_object_or_none(Terminal, name=name)
if terminal:
msg = 'Terminal name %s already used' % name
return Response({'msg': msg}, status=409)
2016-12-25 09:44:39 +00:00
2017-10-31 03:34:20 +00:00
serializer = self.serializer_class(data={
'name': name, 'remote_addr': remote_addr
})
2016-12-25 09:44:39 +00:00
if serializer.is_valid():
terminal = serializer.save()
2017-11-14 01:44:16 +00:00
app_user, access_key = terminal.create_app_user()
data = OrderedDict()
2017-01-20 05:59:53 +00:00
data['terminal'] = copy.deepcopy(serializer.data)
2016-12-25 09:44:39 +00:00
data['user'] = app_user.to_json()
2017-10-31 03:34:20 +00:00
data['access_key'] = {'id': access_key.id,
'secret': access_key.secret}
2016-12-25 09:44:39 +00:00
return Response(data, status=201)
else:
2017-10-31 03:34:20 +00:00
data = serializer.errors
2016-12-27 16:28:52 +00:00
return Response(data, status=400)
2017-10-31 03:34:20 +00:00
def get_permissions(self):
if self.action == "create":
self.permission_classes = (AllowAny,)
2017-11-14 01:44:16 +00:00
return super().get_permissions()
class TerminalStatusViewSet(viewsets.ModelViewSet):
queryset = TerminalStatus.objects.all()
serializer_class = TerminalStatusSerializer
permission_classes = (IsSuperUserOrAppUser,)
session_serializer_class = TerminalSessionSerializer
def create(self, request, *args, **kwargs):
2017-11-22 02:54:59 +00:00
self.handle_sessions()
return super().create(request, *args, **kwargs)
def handle_sessions(self):
2017-11-14 01:44:16 +00:00
sessions_active = []
2017-11-22 02:54:59 +00:00
for session_data in self.request.data.get("sessions", []):
2017-11-14 01:44:16 +00:00
session_data["terminal"] = self.request.user.terminal.id
_id = session_data["id"]
session = get_object_or_none(TerminalSession, id=_id)
if session:
2017-11-22 02:54:59 +00:00
serializer = TerminalSessionSerializer(data=session_data,
instance=session)
2017-11-14 01:44:16 +00:00
else:
serializer = TerminalSessionSerializer(data=session_data)
if serializer.is_valid():
serializer.save()
2017-11-22 02:54:59 +00:00
else:
logger.error("session serializer is not valid {}".format(
serializer.errors))
2017-11-14 01:44:16 +00:00
2017-11-22 02:54:59 +00:00
if not session_data["is_finished"]:
2017-11-14 01:44:16 +00:00
sessions_active.append(session_data["id"])
sessions_in_db_active = TerminalSession.objects.filter(
is_finished=False, terminal=self.request.user.terminal.id
)
2016-10-17 09:28:07 +00:00
2017-11-14 01:44:16 +00:00
for session in sessions_in_db_active:
2017-11-22 02:54:59 +00:00
if str(session.id) not in sessions_active:
2017-11-14 01:44:16 +00:00
session.is_finished = True
session.date_end = timezone.now()
session.save()
def get_queryset(self):
terminal_id = self.kwargs.get("terminal", None)
if terminal_id:
terminal = get_object_or_404(Terminal, id=terminal_id)
self.queryset = terminal.terminalstatus_set.all()
return self.queryset
def perform_create(self, serializer):
serializer.validated_data["terminal"] = self.request.user.terminal
return super().perform_create(serializer)
def get_permissions(self):
if self.action == "create":
self.permission_classes = (IsAppUser,)
2017-10-31 03:34:20 +00:00
return super().get_permissions()
2016-10-24 11:32:53 +00:00
2017-11-14 01:44:16 +00:00
class TerminalSessionViewSet(viewsets.ModelViewSet):
queryset = TerminalSession.objects.all()
serializers_class = TerminalSessionSerializer
permission_classes = (IsSuperUserOrAppUser,)
2017-02-11 11:49:15 +00:00
2017-11-14 01:44:16 +00:00
def get_queryset(self):
terminal_id = self.kwargs.get("terminal", None)
if terminal_id:
terminal = get_object_or_404(Terminal, id=terminal_id)
self.queryset = terminal.terminalstatus_set.all()
return self.queryset
2016-10-24 11:32:53 +00:00
2016-11-13 14:34:38 +00:00
2017-11-14 01:44:16 +00:00
class TerminalTaskViewSet(viewsets.ModelViewSet):
queryset = TerminalTask.objects.all()
serializer_class = TerminalTaskSerializer
permission_classes = (IsSuperUserOrAppUser,)
def get_queryset(self):
terminal_id = self.kwargs.get("terminal", None)
if terminal_id:
terminal = get_object_or_404(Terminal, id=terminal_id)
self.queryset = terminal.terminalstatus_set.all()
2017-02-11 11:49:15 +00:00
2017-11-14 01:44:16 +00:00
if hasattr(self.request.user, "terminal"):
terminal = self.request.user.terminal
self.queryset = terminal.terminalstatus_set.all()
return self.queryset
2017-11-22 02:54:59 +00:00
class SessionReplayAPI(APIView):
permission_classes = (IsSuperUserOrAppUser,)
def post(self, request, **kwargs):
session_id = kwargs.get("pk", None)
session = get_object_or_404(TerminalSession, id=session_id)
record_dir = settings.CONFIG.SESSION_RECORDE_DIR
date = session.date_start.strftime("%Y-%m-%d")
record_dir = os.path.join(record_dir, date)
record_filename = os.path.join(record_dir, str(session.id))
if not os.path.exists(record_dir):
os.makedirs(record_dir)
archive_stream = request.data.get("archive")
if not archive_stream:
return Response("None file upload", status=400)
with open(record_filename, 'wb') as f:
for chunk in archive_stream.chunks():
f.write(chunk)
session.has_replay = True
session.save()
return Response({"session_id": session.id}, status=201)