Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace ldap lib with ldap3 #611

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ install:
- wget -O - https://spritelink.github.io/NIPAP/nipap.gpg.key | sudo apt-key add -
- sudo apt-get update -qq
# install dependencies for installing & running nipap
- 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
- 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
- sudo pip install python3-ldap
# install dependencies for building packages and build NIPAP debian packages
- sudo apt-get install -qq -y --force-yes devscripts python-docutils
# if we are testing the upgrade, first install NIPAP packages from official repo
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def __getattr__(cls, name):
else:
return Mock()

MOCK_MODULES = ['ldap', 'IPy', 'psycopg2.extras', 'psycopg2']
MOCK_MODULES = ['ldap3', 'IPy', 'psycopg2.extras', 'psycopg2']

for mod_name in MOCK_MODULES:
sys.modules[mod_name] = Mock()
2 changes: 1 addition & 1 deletion nipap/debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Standards-Version: 3.9.1

Package: nipap-common
Architecture: all
Depends: python (>= 2.7), ${misc:Depends}, python-pysqlite2, python-ldap, python-ipy
Depends: python (>= 2.7), ${misc:Depends}, python-pysqlite2, python-ipy
Description: Neat IP Address Planner
The Neat IP Address Planner, NIPAP, is a system built for efficiently managing
large amounts of IP addresses. This is the common libraries.
Expand Down
24 changes: 11 additions & 13 deletions nipap/nipap/authlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

# Used by auth modules
import sqlite3
import ldap
import ldap3
import string
import random

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

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



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

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


# auth succeeded
self.authenticated_as = self.username
self._authenticated = True
self.trusted = False
self.readonly = False

try:
res = self._ldap_conn.search_s(self._ldap_basedn, ldap.SCOPE_SUBTREE, 'uid=' + self.username, ['cn'])
self.full_name = res[0][1]['cn'][0]
except:
self.full_name = ''

self._logger.debug('successfully authenticated as %s, username %s' % (self.authenticated_as, self.username))
return self._authenticated

Expand Down
2 changes: 1 addition & 1 deletion nipap/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def get_data_files():
url = nipap.__url__,
packages = ['nipap'],
keywords = ['nipap'],
requires = ['ldap', 'sqlite3', 'IPy', 'psycopg2'],
requires = ['ldap3', 'sqlite3', 'IPy', 'psycopg2'],
data_files = get_data_files(),
classifiers = [
'Development Status :: 4 - Beta',
Expand Down