Package: bda.ldap
LDAP convenience libraryThis Package provides objects for LDAP communication.LDAP SessionYou can work with theLDAPSessionobject.>>> from bda.ldap import ONELEVEL
>>> from bda.ldap import LDAPSession
>>> from bda.ldap import LDAPProps

>>> props = LDAPProps('localhost',
...                   389,
...                   'cn=user,dc=example,dc=com',
...                   'secret'
...                   cache=True,
...                   timeout=12345)
>>> session = LDAPSession(props)
>>> res = session.search('(uid=*)', ONELEVEL)LDAP NodeYou can build and edit LDAP data trees with theLDAPNodewhich is based onzodict.Node.The root Node expects the base DN and the server properties to initialize.>>> from bda.ldap import LDAPNode
>>> root = LDAPNode('dc=my-domain,dc=com', props=props)
>>> root.keys()
['ou=customers']You can create and add new LDAP entries.>>> person = LDAPNode()
>>> person.attributes['objectClass'] = ['top', 'person']
>>> person.attributes['sn'] = 'Mustermann'
>>> person.attributes['cn'] = 'Max'
>>> person.attributes['description'] = 'Description'
>>> customers['cn=max'] = person
>>> customers.keys()
['cn=max']On__call__the modifications of the tree are written to the directory.>>> customers()Modification of entry attributes.>>> person.attributes['description'] = 'Another description'
>>> person()

