jumpserver/apps/audits/api.py

93 lines
2.6 KiB
Python
Raw Normal View History

2016-08-09 09:27:37 +00:00
# ~*~ coding: utf-8 ~*~
#
2016-10-08 16:12:18 +00:00
2016-10-20 11:01:57 +00:00
from __future__ import absolute_import, unicode_literals
2016-12-30 14:21:50 +00:00
2016-11-08 16:36:23 +00:00
from rest_framework import generics, viewsets
2017-01-20 05:59:53 +00:00
from rest_framework_bulk import BulkModelViewSet
2016-10-08 16:12:18 +00:00
2017-02-07 13:51:44 +00:00
from audits.backends import command_store, record_store
from audits.backends.command.serializers import CommandLogSerializer
2017-02-07 13:51:44 +00:00
from audits.backends.record.serializers import RecordSerializer
2016-10-20 11:01:57 +00:00
from . import models, serializers
from .hands import IsSuperUserOrAppUser, IsAppUser
2016-12-30 14:21:50 +00:00
class ProxyLogReceiveView(generics.CreateAPIView):
queryset = models.ProxyLog.objects.all()
serializer_class = serializers.ProxyLogSerializer
permission_classes = (IsAppUser,)
def get_serializer(self, *args, **kwargs):
kwargs['data']['terminal'] = self.request.user.terminal.name
return super(ProxyLogReceiveView, self).get_serializer(*args, **kwargs)
2016-10-08 16:12:18 +00:00
2016-10-09 16:39:24 +00:00
2016-11-08 16:36:23 +00:00
class ProxyLogViewSet(viewsets.ModelViewSet):
2016-10-18 11:28:36 +00:00
"""User proxy to backend server need call this api.
params: {
"username": "",
"name": "",
"hostname": "",
"ip": "",
2016-12-30 14:21:50 +00:00
"terminal": "",
2016-10-18 11:28:36 +00:00
"login_type": "",
"system_user": "",
"was_failed": "",
"date_start": ""
}
"""
2016-10-19 16:26:53 +00:00
queryset = models.ProxyLog.objects.all()
2016-10-09 16:39:24 +00:00
serializer_class = serializers.ProxyLogSerializer
2016-12-25 05:15:28 +00:00
permission_classes = (IsSuperUserOrAppUser,)
2016-10-18 11:28:36 +00:00
2016-10-09 16:39:24 +00:00
class CommandLogViewSet(BulkModelViewSet):
"""接受app发送来的command log, 格式如下
{
"proxy_log_id": 23,
"user": "admin",
"asset": "localhost",
"system_user": "web",
"command_no": 1,
"command": "whoami",
"output": "d2hvbWFp", # base64.b64encode(s)
"timestamp": 1485238673.0
}
"""
queryset = command_store.all()
serializer_class = CommandLogSerializer
2016-12-25 05:15:28 +00:00
permission_classes = (IsSuperUserOrAppUser,)
2016-10-26 11:10:14 +00:00
2017-02-07 13:51:44 +00:00
class RecordLogViewSet(BulkModelViewSet):
"""接受app发送来的record log, 格式如下
{
"proxy_log_id": 23,
"output": "d2hvbWFp", # base64.b64encode(s)
"timestamp": 1485238673.0
}
"""
serializer_class = RecordSerializer
permission_classes = (IsSuperUserOrAppUser,)
def get_queryset(self):
filter_kwargs = {}
proxy_log_id = self.request.query_params.get('proxy_log_id')
data_from_ts = self.request.query_params.get('date_from_ts')
if proxy_log_id:
filter_kwargs['proxy_log_id'] = proxy_log_id
if data_from_ts:
filter_kwargs['date_from_ts'] = data_from_ts
if filter_kwargs:
return record_store.filter(**filter_kwargs)
else:
return record_store.all()