2023-02-08 03:32:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-02-19 07:10:10 +00:00
|
|
|
|
2023-02-08 03:32:05 +00:00
|
|
|
from celery import signals
|
|
|
|
from django.db.models.signals import post_save
|
2023-02-14 12:01:29 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _, gettext_noop
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-13 11:42:42 +00:00
|
|
|
from audits.models import ActivityLog
|
2023-02-19 07:10:10 +00:00
|
|
|
from common.utils import i18n_fmt, get_logger
|
2023-02-13 11:42:42 +00:00
|
|
|
from jumpserver.utils import current_request
|
2023-02-08 03:32:05 +00:00
|
|
|
from ops.celery import app
|
2023-02-14 14:56:36 +00:00
|
|
|
from orgs.models import Organization
|
2023-02-22 13:48:11 +00:00
|
|
|
from orgs.utils import current_org, tmp_to_org
|
2023-02-08 03:32:05 +00:00
|
|
|
from terminal.models import Session
|
|
|
|
from users.models import User
|
|
|
|
from ..const import ActivityChoices
|
2023-02-13 11:42:42 +00:00
|
|
|
from ..models import UserLoginLog
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
logger = get_logger(__name__)
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
|
|
|
|
class ActivityLogHandler:
|
2023-02-08 03:32:05 +00:00
|
|
|
@staticmethod
|
|
|
|
def session_for_activity(obj):
|
2023-02-14 12:01:29 +00:00
|
|
|
detail = i18n_fmt(
|
|
|
|
gettext_noop('User %s use account %s login asset %s'),
|
|
|
|
obj.user, obj.account, obj.asset
|
2023-02-08 03:32:05 +00:00
|
|
|
)
|
2023-02-14 14:56:36 +00:00
|
|
|
return [obj.asset_id, obj.user_id, obj.account_id], detail, ActivityChoices.session_log, obj.org_id
|
2023-02-08 03:32:05 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def login_log_for_activity(obj):
|
2023-02-14 12:01:29 +00:00
|
|
|
login_status = gettext_noop('Success') if obj.status else gettext_noop('Failed')
|
|
|
|
detail = i18n_fmt(gettext_noop('User %s login system %s'), obj.username, login_status)
|
2023-02-17 09:14:53 +00:00
|
|
|
|
|
|
|
username = obj.username
|
|
|
|
user_id = User.objects.filter(username=username) \
|
|
|
|
.values_list('id', flat=True).first()
|
2023-02-14 12:01:29 +00:00
|
|
|
resource_list = []
|
|
|
|
if user_id:
|
2023-02-17 09:14:53 +00:00
|
|
|
resource_list = [user_id]
|
2023-02-14 14:56:36 +00:00
|
|
|
return resource_list, detail, ActivityChoices.login_log, Organization.SYSTEM_ID
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
@staticmethod
|
|
|
|
def task_log_for_celery(headers, body):
|
|
|
|
task_id, task_name = headers.get('id'), headers.get('task')
|
|
|
|
task = app.tasks.get(task_name)
|
|
|
|
if not task:
|
|
|
|
raise ValueError('Task not found: {}'.format(task_name))
|
|
|
|
activity_callback = getattr(task, 'activity_callback', None)
|
|
|
|
if not callable(activity_callback):
|
|
|
|
return [], '', ''
|
|
|
|
args, kwargs = body[:2]
|
|
|
|
data = activity_callback(*args, **kwargs)
|
|
|
|
if data is None:
|
|
|
|
return [], '', ''
|
|
|
|
resource_ids, org_id, user = data + ('',) * (3 - len(data))
|
|
|
|
if not user:
|
|
|
|
user = str(current_request.user) if current_request else 'System'
|
2023-02-23 02:46:59 +00:00
|
|
|
if not org_id:
|
|
|
|
org_id = current_org.id
|
2023-02-17 09:14:53 +00:00
|
|
|
task_display = getattr(task, 'verbose_name', _('Unknown'))
|
|
|
|
detail = i18n_fmt(
|
|
|
|
gettext_noop('User %s perform a task for this resource: %s'),
|
|
|
|
user, task_display
|
|
|
|
)
|
|
|
|
return resource_ids, detail, org_id
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-14 12:01:29 +00:00
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
def create_activities(resource_ids, detail, detail_id, action, org_id):
|
|
|
|
if not resource_ids:
|
2023-10-18 09:25:44 +00:00
|
|
|
return
|
2023-10-10 09:39:41 +00:00
|
|
|
if not org_id:
|
|
|
|
org_id = Organization.ROOT_ID
|
2023-02-14 12:01:29 +00:00
|
|
|
activities = [
|
2023-02-17 09:14:53 +00:00
|
|
|
ActivityLog(
|
|
|
|
resource_id=getattr(resource_id, 'pk', resource_id),
|
|
|
|
type=action, detail=detail, detail_id=detail_id, org_id=org_id
|
|
|
|
)
|
2023-02-14 12:01:29 +00:00
|
|
|
for resource_id in resource_ids
|
|
|
|
]
|
2023-02-22 13:48:11 +00:00
|
|
|
with tmp_to_org(org_id):
|
|
|
|
ActivityLog.objects.bulk_create(activities)
|
2023-02-17 09:14:53 +00:00
|
|
|
return activities
|
2023-02-08 03:32:05 +00:00
|
|
|
|
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
@signals.after_task_publish.connect
|
|
|
|
def after_task_publish_for_activity_log(headers=None, body=None, **kwargs):
|
|
|
|
""" Tip: https://docs.celeryq.dev/en/stable/internals/protocol.html#message-protocol-task-v2 """
|
|
|
|
try:
|
|
|
|
task_id = headers.get('id')
|
|
|
|
resource_ids, detail, org_id = ActivityLogHandler.task_log_for_celery(headers, body)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f'Get celery task info error: {e}', exc_info=True)
|
|
|
|
else:
|
|
|
|
logger.debug(f'Create activity log for celery task: {task_id}')
|
2023-10-12 08:36:08 +00:00
|
|
|
if not resource_ids:
|
|
|
|
return
|
2023-02-17 09:14:53 +00:00
|
|
|
create_activities(resource_ids, detail, task_id, action=ActivityChoices.task, org_id=org_id)
|
2023-02-08 03:32:05 +00:00
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
|
|
|
|
model_activity_handler_map = {
|
|
|
|
Session: ActivityLogHandler.session_for_activity,
|
|
|
|
UserLoginLog: ActivityLogHandler.login_log_for_activity,
|
|
|
|
}
|
2023-02-08 03:32:05 +00:00
|
|
|
|
|
|
|
|
2023-02-14 12:01:29 +00:00
|
|
|
def on_session_or_login_log_created(sender, instance=None, created=False, **kwargs):
|
2023-02-17 09:14:53 +00:00
|
|
|
if not created:
|
2023-02-08 03:32:05 +00:00
|
|
|
return
|
|
|
|
|
2023-02-17 09:14:53 +00:00
|
|
|
func = model_activity_handler_map.get(sender)
|
|
|
|
if not func:
|
|
|
|
logger.error('Activity log handler not found: {}'.format(sender))
|
|
|
|
|
|
|
|
resource_ids, detail, act_type, org_id = func(instance)
|
2023-10-18 09:09:14 +00:00
|
|
|
if not resource_ids:
|
|
|
|
return
|
2023-02-17 09:14:53 +00:00
|
|
|
return create_activities(resource_ids, detail, instance.id, act_type, org_id)
|
2023-02-08 03:32:05 +00:00
|
|
|
|
|
|
|
|
2023-02-14 12:01:29 +00:00
|
|
|
for sd in [Session, UserLoginLog]:
|
|
|
|
post_save.connect(on_session_or_login_log_created, sender=sd)
|