jumpserver/apps/applications/api.py

95 lines
3.3 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# -*- coding: utf-8 -*-
#
from collections import OrderedDict
2016-12-25 09:44:39 +00:00
import copy
2016-11-13 14:34:38 +00:00
from rest_framework import viewsets
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
2016-10-16 14:12:13 +00:00
2016-11-13 14:34:38 +00:00
from .models import Terminal, TerminalHeatbeat
2016-10-18 15:49:04 +00:00
from .serializers import TerminalSerializer, TerminalHeatbeatSerializer
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
2016-10-16 14:12:13 +00:00
2017-10-31 03:34:20 +00:00
class TerminalViewSet(viewsets.ModelViewSet):
queryset = Terminal.objects.all()
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()
app_user, access_key = terminal.create_related_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,)
2016-10-17 09:28:07 +00:00
2017-10-31 03:34:20 +00:00
return super().get_permissions()
2016-10-24 11:32:53 +00:00
2017-02-11 11:49:15 +00:00
tasks = OrderedDict()
# tasks = {1: [{'name': 'kill_proxy', 'proxy_log_id': 23}]}
2016-10-24 11:32:53 +00:00
2016-11-13 14:34:38 +00:00
class TerminalHeatbeatViewSet(viewsets.ModelViewSet):
queryset = TerminalHeatbeat.objects.all()
serializer_class = TerminalHeatbeatSerializer
2016-12-27 16:28:52 +00:00
permission_classes = (IsAppUser,)
2016-11-13 14:34:38 +00:00
def create(self, request, *args, **kwargs):
2016-12-27 16:28:52 +00:00
terminal = request.user.terminal
2016-11-13 14:34:38 +00:00
TerminalHeatbeat.objects.create(terminal=terminal)
2017-02-11 11:49:15 +00:00
task = tasks.get(terminal.name)
tasks[terminal.name] = []
return Response({'msg': 'Success',
'tasks': task},
2017-02-11 11:49:15 +00:00
status=201)
class TerminateConnectionView(APIView):
def post(self, request, *args, **kwargs):
if isinstance(request.data, dict):
data = [request.data]
2017-02-11 11:49:15 +00:00
else:
data = request.data
for d in data:
proxy_log_id = d.get('proxy_log_id')
proxy_log = get_object_or_404(ProxyLog, id=proxy_log_id)
terminal_id = proxy_log.terminal
if terminal_id in tasks:
tasks[terminal_id].append({'name': 'kill_proxy',
'proxy_log_id': proxy_log_id})
else:
tasks[terminal_id] = [{'name': 'kill_proxy',
'proxy_log_id': proxy_log_id}]
2017-02-11 11:49:15 +00:00
return Response({'msg': 'get it'})