]> Frank Brehm's Git Trees - pixelpark/ldap-migration.git/commitdiff
Fixing some bugs and edge cases
authorFrank Brehm <frank.brehm@pixelpark.com>
Fri, 11 Dec 2020 17:09:40 +0000 (18:09 +0100)
committerFrank Brehm <frank.brehm@pixelpark.com>
Fri, 11 Dec 2020 17:09:40 +0000 (18:09 +0100)
lib/ldap_migration/__init__.py

index 8d6c5837d6a8ceb6e41635168e97b3b47d598a23..eb4f6c660daddb079bd00f0c9bbfbbc84c30bce6 100644 (file)
@@ -30,6 +30,7 @@ from ldap3 import Server, Connection, ALL, DSA, IP_V4_PREFERRED, SAFE_SYNC
 from ldap3 import BASE, LEVEL, SUBTREE, DEREF_NEVER, DEREF_SEARCH, DEREF_BASE, DEREF_ALWAYS
 from ldap3 import ALL_ATTRIBUTES, ALL_OPERATIONAL_ATTRIBUTES
 from ldap3 import MODIFY_ADD, MODIFY_DELETE, MODIFY_REPLACE
+from ldap3.core.exceptions import LDAPInvalidDnError
 
 from ldap3.core.exceptions import LDAPException
 
@@ -45,7 +46,7 @@ from .config import LDAPMigrationConfiguration
 from .idict import CaseInsensitiveDict
 from .istringset import CaseInsensitiveStringSet
 
-__version__ = '0.8.10'
+__version__ = '0.8.11'
 
 LOG = logging.getLogger(__name__)
 CFG_BASENAME = 'ldap-migration.ini'
@@ -171,6 +172,7 @@ class LDAPMigrationApplication(BaseApplication):
 
     re_dn_split = re.compile(r'\s*,\s*')
     re_token_split = re.compile(r'^\s*([a-z0-9]+)\s*=\s*(\S(?:.*\S)?)\s*$', re.IGNORECASE)
+    re_plus_in_cn = re.compile(r'(?P<before>\S)(?:\s+\+\s+|\s+\+|\+\s+)\s*(?P<after>\S)')
 
     tz = get_localzone()
 
@@ -598,9 +600,13 @@ class LDAPMigrationApplication(BaseApplication):
             msg = "Trying to get source LDAP item {!r} ...".format(tgt_dn)
             LOG.debug(msg)
 
-        src_status, src_result, src_response, _ = self.source.search(
-            search_base=tgt_dn, search_scope=BASE, search_filter=sfilter,
-            attributes=src_attrs, time_limit=self.config.timeout)
+        try:
+            src_status, src_result, src_response, _ = self.source.search(
+                search_base=tgt_dn, search_scope=BASE, search_filter=sfilter,
+                attributes=src_attrs, time_limit=self.config.timeout)
+        except LDAPInvalidDnError as e:
+            msg = "Could not retrieve entry with DN {dn!r}: {e}".format(dn=tgt_dn, e=e)
+            raise ReadLDAPItemError(msg)
 
         if not src_status:
             msg = "Error retrieving source LDAP item {dn!r}: {res}".format(
@@ -952,6 +958,13 @@ class LDAPMigrationApplication(BaseApplication):
             msg = "F***, Whats that?"
             raise CommonLDAPMigrationError(msg)
         value = match.group(2)
+        if key.lower() in ('cn', 'commonname' ):
+            if self.re_plus_in_cn.search(value):
+                newval = self.re_plus_in_cn.sub(r'\g<before>+\g<after>', value)
+                msg = "Mangling commonName {old!r} => {new!r}.".format(
+                        old=value, new=newval)
+                LOG.debug(msg)
+                return "{key}={val}".format(key=new_key, val=newval)
 
         return "{key}={val}".format(key=new_key, val=value)
 
@@ -964,7 +977,7 @@ class LDAPMigrationApplication(BaseApplication):
             new_token = self.mangle_dn_token(old_token)
             new_parts.append(new_token)
 
-        return ','.join(parts)
+        return ','.join(new_parts)
 
     # -------------------------------------------------------------------------
     def get_reverse_dn(self, dn):