Package: volatildap
volatildapprovides simple helpers for testing code against a LDAP database.Its main features include:Simple configuration:Don’t provide anything; the LDAP server will start with sane defaultsBuilt-in cleanup:As soon as the test ends / the test process exits, the server is instantly removedCross-distribution setup:Automatically discover system paths for OpenLDAP binaries, schemas, etc.UsageimportvolatildapclassMyTests(unittest.TestCase):@classmethoddefsetUpClass(cls):super(MyTests,cls).setUpClass()cls._slapd=volatildap.LdapServer(suffix='dc=example,dc=org')defsetUp(self):# Will start the server, or reset/restart it if already started from a previous test.self._slapd.start()deftest_something(self):conn=ldap.connection(self._slapd.uri)# Do some testsdeftest_with_data(self):# Load some dataself._slapd.add({'ou=people':{'cn':[b'Users']}})# Run the testsThevolatildap.LdapServerprovides a few useful methods:start()Start or restart the server.
This will:Clear all data, if anyStart the server if it’s not yet runningPopulate the initial datastop()Stop the server.This will clean up all data and kill the proces.wait()Wait until the server is asked to stop.Mostly useful when controlling the server in another manner, or to use the volatildap
server as a development instance.add(data)Add some data, see theinitial_datastructure below.get(dn)Retrieve an object by its distinguished name;Returns a dictionary mapping an attribute to the list of its values, as bytes.RaisesKeyErrorif the distinguished name is unknown to the underlying database.add_ldif(contents)Add lines from a LDIF file - contents should be bytes.get_ldif(dn)Return an entry as a list of lines for a LDIF filereset()Restore the server to its pristine, initial state.
This includes loading the inital_data.It also exposes the following attributes:uriThe URI to use to contect the server (e.gldap://localhost:10389/)rootdnThe distinguishedName of the admin accountrootpwThe password of the admin accountsuffixThe suffix used by the LDAP serverportThe TCP port the LDAP server is listening onhostThe hostname the LDAP server is listening ontls_configA named tuple, containing the TLS attributes.
The only guaranteed attribute istls_config.root, which contains the PEM-formatted
server certificate.ConfigurationThevolatildap.LdapServerclass accepts a few parameters:suffixThe suffix to use for the LDAP treeDefault:dc=example,dc=orgrootdnThe administrator account for the LDAP serverDefault:cn=testadmin,dc=example,dc=orgrootpwThe administrator password.Default:A random value, available throughLdapServer.rootpwschemasList of schemas to load; can be either a simple name (e.gcosine.schema; looked up in openldap installation); or a path to a custom one.Default:['core.schema']initial_dataDict mapping a distinguished name to a dict of attribute/values:slapd(initial_data={'ou=people':{'objectClass':['organizationalUnit'],'cn':['People'],},})Note:When adding data, the suffix can be omitted on objects DNs.Default:{}skip_missing_schemasWhen loading schemas, this flag instructsvolatildapto continue if some schemas
can’t be found.Default:FalseportThe port to use.Default:An available TCP port on the systemhostThe hostname or IP to listen on.Default:localhostslapd_debugThe debug level for slapd; seeslapd.confDefault:0max_server_startup_delayThe maximum delay allowed for server startup, in seconds.Default:30tls_configA set of TLS certificate files for configuring the server.
A valid set forlocalhostis provided asvolatildap.LOCALHOST_TLS_CONFIG, but users may also provide their own:tls_config=volatildap.TLSConfig(root=read(ca_path),chain=[read(intermediate_path),],certificate=read(certificate_path),key=read(key_path),)Command linevolatildap provides a command line entrypoint for simplicity:python-mvolatildap.cliIts usage follows:usage: cli.py [-h] [--port PORT] [--host HOST] [--suffix SUFFIX]
              [--rootdn ROOTDN] [--rootpw ROOTPW] [--debug DEBUG]
              [--control CONTROL] [--initial INITIAL]
              [--schemas [SCHEMAS [SCHEMAS ...]]] [--tls]

optional arguments:
  -h, --help            show this help message and exit
  --port PORT           Port to listen on; empty for a dynamic port
  --host HOST           Host to listen on; defaults to localhost
  --suffix SUFFIX       LDAP suffix
  --rootdn ROOTDN       Distinguished Name of LDAP admin user
  --rootpw ROOTPW       Password of LDAP admin user
  --debug DEBUG         slapd debug level
  --control CONTROL     Start the HTTP control server on this address
  --initial INITIAL     Load initial objects from the provided LDIF file
  --schemas [SCHEMAS [SCHEMAS ...]]
                        Schemas to load (multi-valued)
  --tls                 Enable TLS, using a built-in stackRemote controlOnce such a server has been started, if a control server has been provided
(for instance as--control:10380), it is possible to start a Python proxy to control it:def setUpClass(cls):
    super().setUpClass()
    cls._slapd = volatildap.ProxyServer('http://localhost:10380')All commands available on a normal instance will be available on the proxy:reset,start,stop,add,add_ldif,get,get_ldif.The readonly attributes are also available:uri,suffix,rootdn,rootpw,port,host,tls_config.When using TLS, the server’s root certificate authority can be accessed
throughproxy.tls_config.root.Per-distribution specificitiesUbuntuUnder Ubuntu, the default AppArmor policy does not allowslapd(the LDAP daemon) to read temporary folders.
Users should update the/etc/apparmor.d/usr.sbin.slapdfile and add/tmp/**rwk,there.koption is used to acquire lock on files.
Users must also add a line with the path to their home. Using the variable$HOMEwon’t work so you have to add the full path. Something like/path/to/my/home/** rw,.