scripts/print_ldap_entries.py

41 lines
No EOL
1.3 KiB
Python

"""Print the basic ldap entries in a copy-pastable format
Scan the install_utils/db.ldiff file in order to output a nicely
formated representation of the LDAP base config that can be feed
to volatildap by a simple copy-paste into the test.py file
"""
from volatildap.server import ldif_to_dict
# Read the lines in the file
with open('install_utils/db.ldiff') as f:
lines = f.read()
# Separate in entries defined by 2 consecutives line breaks
entries = lines.split('\n\n')
# Transform those entries in dict
# Skip the last trailing empty entry
ldifs = [ldif_to_dict(''.join(entry).encode('ascii')) for entry in entries if len(entry) != 0]
# Print nicely the output
template = (
"ldapentry_{name} = ('{dn}', {{\n"
"{data}\n"
"}})"
)
for ldif in ldifs:
# Delete all modifying values (non-permitted by ldapadd)
del ldif['modifyTimestamp']
del ldif['structuralObjectClass']
del ldif['entryCSN']
del ldif['modifiersName']
del ldif['creatorsName']
del ldif['createTimestamp']
del ldif['entryUUID']
# Cast all values to str instead of bytes
dn = ldif.pop('dn', [b'CHANGE_ME'])[0].decode()
data = '\n'.join("'{}': {},".format(k,[vv.decode() for vv in v]) for k, v in ldif.items())
name = ldif.get('cn', ldif.get('ou', [b'CHANGE_ME']))[0].decode()
print(template.format(name=name, dn=dn, data=data))
print()