]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Better error handling in lib/pp_admintools/app/ldap.py
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 18 Aug 2023 15:58:31 +0000 (17:58 +0200)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 18 Aug 2023 15:58:31 +0000 (17:58 +0200)
lib/pp_admintools/app/ldap.py

index dd0b544f3fb3ddaf2176c0d87c6a17cb18a134ad..41e3e02338bd38397d256464145dd84c463b057f 100644 (file)
@@ -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()