Commit 2d9db56f authored by Daniel Klaffenbach's avatar Daniel Klaffenbach 🐍

Handle LDAP errors

parent 3eeeebf3
Pipeline #31469 passed with stage
in 53 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import ssl
import ldap3
from ldap3 import Server, Connection, SYNC, OFFLINE_SLAPD_2_4, MOCK_SYNC
......@@ -11,7 +12,7 @@ from django.conf import settings
from django.contrib.auth import get_user_model
from django.utils.functional import cached_property
DEFAULT_LDAP_TIMEOUT = 3
DEFAULT_LDAP_TIMEOUT = 3
DEFAULT_LDAP_SYNC_URI = 'ldaps://ldap.tu-chemnitz.de/ou=Users,dc=tu-chemnitz,dc=de'
DEFAULT_LDAP_SYNC_USER_ATTRIBUTES = {
......@@ -20,6 +21,8 @@ DEFAULT_LDAP_SYNC_USER_ATTRIBUTES = {
'mail': 'email',
}
logger = logging.getLogger(__name__)
class Ldap(object):
def __init__(self):
......@@ -64,7 +67,7 @@ class Ldap(object):
server_kwargs['use_ssl'] = True
server_kwargs['tls'] = Tls(ca_certs_file=self.LDAP_CA_CERT, validate=ssl.CERT_REQUIRED)
s=Server(**server_kwargs)
s = Server(**server_kwargs)
connection_kwargs = {
'server': s,
'auto_bind': True,
......@@ -72,8 +75,11 @@ class Ldap(object):
'password': self.LDAP_SYNC_BASE_PASS,
'client_strategy': SYNC,
}
return Connection(**connection_kwargs)
try:
return Connection(**connection_kwargs)
except LDAPException:
logger.warning("LDAP connection failed, LDAP updates will not be available.")
return None
def get_attributes(self, username):
"""
......@@ -105,6 +111,8 @@ class Ldap(object):
}
conn = self.connection
if not conn:
return {}
try:
result = conn.search(**search_kwargs)
except LDAPException:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment