python-ldap: How to fix b'ldapadd: invalid format (line 1) entry: ""\n'

374 views Asked by At

I want to create an ldap test server. So I dumped the ldap data with ldapsearch and created a .ldif file Now I want to create a test ldap server with this data. I want to use the slapdtest-module from python-ldap.

import slapdtest

with slapdtest.SlapdObject() as server:

    server.ldapadd("ldap_dump.ldif")
    server.start()

But I get the error message:

RuntimeError: ['/usr/bin/ldapadd', '-H', 'ldapi://../python-ldap-test-53974/ldapi', '-Y', 'EXTERNAL', '-Q', '-n'] process failed:
b''
b'ldapadd: invalid format (line 1) entry: ""\n'

$file ldap_dump.ldif returns ldap_dump.ldif: ASCII text. So the line endings shouldn't be a problem actually.

This is my reduced ldap_dump file:

dn: cn=User,dc=institute.edu
sn: User
objectClass: top
objectClass: user
cn: User
1

There are 1 answers

0
larsks On BEST ANSWER

The immediate cause of your error is that the ldapadd method expects to receive LDIF-format content, but you are passing it a filename. So you want something more like:

with slapdtest.SlapdObject() as server:
    with open('ldap_dump.ldif') as fd:
      server.ldapadd(fd.read())

You seem to be calling server.start() after ldapadd, and I think you need those lines in the reverse order.

Lastly, your sample file is probably invalid because it doesn't define the higher-level containers (e.g., dc=institute.edu) to contain the objects described in your example.