From 66d7f22a359cfb74a0270005478ecd3edfb50277 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 27 Jun 2025 14:56:01 +0200 Subject: [PATCH] Optimized Attribut replace logic --- main.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index f52a6e4..e421e5d 100755 --- a/main.py +++ b/main.py @@ -19,6 +19,13 @@ import argparse import re import sys +def normalize(def_str: bytes) -> bytes: + """ + Collapse all whitespace (Spaces, Newlines, Tabs) to single spaces, + strip leading/trailing whitespace, for reliable byte-wise comparisons. + """ + return re.sub(rb'\s+', b' ', def_str.strip()) + def main(): parser = argparse.ArgumentParser( description='Create or update OpenLDAP schema entries under cn=config' @@ -144,28 +151,33 @@ def main(): prefix = f'{{{idx}}}' schema_dn = f"cn={prefix}{args.schema_name},{base_dn}" - # Add/update AttributeTypes # Add/update AttributeTypes for atdef in args.attribute_type: encoded = atdef.encode() + try: result = conn.search_s(schema_dn, ldap.SCOPE_BASE, attrlist=['olcAttributeTypes']) existing = result[0][1].get('olcAttributeTypes', []) - if encoded in existing: - print(f"ℹ️ AttributeType already exists, replacing: {atdef}") - # Replace the existing value + + norm_existing = [normalize(v) for v in existing] + norm_encoded = normalize(encoded) + + if norm_encoded in norm_existing: + print(f"ℹ️ AttributeType exists → REPLACE: {atdef}") conn.modify_s(schema_dn, [ (ldap.MOD_REPLACE, 'olcAttributeTypes', [encoded]) ]) print(f"🔄 Replaced AttributeType: {atdef}") else: + print(f"➕ AttributeType fehlt → ADD: {atdef}") conn.modify_s(schema_dn, [ (ldap.MOD_ADD, 'olcAttributeTypes', [encoded]) ]) print(f"➕ Added AttributeType: {atdef}") + except ldap.LDAPError as e: - print(f"❌ LDAP error for AttributeType '{atdef}': {e}", file=sys.stderr) + print(f"❌ LDAP error for AttributeType '{atdef}': {e}", file=sys.stderr) sys.exit(1) # Add/update ObjectClasses