Skip to content

Commit

Permalink
Expose ldap pool active parameter to limit retries
Browse files Browse the repository at this point in the history
Currently the pool is initiated so that servers are retried
indefinetely. This makes it hard to define concrete hard timeout for
login operation.

This change exposes ldap3's ServerPool active parameter as setting. That
way it provides means to resolve situations as with etianen#264.

https://ldap3.readthedocs.io/en/latest/server.html#server-pool
  • Loading branch information
borislaviv committed Jun 4, 2024
1 parent a21ca70 commit b9b40bb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Available settings
LDAP_AUTH_CONNECT_TIMEOUT = None
LDAP_AUTH_RECEIVE_TIMEOUT = None
# Set connection pool `active` parameter on the underlying `ldap3` library.
LDAP_AUTH_POOL_ACTIVE = True
Microsoft Active Directory support
----------------------------------
Expand Down
5 changes: 5 additions & 0 deletions django_python3_ldap/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,10 @@ def __init__(self, settings):
default=None
)

LDAP_AUTH_POOL_ACTIVE = LazySetting(
name="LDAP_AUTH_POOL_ACTIVE",
default=True
)


settings = LazySettings(settings)
6 changes: 5 additions & 1 deletion django_python3_ldap/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ def connection(**kwargs):
password = kwargs.pop("password")
username = format_username(kwargs)
# Build server pool
server_pool = ldap3.ServerPool(None, ldap3.RANDOM, active=True, exhaust=5)
server_pool = ldap3.ServerPool(
None, ldap3.RANDOM,
active=settings.LDAP_AUTH_POOL_ACTIVE,
exhaust=5
)
auth_url = settings.LDAP_AUTH_URL
if not isinstance(auth_url, list):
auth_url = [auth_url]
Expand Down
12 changes: 12 additions & 0 deletions django_python3_ldap/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def testAuthenticateWithFailedRebind(self):
)
self.assertIs(user, None)

def testAuthenticateWithLimitedRetries(self):
# simulate offline server
with self.settings(
LDAP_AUTH_URL=["ldap://example.com:389"],
LDAP_AUTH_POOL_ACTIVE=1,
):
user = authenticate(
username=settings.LDAP_AUTH_TEST_USER_USERNAME,
password=settings.LDAP_AUTH_TEST_USER_PASSWORD,
)
self.assertEqual(user, None)

# User synchronisation.

def testSyncUsersCreatesUsers(self):
Expand Down

0 comments on commit b9b40bb

Please sign in to comment.