jumpserver/apps/audits/views.py

273 lines
11 KiB
Python
Raw Normal View History

2016-10-25 15:23:01 +00:00
# ~*~ coding: utf-8 ~*~
#
import time
from datetime import datetime
2016-10-25 15:23:01 +00:00
2016-10-27 11:35:02 +00:00
from django.views.generic import ListView, UpdateView, DeleteView, DetailView, TemplateView
2016-10-26 11:10:14 +00:00
from django.views.generic.edit import SingleObjectMixin
2016-10-25 15:23:01 +00:00
from django.utils.translation import ugettext as _
2016-11-04 17:15:25 +00:00
from django.utils import timezone
from django.utils.module_loading import import_string
2016-10-25 15:23:01 +00:00
from django.urls import reverse_lazy
from django.http import HttpResponse
2016-10-26 11:10:14 +00:00
from django.conf import settings
2016-11-04 17:15:25 +00:00
from django.db.models import Q
2016-10-25 15:23:01 +00:00
2016-11-14 11:28:21 +00:00
from .models import ProxyLog, CommandLog, LoginLog
2016-11-10 15:54:21 +00:00
from .hands import User, Asset, SystemUser, AdminUserRequiredMixin
from audits.backends import command_store
from audits.backends import CommandLogSerializer
2016-10-25 15:23:01 +00:00
2016-11-04 17:15:25 +00:00
class ProxyLogListView(AdminUserRequiredMixin, ListView):
2016-11-04 11:25:10 +00:00
model = ProxyLog
2017-03-23 16:27:33 +00:00
template_name = 'audits/proxy_log_online_list.html'
2016-11-04 11:25:10 +00:00
context_object_name = 'proxy_log_list'
2017-01-20 12:13:22 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
keyword = username = hostname = system_user = date_from_s = date_to_s = ''
ordering = ['is_finished', '-id']
date_format = '%m/%d/%Y'
2016-10-25 15:23:01 +00:00
2016-11-04 17:15:25 +00:00
def get_queryset(self):
2017-01-17 08:34:47 +00:00
date_now = timezone.localtime(timezone.now())
date_to_default = date_now.strftime(self.date_format)
date_from_default = (date_now-timezone.timedelta(7))\
.strftime(self.date_format)
2017-01-17 08:34:47 +00:00
2016-11-04 17:15:25 +00:00
self.queryset = super(ProxyLogListView, self).get_queryset()
self.keyword = self.request.GET.get('keyword', '')
self.username = self.request.GET.get('username')
self.ip = self.request.GET.get('ip')
self.system_user = self.request.GET.get('system_user')
self.date_from_s = self.request.GET.get('date_from', date_from_default)
self.date_to_s = self.request.GET.get('date_to', date_to_default)
filter_kwargs = {}
if self.date_from_s:
date_from = datetime.strptime(self.date_from_s, self.date_format)
date_from = date_from.replace(tzinfo=timezone.get_current_timezone())
filter_kwargs['date_start__gt'] = date_from
if self.date_to_s:
date_to = timezone.datetime.strptime(
self.date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S')
date_to = date_to.replace(tzinfo=timezone.get_current_timezone())
filter_kwargs['date_start__lt'] = date_to
if self.username:
filter_kwargs['user'] = self.username
if self.ip:
2017-05-15 15:39:54 +00:00
filter_kwargs['asset'] = self.ip
if self.system_user:
filter_kwargs['system_user'] = self.system_user
if self.keyword:
2017-01-20 12:13:22 +00:00
self.queryset = self.queryset.filter(
Q(user__icontains=self.keyword) |
Q(asset__icontains=self.keyword) |
Q(system_user__icontains=self.keyword)).distinct()
if filter_kwargs:
self.queryset = self.queryset.filter(**filter_kwargs)
2016-11-04 17:15:25 +00:00
return self.queryset
2016-10-25 15:23:01 +00:00
def get_context_data(self, **kwargs):
2016-11-04 17:15:25 +00:00
context = {
'app': _('Audits'),
'action': _('Proxy log list'),
2017-01-20 12:13:22 +00:00
'user_list': set(
list(ProxyLog.objects.values_list('user', flat=True))),
2017-01-20 12:13:22 +00:00
'asset_list': set(
list(ProxyLog.objects.values_list('asset', flat=True))),
2017-01-20 12:13:22 +00:00
'system_user_list': set(
list(ProxyLog.objects.values_list('system_user', flat=True))),
2016-11-04 17:15:25 +00:00
'keyword': self.keyword,
'date_from': self.date_from_s,
'date_to': self.date_to_s,
'username': self.username,
'ip': self.ip,
2016-11-05 04:45:59 +00:00
'system_user': self.system_user,
2016-11-04 17:15:25 +00:00
}
kwargs.update(context)
return super(ProxyLogListView, self).get_context_data(**kwargs)
2016-10-25 15:23:01 +00:00
2017-03-23 16:27:33 +00:00
class ProxyLogOfflineListView(ProxyLogListView):
template_name = 'audits/proxy_log_offline_list.html'
2017-03-23 16:27:33 +00:00
def get_queryset(self):
queryset = super(ProxyLogOfflineListView, self).get_queryset()
queryset = queryset.filter(is_finished=True)
return queryset
def get_context_data(self, **kwargs):
context = {
'action': _('Proxy log offline list'),
}
kwargs.update(context)
return super(ProxyLogOfflineListView, self).get_context_data(**kwargs)
class ProxyLogOnlineListView(ProxyLogListView):
template_name = 'audits/proxy_log_online_list.html'
def get_queryset(self):
queryset = super(ProxyLogOnlineListView, self).get_queryset()
queryset = queryset.filter(is_finished=False)
return queryset
def get_context_data(self, **kwargs):
context = {
'action': _('Proxy log online list'),
}
kwargs.update(context)
return super(ProxyLogOnlineListView, self).get_context_data(**kwargs)
2017-01-20 12:13:22 +00:00
class ProxyLogDetailView(AdminUserRequiredMixin,
SingleObjectMixin,
ListView):
2016-10-25 15:23:01 +00:00
template_name = 'audits/proxy_log_detail.html'
2016-10-26 11:10:14 +00:00
context_object_name = 'proxy_log'
2017-01-20 12:13:22 +00:00
object = ''
2016-10-25 15:23:01 +00:00
2016-10-26 11:10:14 +00:00
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=ProxyLog.objects.all())
return super(ProxyLogDetailView, self).get(request, *args, **kwargs)
2016-08-08 16:43:11 +00:00
2016-10-26 11:10:14 +00:00
def get_queryset(self):
return list(command_store.filter(proxy_log_id=self.object.id))
2016-10-26 11:10:14 +00:00
def get_context_data(self, **kwargs):
context = {
'app': 'Audits',
'action': 'Proxy log detail',
}
kwargs.update(context)
return super(ProxyLogDetailView, self).get_context_data(**kwargs)
2016-10-26 16:52:44 +00:00
2017-02-11 04:13:02 +00:00
# class ProxyLogCommandsListView(AdminUserRequiredMixin,
# SingleObjectMixin,
# ListView):
# template_name = 'audits/proxy_log_commands_list_modal.html'
# object = ''
#
# def get(self, request, *args, **kwargs):
# self.object = self.get_object(queryset=ProxyLog.objects.all())
# return super(ProxyLogCommandsListView, self).\
# get(request, *args, **kwargs)
#
# def get_queryset(self):
# return list(self.object.command_log.all())
2016-10-27 11:35:02 +00:00
2016-10-27 14:58:19 +00:00
class CommandLogListView(AdminUserRequiredMixin, ListView):
2016-10-27 11:35:02 +00:00
template_name = 'audits/command_log_list.html'
2016-10-27 14:58:19 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
context_object_name = 'command_list'
username = ip = system_user = command = date_from_s = date_to_s = ''
date_format = '%m/%d/%Y'
ordering = ['-id']
2016-10-27 14:58:19 +00:00
def get_queryset(self):
2017-01-17 08:34:47 +00:00
date_now = timezone.localtime(timezone.now())
date_to_default = date_now.strftime(self.date_format)
date_from_default = (date_now - timezone.timedelta(7)) \
.strftime(self.date_format)
self.command = self.request.GET.get('command', '')
self.username = self.request.GET.get('username')
self.ip = self.request.GET.get('ip')
self.system_user = self.request.GET.get('system_user')
self.date_from_s = \
self.request.GET.get('date_from', date_from_default)
self.date_to_s = \
self.request.GET.get('date_to', date_to_default)
filter_kwargs = {}
if self.date_from_s:
date_from = datetime.strptime(self.date_from_s, self.date_format)\
.replace(tzinfo=timezone.get_current_timezone())
# date_from_utc = date_from.astimezone(pytz.utc)
date_from_ts = time.mktime(date_from.timetuple())
filter_kwargs['date_from_ts'] = date_from_ts
if self.date_to_s:
date_to = datetime.strptime(
self.date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S')\
.replace(tzinfo=timezone.get_current_timezone())
date_to_ts = time.mktime(date_to.timetuple())
filter_kwargs['date_to_ts'] = date_to_ts
if self.username:
filter_kwargs['user'] = self.username
if self.ip:
filter_kwargs['asset'] = self.ip
if self.system_user:
filter_kwargs['system_user'] = self.system_user
if self.command:
filter_kwargs['command'] = self.command
self.queryset = command_store.filter(**filter_kwargs).order_by(*self.ordering)
2016-10-27 14:58:19 +00:00
return self.queryset
2016-10-27 11:35:02 +00:00
def get_context_data(self, **kwargs):
context = {
2016-11-04 17:15:25 +00:00
'app': _('Audits'),
'action': _('Command log list'),
2016-11-05 04:45:59 +00:00
'user_list': User.objects.all().order_by('username'),
'asset_list': Asset.objects.all().order_by('ip'),
'system_user_list': SystemUser.objects.all().order_by('username'),
'command': self.command,
2016-11-05 04:45:59 +00:00
'date_from': self.date_from_s,
'date_to': self.date_to_s,
'username': self.username,
'ip': self.ip,
2016-11-05 04:45:59 +00:00
'system_user': self.system_user,
2016-10-27 11:35:02 +00:00
}
kwargs.update(context)
return super(CommandLogListView, self).get_context_data(**kwargs)
2016-11-14 11:28:21 +00:00
class LoginLogListView(AdminUserRequiredMixin, ListView):
model = LoginLog
2017-01-20 12:13:22 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
2016-11-14 11:28:21 +00:00
template_name = 'audits/login_log_list.html'
context_object_name = 'login_log_list'
2017-01-20 12:13:22 +00:00
keyword = username = date_from_s = date_to_s = ''
2016-11-14 11:28:21 +00:00
def get_queryset(self):
2017-01-17 08:34:47 +00:00
date_now = timezone.localtime(timezone.now())
now_s = date_now.strftime('%m/%d/%Y')
2017-01-20 12:13:22 +00:00
seven_days_ago_s = (date_now - timezone.timedelta(7))\
.strftime('%m/%d/%Y')
2016-11-14 11:28:21 +00:00
self.queryset = super(LoginLogListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.username = username = self.request.GET.get('username', '')
2017-01-20 12:13:22 +00:00
self.date_from_s = date_from_s = self.request.GET.get(
'date_from', '%s' % seven_days_ago_s)
self.date_to_s = date_to_s = self.request.GET.get(
'date_to', '%s' % now_s)
2016-11-14 11:28:21 +00:00
if date_from_s:
date_from = timezone.datetime.strptime(date_from_s, '%m/%d/%Y')
self.queryset = self.queryset.filter(date_login__gt=date_from)
if date_to_s:
2017-01-20 12:13:22 +00:00
date_to = timezone.datetime.strptime(
date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S')
2016-11-14 11:28:21 +00:00
self.queryset = self.queryset.filter(date_login__lt=date_to)
if username:
self.queryset = self.queryset.filter(username=username)
if keyword:
2017-01-20 12:13:22 +00:00
self.queryset = self.queryset.filter(
Q(username__contains=keyword) |
Q(name__icontains=keyword) |
Q(login_ip=keyword)).distinct()
2016-11-14 11:28:21 +00:00
return self.queryset
def get_context_data(self, **kwargs):
context = {
'app': _('Audits'),
'action': _('Proxy log list'),
'user_list': User.objects.all().order_by('username'),
'keyword': self.keyword,
'date_from': self.date_from_s,
'date_to': self.date_to_s,
'username': self.username,
}
kwargs.update(context)
return super(LoginLogListView, self).get_context_data(**kwargs)