]> Frank Brehm's Git Trees - pixelpark/pp-admin-tools.git/commitdiff
Fixing resolving the LDAP servername and the help text of the --base-dn cmdline option
authorFrank Brehm <frank.brehm@pixelpark.com>
Wed, 13 Mar 2024 10:30:13 +0000 (11:30 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Wed, 13 Mar 2024 10:30:13 +0000 (11:30 +0100)
lib/pp_admintools/app/ldap.py

index 3e4471f55e6513f0c8a598809eb1f3f4033ac1df..874e52b13a46ba586d50d1f15cefb3a2094d5ea6 100644 (file)
@@ -64,7 +64,7 @@ from ..errors import DpxNoLdapServerAddressError
 from ..errors import DpxWriteLdapItemError
 from ..xlate import XLATOR, format_list
 
-__version__ = '1.4.0'
+__version__ = '1.4.1'
 LOG = logging.getLogger(__name__)
 
 _ = XLATOR.gettext
@@ -107,9 +107,16 @@ class BaseLdapApplication(BaseDPXApplication):
     pattern_dntoken = r'^([^=\s]+)\s*=\s*(\S.*)\S*$'
     re_dntoken = re.compile(pattern_dntoken)
 
+    pattern_ldap_url_service = r'^ldap[si]?://'
+    re_ldap_url_service = re.compile(pattern_ldap_url_service, re.IGNORECASE)
+
     person_object_classes = FrozenCIStringSet([
         'account', 'inetOrgPerson', 'inetUser', 'posixAccount', 'sambaSamAccount'])
 
+    helptext_ldap_base_dn = _(
+        'Override the configured base DN of the instance used as the '
+        'root for the LDAP searches.')
+
     # -------------------------------------------------------------------------
     @classmethod
     def compare_ldap_dns(cls, x, y):
@@ -445,9 +452,7 @@ class BaseLdapApplication(BaseDPXApplication):
 
                 ldap_group.add_argument(
                     '-b', '--base-dn', metavar='DN', dest='ldap_base_dn',
-                    help=_(
-                        'Override the configured base DN of the instance used as the '
-                        'root for the LDAP searches.')
+                    help=self.helptext_ldap_base_dn,
                 )
 
         ldap_group.add_argument(
@@ -796,15 +801,16 @@ class BaseLdapApplication(BaseDPXApplication):
                 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))
+            hostname = self.re_ldap_url_service.sub('', connect_info.host)
+            LOG.debug(_('Resolving hostname {!r} to an IP address ...').format(hostname))
             try:
-                addresses = self.get_address(connect_info.host)
+                addresses = self.get_address(hostname)
             except gaierror as e:
-                msg = _('Could not resolve hostname {!r}:').format(connect_info.host)
+                msg = _('Could not resolve hostname {!r}:').format(hostname)
                 msg += ' ' + str(e)
                 raise DpxNoLdapServerAddressError(msg)
             if not addresses:
-                msg = _('Could not resolve hostname {!r}.').format(connect_info.host)
+                msg = _('Could not resolve hostname {!r}.').format(hostname)
                 raise DpxNoLdapServerAddressError(msg)
             server_ip = addresses[0]
             connect_info.ip = server_ip