Connection to LDAP server fails through TLS connection
I am using Python 2.7 ldap module, and have tried connecting to an LDAP server with TLS enabled, but so far I have only run into many issues. When trying to debug the issue I get very little information back. Here is a simple script that I am testing with below
import ldap
LDAP_SERVER = 'ldap://ldap.somedomain.com:389'
LDAP_BASE = 'ou=users,dc=ldap,dc=test,dc=com'
try:
conn = ldap.initialize(LDAP_SERVER, bytes_mode=False)
conn.set_option(ldap.OPT_REFERRALS, 0)
conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3)
conn.set_option(ldap.OPT_X_TLS_CACERTFILE, "/path/to/cacert.pem")
conn.set_option(ldap.OPT_X_TLS,ldap.OPT_X_TLS_DEMAND)
conn.set_option(ldap.OPT_X_TLS_DEMAND, True)
conn.set_option(ldap.OPT_DEBUG_LEVEL, 255)
conn.start_tls_s()
except ldap.LDAPError, e:
print e
raise
print 'done'
When testing the script, an Exception is raised when the conn.start_tls_s
line is executed. here is the error that is returned:
ldap.CONNECT_ERROR: {'info': u'(unknown error code)', 'errno': 2, 'desc': u'Connect error'}
Stack Trace:
File "/home/eric/Desktop/test_ldap.py", line 14, in <module>
conn.start_tls_s()
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 864, in start_tls_s
return self._ldap_call(self._l.start_tls_s)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 329, in _ldap_call
reraise(exc_type, exc_value, exc_traceback)
File "/usr/local/lib/python2.7/dist-packages/ldap/ldapobject.py", line 313, in _ldap_call
result = func(*args,**kwargs)
Note - Port 389 on my LDAP server is secure and accepts TLS connections, which is why the LDAP_SERVER
is not set to ldaps://ldap.somedomain.com:636
in the example above.
I am also using an Ubuntu 14.04 virtual machine. Any ideas, and tips are greatly appreciated.