2021-09-10 09:54:34 +00:00
|
|
|
|
from typing import Callable
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
from django.conf import settings
|
2021-10-20 11:45:37 +00:00
|
|
|
|
from django.template.loader import render_to_string
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
from common.utils import get_logger, reverse
|
2021-09-13 09:16:20 +00:00
|
|
|
|
from common.utils import lazyproperty
|
2021-10-22 12:06:16 +00:00
|
|
|
|
from common.utils.timezone import local_now_display
|
2023-02-01 08:43:43 +00:00
|
|
|
|
from notifications.backends import BACKEND
|
|
|
|
|
from notifications.models import SystemMsgSubscription
|
2023-07-11 04:06:11 +00:00
|
|
|
|
from notifications.notifications import SystemMessage, UserMessage
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from terminal.const import RiskLevelChoices
|
2023-02-01 08:43:43 +00:00
|
|
|
|
from terminal.models import Session, Command
|
|
|
|
|
from users.models import User
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
2023-07-17 12:52:54 +00:00
|
|
|
|
__all__ = (
|
|
|
|
|
'CommandAlertMessage', 'CommandExecutionAlert', 'StorageConnectivityMessage',
|
|
|
|
|
'CommandWarningMessage'
|
|
|
|
|
)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
CATEGORY = 'terminal'
|
2021-06-16 05:20:50 +00:00
|
|
|
|
CATEGORY_LABEL = _('Sessions')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandAlertMixin:
|
2021-09-10 09:54:34 +00:00
|
|
|
|
command: dict
|
|
|
|
|
_get_message: Callable
|
|
|
|
|
message_type_label: str
|
|
|
|
|
|
2023-07-11 04:06:11 +00:00
|
|
|
|
def __str__(self):
|
|
|
|
|
return str(self.message_type_label)
|
|
|
|
|
|
2021-09-13 09:16:20 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def subject(self):
|
2021-09-10 09:54:34 +00:00
|
|
|
|
_input = self.command['input']
|
|
|
|
|
if isinstance(_input, str):
|
|
|
|
|
_input = _input.replace('\r\n', ' ').replace('\r', ' ').replace('\n', ' ')
|
|
|
|
|
|
2023-07-11 04:06:11 +00:00
|
|
|
|
subject = self.message_type_label + ": %(cmd)s" % {
|
2021-09-10 09:54:34 +00:00
|
|
|
|
'cmd': _input
|
|
|
|
|
}
|
|
|
|
|
return subject
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def post_insert_to_db(cls, subscription: SystemMsgSubscription):
|
|
|
|
|
"""
|
2021-09-10 09:54:34 +00:00
|
|
|
|
兼容操作,试图用 `settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER` 的邮件地址
|
|
|
|
|
assets_systemuser_assets找到用户,把用户设置为默认接收者
|
2021-05-31 09:20:38 +00:00
|
|
|
|
"""
|
2021-06-16 06:38:17 +00:00
|
|
|
|
from settings.models import Setting
|
2021-09-10 09:54:34 +00:00
|
|
|
|
db_setting = Setting.objects.filter(
|
|
|
|
|
name='SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER'
|
|
|
|
|
).first()
|
2021-06-16 06:38:17 +00:00
|
|
|
|
if db_setting:
|
|
|
|
|
emails = db_setting.value
|
2021-06-18 02:05:44 +00:00
|
|
|
|
else:
|
|
|
|
|
emails = settings.SECURITY_INSECURE_COMMAND_EMAIL_RECEIVER
|
2021-06-16 06:38:17 +00:00
|
|
|
|
emails = emails.split(',')
|
|
|
|
|
emails = [email.strip().strip('"') for email in emails]
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
users = User.objects.filter(email__in=emails)
|
2021-06-18 02:05:44 +00:00
|
|
|
|
if users:
|
|
|
|
|
subscription.users.add(*users)
|
|
|
|
|
subscription.receive_backends = [BACKEND.EMAIL]
|
|
|
|
|
subscription.save()
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
|
2023-07-11 04:06:11 +00:00
|
|
|
|
class CommandWarningMessage(CommandAlertMixin, UserMessage):
|
2023-07-19 02:11:47 +00:00
|
|
|
|
message_type_label = _('Command warning')
|
2023-07-11 04:06:11 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, user, command):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.command = command
|
|
|
|
|
|
2023-07-17 12:52:54 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
command = self.command
|
|
|
|
|
|
|
|
|
|
command_input = command['input']
|
|
|
|
|
user = command['user']
|
|
|
|
|
asset = command['asset']
|
2023-07-18 07:19:54 +00:00
|
|
|
|
account = command.get('_account', '')
|
2023-07-17 12:52:54 +00:00
|
|
|
|
cmd_acl = command.get('_cmd_filter_acl')
|
|
|
|
|
cmd_group = command.get('_cmd_group')
|
2023-07-19 03:00:58 +00:00
|
|
|
|
session_id = command.get('session', '')
|
|
|
|
|
risk_level = command['risk_level']
|
2023-07-17 12:52:54 +00:00
|
|
|
|
org_id = command['org_id']
|
|
|
|
|
org_name = command.get('_org_name') or org_id
|
|
|
|
|
|
|
|
|
|
if session_id:
|
|
|
|
|
session_url = reverse(
|
|
|
|
|
'api-terminal:session-detail', kwargs={'pk': session_id},
|
|
|
|
|
external=True, api_to_ui=True
|
|
|
|
|
) + '?oid={}'.format(org_id)
|
|
|
|
|
session_url = session_url.replace('/terminal/sessions/', '/audit/sessions/sessions/')
|
2023-07-19 03:22:33 +00:00
|
|
|
|
else:
|
|
|
|
|
session_url = ''
|
2023-07-17 12:52:54 +00:00
|
|
|
|
|
|
|
|
|
# Command ACL
|
|
|
|
|
cmd_acl_name = cmd_group_name = ''
|
|
|
|
|
if cmd_acl:
|
|
|
|
|
cmd_acl_name = cmd_acl.name
|
|
|
|
|
if cmd_group:
|
|
|
|
|
cmd_group_name = cmd_group.name
|
2023-07-11 04:06:11 +00:00
|
|
|
|
|
|
|
|
|
context = {
|
2023-07-17 12:52:54 +00:00
|
|
|
|
'command': command_input,
|
|
|
|
|
'user': user,
|
|
|
|
|
'asset': asset,
|
|
|
|
|
'account': account,
|
|
|
|
|
'cmd_filter_acl': cmd_acl_name,
|
|
|
|
|
'cmd_group': cmd_group_name,
|
|
|
|
|
'session_url': session_url,
|
2023-07-19 03:00:58 +00:00
|
|
|
|
'risk_level': RiskLevelChoices.get_label(risk_level),
|
2023-07-17 12:52:54 +00:00
|
|
|
|
'org': org_name,
|
2023-07-11 04:06:11 +00:00
|
|
|
|
}
|
2023-07-17 12:52:54 +00:00
|
|
|
|
|
2023-07-11 04:06:11 +00:00
|
|
|
|
message = render_to_string('terminal/_msg_command_warning.html', context)
|
|
|
|
|
return {
|
|
|
|
|
'subject': self.subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-31 09:20:38 +00:00
|
|
|
|
class CommandAlertMessage(CommandAlertMixin, SystemMessage):
|
|
|
|
|
category = CATEGORY
|
|
|
|
|
category_label = CATEGORY_LABEL
|
2023-07-19 02:11:47 +00:00
|
|
|
|
message_type_label = _('Command reject')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, command):
|
|
|
|
|
self.command = command
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
2023-02-01 08:43:43 +00:00
|
|
|
|
command = Command.objects.first()
|
|
|
|
|
if not command:
|
|
|
|
|
command = Command(user='test', asset='test', input='test', session='111111111')
|
|
|
|
|
else:
|
|
|
|
|
command['session'] = Session.objects.first().id
|
2021-10-20 11:45:37 +00:00
|
|
|
|
return cls(command)
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-05-31 09:20:38 +00:00
|
|
|
|
command = self.command
|
2021-09-10 09:54:34 +00:00
|
|
|
|
session_detail_url = reverse(
|
|
|
|
|
'api-terminal:session-detail', kwargs={'pk': command['session']},
|
|
|
|
|
external=True, api_to_ui=True
|
2021-10-22 12:06:16 +00:00
|
|
|
|
) + '?oid={}'.format(self.command['org_id'])
|
2022-03-17 06:02:35 +00:00
|
|
|
|
session_detail_url = session_detail_url.replace(
|
|
|
|
|
'/terminal/sessions/', '/audit/sessions/sessions/'
|
|
|
|
|
)
|
2023-07-19 03:00:58 +00:00
|
|
|
|
level = RiskLevelChoices.get_label(command['risk_level'])
|
2021-10-22 12:06:16 +00:00
|
|
|
|
items = {
|
|
|
|
|
_("Asset"): command['asset'],
|
|
|
|
|
_("User"): command['user'],
|
|
|
|
|
_("Level"): level,
|
|
|
|
|
_("Date"): local_now_display(),
|
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
context = {
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'items': items,
|
|
|
|
|
'session_url': session_detail_url,
|
|
|
|
|
"command": command['input'],
|
2021-05-31 09:20:38 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('terminal/_msg_command_alert.html', context)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
return {
|
2021-09-13 09:16:20 +00:00
|
|
|
|
'subject': self.subject,
|
|
|
|
|
'message': message
|
2021-05-31 09:20:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandExecutionAlert(CommandAlertMixin, SystemMessage):
|
|
|
|
|
category = CATEGORY
|
|
|
|
|
category_label = CATEGORY_LABEL
|
2021-06-16 05:20:50 +00:00
|
|
|
|
message_type_label = _('Batch danger command alert')
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
|
|
|
|
def __init__(self, command):
|
|
|
|
|
self.command = command
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from assets.models import Asset
|
|
|
|
|
from users.models import User
|
|
|
|
|
cmd = {
|
|
|
|
|
'input': 'ifconfig eth0',
|
|
|
|
|
'assets': Asset.objects.all()[:10],
|
|
|
|
|
'user': str(User.objects.first()),
|
|
|
|
|
'risk_level': 5,
|
2021-09-13 09:16:20 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
return cls(cmd)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
def get_html_msg(self) -> dict:
|
2021-09-13 09:16:20 +00:00
|
|
|
|
command = self.command
|
2021-10-20 11:45:37 +00:00
|
|
|
|
assets_with_url = []
|
|
|
|
|
for asset in command['assets']:
|
2021-10-22 12:06:16 +00:00
|
|
|
|
url = reverse(
|
|
|
|
|
'assets:asset-detail', kwargs={'pk': asset.id},
|
2022-07-18 11:24:17 +00:00
|
|
|
|
api_to_ui=True, external=True, is_console=True
|
2021-10-22 12:06:16 +00:00
|
|
|
|
) + '?oid={}'.format(asset.org_id)
|
2021-10-20 11:45:37 +00:00
|
|
|
|
assets_with_url.append([asset, url])
|
2021-09-13 09:16:20 +00:00
|
|
|
|
|
2023-07-19 03:00:58 +00:00
|
|
|
|
level = RiskLevelChoices.get_label(command['risk_level'])
|
|
|
|
|
|
2021-10-22 12:06:16 +00:00
|
|
|
|
items = {
|
|
|
|
|
_("User"): command['user'],
|
|
|
|
|
_("Level"): level,
|
|
|
|
|
_("Date"): local_now_display(),
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-20 11:45:37 +00:00
|
|
|
|
context = {
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'items': items,
|
2021-10-20 11:45:37 +00:00
|
|
|
|
'assets_with_url': assets_with_url,
|
2021-10-22 12:06:16 +00:00
|
|
|
|
'command': command['input'],
|
2021-09-13 09:16:20 +00:00
|
|
|
|
}
|
2021-10-20 11:45:37 +00:00
|
|
|
|
message = render_to_string('terminal/_msg_command_execute_alert.html', context)
|
2021-05-31 09:20:38 +00:00
|
|
|
|
return {
|
2021-09-13 09:16:20 +00:00
|
|
|
|
'subject': self.subject,
|
2021-05-31 09:20:38 +00:00
|
|
|
|
'message': message
|
|
|
|
|
}
|
2023-05-30 10:45:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class StorageConnectivityMessage(SystemMessage):
|
|
|
|
|
category = 'storage'
|
|
|
|
|
category_label = _('Command and replay storage')
|
|
|
|
|
message_type_label = _('Connectivity alarm')
|
|
|
|
|
|
|
|
|
|
def __init__(self, errors):
|
|
|
|
|
self.errors = errors
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def post_insert_to_db(cls, subscription: SystemMsgSubscription):
|
|
|
|
|
subscription.receive_backends = [b for b in BACKEND if b.is_enable]
|
|
|
|
|
subscription.save()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def gen_test_msg(cls):
|
|
|
|
|
from terminal.models import ReplayStorage
|
|
|
|
|
replay = ReplayStorage.objects.first()
|
|
|
|
|
errors = [{
|
|
|
|
|
'msg': str(_("Test failure: Account invalid")),
|
|
|
|
|
'type': replay.get_type_display(),
|
|
|
|
|
'name': replay.name
|
|
|
|
|
}]
|
|
|
|
|
return cls(errors)
|
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
context = {
|
|
|
|
|
'items': self.errors,
|
|
|
|
|
}
|
|
|
|
|
subject = str(_("Invalid storage"))
|
|
|
|
|
message = render_to_string(
|
|
|
|
|
'terminal/_msg_check_command_replay_storage_connectivity.html', context
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
'subject': subject,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|
2023-07-26 11:36:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SessionSharingMessage(UserMessage):
|
|
|
|
|
message_type_label = _('Session sharing')
|
|
|
|
|
|
|
|
|
|
def __init__(self, user, instance):
|
|
|
|
|
super().__init__(user)
|
|
|
|
|
self.instance = instance
|
|
|
|
|
|
|
|
|
|
def get_html_msg(self) -> dict:
|
|
|
|
|
instance = self.instance
|
|
|
|
|
context = {
|
|
|
|
|
'asset': instance.session.asset,
|
|
|
|
|
'created_by': instance.created_by,
|
|
|
|
|
'account': instance.session.account,
|
2023-07-27 04:52:33 +00:00
|
|
|
|
'url': instance.url,
|
2023-07-26 11:36:15 +00:00
|
|
|
|
'verify_code': instance.verify_code,
|
|
|
|
|
'org': instance.org_name,
|
|
|
|
|
}
|
|
|
|
|
message = render_to_string('terminal/_msg_session_sharing.html', context)
|
|
|
|
|
return {
|
|
|
|
|
'subject': self.message_type_label + ' ' + self.instance.created_by,
|
|
|
|
|
'message': message
|
|
|
|
|
}
|