Buenas tardes. Recientemente configuré PgAdmin 4.25 con autenticación LDAP y pasé varias horas tratando de encontrar el error ya que siempre obtenía como resultado "Could not find the specified user.".
Me percate que el código del método LDAPAuthentication.search_ldap_user() no funciona sobre un un directorio con una gran cantidad de resultados. Por defecto los servidores como OpenLDAP limitan la cantidad de resultados que puede ser obtenido por un cliente, normalmente solo las cuentas administradoras pueden iterar sobre todo el directorio. El código en la función indicada se basa en iterar todos los resultados obtenidos y comparar el nombre de usuario, lo cual es ineficiente y provoca que en directos muy extensos los usuarios no sean encontrados. Adjunto un patch del archivo ldap.py que simplifica la búsqueda agregando a la condición de búsqueda del usuario directamente en el filtro aplicada en la búsqueda. Quedo atento a cualquier observación. Saludos y muy agradecido por su trabajo. -- Rómulo J. Rodríguez Rojas rodriguezr...@gmail.com
diff --git a/web/pgadmin/authenticate/ldap.py b/web/pgadmin/authenticate/ldap.py index b72d7d674..208ac6739 100644 --- a/web/pgadmin/authenticate/ldap.py +++ b/web/pgadmin/authenticate/ldap.py @@ -225,8 +225,14 @@ class LDAPAuthentication(BaseAuthentication): elif not search_base_dn or search_base_dn == '<Search-Base-DN>': search_base_dn = config.LDAP_BASE_DN + search_filter = "({0}={1})".format(config.LDAP_USERNAME_ATTRIBUTE, + self.username) + if config.LDAP_SEARCH_FILTER: + search_filter = "(&{0}{1})".format(search_filter, + config.LDAP_SEARCH_FILTER) + self.conn.search(search_base=search_base_dn, - search_filter=config.LDAP_SEARCH_FILTER, + search_filter=search_filter, search_scope=config.LDAP_SEARCH_SCOPE, attributes=ALL_ATTRIBUTES ) @@ -247,9 +253,11 @@ class LDAPAuthentication(BaseAuthentication): ) return False, ERROR_SEARCHING_LDAP_DIRECTORY.format(e.args[0]) - for entry in self.conn.entries: - if config.LDAP_USERNAME_ATTRIBUTE in entry and self.username == \ - entry[config.LDAP_USERNAME_ATTRIBUTE].value: - return True, entry - return False, ERROR_SEARCHING_LDAP_DIRECTORY.format( - "Could not find the specified user.") + results = len(self.conn.entries) + if results > 1: + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format( + "More than one result found.") + elif results < 1: + return False, ERROR_SEARCHING_LDAP_DIRECTORY.format( + "Could not find the specified user.") + return True, self.conn.entries[0]