diff --git a/apps/audits/signals_handler.py b/apps/audits/signals_handler.py index 0193e830b..3e5086831 100644 --- a/apps/audits/signals_handler.py +++ b/apps/audits/signals_handler.py @@ -22,13 +22,15 @@ from users.models import User from users.signals import post_user_change_password from terminal.models import Session, Command from .utils import write_login_log -from . import models +from . import models, serializers from .models import OperateLog from orgs.utils import current_org from perms.models import AssetPermission, ApplicationPermission +from terminal.backends.command.serializers import SessionCommandSerializer +from terminal.serializers import SessionSerializer from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR from common.utils import get_request_ip, get_logger, get_syslogger -from common.utils.encode import model_to_json +from common.utils.encode import data_to_json logger = get_logger(__name__) sys_logger = get_syslogger(__name__) @@ -255,20 +257,27 @@ def on_user_change_password(sender, user=None, **kwargs): def on_audits_log_create(sender, instance=None, **kwargs): if sender == models.UserLoginLog: category = "login_log" + serializer_cls = serializers.UserLoginLogSerializer elif sender == models.FTPLog: category = "ftp_log" + serializer_cls = serializers.FTPLogSerializer elif sender == models.OperateLog: category = "operation_log" + serializer_cls = serializers.OperateLogSerializer elif sender == models.PasswordChangeLog: category = "password_change_log" + serializer_cls = serializers.PasswordChangeLogSerializer elif sender == Session: category = "host_session_log" + serializer_cls = SessionSerializer elif sender == Command: category = "session_command_log" + serializer_cls = SessionCommandSerializer else: return - data = model_to_json(instance, indent=None) + serializer = serializer_cls(instance) + data = data_to_json(serializer.data, indent=None) msg = "{} - {}".format(category, data) sys_logger.info(msg) diff --git a/apps/common/utils/encode.py b/apps/common/utils/encode.py index ee35b5c76..d108a2094 100644 --- a/apps/common/utils/encode.py +++ b/apps/common/utils/encode.py @@ -208,30 +208,7 @@ def ensure_last_char_is_ascii(data): secret_pattern = re.compile(r'password|secret|key', re.IGNORECASE) -def model_to_dict_pro(instance, fields=None, exclude=None): - from ..fields.model import EncryptMixin - opts = instance._meta - data = {} - for f in chain(opts.concrete_fields, opts.private_fields): - if not getattr(f, 'editable', False): - continue - if fields and f.name not in fields: - continue - if exclude and f.name in exclude: - continue - if isinstance(f, FileField): - continue - if isinstance(f, EncryptMixin): - continue - if secret_pattern.search(f.name): - continue - value = f.value_from_object(instance) - data[f.name] = value - return data - - -def model_to_json(instance, sort_keys=True, indent=2, cls=None): - data = model_to_dict_pro(instance) +def data_to_json(data, sort_keys=True, indent=2, cls=None): if cls is None: cls = DjangoJSONEncoder return json.dumps(data, sort_keys=sort_keys, indent=indent, cls=cls) diff --git a/apps/terminal/api/session.py b/apps/terminal/api/session.py index 28918a32b..65ca88243 100644 --- a/apps/terminal/api/session.py +++ b/apps/terminal/api/session.py @@ -12,7 +12,7 @@ from rest_framework import viewsets, views from rest_framework.response import Response from rest_framework.decorators import action -from common.utils import model_to_json +from common.utils import data_to_json from .. import utils from common.const.http import GET from common.utils import get_logger, get_object_or_none @@ -62,7 +62,9 @@ class SessionViewSet(OrgBulkModelViewSet): os.chdir(dir_path) with open(meta_filename, 'wt') as f: - f.write(model_to_json(session)) + serializer = serializers.SessionDisplaySerializer(session) + data = data_to_json(serializer.data) + f.write(data) with tarfile.open(offline_filename, 'w') as f: f.add(replay_filename)