]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Caching IP address of LDAP server.
authorFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Feb 2024 14:09:45 +0000 (15:09 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Tue, 6 Feb 2024 14:09:45 +0000 (15:09 +0100)
lib/pp_admintools/app/ldap.py
lib/pp_admintools/config/ldap.py
lib/pp_admintools/errors.py

index 372001acff116396b3ea60a18e41780cda2468ce..df186f3918dadce2ebb9f3a67f9c2fe68d3e2725 100644 (file)
@@ -12,12 +12,14 @@ from __future__ import absolute_import
 import logging
 import os
 import re
+import socket
 import time
 try:
     from pathlib import Path
 except ImportError:
     from pathlib2 import Path
 from functools import cmp_to_key
+from socket import gaierror
 
 # Third party modules
 from fb_tools.argparse_actions import TimeoutOptionAction
@@ -58,10 +60,11 @@ from ..errors import DpxLdapExecError
 from ..errors import DpxLdapParseError
 from ..errors import DpxLdapSearchError
 from ..errors import DpxLdapSessionError
+from ..errors import DpxNoLdapServerAddressError
 from ..errors import DpxWriteLdapItemError
 from ..xlate import XLATOR, format_list
 
-__version__ = '1.3.8'
+__version__ = '1.4.0'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -83,6 +86,8 @@ class BaseLdapApplication(BaseDPXApplication):
     max_retries_on_conn_error = 100
     max_wait_on_conn_error = 600
 
+    default_address_family = socket.AF_INET
+
     # pattern_re_ldap_dn = (
     # '^([a-z][a-z0-9-]*)=(?![ #])(((?![\\="+,;<>]).)|(\\[ \\#="+,;<>])|(\\[a-f0-9][a-f0-9]))*'
     # '(,([a-z][a-z0-9-]*)=(?![ #])(((?![\\="+,;<>]).)|(\\[ \\#="+,;<>])|(\\[a-f0-9][a-f0-9]))*)*$'
@@ -784,6 +789,26 @@ class BaseLdapApplication(BaseDPXApplication):
             msg = _('Trying to get LDAP server object for {} ...').format(connect_info.url)
             LOG.debug(msg)
 
+        server_ip = None
+        if connect_info.ip:
+            server_ip = connect_info.ip
+            if self.verbose >= min_verb_level:
+                LOG.debug(_('Already resolved hostname {h!r} to {a!r}.').format(
+                    h=connect_info.host, a=str(server_ip)))
+        else:
+            LOG.debug(_('Resolving hostname {!r} to an IP address ...').format(connect_info.host))
+            try:
+                addresses = self.get_address(connect_info.host)
+            except gaierror as e:
+                msg = _('Could not resolve hostname {!r}:').format(connect_info.host)
+                msg += ' ' + str(e)
+                raise DpxNoLdapServerAddressError(msg)
+            if not addresses:
+                msg = _('Could not resolve hostname {!r}.').format(connect_info.host)
+                raise DpxNoLdapServerAddressError(msg)
+            server_ip = addresses[0]
+            connect_info.ip = server_ip
+
         server_opts = {}
         if connect_info.use_ldaps:
             server_opts['use_ssl'] = True
@@ -801,7 +826,7 @@ class BaseLdapApplication(BaseDPXApplication):
             msg += ' ' + pp(server_opts)
             LOG.debug(msg)
 
-        ldap_server = Server(connect_info.host, **server_opts)
+        ldap_server = Server(str(server_ip), **server_opts)
 
         if self.verbose > min_verb_level:
             LOG.debug(_('LDAP server {s}: {re}').format(s=ldap_server, re=repr(ldap_server)))
@@ -826,8 +851,8 @@ class BaseLdapApplication(BaseDPXApplication):
         cur_try = 0
 
         if self.verbose > min_verb_level:
-            msg = _('Connecting to LDAP server {url} as {dn!r} ...').format(
-                url=connect_info.url, dn=bind_dn)
+            msg = _('Connecting to LDAP server {url} ({addr}) as {dn!r} ...').format(
+                url=connect_info.url, addr=connect_info.ip, dn=bind_dn)
             LOG.debug(msg)
 
         while True:
@@ -835,7 +860,7 @@ class BaseLdapApplication(BaseDPXApplication):
             if self.verbose > 2:
                 LOG.debug(_(
                     'Try number {nr} for connecting to LDAP server instance {i!r} ...').format(
-                    cur_try))
+                    nr=cur_try, i=inst))
 
             try:
                 ldap_connection = Connection(
@@ -908,7 +933,7 @@ class BaseLdapApplication(BaseDPXApplication):
                 if self.verbose > 2:
                     LOG.debug(_(
                         'Try number {nr} for disconnecting from LDAP server '
-                        'instance {i!r} ...').format(cur_try))
+                        'instance {i!r} ...').format(nr=cur_try, i=inst))
                 try:
                     ldap_connection.unbind()
                     ldap_connection = None
index fe327c533b6b4b5780875a620573e8417b726af0..86cc4a3824046bc5375fcc2dc4ea37a8d405b8f6 100644 (file)
@@ -31,7 +31,7 @@ from .. import DEFAULT_CONFIG_DIR
 from .. import MAX_PORT_NUMBER
 from ..xlate import XLATOR
 
-__version__ = '0.8.0'
+__version__ = '0.8.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -82,6 +82,7 @@ class LdapConnectionInfo(FbBaseObject):
         self._tier = DEFAULT_TIER
         self._sync_source = None
         self._admin_dn = self.default_admin_dn
+        self.ip = None
 
         super(LdapConnectionInfo, self).__init__(
             appname=appname, verbose=verbose, version=version, base_dir=base_dir,
index 170c5c25f4829c05cee0d5104276896a509167f6..d345e2f68f1d02ce41612886c222b2aabf7a7f55 100644 (file)
@@ -10,7 +10,7 @@
 from fb_tools.errors import FbAppError, FbError
 
 
-__version__ = '0.8.1'
+__version__ = '0.8.2'
 
 # =============================================================================
 class DpxError(FbError):
@@ -95,6 +95,13 @@ class DpxLdapSessionError(DpxFatalLdapError):
     pass
 
 
+# =============================================================================
+class DpxNoLdapServerAddressError(DpxFatalLdapError):
+    """Error in case the LDAP server name could not resolved."""
+
+    pass
+
+
 # =============================================================================
 class DpxMailAppError(DpxAppError):
     """Base exception class for all exceptions in all mail sending application classes."""