feat: 优化命令导出时间戳可读性

pull/8035/head
Jiangjie.Bai 2022-04-13 18:20:29 +08:00 committed by 老广
parent c630b11bd5
commit 10b033010e
4 changed files with 7 additions and 28 deletions

View File

@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
#
import time
from django.conf import settings
from django.utils import timezone
from django.shortcuts import HttpResponse
from rest_framework import generics
from rest_framework.fields import DateTimeField
from rest_framework.response import Response
from django.template import loader
from terminal.models import CommandStorage, Session, Command
from terminal.filters import CommandFilter
@ -23,7 +20,7 @@ from ..backends import (
from ..notifications import CommandAlertMessage
logger = get_logger(__name__)
__all__ = ['CommandViewSet', 'CommandExportApi', 'InsecureCommandAlertAPI']
__all__ = ['CommandViewSet', 'InsecureCommandAlertAPI']
class CommandQueryMixin:
@ -191,29 +188,6 @@ class CommandViewSet(JMSBulkModelViewSet):
return Response({"msg": msg}, status=401)
class CommandExportApi(CommandQueryMixin, generics.ListAPIView):
serializer_class = SessionCommandSerializer
rbac_perms = {
'list': 'terminal.view_command'
}
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
template = 'terminal/command_report.html'
context = {
'queryset': queryset,
'total_count': len(queryset),
'now': time.time(),
}
content = loader.render_to_string(template, context, request)
content_type = 'application/octet-stream'
response = HttpResponse(content, content_type)
filename = 'command-report-{}.html'.format(int(time.time()))
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
class InsecureCommandAlertAPI(generics.CreateAPIView):
serializer_class = InsecureCommandAlertSerializer
rbac_perms = {

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
#
from datetime import datetime
import uuid
from django.db import models
from django.utils.translation import ugettext_lazy as _
@ -28,6 +29,10 @@ class AbstractSessionCommand(OrgModelMixin):
class Meta:
abstract = True
@lazyproperty
def timestamp_display(self):
return datetime.fromtimestamp(self.timestamp)
@lazyproperty
def remote_addr(self):
from terminal.models import Session

View File

@ -36,6 +36,7 @@ class SessionCommandSerializer(SimpleSessionCommandSerializer):
output = serializers.CharField(max_length=2048, allow_blank=True, label=_("Output"))
risk_level_display = serializers.SerializerMethodField(label=_('Risk level display'))
timestamp = serializers.IntegerField(label=_('Timestamp'))
timestamp_display = serializers.DateTimeField(label=_('Datetime'))
remote_addr = serializers.CharField(read_only=True, label=_('Remote Address'))
@staticmethod

View File

@ -36,7 +36,6 @@ urlpatterns = [
path('tasks/kill-session/', api.KillSessionAPI.as_view(), name='kill-session'),
path('tasks/kill-session-for-ticket/', api.KillSessionForTicketAPI.as_view(), name='kill-session-for-ticket'),
path('terminals/config/', api.TerminalConfig.as_view(), name='terminal-config'),
path('commands/export/', api.CommandExportApi.as_view(), name="command-export"),
path('commands/insecure-command/', api.InsecureCommandAlertAPI.as_view(), name="command-alert"),
path('replay-storages/<uuid:pk>/test-connective/', api.ReplayStorageTestConnectiveApi.as_view(), name='replay-storage-test-connective'),
path('command-storages/<uuid:pk>/test-connective/', api.CommandStorageTestConnectiveApi.as_view(), name='command-storage-test-connective'),