commit 494b916b86648c557ed5bede6de22e3480edd4cf Author: moamoak Date: Thu Jun 21 12:32:23 2018 +0200 Add print_ldap_entries script diff --git a/print_ldap_entries.py b/print_ldap_entries.py new file mode 100644 index 0000000..1b28a81 --- /dev/null +++ b/print_ldap_entries.py @@ -0,0 +1,32 @@ +"""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() \ No newline at end of file