From 57b5f325b356105851adfab09bf0146bf6470713 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 18 Aug 2023 17:58:31 +0200 Subject: [PATCH] Better error handling in lib/pp_admintools/app/ldap.py --- lib/pp_admintools/app/ldap.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/pp_admintools/app/ldap.py b/lib/pp_admintools/app/ldap.py index dd0b544..41e3e02 100644 --- a/lib/pp_admintools/app/ldap.py +++ b/lib/pp_admintools/app/ldap.py @@ -34,6 +34,7 @@ from ldap3 import ALL_ATTRIBUTES from ldap3 import BASE, SUBTREE from ldap3 import Connection, DSA, IP_V4_PREFERRED, SAFE_SYNC, Server from ldap3 import MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE +from ldap3.core.exceptions import LDAPBindError from ldap3.core.exceptions import LDAPException # Own modules @@ -45,7 +46,7 @@ from ..config.ldap import DEFAULT_TIMEOUT from ..config.ldap import LdapConfiguration, LdapConnectionInfo from ..xlate import XLATOR, format_list -__version__ = '0.11.8' +__version__ = '0.11.9' LOG = logging.getLogger(__name__) _ = XLATOR.gettext @@ -737,8 +738,19 @@ class BaseLdapApplication(BaseDPXApplication): url=connect_info.url, dn=bind_dn) LOG.debug(msg) - ldap_connection = Connection( - ldap_server, bind_dn, bind_pw, client_strategy=SAFE_SYNC, auto_bind=True) + try: + ldap_connection = Connection( + ldap_server, bind_dn, bind_pw, client_strategy=SAFE_SYNC, auto_bind=True) + except LDAPBindError as e: + msg = _('Could not connect to LDAP server {url!r} as {user!r}: {e}.').format( + url=connect_info.url, user=bind_dn, e=e) + LOG.error(msg) + self.exit(8) + except LDAPException as e: + msg = _('{c} on connecting to LDAP server {url!r} as {user!r}: {e}.').format( + c=e.__class__.__name__, url=connect_info.url, user=bind_dn, e=e) + LOG.error(msg) + self.exit(9) return ldap_connection @@ -746,6 +758,7 @@ class BaseLdapApplication(BaseDPXApplication): def post_run(self): """Execute after the the payload action of the application.""" LOG.debug(_('Finishing ...')) + super(BaseLdapApplication, self).post_run() self.disconnect_all() -- 2.39.5