Merge pull request #9634 from jumpserver/pr@dev@perf_audit_log2

pref: 优化 audit log
This commit is contained in:
老广 2023-02-20 14:24:16 +08:00 committed by GitHub
commit 7c3b98cf3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 20 deletions

View File

@ -5,14 +5,12 @@ from importlib import import_module
from django.conf import settings from django.conf import settings
from django.db.models import F, Value, CharField, Q from django.db.models import F, Value, CharField, Q
from rest_framework import generics from rest_framework import generics
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin
from rest_framework.permissions import IsAuthenticated from rest_framework.permissions import IsAuthenticated
from common.utils import is_uuid
from common.api import JMSGenericViewSet
from common.drf.filters import DatetimeRangeFilter from common.drf.filters import DatetimeRangeFilter
from common.plugins.es import QuerySet as ESQuerySet from common.plugins.es import QuerySet as ESQuerySet
from orgs.mixins.api import OrgGenericViewSet, OrgBulkModelViewSet from common.utils import is_uuid
from orgs.mixins.api import OrgReadonlyModelViewSet
from orgs.utils import current_org, tmp_to_root_org from orgs.utils import current_org, tmp_to_root_org
from users.models import User from users.models import User
from .backends import TYPE_ENGINE_MAPPING from .backends import TYPE_ENGINE_MAPPING
@ -25,7 +23,7 @@ from .serializers import (
) )
class JobAuditViewSet(OrgBulkModelViewSet): class JobAuditViewSet(OrgReadonlyModelViewSet):
model = JobLog model = JobLog
extra_filter_backends = [DatetimeRangeFilter] extra_filter_backends = [DatetimeRangeFilter]
date_range_filter_fields = [ date_range_filter_fields = [
@ -34,10 +32,9 @@ class JobAuditViewSet(OrgBulkModelViewSet):
search_fields = ['creator__name', 'material'] search_fields = ['creator__name', 'material']
serializer_class = JobLogSerializer serializer_class = JobLogSerializer
ordering = ['-date_start'] ordering = ['-date_start']
http_method_names = ('get', 'head', 'options')
class FTPLogViewSet(CreateModelMixin, ListModelMixin, OrgGenericViewSet): class FTPLogViewSet(OrgReadonlyModelViewSet):
model = FTPLog model = FTPLog
serializer_class = FTPLogSerializer serializer_class = FTPLogSerializer
extra_filter_backends = [DatetimeRangeFilter] extra_filter_backends = [DatetimeRangeFilter]
@ -50,7 +47,7 @@ class FTPLogViewSet(CreateModelMixin, ListModelMixin, OrgGenericViewSet):
class UserLoginCommonMixin: class UserLoginCommonMixin:
queryset = UserLoginLog.objects.all() model = UserLoginLog
serializer_class = UserLoginLogSerializer serializer_class = UserLoginLogSerializer
extra_filter_backends = [DatetimeRangeFilter] extra_filter_backends = [DatetimeRangeFilter]
date_range_filter_fields = [ date_range_filter_fields = [
@ -60,9 +57,7 @@ class UserLoginCommonMixin:
search_fields = ['id', 'username', 'ip', 'city'] search_fields = ['id', 'username', 'ip', 'city']
class UserLoginLogViewSet( class UserLoginLogViewSet(UserLoginCommonMixin, OrgReadonlyModelViewSet):
UserLoginCommonMixin, RetrieveModelMixin, ListModelMixin, JMSGenericViewSet
):
@staticmethod @staticmethod
def get_org_members(): def get_org_members():
users = current_org.get_members().values_list('username', flat=True) users = current_org.get_members().values_list('username', flat=True)
@ -77,7 +72,7 @@ class UserLoginLogViewSet(
return queryset return queryset
class MyLoginLogAPIView(UserLoginCommonMixin, generics.ListAPIView): class MyLoginLogViewSet(UserLoginCommonMixin, OrgReadonlyModelViewSet):
permission_classes = [IsAuthenticated] permission_classes = [IsAuthenticated]
def get_queryset(self): def get_queryset(self):
@ -88,6 +83,7 @@ class MyLoginLogAPIView(UserLoginCommonMixin, generics.ListAPIView):
class ResourceActivityAPIView(generics.ListAPIView): class ResourceActivityAPIView(generics.ListAPIView):
serializer_class = ActivityUnionLogSerializer serializer_class = ActivityUnionLogSerializer
ordering_fields = ['datetime']
rbac_perms = { rbac_perms = {
'GET': 'audits.view_activitylog', 'GET': 'audits.view_activitylog',
} }
@ -129,7 +125,7 @@ class ResourceActivityAPIView(generics.ListAPIView):
return queryset.order_by('-datetime')[:limit] return queryset.order_by('-datetime')[:limit]
class OperateLogViewSet(RetrieveModelMixin, ListModelMixin, OrgGenericViewSet): class OperateLogViewSet(OrgReadonlyModelViewSet):
model = OperateLog model = OperateLog
serializer_class = OperateLogSerializer serializer_class = OperateLogSerializer
extra_filter_backends = [DatetimeRangeFilter] extra_filter_backends = [DatetimeRangeFilter]
@ -161,8 +157,8 @@ class OperateLogViewSet(RetrieveModelMixin, ListModelMixin, OrgGenericViewSet):
return qs return qs
class PasswordChangeLogViewSet(ListModelMixin, JMSGenericViewSet): class PasswordChangeLogViewSet(OrgReadonlyModelViewSet):
queryset = PasswordChangeLog.objects.all() model = PasswordChangeLog
serializer_class = PasswordChangeLogSerializer serializer_class = PasswordChangeLogSerializer
extra_filter_backends = [DatetimeRangeFilter] extra_filter_backends = [DatetimeRangeFilter]
date_range_filter_fields = [ date_range_filter_fields = [

View File

@ -1,7 +1,7 @@
# ~*~ coding: utf-8 ~*~ # ~*~ coding: utf-8 ~*~
from __future__ import unicode_literals from __future__ import unicode_literals
from django.urls.conf import re_path, path from django.urls.conf import path
from rest_framework.routers import DefaultRouter from rest_framework.routers import DefaultRouter
from .. import api from .. import api
@ -14,9 +14,9 @@ router.register(r'login-logs', api.UserLoginLogViewSet, 'login-log')
router.register(r'operate-logs', api.OperateLogViewSet, 'operate-log') router.register(r'operate-logs', api.OperateLogViewSet, 'operate-log')
router.register(r'password-change-logs', api.PasswordChangeLogViewSet, 'password-change-log') router.register(r'password-change-logs', api.PasswordChangeLogViewSet, 'password-change-log')
router.register(r'job-logs', api.JobAuditViewSet, 'job-log') router.register(r'job-logs', api.JobAuditViewSet, 'job-log')
router.register(r'my-login-logs', api.MyLoginLogViewSet, 'my-login-log')
urlpatterns = [ urlpatterns = [
path('my-login-logs/', api.MyLoginLogAPIView.as_view(), name='my-login-log'),
path('activities/', api.ResourceActivityAPIView.as_view(), name='resource-activities'), path('activities/', api.ResourceActivityAPIView.as_view(), name='resource-activities'),
] ]

View File

@ -10,7 +10,7 @@ from ..utils import set_to_root_org
__all__ = [ __all__ = [
'RootOrgViewMixin', 'OrgModelViewSet', 'OrgBulkModelViewSet', 'OrgQuerySetMixin', 'RootOrgViewMixin', 'OrgModelViewSet', 'OrgBulkModelViewSet', 'OrgQuerySetMixin',
'OrgGenericViewSet', 'OrgRelationMixin' 'OrgGenericViewSet', 'OrgRelationMixin', 'OrgReadonlyModelViewSet'
] ]
@ -62,6 +62,10 @@ class OrgBulkModelViewSet(CommonApiMixin, OrgViewSetMixin, BulkModelViewSet):
return False return False
class OrgReadonlyModelViewSet(OrgModelViewSet):
http_method_names = ['get', 'head', 'options']
class OrgRelationMixin(RelationMixin): class OrgRelationMixin(RelationMixin):
def get_queryset(self): def get_queryset(self):
queryset = super().get_queryset() queryset = super().get_queryset()

View File

@ -96,9 +96,11 @@ class RBACPermission(permissions.DjangoModelPermissions):
model_cls = queryset[0].__class__ model_cls = queryset[0].__class__
else: else:
model_cls = queryset.model model_cls = queryset.model
except AssertionError: except AssertionError as e:
logger.error('Error get model cls: ', e)
model_cls = None model_cls = None
except AttributeError: except AttributeError as e:
logger.error('Error get model cls: ', e)
model_cls = None model_cls = None
except Exception as e: except Exception as e:
logger.error('Error get model class: {} of {}'.format(e, view)) logger.error('Error get model class: {} of {}'.format(e, view))