>>> del person.attributes['description']
>>> person()Deleting of entries.>>> del customers['cn=max']
>>> customers()For more details see the corresponding source and test files.Character EncodingLDAP (v3 at least,RFC 2251) uses utf8 string encoding. Since 1.5.1,
LDAPSession and LDAPNode translate these to unicodes for you. Consider it a
bug, if you receive anything else than unicode from LDAPSession or LDAPNode.
Everything below that LDAPConnector and LDAPCommunicator give you the real ldap
experience. - Should we change that, too?Unicode strings you pass to nodes or sessions are automatically encoded to uft8
for LDAP. If you feed them normal strings they are decoded as utf8 and
reencoded as utf8 to make sure they are utf8 or compatible, e.g. ascii.If decoding as utf8 fails, the value is assumed to be in binary and left as a
string (see TODO).If you have an LDAP server that does not use utf8, monkey-patch
bda.ldap.strcodec.LDAP_CHARACTER_ENCODING.If you are confused by all that encoding/decoding: python knows in what
encoding it stores its unicodes, however, it cannot know for normal strs.
Therefore, you should only use unicodes. In order to get a unicode for a str, a
string is decoded according to a given encoding schema (eg utf8). And encoding
a unicode produces a str in a specific encoding (eg utf8).Caching Supportbda.ldapcaches LDAP searches using the lightweightbda.cache. You need
to provide a utility in you application in order to make caching work. If you
dont,bda.ldapfalls back to use the NullCache, which does not cache
anything.To provide an cache based onMemcachedinstall the memcached server,
configure and start it. I suppose its started on localhost port 11211 (which is
a common default). Then you need to provide a utility acting as a factory.>>> from bda.ldap.cache import MemcachedProviderFactory
>>> providerfactory = MemcachedProviderFactory()
>>> from zope.component import provideUtility
>>> provideUtility(providerfactory)In the case you have more than one memcached server running or hav it running on
another maschine, you need to initialize the factory different:>>> providerfactory = MemcachedProviderFactory(servers=[10.0.0.10:22122,
...                                                     10.0.0.11:22322])
>>> provideUtility(providerfactory)Dependenciespython-ldapzodictbda.cacheNotes on python-ldapAlthough python-ldap is available via pypi, we excluded it frominstall_requiresdue to different compile issues on different platforms.So you have to make sure thatpyhon-ldapis available on your system in
any way.TODOTLS/SSL Support. in LDAPConnector
could be useful: python-ldap’s class SmartLDAPObject(ReconnectLDAPObject) -
Mainly the __init__() method does some smarter things like negotiating the
LDAP protocol version and calling LDAPObject.start_tls_s().Improve retry logic in LDAPSession
could be useful, python-ldap’s class ReconnectLDAPObject(SimpleLDAPObject) -
In case of server failure (ldap.SERVER_DOWN) the implementations of all
synchronous operation methods (search_s() etc.) are doing an automatic
reconnect and rebind and will retry the very same operation.Extend LDAPSession object to handle Fallback server(s)Encoding/Decoding the data sent to ldap changed the order of dict entries,
probably due to dict implementation. Investigate effects of that. I had the
impression so far that ldap (at least openldap) preserves the order if you
give it an ldif file. Iff, then python-ldap should use odicts not dicts.check/implement silent sort on only the keys LDAPNode.sortonkeys()binary attributes: 1. introduce Binary:node['cn=foo'].attributes['image']= Binary(stream), 2. parse ldap schema to identify binary attributesChanges1.5.2assume strings that fail to decode to be binary and leave them as-is
(chaoflow, 2010-07-19)session.search, default filter'(objectClass=*)'and scopeBASE, i.e.
just calling search returns the basedn entry. Further it is possible to call
session.search(scope=ONELEVEL) to get all entries one level below the basedn.
(chaoflow, 2010-07-19)1.5.1character encoding: LDAPSession and LDAPNode only return unicode and
enforces utf8 or compatible encoding on all strings they receive,
seeCharacter Encoding.
(chaoflow, 2010-07-17)introduced strcodec module for unicode->str->unicode translation
(chaoflow, 2010-07-17)add LDAPNode.get to use LDAPNode.__getitem__ instead of odict’s
(chaoflow, 2010-07-16)more tests, explode_dn for dn handling (with spaces and escaped commas)
(chaoflow, 2010-07-16)ignore results with dn=None. ActiveDirectory produces them
(chaoflow, 2010-07-15)default filter for session.search, if you pass ‘’, u’’ or None as filter
(chaoflow, 2010-07-15)tests for attrlist and attrsonly
(chaoflow, 2010-07-15)adopt for latest zodict.
(rnix, 2010-07-15)added support for sort to node. Note: This wakes up all children of Node.
(jensens, 2010-04-16)added support for “items() to Node”.
(jensens, 2010-04-16)BBB compatibility for zope2.9
(rnix, jensens, 2010-02-17)If a Node was added and no child added __iter__ failed. Fixed now.
(jensens, 2010-01-19)If a Node was added we cant load its attributes. Takes this into account now.
(jensens, 2010-01-17)1.5.0MadeMemcachedProviderFactoryconfigureable. Defaults behave like in prior
versions. New: We can passserver=keyword argument to the
constructor expecting a list of servers, each in the formserver:port.
(jensens, 2009-12-30)Dont provide any cache provider factory by default. Added anullCacheProviderFactorywhich  provides a non-caching behaviour. Use this
as fallback if no utility was registered.
(jensens, 2009-12-30)Add read propertyldap_sessiontoLDAPNode. This way its clean to take
the session ofLDAPNodein an application i.e. for searching. Be careful
while using the session directly to manipulate the LDAP; responsibility to
invalidate theLDAPNodeinstances is on the application developer.
(jensens, 2009-12-30)1.4.0AddLDAPPropsobject. Its points toLDAPServerPropertiesclass. The
latter one will be renamed toLDAPPropsin version 1.5. Too long class
name. (rnix, 2009-12-23)AddLDAPSession.unbindfunction. (rnix, 2009-12-23)Add some tests forLDAPSession. (rnix, 2009-12-23)Remove deprecatedcachekwarg fromLDAPSession.__init__.. Cache
timeout and flag if cache is enabled is done due toLDAPServerProperties.
(rnix, 2009-12-23)Deprecate Signature ofLDAPConnector.__init__. (rnix, 2009-12-23)DeprecateLDAPConnector.setProtocol,LDAPCommunicator.setBaseDN,LDAPCommunicator.getBaseDN,LDAPSession.setBaseDN. (rnix, 2009-12-23)Refactor the wholeLDAPNodeto usezodict.LifecycleNode. Clean up of
theLDAPNodecode. (jensens, rnix, 2009-12-22)improved stop mechanism of ldap server in tests (jensens, 2009-12-16)remove deprecation warning: usehashlibfor md5 and fallback tomd5with python2.4. (jensens, 2009-12-16)1.3.2handle timeout of cache, workaround atm (rnix, 2009-09-02)1.3.1addcacheproperty toLDAPProperties. (rnix, 2009-05-08)modify session to fit this new cache property. (rnix, 2009-05-07)addqueryNodefunction. (rnix, 2009-05-07)addgetfunction to node, this failed due LDAP read logic.
(rnix, 2009-05-07)1.3supportattrlistandattrsonlyfor search functions.
(rnix, 2009-04-16)add LDAPEntry object. (rnix, 2009-04-16)add search base to cache key. (rnix, 2009-04-16)1.2.3bugfix inLDAPSession. Passforce_reloadto relevant execution
function. (rnix, 2009-02-11)1.2.2add buildout for standalone testing. (rnix, jensens - 2009-02-11)add tests. (rnix, jensens - 2009-02-11)provide relevant objects via package__init__.
(rnix, jensens - 2009-02-11)1.2.1provide samesearch()signature inLDAPSessionas
inLDAPCommunicator. (rnix - 2009-02-10)log only on debug. (rnix - 2009-02-10)<= 1.2make it work.
(all contributors)CopyrightCopyright (c) 2006-2009: BlueDynamics Alliance, AustriaCreditsRobert Niederreiter <rnix@squarewave.at>Jens Klein <jens@bluedynamics.com>Georg Bernhard <g.bernhard@akbild.ac.at>Florian Friesdorf <flo@chaoflow.net>Johannes Raggam <johannes@bluedynamics.com>