make ldappool py3 compatible

* properly utf8 encode password
* switch to pyldap, the drop in replacement for python-ldap

Change-Id: Ic0fc838b8e015d90c946c3d161d096afe0bb7a21
Depends-On: I9cf13a5be63a0abc964c5a6e29c92d16365d89d8
Depends-On: I330b9b8fd3b53a588f0a1ae0c7de16cf03adb3cd
This commit is contained in:
Steve Martinelli 2016-05-12 12:16:34 -07:00 committed by Morgan Fainberg
parent cee42d218e
commit 5f6748212b
2 changed files with 28 additions and 6 deletions

View File

@ -35,6 +35,7 @@
# ***** END LICENSE BLOCK ***** # ***** END LICENSE BLOCK *****
""" LDAP Connection Pool. """ LDAP Connection Pool.
""" """
import codecs
from contextlib import contextmanager from contextlib import contextmanager
import logging import logging
from threading import RLock from threading import RLock
@ -42,9 +43,29 @@ import time
import ldap import ldap
from ldap.ldapobject import ReconnectLDAPObject from ldap.ldapobject import ReconnectLDAPObject
import six
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
_utf8_encoder = codecs.getencoder('utf-8')
def utf8_encode(value):
"""Encode a basestring to UTF-8.
If the string is unicode encode it to UTF-8, if the string is
str then assume it's already encoded. Otherwise raise a TypeError.
:param value: A basestring
:returns: UTF-8 encoded version of value
:raises TypeError: If value is not basestring
"""
if isinstance(value, six.text_type):
return _utf8_encoder(value)[0]
elif isinstance(value, six.binary_type):
return value
else:
raise TypeError("bytes or Unicode expected, got %s"
% type(value).__name__)
class MaxConnectionReachedError(Exception): class MaxConnectionReachedError(Exception):
@ -146,7 +167,7 @@ class ConnectionManager(object):
def _match(self, bind, passwd): def _match(self, bind, passwd):
if passwd is not None: if passwd is not None:
passwd = passwd.encode('utf8') passwd = utf8_encode(passwd)
with self._pool_lock: with self._pool_lock:
inactives = [] inactives = []
@ -214,7 +235,7 @@ class ConnectionManager(object):
tries = 0 tries = 0
connected = False connected = False
if passwd is not None: if passwd is not None:
passwd = passwd.encode('utf8') passwd = utf8_encode(passwd)
exc = None exc = None
conn = None conn = None
@ -226,7 +247,8 @@ class ConnectionManager(object):
conn.timeout = self.timeout conn.timeout = self.timeout
self._bind(conn, bind, passwd) self._bind(conn, bind, passwd)
connected = True connected = True
except ldap.LDAPError as exc: except ldap.LDAPError as error:
exc = error
time.sleep(self.retry_delay) time.sleep(self.retry_delay)
if tries < self.retry_max: if tries < self.retry_max:
log.info('Failure attempting to create and bind ' log.info('Failure attempting to create and bind '
@ -348,7 +370,7 @@ class ConnectionManager(object):
return return
if passwd is not None: if passwd is not None:
passwd = passwd.encode('utf8') passwd = utf8_encode(passwd)
with self._pool_lock: with self._pool_lock:
for conn in list(self._pool): for conn in list(self._pool):

View File

@ -1 +1 @@
python-ldap>=2.4:python_version=='2.7' # PSF pyldap>=2.4 # PSF