scripts/print_ldap_entries.py
2018-06-21 12:32:23 +02:00

32 lines
No EOL
1 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:
# Cast all values to str instead of bytes
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=ldif['dn'][0].decode(), data=data))
print()