From: Frank Brehm Date: Fri, 18 Aug 2023 15:58:31 +0000 (+0200) Subject: Better error handling in lib/pp_admintools/app/ldap.py X-Git-Tag: 0.11.0~3^2~14 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=57b5f325b356105851adfab09bf0146bf6470713;p=pixelpark%2Fpp-admin-tools.git Better error handling in lib/pp_admintools/app/ldap.py --- 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()