LDAP Handler

The clacks agent includes a LDAPHandler class and a couple of utilities make LDAP connections a little bit easier to use.


class clacks.agent.ldap_utils.LDAPHandler

The LDAPHandler provides a connection pool with automatically reconnecting LDAP connections and is accessible thru the clacks.agent.ldap_utils.LDAPHandler.get_instance() method.

Example:

>>> from clacks.agent.ldap_utils import LDAPHandler
>>> from ldap.filter import filter_format
>>> lh = LDAPHandler.get_instance()
>>> uuid = 'you-will-not-find-anything'
>>> with lh.get_handle() as con:
...     res = con.search_s(lh.get_base(),
...         ldap.SCOPE_SUBTREE,
...         filter_format("(&(objectClass=device)(uuid=%s))", uuid),
...         ['deviceStatus'])
...

This example uses the connection manager get_handle to retrieve and free a LDAP connection. Please note that you’ve to release a LDAP connection after you’ve used it.

The LDAPHandler creates connections based on what’s configured in the [ldap] section of the clacks configuration files. Here’s a list of valid keywords:

Key Description
url LDAP URL to connect to
bind_dn DN to connect with
bind_secret Password to connect with
pool_size Number of parallel connections in the pool
retry_max How often a connection should be tried after the service is considered dead
retry_delay Time delta on which to try a reconnection

Example:

[ldap]
url = ldap://ldap.example.net/dc=example,dc=net
bind_dn = cn=manager,dc=example,dc=net
bind_secret = secret
pool_size = 10

Warning

The LDAPHandler should not be used for ordinary object handling, because there’s an object abstraction layer which does related things automatically. See Object abstraction.

free_connection(conn)

Free an allocated pool connection to make it available for others.

Parameter Description
conn Allocated LDAP connection
get_base(utf_8=True)

Return the configured base DN.

Return: base DN

get_connection()

Get a new connection from the pool.

Return: LDAP connection

get_handle(*args, **kwds)

Context manager which is meant to be used with the with() statement. For an example see above.

Return: LDAP connection

static get_instance()

Singleton for LDAPHandler objects. Return the instance.

Return: LDAPHandler instance

clacks.agent.ldap_utils.map_ldap_value(value)

Method to map various data into LDAP compatible values. Maps bool values to TRUE/FALSE and unicode values to be ‘utf-8’ encoded.

Parameter Description
value data to be prepared for LDAP

Return: adapted dict

clacks.agent.ldap_utils.normalize_ldap(data)

Convert single values to lists.

Parameter Description
data input string or list

Return: adapted data

clacks.agent.ldap_utils.unicode2utf8(data)

Method to map unicode strings to utf-8.

Parameter Description
data string or list to convert

Return: adapted data