2018-04-06 03:27:52 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-11-04 06:22:38 +00:00
|
|
|
from importlib import import_module
|
|
|
|
|
|
|
|
from django.conf import settings
|
2021-12-13 02:50:10 +00:00
|
|
|
from rest_framework import generics
|
2022-12-19 10:04:11 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin
|
2018-04-06 03:27:52 +00:00
|
|
|
|
2022-12-16 07:52:02 +00:00
|
|
|
from ops.models.job import JobAuditLog
|
2022-12-19 10:04:11 +00:00
|
|
|
from common.api import CommonGenericViewSet
|
|
|
|
from common.drf.filters import DatetimeRangeFilter
|
|
|
|
from common.plugins.es import QuerySet as ESQuerySet
|
2020-04-29 11:05:56 +00:00
|
|
|
from orgs.utils import current_org
|
2022-12-19 10:04:11 +00:00
|
|
|
from orgs.mixins.api import OrgGenericViewSet, OrgBulkModelViewSet
|
2022-11-04 06:22:38 +00:00
|
|
|
from .backends import TYPE_ENGINE_MAPPING
|
2020-05-08 08:48:26 +00:00
|
|
|
from .models import FTPLog, UserLoginLog, OperateLog, PasswordChangeLog
|
2022-12-16 07:52:02 +00:00
|
|
|
from .serializers import FTPLogSerializer, UserLoginLogSerializer, JobAuditLogSerializer
|
2022-11-04 06:22:38 +00:00
|
|
|
from .serializers import (
|
2022-12-19 10:04:11 +00:00
|
|
|
OperateLogSerializer, OperateLogActionDetailSerializer, PasswordChangeLogSerializer
|
2022-11-04 06:22:38 +00:00
|
|
|
)
|
2018-04-06 03:27:52 +00:00
|
|
|
|
|
|
|
|
2022-12-16 07:52:02 +00:00
|
|
|
class JobAuditViewSet(OrgBulkModelViewSet):
|
|
|
|
model = JobAuditLog
|
2022-12-19 10:04:11 +00:00
|
|
|
serializer_class = JobAuditLogSerializer
|
|
|
|
http_method_names = ('get', 'head', 'options')
|
2022-12-16 07:52:02 +00:00
|
|
|
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
class FTPLogViewSet(CreateModelMixin, ListModelMixin, OrgGenericViewSet):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = FTPLog
|
2018-04-06 03:27:52 +00:00
|
|
|
serializer_class = FTPLogSerializer
|
2020-05-08 04:46:18 +00:00
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('date_start', ('date_from', 'date_to'))
|
|
|
|
]
|
2022-12-20 11:18:39 +00:00
|
|
|
filterset_fields = ['user', 'asset', 'account', 'filename']
|
2021-01-07 02:53:10 +00:00
|
|
|
search_fields = filterset_fields
|
2020-06-23 10:24:45 +00:00
|
|
|
ordering = ['-date_start']
|
2020-04-29 11:05:56 +00:00
|
|
|
|
|
|
|
|
2021-12-13 02:50:10 +00:00
|
|
|
class UserLoginCommonMixin:
|
2020-04-29 11:05:56 +00:00
|
|
|
queryset = UserLoginLog.objects.all()
|
|
|
|
serializer_class = UserLoginLogSerializer
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['username', 'ip', 'city', 'type', 'status', 'mfa']
|
2021-10-19 07:10:18 +00:00
|
|
|
search_fields = ['username', 'ip', 'city']
|
2020-04-29 11:05:56 +00:00
|
|
|
|
2021-12-13 02:50:10 +00:00
|
|
|
|
|
|
|
class UserLoginLogViewSet(UserLoginCommonMixin, ListModelMixin, CommonGenericViewSet):
|
|
|
|
|
2020-04-29 11:05:56 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_org_members():
|
2020-07-20 02:42:22 +00:00
|
|
|
users = current_org.get_members().values_list('username', flat=True)
|
2020-04-29 11:05:56 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2021-10-19 07:10:18 +00:00
|
|
|
if current_org.is_root():
|
|
|
|
return queryset
|
|
|
|
users = self.get_org_members()
|
|
|
|
queryset = queryset.filter(username__in=users)
|
2020-04-29 11:05:56 +00:00
|
|
|
return queryset
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
|
2021-12-13 02:50:10 +00:00
|
|
|
class MyLoginLogAPIView(UserLoginCommonMixin, generics.ListAPIView):
|
|
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = super().get_queryset()
|
|
|
|
qs = qs.filter(username=self.request.user.username)
|
|
|
|
return qs
|
|
|
|
|
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
class OperateLogViewSet(RetrieveModelMixin, ListModelMixin, OrgGenericViewSet):
|
2020-05-08 08:48:26 +00:00
|
|
|
model = OperateLog
|
|
|
|
serializer_class = OperateLogSerializer
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['user', 'action', 'resource_type', 'resource', 'remote_addr']
|
2020-06-02 07:41:27 +00:00
|
|
|
search_fields = ['resource']
|
2020-06-02 07:40:07 +00:00
|
|
|
ordering = ['-datetime']
|
2020-05-08 08:48:26 +00:00
|
|
|
|
2022-11-04 06:22:38 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.request.query_params.get('type') == 'action_detail':
|
|
|
|
return OperateLogActionDetailSerializer
|
|
|
|
return super().get_serializer_class()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
qs = OperateLog.objects.all()
|
|
|
|
es_config = settings.OPERATE_LOG_ELASTICSEARCH_CONFIG
|
|
|
|
if es_config:
|
|
|
|
engine_mod = import_module(TYPE_ENGINE_MAPPING['es'])
|
|
|
|
store = engine_mod.OperateLogStore(es_config)
|
|
|
|
if store.ping(timeout=2):
|
|
|
|
qs = ESQuerySet(store)
|
|
|
|
qs.model = OperateLog
|
|
|
|
return qs
|
|
|
|
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
class PasswordChangeLogViewSet(ListModelMixin, CommonGenericViewSet):
|
|
|
|
queryset = PasswordChangeLog.objects.all()
|
|
|
|
serializer_class = PasswordChangeLogSerializer
|
|
|
|
extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
date_range_filter_fields = [
|
|
|
|
('datetime', ('date_from', 'date_to'))
|
|
|
|
]
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['user', 'change_by', 'remote_addr']
|
2021-07-13 10:03:02 +00:00
|
|
|
search_fields = filterset_fields
|
2020-06-02 07:40:07 +00:00
|
|
|
ordering = ['-datetime']
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
users = current_org.get_members()
|
2020-05-08 08:48:26 +00:00
|
|
|
queryset = super().get_queryset().filter(
|
|
|
|
user__in=[user.__str__() for user in users]
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
# Todo: 看看怎么搞
|
|
|
|
# class CommandExecutionViewSet(ListModelMixin, OrgGenericViewSet):
|
|
|
|
# model = CommandExecution
|
|
|
|
# serializer_class = CommandExecutionSerializer
|
|
|
|
# extra_filter_backends = [DatetimeRangeFilter]
|
|
|
|
# date_range_filter_fields = [
|
|
|
|
# ('date_start', ('date_from', 'date_to'))
|
|
|
|
# ]
|
|
|
|
# filterset_fields = [
|
|
|
|
# 'user__name', 'user__username', 'command',
|
|
|
|
# 'account', 'is_finished'
|
|
|
|
# ]
|
|
|
|
# search_fields = [
|
|
|
|
# 'command', 'user__name', 'user__username',
|
|
|
|
# 'account__username',
|
|
|
|
# ]
|
|
|
|
# ordering = ['-date_created']
|
|
|
|
#
|
|
|
|
# def get_queryset(self):
|
|
|
|
# queryset = super().get_queryset()
|
|
|
|
# if getattr(self, 'swagger_fake_view', False):
|
|
|
|
# return queryset.model.objects.none()
|
|
|
|
# if current_org.is_root():
|
|
|
|
# return queryset
|
|
|
|
# # queryset = queryset.filter(run_as__org_id=current_org.org_id())
|
|
|
|
# return queryset
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# class CommandExecutionHostRelationViewSet(OrgRelationMixin, OrgBulkModelViewSet):
|
|
|
|
# serializer_class = CommandExecutionHostsRelationSerializer
|
|
|
|
# m2m_field = CommandExecution.hosts.field
|
|
|
|
# filterset_fields = [
|
|
|
|
# 'id', 'asset', 'commandexecution'
|
|
|
|
# ]
|
|
|
|
# search_fields = ('asset__name', )
|
|
|
|
# http_method_names = ['options', 'get']
|
|
|
|
# rbac_perms = {
|
|
|
|
# 'GET': 'ops.view_commandexecution',
|
|
|
|
# 'list': 'ops.view_commandexecution',
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# def get_queryset(self):
|
|
|
|
# queryset = super().get_queryset()
|
|
|
|
# queryset = queryset.annotate(
|
|
|
|
# asset_display=Concat(
|
|
|
|
# F('asset__name'), Value('('),
|
|
|
|
# F('asset__address'), Value(')')
|
|
|
|
# )
|
|
|
|
# )
|
|
|
|
# return queryset
|