From: Frank Brehm Date: Fri, 11 Dec 2020 17:09:40 +0000 (+0100) Subject: Fixing some bugs and edge cases X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=935c2c05dcb0b785d684a5b56eee87e61477ca1a;p=pixelpark%2Fldap-migration.git Fixing some bugs and edge cases --- diff --git a/lib/ldap_migration/__init__.py b/lib/ldap_migration/__init__.py index 8d6c583..eb4f6c6 100644 --- a/lib/ldap_migration/__init__.py +++ b/lib/ldap_migration/__init__.py @@ -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\S)(?:\s+\+\s+|\s+\+|\+\s+)\s*(?P\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+\g', 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):