2019-11-11 08:41:32 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
import json
|
2023-05-25 09:35:36 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.cache import cache
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-03-12 08:24:38 +00:00
|
|
|
from ldap3 import Server, Connection, SIMPLE
|
|
|
|
from ldap3.core.exceptions import (
|
|
|
|
LDAPSocketOpenError,
|
|
|
|
LDAPSocketReceiveError,
|
|
|
|
LDAPSessionTerminatedByServerError,
|
|
|
|
LDAPUserNameIsMandatoryError,
|
|
|
|
LDAPPasswordIsMandatoryError,
|
|
|
|
LDAPInvalidDnError,
|
|
|
|
LDAPInvalidServerError,
|
|
|
|
LDAPBindError,
|
|
|
|
LDAPInvalidFilterError,
|
|
|
|
LDAPExceptionError,
|
|
|
|
LDAPConfigurationError,
|
|
|
|
LDAPAttributeError,
|
|
|
|
)
|
2019-11-11 08:41:32 +00:00
|
|
|
|
2023-05-25 09:35:36 +00:00
|
|
|
from authentication.backends.ldap import LDAPAuthorizationBackend, LDAPUser
|
2019-11-11 08:41:32 +00:00
|
|
|
from common.const import LDAP_AD_ACCOUNT_DISABLE
|
2021-11-18 10:47:01 +00:00
|
|
|
from common.db.utils import close_old_connections
|
2023-05-25 09:35:36 +00:00
|
|
|
from common.utils import timeit, get_logger
|
2023-07-18 02:10:24 +00:00
|
|
|
from common.utils.http import is_true
|
2023-05-25 09:35:36 +00:00
|
|
|
from orgs.utils import tmp_to_org
|
2022-05-10 11:15:17 +00:00
|
|
|
from users.models import User, UserGroup
|
2023-05-25 09:35:36 +00:00
|
|
|
from users.utils import construct_user_email
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'LDAPConfig', 'LDAPServerUtil', 'LDAPCacheUtil', 'LDAPImportUtil',
|
2020-03-12 08:24:38 +00:00
|
|
|
'LDAPSyncUtil', 'LDAP_USE_CACHE_FLAGS', 'LDAPTestUtil',
|
2019-11-11 08:41:32 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
LDAP_USE_CACHE_FLAGS = [1, '1', 'true', 'True', True]
|
|
|
|
|
|
|
|
|
|
|
|
class LDAPConfig(object):
|
|
|
|
|
|
|
|
def __init__(self, config=None):
|
|
|
|
self.server_uri = None
|
|
|
|
self.bind_dn = None
|
|
|
|
self.password = None
|
|
|
|
self.use_ssl = None
|
2020-03-12 08:24:38 +00:00
|
|
|
self.search_ou = None
|
2019-11-11 08:41:32 +00:00
|
|
|
self.search_filter = None
|
|
|
|
self.attr_map = None
|
2020-03-12 08:24:38 +00:00
|
|
|
self.auth_ldap = None
|
2019-11-11 08:41:32 +00:00
|
|
|
if isinstance(config, dict):
|
|
|
|
self.load_from_config(config)
|
|
|
|
else:
|
|
|
|
self.load_from_settings()
|
|
|
|
|
|
|
|
def load_from_config(self, config):
|
|
|
|
self.server_uri = config.get('server_uri')
|
|
|
|
self.bind_dn = config.get('bind_dn')
|
|
|
|
self.password = config.get('password')
|
|
|
|
self.use_ssl = config.get('use_ssl')
|
2020-03-12 08:24:38 +00:00
|
|
|
self.search_ou = config.get('search_ou')
|
2019-11-11 08:41:32 +00:00
|
|
|
self.search_filter = config.get('search_filter')
|
|
|
|
self.attr_map = config.get('attr_map')
|
2020-03-12 08:24:38 +00:00
|
|
|
self.auth_ldap = config.get('auth_ldap')
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
def load_from_settings(self):
|
|
|
|
self.server_uri = settings.AUTH_LDAP_SERVER_URI
|
|
|
|
self.bind_dn = settings.AUTH_LDAP_BIND_DN
|
|
|
|
self.password = settings.AUTH_LDAP_BIND_PASSWORD
|
|
|
|
self.use_ssl = settings.AUTH_LDAP_START_TLS
|
2020-03-12 08:24:38 +00:00
|
|
|
self.search_ou = settings.AUTH_LDAP_SEARCH_OU
|
2019-11-11 08:41:32 +00:00
|
|
|
self.search_filter = settings.AUTH_LDAP_SEARCH_FILTER
|
|
|
|
self.attr_map = settings.AUTH_LDAP_USER_ATTR_MAP
|
2020-03-12 08:24:38 +00:00
|
|
|
self.auth_ldap = settings.AUTH_LDAP
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LDAPServerUtil(object):
|
|
|
|
|
|
|
|
def __init__(self, config=None):
|
|
|
|
if isinstance(config, dict):
|
|
|
|
self.config = LDAPConfig(config=config)
|
|
|
|
elif isinstance(config, LDAPConfig):
|
|
|
|
self.config = config
|
|
|
|
else:
|
|
|
|
self.config = LDAPConfig()
|
|
|
|
self._conn = None
|
|
|
|
self._paged_size = self.get_paged_size()
|
|
|
|
self.search_users = None
|
|
|
|
self.search_value = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def connection(self):
|
|
|
|
if self._conn:
|
|
|
|
return self._conn
|
|
|
|
server = Server(self.config.server_uri, use_ssl=self.config.use_ssl)
|
|
|
|
conn = Connection(server, self.config.bind_dn, self.config.password)
|
|
|
|
conn.bind()
|
|
|
|
self._conn = conn
|
|
|
|
return self._conn
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_paged_size():
|
|
|
|
paged_size = settings.AUTH_LDAP_SEARCH_PAGED_SIZE
|
|
|
|
if isinstance(paged_size, int):
|
|
|
|
return paged_size
|
|
|
|
return None
|
|
|
|
|
|
|
|
def paged_cookie(self):
|
|
|
|
if self._paged_size is None:
|
|
|
|
return None
|
2019-11-22 05:52:29 +00:00
|
|
|
try:
|
|
|
|
cookie = self.connection.result['controls']['1.2.840.113556.1.4.319']['value']['cookie']
|
|
|
|
return cookie
|
|
|
|
except Exception as e:
|
2021-11-18 10:06:38 +00:00
|
|
|
logger.debug(e, exc_info=True)
|
2019-11-22 05:52:29 +00:00
|
|
|
return None
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
def get_search_filter_extra(self):
|
|
|
|
extra = ''
|
|
|
|
if self.search_users:
|
|
|
|
mapping_username = self.config.attr_map.get('username')
|
|
|
|
for user in self.search_users:
|
|
|
|
extra += '({}={})'.format(mapping_username, user)
|
|
|
|
return '(|{})'.format(extra)
|
|
|
|
if self.search_value:
|
|
|
|
for attr in self.config.attr_map.values():
|
2020-11-05 06:55:22 +00:00
|
|
|
extra += '({}={})'.format(attr, '*{}*'.format(self.search_value))
|
2019-11-11 08:41:32 +00:00
|
|
|
return '(|{})'.format(extra)
|
|
|
|
return extra
|
|
|
|
|
|
|
|
def get_search_filter(self):
|
|
|
|
search_filter = self.config.search_filter % {'user': '*'}
|
|
|
|
search_filter_extra = self.get_search_filter_extra()
|
|
|
|
if search_filter_extra:
|
|
|
|
search_filter = '(&{}{})'.format(search_filter, search_filter_extra)
|
|
|
|
return search_filter
|
|
|
|
|
|
|
|
def search_user_entries_ou(self, search_ou, paged_cookie=None):
|
|
|
|
search_filter = self.get_search_filter()
|
|
|
|
attributes = list(self.config.attr_map.values())
|
2019-11-22 04:25:49 +00:00
|
|
|
self.connection.search(
|
2019-11-11 08:41:32 +00:00
|
|
|
search_base=search_ou, search_filter=search_filter,
|
|
|
|
attributes=attributes, paged_size=self._paged_size,
|
|
|
|
paged_cookie=paged_cookie
|
|
|
|
)
|
|
|
|
|
2020-12-11 10:23:12 +00:00
|
|
|
@staticmethod
|
|
|
|
def distinct_user_entries(user_entries):
|
|
|
|
distinct_user_entries = list()
|
|
|
|
distinct_user_entries_dn = set()
|
|
|
|
for user_entry in user_entries:
|
|
|
|
if user_entry.entry_dn in distinct_user_entries_dn:
|
|
|
|
continue
|
|
|
|
distinct_user_entries_dn.add(user_entry.entry_dn)
|
|
|
|
distinct_user_entries.append(user_entry)
|
|
|
|
return distinct_user_entries
|
|
|
|
|
2019-11-11 08:41:32 +00:00
|
|
|
@timeit
|
2020-12-11 09:38:33 +00:00
|
|
|
def search_user_entries(self, search_users=None, search_value=None):
|
2019-11-11 08:41:32 +00:00
|
|
|
logger.info("Search user entries")
|
2020-12-11 09:38:33 +00:00
|
|
|
self.search_users = search_users
|
|
|
|
self.search_value = search_value
|
2019-11-11 08:41:32 +00:00
|
|
|
user_entries = list()
|
2020-03-12 08:24:38 +00:00
|
|
|
search_ous = str(self.config.search_ou).split('|')
|
2019-11-11 08:41:32 +00:00
|
|
|
for search_ou in search_ous:
|
2021-11-18 04:15:34 +00:00
|
|
|
search_ou = search_ou.strip()
|
2019-11-11 10:50:05 +00:00
|
|
|
logger.info("Search user entries ou: {}".format(search_ou))
|
2019-11-11 08:41:32 +00:00
|
|
|
self.search_user_entries_ou(search_ou)
|
|
|
|
user_entries.extend(self.connection.entries)
|
|
|
|
while self.paged_cookie():
|
|
|
|
self.search_user_entries_ou(search_ou, self.paged_cookie())
|
|
|
|
user_entries.extend(self.connection.entries)
|
2020-12-11 10:23:12 +00:00
|
|
|
user_entries = self.distinct_user_entries(user_entries)
|
2019-11-11 08:41:32 +00:00
|
|
|
return user_entries
|
|
|
|
|
|
|
|
def user_entry_to_dict(self, entry):
|
|
|
|
user = {}
|
|
|
|
attr_map = self.config.attr_map.items()
|
|
|
|
for attr, mapping in attr_map:
|
|
|
|
if not hasattr(entry, mapping):
|
|
|
|
continue
|
|
|
|
value = getattr(entry, mapping).value or ''
|
2023-07-18 02:10:24 +00:00
|
|
|
if attr == 'is_active':
|
|
|
|
if mapping.lower() == 'useraccountcontrol' and value:
|
|
|
|
value = int(value) & LDAP_AD_ACCOUNT_DISABLE != LDAP_AD_ACCOUNT_DISABLE
|
|
|
|
else:
|
|
|
|
value = is_true(value)
|
|
|
|
|
2022-05-10 11:15:17 +00:00
|
|
|
if attr == 'groups' and mapping.lower() == 'memberof':
|
|
|
|
# AD: {'groups': 'memberOf'}
|
|
|
|
if isinstance(value, str) and value:
|
|
|
|
value = [value]
|
|
|
|
if not isinstance(value, list):
|
|
|
|
value = []
|
2021-10-28 06:42:32 +00:00
|
|
|
user[attr] = value.strip() if isinstance(value, str) else value
|
2019-11-11 08:41:32 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
def user_entries_to_dict(self, user_entries):
|
|
|
|
users = []
|
|
|
|
for user_entry in user_entries:
|
|
|
|
user = self.user_entry_to_dict(user_entry)
|
|
|
|
users.append(user)
|
|
|
|
return users
|
|
|
|
|
2020-12-11 09:38:33 +00:00
|
|
|
def search_for_user_dn(self, username):
|
|
|
|
user_entries = self.search_user_entries(search_users=[username])
|
|
|
|
if len(user_entries) == 1:
|
|
|
|
user_entry = user_entries[0]
|
|
|
|
user_dn = user_entry.entry_dn
|
|
|
|
else:
|
|
|
|
user_dn = None
|
|
|
|
return user_dn
|
|
|
|
|
2019-11-11 08:41:32 +00:00
|
|
|
@timeit
|
|
|
|
def search(self, search_users=None, search_value=None):
|
|
|
|
logger.info("Search ldap users")
|
2020-12-11 09:38:33 +00:00
|
|
|
user_entries = self.search_user_entries(
|
|
|
|
search_users=search_users, search_value=search_value
|
|
|
|
)
|
2019-11-11 08:41:32 +00:00
|
|
|
users = self.user_entries_to_dict(user_entries)
|
|
|
|
return users
|
|
|
|
|
|
|
|
|
|
|
|
class LDAPCacheUtil(object):
|
|
|
|
CACHE_KEY_USERS = 'CACHE_KEY_LDAP_USERS'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.search_users = None
|
|
|
|
self.search_value = None
|
|
|
|
|
|
|
|
def set_users(self, users):
|
|
|
|
logger.info('Set ldap users to cache, count: {}'.format(len(users)))
|
|
|
|
cache.set(self.CACHE_KEY_USERS, users, None)
|
|
|
|
|
|
|
|
def get_users(self):
|
|
|
|
users = cache.get(self.CACHE_KEY_USERS)
|
2019-11-11 09:45:39 +00:00
|
|
|
count = users if users is None else len(users)
|
|
|
|
logger.info('Get ldap users from cache, count: {}'.format(count))
|
2019-11-11 08:41:32 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
def delete_users(self):
|
|
|
|
logger.info('Delete ldap users from cache')
|
|
|
|
cache.delete(self.CACHE_KEY_USERS)
|
|
|
|
|
|
|
|
def filter_users(self, users):
|
2019-11-11 09:45:39 +00:00
|
|
|
if users is None:
|
|
|
|
return users
|
2019-11-11 08:41:32 +00:00
|
|
|
if self.search_users:
|
|
|
|
filter_users = [
|
|
|
|
user for user in users
|
|
|
|
if user['username'] in self.search_users
|
|
|
|
]
|
|
|
|
elif self.search_value:
|
2022-05-10 11:15:17 +00:00
|
|
|
filter_users = []
|
|
|
|
for u in users:
|
|
|
|
search_value = self.search_value.lower()
|
|
|
|
user_all_attr_value = [v for v in u.values() if isinstance(v, str)]
|
|
|
|
if search_value not in ','.join(user_all_attr_value).lower():
|
|
|
|
continue
|
|
|
|
filter_users.append(u)
|
2019-11-11 08:41:32 +00:00
|
|
|
else:
|
|
|
|
filter_users = users
|
|
|
|
return filter_users
|
|
|
|
|
|
|
|
def search(self, search_users=None, search_value=None):
|
|
|
|
self.search_users = search_users
|
|
|
|
self.search_value = search_value
|
|
|
|
users = self.get_users()
|
|
|
|
users = self.filter_users(users)
|
|
|
|
return users
|
|
|
|
|
|
|
|
|
|
|
|
class LDAPSyncUtil(object):
|
|
|
|
CACHE_KEY_LDAP_USERS_SYNC_TASK_ERROR_MSG = 'CACHE_KEY_LDAP_USERS_SYNC_TASK_ERROR_MSG'
|
|
|
|
|
|
|
|
CACHE_KEY_LDAP_USERS_SYNC_TASK_STATUS = 'CACHE_KEY_LDAP_USERS_SYNC_TASK_STATUS'
|
|
|
|
TASK_STATUS_IS_RUNNING = 'RUNNING'
|
|
|
|
TASK_STATUS_IS_OVER = 'OVER'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.server_util = LDAPServerUtil()
|
|
|
|
self.cache_util = LDAPCacheUtil()
|
|
|
|
self.task_error_msg = None
|
|
|
|
|
|
|
|
def clear_cache(self):
|
|
|
|
logger.info('Clear ldap sync cache')
|
|
|
|
self.delete_task_status()
|
|
|
|
self.delete_task_error_msg()
|
|
|
|
self.cache_util.delete_users()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def task_no_start(self):
|
|
|
|
status = self.get_task_status()
|
|
|
|
return status is None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def task_is_running(self):
|
|
|
|
status = self.get_task_status()
|
|
|
|
return status == self.TASK_STATUS_IS_RUNNING
|
|
|
|
|
|
|
|
@property
|
|
|
|
def task_is_over(self):
|
|
|
|
status = self.get_task_status()
|
|
|
|
return status == self.TASK_STATUS_IS_OVER
|
|
|
|
|
|
|
|
def set_task_status(self, status):
|
|
|
|
logger.info('Set task status: {}'.format(status))
|
|
|
|
cache.set(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_STATUS, status, None)
|
|
|
|
|
|
|
|
def get_task_status(self):
|
|
|
|
status = cache.get(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_STATUS)
|
|
|
|
logger.info('Get task status: {}'.format(status))
|
|
|
|
return status
|
|
|
|
|
|
|
|
def delete_task_status(self):
|
|
|
|
logger.info('Delete task status')
|
|
|
|
cache.delete(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_STATUS)
|
|
|
|
|
|
|
|
def set_task_error_msg(self, error_msg):
|
|
|
|
logger.info('Set task error msg')
|
|
|
|
cache.set(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_ERROR_MSG, error_msg, None)
|
|
|
|
|
|
|
|
def get_task_error_msg(self):
|
|
|
|
logger.info('Get task error msg')
|
|
|
|
error_msg = cache.get(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_ERROR_MSG)
|
|
|
|
return error_msg
|
|
|
|
|
|
|
|
def delete_task_error_msg(self):
|
|
|
|
logger.info('Delete task error msg')
|
|
|
|
cache.delete(self.CACHE_KEY_LDAP_USERS_SYNC_TASK_ERROR_MSG)
|
|
|
|
|
|
|
|
def pre_sync(self):
|
|
|
|
self.set_task_status(self.TASK_STATUS_IS_RUNNING)
|
|
|
|
|
|
|
|
def sync(self):
|
|
|
|
users = self.server_util.search()
|
|
|
|
self.cache_util.set_users(users)
|
|
|
|
|
|
|
|
def post_sync(self):
|
|
|
|
self.set_task_status(self.TASK_STATUS_IS_OVER)
|
|
|
|
|
|
|
|
def perform_sync(self):
|
|
|
|
logger.info('Start perform sync ldap users from server to cache')
|
|
|
|
try:
|
2021-11-18 10:47:01 +00:00
|
|
|
self.pre_sync()
|
2019-11-11 08:41:32 +00:00
|
|
|
self.sync()
|
2021-11-18 10:47:01 +00:00
|
|
|
self.post_sync()
|
2019-11-11 08:41:32 +00:00
|
|
|
except Exception as e:
|
|
|
|
error_msg = str(e)
|
|
|
|
logger.error(error_msg)
|
|
|
|
self.set_task_error_msg(error_msg)
|
2021-11-18 10:47:01 +00:00
|
|
|
finally:
|
|
|
|
logger.info('End perform sync ldap users from server to cache')
|
|
|
|
close_old_connections()
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LDAPImportUtil(object):
|
2022-05-10 11:15:17 +00:00
|
|
|
user_group_name_prefix = 'AD '
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_email(user):
|
|
|
|
username = user['username']
|
|
|
|
email = user['email']
|
|
|
|
email = construct_user_email(username, email)
|
|
|
|
return email
|
|
|
|
|
|
|
|
def update_or_create(self, user):
|
|
|
|
user['email'] = self.get_user_email(user)
|
|
|
|
if user['username'] not in ['admin']:
|
2020-12-09 10:43:13 +00:00
|
|
|
user['source'] = User.Source.ldap.value
|
2019-11-11 08:41:32 +00:00
|
|
|
obj, created = User.objects.update_or_create(
|
|
|
|
username=user['username'], defaults=user
|
|
|
|
)
|
|
|
|
return obj, created
|
|
|
|
|
2022-05-10 11:15:17 +00:00
|
|
|
def get_user_group_names(self, groups) -> list:
|
|
|
|
if not isinstance(groups, list):
|
|
|
|
logger.error('Groups type not list')
|
|
|
|
return []
|
|
|
|
group_names = []
|
|
|
|
for group in groups:
|
|
|
|
if not group:
|
|
|
|
continue
|
|
|
|
if not isinstance(group, str):
|
|
|
|
continue
|
|
|
|
# get group name for AD, Such as: CN=Users,CN=Builtin,DC=jms,DC=com
|
|
|
|
group_name = group.split(',')[0].split('=')[-1]
|
|
|
|
group_name = f'{self.user_group_name_prefix}{group_name}'.strip()
|
|
|
|
group_names.append(group_name)
|
|
|
|
return group_names
|
|
|
|
|
2023-05-25 09:35:36 +00:00
|
|
|
def perform_import(self, users, orgs):
|
2019-11-11 08:41:32 +00:00
|
|
|
logger.info('Start perform import ldap users, count: {}'.format(len(users)))
|
|
|
|
errors = []
|
2021-06-22 11:09:34 +00:00
|
|
|
objs = []
|
2022-05-10 11:15:17 +00:00
|
|
|
group_users_mapper = defaultdict(set)
|
2019-11-11 08:41:32 +00:00
|
|
|
for user in users:
|
2022-05-10 11:15:17 +00:00
|
|
|
groups = user.pop('groups', [])
|
2019-11-11 08:41:32 +00:00
|
|
|
try:
|
2021-06-22 11:09:34 +00:00
|
|
|
obj, created = self.update_or_create(user)
|
|
|
|
objs.append(obj)
|
2019-11-11 08:41:32 +00:00
|
|
|
except Exception as e:
|
|
|
|
errors.append({user['username']: str(e)})
|
|
|
|
logger.error(e)
|
2022-05-10 11:15:17 +00:00
|
|
|
continue
|
|
|
|
try:
|
|
|
|
group_names = self.get_user_group_names(groups)
|
|
|
|
for group_name in group_names:
|
|
|
|
group_users_mapper[group_name].add(obj)
|
|
|
|
except Exception as e:
|
|
|
|
errors.append({user['username']: str(e)})
|
|
|
|
logger.error(e)
|
|
|
|
continue
|
2023-05-25 09:35:36 +00:00
|
|
|
for org in orgs:
|
|
|
|
self.bind_org(org, objs, group_users_mapper)
|
|
|
|
logger.info('End perform import ldap users')
|
|
|
|
return errors
|
|
|
|
|
|
|
|
@staticmethod
|
2023-10-09 05:55:55 +00:00
|
|
|
def exit_user_group(user_groups_mapper):
|
|
|
|
# 通过对比查询本次导入用户需要移除的用户组
|
|
|
|
group_remove_users_mapper = defaultdict(set)
|
|
|
|
for user, current_groups in user_groups_mapper.items():
|
|
|
|
old_groups = set(user.groups.all())
|
|
|
|
exit_groups = old_groups - current_groups
|
|
|
|
logger.debug(f'Ldap user {user} exits user groups {exit_groups}')
|
|
|
|
for g in exit_groups:
|
|
|
|
group_remove_users_mapper[g].add(user)
|
|
|
|
|
|
|
|
# 根据用户组统一移除用户
|
|
|
|
for g, rm_users in group_remove_users_mapper.items():
|
|
|
|
g.users.remove(*rm_users)
|
|
|
|
|
|
|
|
def bind_org(self, org, users, group_users_mapper):
|
2022-04-20 08:05:33 +00:00
|
|
|
if not org:
|
|
|
|
return
|
|
|
|
if org.is_root():
|
2022-02-18 06:28:28 +00:00
|
|
|
return
|
2022-05-10 11:15:17 +00:00
|
|
|
# add user to org
|
2023-05-25 09:35:36 +00:00
|
|
|
for user in users:
|
|
|
|
org.add_member(user)
|
2022-05-10 11:15:17 +00:00
|
|
|
# add user to group
|
|
|
|
with tmp_to_org(org):
|
2023-10-09 05:55:55 +00:00
|
|
|
user_groups_mapper = defaultdict(set)
|
2022-05-10 11:15:17 +00:00
|
|
|
for group_name, users in group_users_mapper.items():
|
|
|
|
group, created = UserGroup.objects.get_or_create(
|
|
|
|
name=group_name, defaults={'name': group_name}
|
|
|
|
)
|
2023-10-09 05:55:55 +00:00
|
|
|
for user in users:
|
|
|
|
user_groups_mapper[user].add(group)
|
2022-05-10 11:15:17 +00:00
|
|
|
group.users.add(*users)
|
2023-10-09 05:55:55 +00:00
|
|
|
self.exit_user_group(user_groups_mapper)
|
2019-11-11 08:41:32 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class LDAPTestUtil(object):
|
|
|
|
class LDAPInvalidSearchOuOrFilterError(LDAPExceptionError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class LDAPInvalidAttributeMapError(LDAPExceptionError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class LDAPNotEnabledAuthError(LDAPExceptionError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class LDAPBeforeLoginCheckError(LDAPExceptionError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, config=None):
|
|
|
|
self.config = LDAPConfig(config)
|
|
|
|
self.user_entries = []
|
|
|
|
|
|
|
|
def _test_connection_bind(self, authentication=None, user=None, password=None):
|
|
|
|
server = Server(self.config.server_uri)
|
|
|
|
connection = Connection(
|
|
|
|
server, user=user, password=password, authentication=authentication
|
|
|
|
)
|
|
|
|
ret = connection.bind()
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# test server uri
|
|
|
|
|
2021-07-23 03:16:12 +00:00
|
|
|
def _check_server_uri(self):
|
|
|
|
if not any([self.config.server_uri.startswith('ldap://') or
|
2021-07-23 06:48:40 +00:00
|
|
|
self.config.server_uri.startswith('ldaps://')]):
|
2021-07-23 03:16:12 +00:00
|
|
|
err = _('ldap:// or ldaps:// protocol is used.')
|
|
|
|
raise LDAPInvalidServerError(err)
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def _test_server_uri(self):
|
|
|
|
self._test_connection_bind()
|
|
|
|
|
|
|
|
def test_server_uri(self):
|
|
|
|
try:
|
2021-07-23 03:16:12 +00:00
|
|
|
self._check_server_uri()
|
2020-03-12 08:24:38 +00:00
|
|
|
self._test_server_uri()
|
|
|
|
except LDAPSocketOpenError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _("Host or port is disconnected: {}").format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPSessionTerminatedByServerError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('The port is not the port of the LDAP service: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPSocketReceiveError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Please add certificate: {}').format(e)
|
2021-07-23 03:16:12 +00:00
|
|
|
except LDAPInvalidServerError as e:
|
|
|
|
error = str(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except Exception as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Unknown error: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
raise LDAPInvalidServerError(error)
|
|
|
|
|
|
|
|
# test bind dn
|
|
|
|
|
|
|
|
def _test_bind_dn(self):
|
|
|
|
user = self.config.bind_dn
|
|
|
|
password = self.config.password
|
|
|
|
ret = self._test_connection_bind(
|
|
|
|
authentication=SIMPLE, user=user, password=password
|
|
|
|
)
|
|
|
|
if not ret:
|
2020-03-12 11:00:22 +00:00
|
|
|
msg = _('Bind DN or Password incorrect')
|
2020-03-12 08:24:38 +00:00
|
|
|
raise LDAPInvalidDnError(msg)
|
|
|
|
|
|
|
|
def test_bind_dn(self):
|
|
|
|
try:
|
|
|
|
self._test_bind_dn()
|
|
|
|
except LDAPUserNameIsMandatoryError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Please enter Bind DN: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPPasswordIsMandatoryError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Please enter Password: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPInvalidDnError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Please enter correct Bind DN and Password: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except Exception as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Unknown error: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
raise LDAPBindError(error)
|
|
|
|
|
|
|
|
# test search ou
|
|
|
|
|
|
|
|
def _test_search_ou_and_filter(self):
|
|
|
|
config = deepcopy(self.config)
|
|
|
|
util = LDAPServerUtil(config=config)
|
|
|
|
search_ous = str(self.config.search_ou).split('|')
|
|
|
|
for search_ou in search_ous:
|
|
|
|
util.config.search_ou = search_ou
|
|
|
|
user_entries = util.search_user_entries()
|
2020-06-16 09:25:57 +00:00
|
|
|
logger.debug('Search ou: {}, count user: {}'.format(search_ou, len(user_entries)))
|
2020-03-12 08:24:38 +00:00
|
|
|
if len(user_entries) == 0:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Invalid User OU or User search filter: {}').format(search_ou)
|
2020-03-12 08:24:38 +00:00
|
|
|
raise self.LDAPInvalidSearchOuOrFilterError(error)
|
|
|
|
|
|
|
|
def test_search_ou_and_filter(self):
|
|
|
|
try:
|
|
|
|
self._test_search_ou_and_filter()
|
|
|
|
except LDAPInvalidFilterError as e:
|
|
|
|
error = e
|
|
|
|
except self.LDAPInvalidSearchOuOrFilterError as e:
|
|
|
|
error = e
|
|
|
|
except LDAPAttributeError as e:
|
|
|
|
error = e
|
|
|
|
raise self.LDAPInvalidAttributeMapError(error)
|
|
|
|
except Exception as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Unknown error: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
raise self.LDAPInvalidSearchOuOrFilterError(error)
|
|
|
|
|
|
|
|
# test attr map
|
|
|
|
|
|
|
|
def _test_attr_map(self):
|
|
|
|
attr_map = self.config.attr_map
|
|
|
|
if not isinstance(attr_map, dict):
|
|
|
|
attr_map = json.loads(attr_map)
|
|
|
|
self.config.attr_map = attr_map
|
|
|
|
|
|
|
|
should_contain_attr = {'username', 'name', 'email'}
|
|
|
|
actually_contain_attr = set(attr_map.keys())
|
|
|
|
result = should_contain_attr - actually_contain_attr
|
|
|
|
if len(result) != 0:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('LDAP User attr map not include: {}').format(result)
|
2020-03-12 08:24:38 +00:00
|
|
|
raise self.LDAPInvalidAttributeMapError(error)
|
|
|
|
|
|
|
|
def test_attr_map(self):
|
|
|
|
try:
|
|
|
|
self._test_attr_map()
|
|
|
|
except json.JSONDecodeError:
|
2020-03-12 11:00:22 +00:00
|
|
|
error = _('LDAP User attr map is not dict')
|
2020-03-12 08:24:38 +00:00
|
|
|
except self.LDAPInvalidAttributeMapError as e:
|
|
|
|
error = e
|
|
|
|
except Exception as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
error = _('Unknown error: {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
raise self.LDAPInvalidAttributeMapError(error)
|
|
|
|
|
|
|
|
# test search
|
|
|
|
|
|
|
|
def test_search(self):
|
|
|
|
util = LDAPServerUtil(config=self.config)
|
|
|
|
self.user_entries = util.search_user_entries()
|
|
|
|
|
|
|
|
# test auth ldap enabled
|
|
|
|
|
|
|
|
def test_enabled_auth_ldap(self):
|
|
|
|
if not self.config.auth_ldap:
|
|
|
|
error = _('LDAP authentication is not enabled')
|
|
|
|
raise self.LDAPNotEnabledAuthError(error)
|
|
|
|
|
|
|
|
# test config
|
|
|
|
|
|
|
|
def _test_config(self):
|
|
|
|
self.test_server_uri()
|
|
|
|
self.test_bind_dn()
|
|
|
|
self.test_attr_map()
|
|
|
|
self.test_search_ou_and_filter()
|
|
|
|
self.test_search()
|
|
|
|
self.test_enabled_auth_ldap()
|
|
|
|
|
|
|
|
def test_config(self):
|
|
|
|
status = False
|
|
|
|
try:
|
|
|
|
self._test_config()
|
|
|
|
except LDAPInvalidServerError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Error (Invalid LDAP server): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPBindError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Error (Invalid Bind DN): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except self.LDAPInvalidAttributeMapError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Error (Invalid LDAP User attr map): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except self.LDAPInvalidSearchOuOrFilterError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Error (Invalid User OU or User search filter): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except self.LDAPNotEnabledAuthError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Error (Not enabled LDAP authentication): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except Exception as e:
|
|
|
|
msg = _('Error (Unknown): {}').format(e)
|
|
|
|
else:
|
|
|
|
status = True
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Succeed: Match {} s user').format(len(self.user_entries))
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
if not status:
|
|
|
|
logger.error(msg, exc_info=True)
|
|
|
|
return status, msg
|
|
|
|
|
|
|
|
# test login
|
|
|
|
|
|
|
|
def _test_before_login_check(self, username, password):
|
|
|
|
ok, msg = self.test_config()
|
|
|
|
if not ok:
|
|
|
|
raise LDAPConfigurationError(msg)
|
|
|
|
|
|
|
|
backend = LDAPAuthorizationBackend()
|
|
|
|
ok, msg = backend.pre_check(username, password)
|
|
|
|
if not ok:
|
|
|
|
raise self.LDAPBeforeLoginCheckError(msg)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _test_login_auth(username, password):
|
|
|
|
backend = LDAPAuthorizationBackend()
|
|
|
|
ldap_user = LDAPUser(backend, username=username.strip())
|
|
|
|
ldap_user._authenticate_user_dn(password)
|
|
|
|
|
|
|
|
def _test_login(self, username, password):
|
|
|
|
self._test_before_login_check(username, password)
|
|
|
|
self._test_login_auth(username, password)
|
|
|
|
|
|
|
|
def test_login(self, username, password):
|
|
|
|
status = False
|
|
|
|
try:
|
|
|
|
self._test_login(username, password)
|
|
|
|
except LDAPConfigurationError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Authentication failed (configuration incorrect): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except self.LDAPBeforeLoginCheckError as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Authentication failed (before login check failed): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except LDAPUser.AuthenticationFailed as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _('Authentication failed (username or password incorrect): {}').format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
except Exception as e:
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _("Authentication failed (Unknown): {}").format(e)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
|
|
|
status = True
|
2020-06-10 07:10:43 +00:00
|
|
|
msg = _("Authentication success: {}").format(username)
|
2019-11-11 08:41:32 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
if not status:
|
|
|
|
logger.error(msg, exc_info=True)
|
|
|
|
return status, msg
|