Add print_ldap_entries script
This commit is contained in:
commit
494b916b86
1 changed files with 32 additions and 0 deletions
32
print_ldap_entries.py
Normal file
32
print_ldap_entries.py
Normal file
|
@ -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()
|
Loading…
Add table
Add a link
Reference in a new issue