Optimized Attribut replace logic
This commit is contained in:
22
main.py
22
main.py
@@ -19,6 +19,13 @@ import argparse
|
|||||||
import re
|
import re
|
||||||
import sys
|
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():
|
def main():
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description='Create or update OpenLDAP schema entries under cn=config'
|
description='Create or update OpenLDAP schema entries under cn=config'
|
||||||
@@ -144,28 +151,33 @@ def main():
|
|||||||
prefix = f'{{{idx}}}'
|
prefix = f'{{{idx}}}'
|
||||||
schema_dn = f"cn={prefix}{args.schema_name},{base_dn}"
|
schema_dn = f"cn={prefix}{args.schema_name},{base_dn}"
|
||||||
|
|
||||||
# Add/update AttributeTypes
|
|
||||||
# Add/update AttributeTypes
|
# Add/update AttributeTypes
|
||||||
for atdef in args.attribute_type:
|
for atdef in args.attribute_type:
|
||||||
encoded = atdef.encode()
|
encoded = atdef.encode()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = conn.search_s(schema_dn, ldap.SCOPE_BASE,
|
result = conn.search_s(schema_dn, ldap.SCOPE_BASE,
|
||||||
attrlist=['olcAttributeTypes'])
|
attrlist=['olcAttributeTypes'])
|
||||||
existing = result[0][1].get('olcAttributeTypes', [])
|
existing = result[0][1].get('olcAttributeTypes', [])
|
||||||
if encoded in existing:
|
|
||||||
print(f"ℹ️ AttributeType already exists, replacing: {atdef}")
|
norm_existing = [normalize(v) for v in existing]
|
||||||
# Replace the existing value
|
norm_encoded = normalize(encoded)
|
||||||
|
|
||||||
|
if norm_encoded in norm_existing:
|
||||||
|
print(f"ℹ️ AttributeType exists → REPLACE: {atdef}")
|
||||||
conn.modify_s(schema_dn, [
|
conn.modify_s(schema_dn, [
|
||||||
(ldap.MOD_REPLACE, 'olcAttributeTypes', [encoded])
|
(ldap.MOD_REPLACE, 'olcAttributeTypes', [encoded])
|
||||||
])
|
])
|
||||||
print(f"🔄 Replaced AttributeType: {atdef}")
|
print(f"🔄 Replaced AttributeType: {atdef}")
|
||||||
else:
|
else:
|
||||||
|
print(f"➕ AttributeType fehlt → ADD: {atdef}")
|
||||||
conn.modify_s(schema_dn, [
|
conn.modify_s(schema_dn, [
|
||||||
(ldap.MOD_ADD, 'olcAttributeTypes', [encoded])
|
(ldap.MOD_ADD, 'olcAttributeTypes', [encoded])
|
||||||
])
|
])
|
||||||
print(f"➕ Added AttributeType: {atdef}")
|
print(f"➕ Added AttributeType: {atdef}")
|
||||||
|
|
||||||
except ldap.LDAPError as e:
|
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)
|
sys.exit(1)
|
||||||
|
|
||||||
# Add/update ObjectClasses
|
# Add/update ObjectClasses
|
||||||
|
|||||||
Reference in New Issue
Block a user