mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-19 18:34:28 +00:00
feat: 站内信 (#6183)
* 添加站内信 * s * s * 添加接口 * fix * fix * 重构了一些 * 完成 * 完善 * s * s * s * s * s * s * 测试ok * 替换业务中发送消息的方式 * 修改 * s * 去掉 update 兼容 create * 添加 unread total 接口 * 调整json字段 Co-authored-by: xinwen <coderWen@126.com>
This commit is contained in:
@@ -4,28 +4,24 @@ import time
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
from django.shortcuts import HttpResponse
|
||||
from rest_framework import viewsets
|
||||
from rest_framework import generics
|
||||
from rest_framework.fields import DateTimeField
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import action
|
||||
from django.template import loader
|
||||
|
||||
from common.http import is_true
|
||||
from terminal.models import CommandStorage, Command
|
||||
from terminal.models import CommandStorage
|
||||
from terminal.filters import CommandFilter
|
||||
from orgs.utils import current_org
|
||||
from common.permissions import IsOrgAdminOrAppUser, IsOrgAuditor, IsAppUser
|
||||
from common.const.http import GET
|
||||
from common.drf.api import JMSBulkModelViewSet
|
||||
from common.utils import get_logger
|
||||
from terminal.utils import send_command_alert_mail
|
||||
from terminal.serializers import InsecureCommandAlertSerializer
|
||||
from terminal.exceptions import StorageInvalid
|
||||
from ..backends import (
|
||||
get_command_storage, get_multi_command_storage,
|
||||
SessionCommandSerializer,
|
||||
)
|
||||
from ..notifications import CommandAlertMessage
|
||||
|
||||
logger = get_logger(__name__)
|
||||
__all__ = ['CommandViewSet', 'CommandExportApi', 'InsecureCommandAlertAPI']
|
||||
@@ -211,5 +207,5 @@ class InsecureCommandAlertAPI(generics.CreateAPIView):
|
||||
if command['risk_level'] >= settings.SECURITY_INSECURE_COMMAND_LEVEL and \
|
||||
settings.SECURITY_INSECURE_COMMAND and \
|
||||
settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER:
|
||||
send_command_alert_mail(command)
|
||||
CommandAlertMessage(command).publish_async()
|
||||
return Response()
|
||||
|
@@ -10,4 +10,5 @@ class TerminalConfig(AppConfig):
|
||||
|
||||
def ready(self):
|
||||
from . import signals_handler
|
||||
from . import notifications
|
||||
return super().ready()
|
||||
|
142
apps/terminal/notifications.py
Normal file
142
apps/terminal/notifications.py
Normal file
@@ -0,0 +1,142 @@
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.conf import settings
|
||||
|
||||
from users.models import User
|
||||
from common.utils import get_logger, reverse
|
||||
from notifications.notifications import SystemMessage
|
||||
from terminal.models import Session, Command
|
||||
from notifications.models import SystemMsgSubscription
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
__all__ = ('CommandAlertMessage', 'CommandExecutionAlert')
|
||||
|
||||
CATEGORY = 'terminal'
|
||||
CATEGORY_LABEL = _('Terminal')
|
||||
|
||||
|
||||
class CommandAlertMixin:
|
||||
@classmethod
|
||||
def post_insert_to_db(cls, subscription: SystemMsgSubscription):
|
||||
"""
|
||||
兼容操作,试图用 `settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER` 的邮件地址找到
|
||||
用户,把用户设置为默认接收者
|
||||
"""
|
||||
emails = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER.split(',')
|
||||
emails = [email.strip() for email in emails]
|
||||
|
||||
users = User.objects.filter(email__in=emails)
|
||||
subscription.users.add(*users)
|
||||
|
||||
|
||||
class CommandAlertMessage(CommandAlertMixin, SystemMessage):
|
||||
category = CATEGORY
|
||||
category_label = CATEGORY_LABEL
|
||||
message_type_label = _('Terminal command alert')
|
||||
|
||||
def __init__(self, command):
|
||||
self.command = command
|
||||
|
||||
def _get_message(self):
|
||||
command = self.command
|
||||
session_obj = Session.objects.get(id=command['session'])
|
||||
|
||||
message = _("""
|
||||
Command: %(command)s
|
||||
<br>
|
||||
Asset: %(host_name)s (%(host_ip)s)
|
||||
<br>
|
||||
User: %(user)s
|
||||
<br>
|
||||
Level: %(risk_level)s
|
||||
<br>
|
||||
Session: <a href="%(session_detail_url)s">session detail</a>
|
||||
<br>
|
||||
""") % {
|
||||
'command': command['input'],
|
||||
'host_name': command['asset'],
|
||||
'host_ip': session_obj.asset_obj.ip,
|
||||
'user': command['user'],
|
||||
'risk_level': Command.get_risk_level_str(command['risk_level']),
|
||||
'session_detail_url': reverse('api-terminal:session-detail',
|
||||
kwargs={'pk': command['session']},
|
||||
external=True, api_to_ui=True),
|
||||
}
|
||||
|
||||
return message
|
||||
|
||||
def get_common_msg(self):
|
||||
return self._get_message()
|
||||
|
||||
def get_email_msg(self):
|
||||
command = self.command
|
||||
session_obj = Session.objects.get(id=command['session'])
|
||||
|
||||
input = command['input']
|
||||
if isinstance(input, str):
|
||||
input = input.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
|
||||
|
||||
subject = _("Insecure Command Alert: [%(name)s->%(login_from)s@%(remote_addr)s] $%(command)s") % {
|
||||
'name': command['user'],
|
||||
'login_from': session_obj.get_login_from_display(),
|
||||
'remote_addr': session_obj.remote_addr,
|
||||
'command': input
|
||||
}
|
||||
|
||||
message = self._get_message(command)
|
||||
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
|
||||
|
||||
class CommandExecutionAlert(CommandAlertMixin, SystemMessage):
|
||||
category = CATEGORY
|
||||
category_label = CATEGORY_LABEL
|
||||
message_type_label = _('Batch command alert')
|
||||
|
||||
def __init__(self, command):
|
||||
self.command = command
|
||||
|
||||
def _get_message(self):
|
||||
command = self.command
|
||||
input = command['input']
|
||||
input = input.replace('\n', '<br>')
|
||||
|
||||
assets = ', '.join([str(asset) for asset in command['assets']])
|
||||
message = _("""
|
||||
<br>
|
||||
Assets: %(assets)s
|
||||
<br>
|
||||
User: %(user)s
|
||||
<br>
|
||||
Level: %(risk_level)s
|
||||
<br>
|
||||
|
||||
----------------- Commands ---------------- <br>
|
||||
%(command)s <br>
|
||||
----------------- Commands ---------------- <br>
|
||||
""") % {
|
||||
'command': input,
|
||||
'assets': assets,
|
||||
'user': command['user'],
|
||||
'risk_level': Command.get_risk_level_str(command['risk_level']),
|
||||
}
|
||||
return message
|
||||
|
||||
def get_common_msg(self):
|
||||
return self._get_message()
|
||||
|
||||
def get_email_msg(self):
|
||||
command = self.command
|
||||
|
||||
subject = _("Insecure Web Command Execution Alert: [%(name)s]") % {
|
||||
'name': command['user'],
|
||||
}
|
||||
message = self._get_message(command)
|
||||
|
||||
return {
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
@@ -68,78 +68,6 @@ def get_session_replay_url(session):
|
||||
return local_path, url
|
||||
|
||||
|
||||
def send_command_alert_mail(command):
|
||||
session_obj = Session.objects.get(id=command['session'])
|
||||
|
||||
input = command['input']
|
||||
if isinstance(input, str):
|
||||
input = input.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
|
||||
|
||||
subject = _("Insecure Command Alert: [%(name)s->%(login_from)s@%(remote_addr)s] $%(command)s") % {
|
||||
'name': command['user'],
|
||||
'login_from': session_obj.get_login_from_display(),
|
||||
'remote_addr': session_obj.remote_addr,
|
||||
'command': input
|
||||
}
|
||||
|
||||
recipient_list = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER.split(',')
|
||||
message = _("""
|
||||
Command: %(command)s
|
||||
<br>
|
||||
Asset: %(host_name)s (%(host_ip)s)
|
||||
<br>
|
||||
User: %(user)s
|
||||
<br>
|
||||
Level: %(risk_level)s
|
||||
<br>
|
||||
Session: <a href="%(session_detail_url)s">session detail</a>
|
||||
<br>
|
||||
""") % {
|
||||
'command': command['input'],
|
||||
'host_name': command['asset'],
|
||||
'host_ip': session_obj.asset_obj.ip,
|
||||
'user': command['user'],
|
||||
'risk_level': Command.get_risk_level_str(command['risk_level']),
|
||||
'session_detail_url': reverse('api-terminal:session-detail',
|
||||
kwargs={'pk': command['session']},
|
||||
external=True, api_to_ui=True),
|
||||
}
|
||||
logger.debug(message)
|
||||
|
||||
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
||||
|
||||
|
||||
def send_command_execution_alert_mail(command):
|
||||
subject = _("Insecure Web Command Execution Alert: [%(name)s]") % {
|
||||
'name': command['user'],
|
||||
}
|
||||
input = command['input']
|
||||
input = input.replace('\n', '<br>')
|
||||
recipient_list = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER.split(',')
|
||||
|
||||
assets = ', '.join([str(asset) for asset in command['assets']])
|
||||
message = _("""
|
||||
<br>
|
||||
Assets: %(assets)s
|
||||
<br>
|
||||
User: %(user)s
|
||||
<br>
|
||||
Level: %(risk_level)s
|
||||
<br>
|
||||
|
||||
----------------- Commands ---------------- <br>
|
||||
%(command)s <br>
|
||||
----------------- Commands ---------------- <br>
|
||||
""") % {
|
||||
'command': input,
|
||||
'assets': assets,
|
||||
'user': command['user'],
|
||||
'risk_level': Command.get_risk_level_str(command['risk_level']),
|
||||
}
|
||||
|
||||
send_mail_async.delay(subject, message, recipient_list, html_message=message)
|
||||
|
||||
|
||||
class ComputeStatUtil:
|
||||
# system status
|
||||
@staticmethod
|
||||
|
Reference in New Issue
Block a user