Compare commits

..

No commits in common. "31eeff367c50b0c3481e9bfa465cbe411cc866b6" and "6adcc1eeed187893cbf4ff2eb1492a26e3b8b234" have entirely different histories.

View file

@ -116,7 +116,7 @@ class LdapServer:
return ldap.SCOPE_SUBTREE # pylint: disable=no-member return ldap.SCOPE_SUBTREE # pylint: disable=no-member
raise Exception(f'Unknown LDAP scope "{scope}"') raise Exception(f'Unknown LDAP scope "{scope}"')
def search(self, basedn, filterstr=None, attrs=None, sizelimit=None, scope=None): def search(self, basedn, filterstr=None, attrs=None, sizelimit=0, scope=None):
""" Run a search on LDAP server """ """ Run a search on LDAP server """
assert self.con or self.connect() assert self.con or self.connect()
res_id = self.con.search( res_id = self.con.search(
@ -141,18 +141,10 @@ class LdapServer:
result = self.search(dn, filterstr=filterstr, scope='base', attrs=attrs) result = self.search(dn, filterstr=filterstr, scope='base', attrs=attrs)
return result[dn] if dn in result else None return result[dn] if dn in result else None
def paged_search(self, basedn, filterstr=None, attrs=None, scope=None, pagesize=None, def paged_search(self, basedn, filterstr, attrs, scope='sub', pagesize=500):
sizelimit=None):
""" Run a paged search on LDAP server """ """ Run a paged search on LDAP server """
assert not self.v2, "Paged search is not available on LDAP version 2" assert not self.v2, "Paged search is not available on LDAP version 2"
assert self.con or self.connect() assert self.con or self.connect()
# Set parameters default values (if not defined)
filterstr = filterstr if filterstr else '(objectClass=*)'
attrs = attrs if attrs else []
scope = scope if scope else 'sub'
pagesize = pagesize if pagesize else 500
# Initialize SimplePagedResultsControl object # Initialize SimplePagedResultsControl object
page_control = SimplePagedResultsControl( page_control = SimplePagedResultsControl(
True, True,
@ -216,13 +208,6 @@ class LdapServer:
# Store results of this page # Store results of this page
for obj_dn, obj_attrs in rdata: for obj_dn, obj_attrs in rdata:
ret[obj_dn] = obj_attrs ret[obj_dn] = obj_attrs
# If sizelimit reached, stop
if sizelimit and len(ret) >= sizelimit:
break
# If sizelimit reached, stop
if sizelimit and len(ret) >= sizelimit:
break
# If no cookie returned, we are done # If no cookie returned, we are done
if not result_page_control.cookie: if not result_page_control.cookie:
@ -538,8 +523,7 @@ class LdapClient:
return vals if all_values else vals[0] return vals if all_values else vals[0]
return default if default or not all_values else [] return default if default or not all_values else []
def get_objects(self, name, filterstr, basedn, attrs, key_attr=None, warn=True, def get_objects(self, name, filterstr, basedn, attrs, key_attr=None, warn=True):
paged_search=False, pagesize=None):
""" """
Retrieve objects from LDAP Retrieve objects from LDAP
@ -552,25 +536,17 @@ class LdapClient:
:param warn: If True, a warning message will be logged if no object is found :param warn: If True, a warning message will be logged if no object is found
in LDAP directory (otherwise, it will be just a debug message) in LDAP directory (otherwise, it will be just a debug message)
(optional, default: True) (optional, default: True)
:param paged_search: If True, use paged search to list objects from LDAP directory
(optional, default: False)
:param pagesize: When using paged search, the page size
(optional, default: see LdapServer.paged_search)
""" """
if name in self._cached_objects: if name in self._cached_objects:
log.debug('Retreived %s objects from cache', name) log.debug('Retreived %s objects from cache', name)
else: else:
assert self._conn or self.initialize() assert self._conn or self.initialize()
log.debug('Looking for LDAP %s with (filter="%s" / basedn="%s")', name, filterstr, basedn) log.debug('Looking for LDAP %s with (filter="%s" / basedn="%s")', name, filterstr, basedn)
if paged_search: ldap_data = self._conn.search(
ldap_data = self._conn.paged_search( basedn=basedn,
basedn=basedn, filterstr=filterstr, attrs=attrs, filterstr=filterstr,
pagesize=pagesize attrs=attrs
) )
else:
ldap_data = self._conn.search(
basedn=basedn, filterstr=filterstr, attrs=attrs,
)
if not ldap_data: if not ldap_data:
if warn: if warn: