You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/settings/tasks/ldap.py

146 lines
4.6 KiB

# coding: utf-8
import time
from celery import shared_task
from django.conf import settings
from django.utils.translation import gettext_lazy as _
from common.utils import get_logger
from common.utils.timezone import local_now_display
from ops.celery.decorator import after_app_ready_start
from ops.celery.utils import (
create_or_update_celery_periodic_tasks
)
from orgs.models import Organization
from settings.notifications import LDAPImportMessage
from users.models import User
from ..utils import LDAPSyncUtil, LDAPServerUtil, LDAPImportUtil
3 months ago
__all__ = [
'sync_ldap_user', 'import_ldap_user_periodic', 'import_ldap_ha_user_periodic',
'import_ldap_user', 'import_ldap_ha_user'
]
logger = get_logger(__file__)
3 months ago
def sync_ldap_user(category='ldap'):
LDAPSyncUtil(category=category).perform_sync()
3 months ago
def perform_import(category, util_server):
start_time = time.time()
time_start_display = local_now_display()
3 months ago
logger.info(f"Start import {category} ldap user task")
util_import = LDAPImportUtil()
users = util_server.search()
3 months ago
if settings.XPACK_ENABLED:
3 months ago
org_ids = getattr(settings, f"AUTH_{category.upper()}_SYNC_ORG_IDS")
default_org = None
else:
org_ids = [Organization.DEFAULT_ID]
default_org = Organization.default()
3 months ago
orgs = list(set([Organization.get_instance(org_id, default=default_org) for org_id in org_ids]))
new_users, errors = util_import.perform_import(users, orgs)
3 months ago
if errors:
3 months ago
logger.error(f"Imported {category} LDAP users errors: {errors}")
else:
3 months ago
logger.info(f"Imported {len(users)} {category} users successfully")
receivers_setting = f"AUTH_{category.upper()}_SYNC_RECEIVERS"
if getattr(settings, receivers_setting, None):
user_ids = getattr(settings, receivers_setting)
recipient_list = User.objects.filter(id__in=list(user_ids))
end_time = time.time()
extra_kwargs = {
'orgs': orgs,
'end_time': end_time,
'start_time': start_time,
'time_start_display': time_start_display,
'new_users': new_users,
'errors': errors,
'cost_time': end_time - start_time,
}
for user in recipient_list:
LDAPImportMessage(user, extra_kwargs).publish()
@shared_task(
3 months ago
verbose_name=_('Periodic import ldap user'),
description=_(
"When LDAP auto-sync is configured, this task will be invoked to synchronize users"
)
)
3 months ago
def import_ldap_user():
perform_import('ldap', LDAPServerUtil())
@shared_task(
verbose_name=_('Periodic import ldap ha user'),
description=_(
"When LDAP auto-sync is configured, this task will be invoked to synchronize users"
3 months ago
)
)
def import_ldap_ha_user():
perform_import('ldap_ha', LDAPServerUtil(category='ldap_ha'))
def register_periodic_task(task_name, task_func, interval_key, enabled_key, crontab_key, **kwargs):
interval = kwargs.get(interval_key, settings.AUTH_LDAP_SYNC_INTERVAL)
enabled = kwargs.get(enabled_key, settings.AUTH_LDAP_SYNC_IS_PERIODIC)
crontab = kwargs.get(crontab_key, settings.AUTH_LDAP_SYNC_CRONTAB)
if isinstance(interval, int):
interval = interval * 3600
else:
interval = None
3 months ago
if crontab:
3 months ago
interval = None # 优先使用 crontab
tasks = {
task_name: {
3 months ago
'task': task_func.name,
'interval': interval,
'crontab': crontab,
'enabled': enabled
}
}
create_or_update_celery_periodic_tasks(tasks)
3 months ago
@shared_task(
verbose_name=_('Registration periodic import ldap user task'),
description=_(
"""When LDAP auto-sync parameters change, such as Crontab parameters, the LDAP sync task
will be re-registered or updated, and this task will be invoked"""
3 months ago
)
)
@after_app_ready_start
def import_ldap_user_periodic(**kwargs):
register_periodic_task(
'import_ldap_user_periodic', import_ldap_user,
'AUTH_LDAP_SYNC_INTERVAL', 'AUTH_LDAP_SYNC_IS_PERIODIC',
'AUTH_LDAP_SYNC_CRONTAB', **kwargs
)
@shared_task(
verbose_name=_('Registration periodic import ldap ha user task'),
description=_(
"""When LDAP HA auto-sync parameters change, such as Crontab parameters, the LDAP HA sync task
will be re-registered or updated, and this task will be invoked"""
3 months ago
)
)
@after_app_ready_start
def import_ldap_ha_user_periodic(**kwargs):
register_periodic_task(
'import_ldap_ha_user_periodic', import_ldap_ha_user,
'AUTH_LDAP_HA_SYNC_INTERVAL', 'AUTH_LDAP_HA_SYNC_IS_PERIODIC',
'AUTH_LDAP_HA_SYNC_CRONTAB', **kwargs
)