2018-09-03 03:24:25 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2021-07-22 11:53:14 +00:00
|
|
|
from django.db.models.signals import post_save, post_delete, m2m_changed
|
2018-09-03 03:24:25 +00:00
|
|
|
from django.dispatch import receiver
|
2021-02-26 09:33:11 +00:00
|
|
|
from django.conf import settings
|
2018-09-03 03:24:25 +00:00
|
|
|
from django.db import transaction
|
2019-11-05 10:46:29 +00:00
|
|
|
from django.utils import timezone
|
2021-02-26 09:33:11 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2020-12-09 10:43:13 +00:00
|
|
|
from django.contrib.auth import BACKEND_SESSION_KEY
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-08-21 12:27:21 +00:00
|
|
|
from rest_framework.renderers import JSONRenderer
|
2019-11-05 10:46:29 +00:00
|
|
|
from rest_framework.request import Request
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2021-08-18 08:11:37 +00:00
|
|
|
from assets.models import Asset, SystemUser
|
2021-07-22 11:53:14 +00:00
|
|
|
from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR
|
2018-09-03 03:24:25 +00:00
|
|
|
from jumpserver.utils import current_request
|
2019-08-21 12:27:21 +00:00
|
|
|
from common.utils import get_request_ip, get_logger, get_syslogger
|
2018-09-03 03:24:25 +00:00
|
|
|
from users.models import User
|
2019-12-16 08:53:29 +00:00
|
|
|
from users.signals import post_user_change_password
|
2019-11-05 10:46:29 +00:00
|
|
|
from authentication.signals import post_auth_failed, post_auth_success
|
2019-09-25 08:31:58 +00:00
|
|
|
from terminal.models import Session, Command
|
2019-12-05 07:09:25 +00:00
|
|
|
from common.utils.encode import model_to_json
|
2019-12-27 08:00:32 +00:00
|
|
|
from .utils import write_login_log
|
2019-12-05 07:09:25 +00:00
|
|
|
from . import models
|
2021-07-22 11:53:14 +00:00
|
|
|
from .models import OperateLog
|
|
|
|
from orgs.utils import current_org
|
|
|
|
from perms.models import AssetPermission, ApplicationPermission
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2019-02-21 11:22:23 +00:00
|
|
|
logger = get_logger(__name__)
|
2019-12-16 08:53:29 +00:00
|
|
|
sys_logger = get_syslogger(__name__)
|
2019-08-21 12:27:21 +00:00
|
|
|
json_render = JSONRenderer()
|
2019-02-21 11:22:23 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
MODELS_NEED_RECORD = (
|
2021-04-20 05:05:06 +00:00
|
|
|
# users
|
|
|
|
'User', 'UserGroup',
|
|
|
|
# acls
|
|
|
|
'LoginACL', 'LoginAssetACL',
|
|
|
|
# assets
|
|
|
|
'Asset', 'Node', 'AdminUser', 'SystemUser', 'Domain', 'Gateway', 'CommandFilterRule',
|
2021-08-18 08:11:37 +00:00
|
|
|
'CommandFilter', 'Platform', 'AuthBook',
|
2021-04-20 05:05:06 +00:00
|
|
|
# applications
|
|
|
|
'Application',
|
|
|
|
# orgs
|
|
|
|
'Organization',
|
|
|
|
# settings
|
|
|
|
'Setting',
|
|
|
|
# perms
|
|
|
|
'AssetPermission', 'ApplicationPermission',
|
|
|
|
# xpack
|
|
|
|
'License', 'Account', 'SyncInstanceTask', 'ChangeAuthPlan', 'GatherUserTask',
|
2018-09-03 03:24:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-02-26 09:33:11 +00:00
|
|
|
class AuthBackendLabelMapping(LazyObject):
|
|
|
|
@staticmethod
|
|
|
|
def get_login_backends():
|
|
|
|
backend_label_mapping = {}
|
|
|
|
for source, backends in User.SOURCE_BACKEND_MAPPING.items():
|
|
|
|
for backend in backends:
|
|
|
|
backend_label_mapping[backend] = source.label
|
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_PUBKEY] = _('SSH Key')
|
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_MODEL] = _('Password')
|
2021-04-12 04:32:15 +00:00
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_SSO] = _('SSO')
|
2021-06-10 10:02:55 +00:00
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_AUTH_TOKEN] = _('Auth Token')
|
2021-03-24 11:01:35 +00:00
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_WECOM] = _('WeCom')
|
|
|
|
backend_label_mapping[settings.AUTH_BACKEND_DINGTALK] = _('DingTalk')
|
2021-02-26 09:33:11 +00:00
|
|
|
return backend_label_mapping
|
|
|
|
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = self.get_login_backends()
|
|
|
|
|
|
|
|
|
|
|
|
AUTH_BACKEND_LABEL_MAPPING = AuthBackendLabelMapping()
|
2020-12-09 10:43:13 +00:00
|
|
|
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
def create_operate_log(action, sender, resource):
|
|
|
|
user = current_request.user if current_request else None
|
|
|
|
if not user or not user.is_authenticated:
|
|
|
|
return
|
|
|
|
model_name = sender._meta.object_name
|
|
|
|
if model_name not in MODELS_NEED_RECORD:
|
|
|
|
return
|
|
|
|
resource_type = sender._meta.verbose_name
|
|
|
|
remote_addr = get_request_ip(current_request)
|
2019-02-21 11:22:23 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"user": str(user), 'action': action, 'resource_type': resource_type,
|
|
|
|
'resource': str(resource), 'remote_addr': remote_addr,
|
|
|
|
}
|
2018-09-03 03:24:25 +00:00
|
|
|
with transaction.atomic():
|
2019-02-21 11:22:23 +00:00
|
|
|
try:
|
2019-08-21 12:27:21 +00:00
|
|
|
models.OperateLog.objects.create(**data)
|
2019-02-21 11:22:23 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error("Create operate log error: {}".format(e))
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
|
2021-07-22 11:53:14 +00:00
|
|
|
M2M_NEED_RECORD = {
|
|
|
|
'OrganizationMember': (
|
|
|
|
_('User and Organization'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{User} JOINED {Organization}'),
|
|
|
|
_('{User} LEFT {Organization}')
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
User.groups.through._meta.object_name: (
|
|
|
|
_('User and Group'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{User} JOINED {UserGroup}'),
|
|
|
|
_('{User} LEFT {UserGroup}')
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
2021-08-18 08:11:37 +00:00
|
|
|
SystemUser.assets.through._meta.object_name: (
|
|
|
|
_('Asset and SystemUser'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{Asset} ADD {SystemUser}'),
|
|
|
|
_('{Asset} REMOVE {SystemUser}')
|
2021-08-18 08:11:37 +00:00
|
|
|
),
|
2021-07-22 11:53:14 +00:00
|
|
|
Asset.nodes.through._meta.object_name: (
|
|
|
|
_('Node and Asset'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{Node} ADD {Asset}'),
|
|
|
|
_('{Node} REMOVE {Asset}')
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
AssetPermission.users.through._meta.object_name: (
|
|
|
|
_('User asset permissions'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{AssetPermission} ADD {User}'),
|
|
|
|
_('{AssetPermission} REMOVE {User}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
AssetPermission.user_groups.through._meta.object_name: (
|
|
|
|
_('User group asset permissions'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{AssetPermission} ADD {UserGroup}'),
|
|
|
|
_('{AssetPermission} REMOVE {UserGroup}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
AssetPermission.assets.through._meta.object_name: (
|
|
|
|
_('Asset permission'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{AssetPermission} ADD {Asset}'),
|
|
|
|
_('{AssetPermission} REMOVE {Asset}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
AssetPermission.nodes.through._meta.object_name: (
|
|
|
|
_('Node permission'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{AssetPermission} ADD {Node}'),
|
|
|
|
_('{AssetPermission} REMOVE {Node}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
AssetPermission.system_users.through._meta.object_name: (
|
|
|
|
_('Asset permission and SystemUser'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{AssetPermission} ADD {SystemUser}'),
|
|
|
|
_('{AssetPermission} REMOVE {SystemUser}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
ApplicationPermission.users.through._meta.object_name: (
|
|
|
|
_('User application permissions'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{ApplicationPermission} ADD {User}'),
|
|
|
|
_('{ApplicationPermission} REMOVE {User}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
ApplicationPermission.user_groups.through._meta.object_name: (
|
|
|
|
_('User group application permissions'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{ApplicationPermission} ADD {UserGroup}'),
|
|
|
|
_('{ApplicationPermission} REMOVE {UserGroup}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
ApplicationPermission.applications.through._meta.object_name: (
|
|
|
|
_('Application permission'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{ApplicationPermission} ADD {Application}'),
|
|
|
|
_('{ApplicationPermission} REMOVE {Application}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
ApplicationPermission.system_users.through._meta.object_name: (
|
|
|
|
_('Application permission and SystemUser'),
|
2021-09-10 10:12:28 +00:00
|
|
|
_('{ApplicationPermission} ADD {SystemUser}'),
|
|
|
|
_('{ApplicationPermission} REMOVE {SystemUser}'),
|
2021-07-22 11:53:14 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
M2M_ACTION = {
|
|
|
|
POST_ADD: 'add',
|
|
|
|
POST_REMOVE: 'remove',
|
|
|
|
POST_CLEAR: 'remove',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(m2m_changed)
|
|
|
|
def on_m2m_changed(sender, action, instance, reverse, model, pk_set, **kwargs):
|
|
|
|
if action not in M2M_ACTION:
|
|
|
|
return
|
|
|
|
|
|
|
|
user = current_request.user if current_request else None
|
|
|
|
if not user or not user.is_authenticated:
|
|
|
|
return
|
|
|
|
|
|
|
|
sender_name = sender._meta.object_name
|
|
|
|
if sender_name in M2M_NEED_RECORD:
|
|
|
|
action = M2M_ACTION[action]
|
|
|
|
org_id = current_org.id
|
|
|
|
remote_addr = get_request_ip(current_request)
|
|
|
|
user = str(user)
|
|
|
|
resource_type, resource_tmpl_add, resource_tmpl_remove = M2M_NEED_RECORD[sender_name]
|
|
|
|
if action == 'add':
|
|
|
|
resource_tmpl = resource_tmpl_add
|
|
|
|
elif action == 'remove':
|
|
|
|
resource_tmpl = resource_tmpl_remove
|
|
|
|
|
|
|
|
to_create = []
|
|
|
|
objs = model.objects.filter(pk__in=pk_set)
|
|
|
|
|
|
|
|
instance_name = instance._meta.object_name
|
|
|
|
instance_value = str(instance)
|
|
|
|
|
|
|
|
model_name = model._meta.object_name
|
|
|
|
|
|
|
|
for obj in objs:
|
|
|
|
resource = resource_tmpl.format(**{
|
|
|
|
instance_name: instance_value,
|
|
|
|
model_name: str(obj)
|
2021-08-04 03:28:51 +00:00
|
|
|
})[:128] # `resource` 字段只有 128 个字符长 😔
|
2021-07-22 11:53:14 +00:00
|
|
|
|
|
|
|
to_create.append(OperateLog(
|
|
|
|
user=user, action=action, resource_type=resource_type,
|
|
|
|
resource=resource, remote_addr=remote_addr, org_id=org_id
|
|
|
|
))
|
|
|
|
OperateLog.objects.bulk_create(to_create)
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@receiver(post_save)
|
2019-12-05 07:09:25 +00:00
|
|
|
def on_object_created_or_update(sender, instance=None, created=False, update_fields=None, **kwargs):
|
2021-02-26 09:33:11 +00:00
|
|
|
# last_login 改变是最后登录日期, 每次登录都会改变
|
2019-12-05 07:09:25 +00:00
|
|
|
if instance._meta.object_name == 'User' and \
|
|
|
|
update_fields and 'last_login' in update_fields:
|
|
|
|
return
|
2018-09-03 03:24:25 +00:00
|
|
|
if created:
|
2019-08-21 12:27:21 +00:00
|
|
|
action = models.OperateLog.ACTION_CREATE
|
2018-09-03 03:24:25 +00:00
|
|
|
else:
|
2019-08-21 12:27:21 +00:00
|
|
|
action = models.OperateLog.ACTION_UPDATE
|
2018-09-03 03:24:25 +00:00
|
|
|
create_operate_log(action, sender, instance)
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@receiver(post_delete)
|
2018-09-03 03:24:25 +00:00
|
|
|
def on_object_delete(sender, instance=None, **kwargs):
|
2019-08-21 12:27:21 +00:00
|
|
|
create_operate_log(models.OperateLog.ACTION_DELETE, sender, instance)
|
2018-09-03 03:24:25 +00:00
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@receiver(post_user_change_password, sender=User)
|
|
|
|
def on_user_change_password(sender, user=None, **kwargs):
|
|
|
|
if not current_request:
|
|
|
|
remote_addr = '127.0.0.1'
|
|
|
|
change_by = 'System'
|
|
|
|
else:
|
|
|
|
remote_addr = get_request_ip(current_request)
|
|
|
|
if not current_request.user.is_authenticated:
|
|
|
|
change_by = str(user)
|
|
|
|
else:
|
|
|
|
change_by = str(current_request.user)
|
|
|
|
with transaction.atomic():
|
|
|
|
models.PasswordChangeLog.objects.create(
|
|
|
|
user=str(user), change_by=change_by,
|
|
|
|
remote_addr=remote_addr,
|
|
|
|
)
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def on_audits_log_create(sender, instance=None, **kwargs):
|
|
|
|
if sender == models.UserLoginLog:
|
|
|
|
category = "login_log"
|
|
|
|
elif sender == models.FTPLog:
|
|
|
|
category = "ftp_log"
|
|
|
|
elif sender == models.OperateLog:
|
|
|
|
category = "operation_log"
|
|
|
|
elif sender == models.PasswordChangeLog:
|
|
|
|
category = "password_change_log"
|
|
|
|
elif sender == Session:
|
|
|
|
category = "host_session_log"
|
2019-09-25 08:31:58 +00:00
|
|
|
elif sender == Command:
|
|
|
|
category = "session_command_log"
|
2019-08-21 12:27:21 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
data = model_to_json(instance, indent=None)
|
2019-08-21 12:27:21 +00:00
|
|
|
msg = "{} - {}".format(category, data)
|
|
|
|
sys_logger.info(msg)
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
2020-12-09 10:43:13 +00:00
|
|
|
def get_login_backend(request):
|
2021-02-26 09:33:11 +00:00
|
|
|
backend = request.session.get('auth_backend', '') or \
|
|
|
|
request.session.get(BACKEND_SESSION_KEY, '')
|
2021-01-20 10:56:12 +00:00
|
|
|
|
2021-02-26 09:33:11 +00:00
|
|
|
backend_label = AUTH_BACKEND_LABEL_MAPPING.get(backend, None)
|
|
|
|
if backend_label is None:
|
|
|
|
backend_label = ''
|
|
|
|
return backend_label
|
2020-12-09 10:43:13 +00:00
|
|
|
|
|
|
|
|
2021-06-10 10:02:55 +00:00
|
|
|
def generate_data(username, request, login_type=None):
|
2019-11-05 10:46:29 +00:00
|
|
|
user_agent = request.META.get('HTTP_USER_AGENT', '')
|
2020-01-19 04:13:42 +00:00
|
|
|
login_ip = get_request_ip(request) or '0.0.0.0'
|
2021-06-10 10:02:55 +00:00
|
|
|
|
|
|
|
if login_type is None and isinstance(request, Request):
|
2021-04-14 06:45:15 +00:00
|
|
|
login_type = request.META.get('HTTP_X_JMS_LOGIN_TYPE', 'U')
|
2021-06-10 10:02:55 +00:00
|
|
|
if login_type is None:
|
2019-11-05 10:46:29 +00:00
|
|
|
login_type = 'W'
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'username': username,
|
|
|
|
'ip': login_ip,
|
|
|
|
'type': login_type,
|
2021-04-08 08:51:44 +00:00
|
|
|
'user_agent': user_agent[0:254],
|
2020-12-09 10:43:13 +00:00
|
|
|
'datetime': timezone.now(),
|
|
|
|
'backend': get_login_backend(request)
|
2019-11-05 10:46:29 +00:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_auth_success)
|
2021-06-10 10:02:55 +00:00
|
|
|
def on_user_auth_success(sender, user, request, login_type=None, **kwargs):
|
2019-11-05 10:46:29 +00:00
|
|
|
logger.debug('User login success: {}'.format(user.username))
|
2021-06-10 10:02:55 +00:00
|
|
|
data = generate_data(user.username, request, login_type=login_type)
|
2019-11-18 08:30:26 +00:00
|
|
|
data.update({'mfa': int(user.mfa_enabled), 'status': True})
|
2019-12-27 08:00:32 +00:00
|
|
|
write_login_log(**data)
|
2019-11-05 10:46:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_auth_failed)
|
2020-04-28 13:00:22 +00:00
|
|
|
def on_user_auth_failed(sender, username, request, reason='', **kwargs):
|
2019-11-05 10:46:29 +00:00
|
|
|
logger.debug('User login failed: {}'.format(username))
|
|
|
|
data = generate_data(username, request)
|
2020-04-28 13:00:22 +00:00
|
|
|
data.update({'reason': reason[:128], 'status': False})
|
2019-12-27 08:00:32 +00:00
|
|
|
write_login_log(**data)
|