Skip to content

Commit a6e9138

Browse files
committed
Replace ldap lib with ldap3
Replace ldap with ldap3 because ldap lib is not py3 compatible. Functionality is the same.
1 parent 2d9acbe commit a6e9138

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ install:
4040
- wget -O - https://spritelink.github.io/NIPAP/nipap.gpg.key | sudo apt-key add -
4141
- sudo apt-get update -qq
4242
# install dependencies for installing & running nipap
43-
- sudo apt-get install -qq -y --force-yes python-pysqlite2 python-psycopg2 python-ipy python-ldap python-docutils postgresql postgresql-9.1-ip4r python-tornado python-flask python-flask-xml-rpc python-flask-compress
43+
- sudo apt-get install -qq -y --force-yes python-pysqlite2 python-psycopg2 python-ipy python-docutils postgresql postgresql-9.1-ip4r python-tornado python-flask python-flask-xml-rpc python-flask-compress
44+
- sudo pip install python3-ldap
4445
# install dependencies for building packages and build NIPAP debian packages
4546
- sudo apt-get install -qq -y --force-yes devscripts python-docutils
4647
# if we are testing the upgrade, first install NIPAP packages from official repo

docs/sphinx/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def __getattr__(cls, name):
217217
else:
218218
return Mock()
219219

220-
MOCK_MODULES = ['ldap', 'IPy', 'psycopg2.extras', 'psycopg2']
220+
MOCK_MODULES = ['ldap3', 'IPy', 'psycopg2.extras', 'psycopg2']
221221

222222
for mod_name in MOCK_MODULES:
223223
sys.modules[mod_name] = Mock()

nipap/debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Standards-Version: 3.9.1
88

99
Package: nipap-common
1010
Architecture: all
11-
Depends: python (>= 2.7), ${misc:Depends}, python-pysqlite2, python-ldap, python-ipy
11+
Depends: python (>= 2.7), ${misc:Depends}, python-pysqlite2, python-ipy
1212
Description: Neat IP Address Planner
1313
The Neat IP Address Planner, NIPAP, is a system built for efficiently managing
1414
large amounts of IP addresses. This is the common libraries.

nipap/nipap/authlib.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272

7373
# Used by auth modules
7474
import sqlite3
75-
import ldap
75+
import ldap3
7676
import string
7777
import random
7878

@@ -298,7 +298,7 @@ def __init__(self, name, username, password, authoritative_source, auth_options=
298298
self._logger.debug('Creating LdapAuth instance')
299299

300300
self._logger.debug('LDAP URI: ' + self._ldap_uri)
301-
self._ldap_conn = ldap.initialize(self._ldap_uri)
301+
self._ldap_conn = ldap3.Server(self._ldap_uri)
302302

303303

304304

@@ -314,29 +314,27 @@ def authenticate(self):
314314
return self._authenticated
315315

316316
try:
317-
self._ldap_conn.simple_bind_s('uid=' + self.username + ',' + self._ldap_basedn, self.password)
318-
except ldap.SERVER_DOWN as exc:
317+
with ldap3.Connection(self._ldap_conn, 'uid=' + self.username + ',' + self._ldap_basedn, self.password, raise_exceptions = True) as con:
318+
res = con.search(self._ldap_basedn, '(uid=' + self.username + ')', ldap3.SEARCH_SCOPE_WHOLE_SUBTREE, attributes = ['cn'], size_limit = 1)
319+
if (not res) or (not con.response):
320+
self.full_name = ''
321+
else:
322+
self.full_name = con.response[0]['attributes']['cn'][0]
323+
except ldap3.LDAPSocketOpenError as exc:
319324
raise AuthError('Could not connect to LDAP server')
320-
except (ldap.INVALID_CREDENTIALS, ldap.INVALID_DN_SYNTAX,
321-
ldap.UNWILLING_TO_PERFORM) as exc:
325+
except (ldap3.LDAPInvalidCredentialsResult, ldap3.LDAPInvalidDNSyntaxResult,
326+
ldap3.LDAPUnwillingToPerformResult) as exc:
322327
# Auth failed
323328
self._logger.debug('erroneous password for user %s' % self.username)
324329
self._authenticated = False
325330
return self._authenticated
326331

327-
328332
# auth succeeded
329333
self.authenticated_as = self.username
330334
self._authenticated = True
331335
self.trusted = False
332336
self.readonly = False
333337

334-
try:
335-
res = self._ldap_conn.search_s(self._ldap_basedn, ldap.SCOPE_SUBTREE, 'uid=' + self.username, ['cn'])
336-
self.full_name = res[0][1]['cn'][0]
337-
except:
338-
self.full_name = ''
339-
340338
self._logger.debug('successfully authenticated as %s, username %s' % (self.authenticated_as, self.username))
341339
return self._authenticated
342340

nipap/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_data_files():
4949
url = nipap.__url__,
5050
packages = ['nipap'],
5151
keywords = ['nipap'],
52-
requires = ['ldap', 'sqlite3', 'IPy', 'psycopg2'],
52+
requires = ['ldap3', 'sqlite3', 'IPy', 'psycopg2'],
5353
data_files = get_data_files(),
5454
classifiers = [
5555
'Development Status :: 4 - Beta',

0 commit comments

Comments
 (0)