Skip to content

Commit 65a4619

Browse files
Merge pull request #395 from shaardie/ldap_pool_lifetime
Add option pool_lifetime option to ldap
2 parents 2644c73 + 97cbdf8 commit 65a4619

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

example/plugins/microservices/ldap_attribute_store.yaml.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ config:
2727
# pool_keepalive: seconds to wait between calls to server to keep the
2828
# connection alive; default: 10
2929
pool_keepalive: 10
30+
# pool_lifetime: number of seconds before recreating a new connection
31+
# in a pooled connection strategy.
32+
pool_lifetime: None
3033

3134
# Attributes to return from LDAP query.
3235
query_return_attributes:

src/satosa/micro_services/ldap_attribute_store.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class LdapAttributeStore(ResponseMicroService):
6161
"client_strategy": "REUSABLE",
6262
"pool_size": 10,
6363
"pool_keepalive": 10,
64+
"pool_lifetime": None,
6465
}
6566

6667
def __init__(self, config, *args, **kwargs):
@@ -307,13 +308,17 @@ def _ldap_connection_factory(self, config):
307308

308309
pool_size = config["pool_size"]
309310
pool_keepalive = config["pool_keepalive"]
311+
pool_lifetime = config["pool_lifetime"]
310312
pool_name = ''.join(random.sample(string.ascii_lowercase, 6))
311313

312314
if client_strategy == ldap3.REUSABLE:
313315
msg = "Using pool size {}".format(pool_size)
314316
logger.debug(msg)
315317
msg = "Using pool keep alive {}".format(pool_keepalive)
316318
logger.debug(msg)
319+
if pool_lifetime:
320+
msg = "Using pool lifetime {}".format(pool_lifetime)
321+
logger.debug(msg)
317322

318323
try:
319324
connection = ldap3.Connection(
@@ -327,6 +332,7 @@ def _ldap_connection_factory(self, config):
327332
pool_name=pool_name,
328333
pool_size=pool_size,
329334
pool_keepalive=pool_keepalive,
335+
pool_lifetime=pool_lifetime,
330336
)
331337
msg = "Successfully connected to LDAP server"
332338
logger.debug(msg)

tests/satosa/micro_services/test_ldap_attribute_store.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from copy import deepcopy
44

5+
from ldap3 import AUTO_BIND_NO_TLS, MOCK_SYNC
6+
57
from satosa.internal import AuthenticationInformation
68
from satosa.internal import InternalData
79
from satosa.micro_services.ldap_attribute_store import LdapAttributeStore
@@ -107,3 +109,60 @@ def test_attributes_general(self, ldap_attribute_store):
107109
internal_attr = ldap_to_internal_map[ldap_attr]
108110
response_attr = response.attributes[internal_attr]
109111
assert(ldap_value in response_attr)
112+
113+
@pytest.mark.parametrize(
114+
'config,connection_attributes',
115+
[
116+
(
117+
{
118+
'auto_bind': 'AUTO_BIND_NO_TLS',
119+
'client_strategy': 'MOCK_SYNC',
120+
'ldap_url': 'ldap://satosa.example.com',
121+
'bind_dn': 'uid=readonly_user,ou=system,dc=example,dc=com',
122+
'bind_password': 'password',
123+
},
124+
{
125+
'user': 'uid=readonly_user,ou=system,dc=example,dc=com',
126+
'password': 'password',
127+
'auto_bind': AUTO_BIND_NO_TLS,
128+
'strategy_type': MOCK_SYNC,
129+
'read_only': True,
130+
'version': 3,
131+
'pool_size': 10,
132+
'pool_keepalive': 10,
133+
'pool_lifetime': None,
134+
},
135+
),
136+
(
137+
{
138+
'auto_bind': 'AUTO_BIND_NO_TLS',
139+
'client_strategy': 'MOCK_SYNC',
140+
'ldap_url': 'ldap://satosa.example.com',
141+
'bind_dn': 'uid=readonly_user,ou=system,dc=example,dc=com',
142+
'bind_password': 'password',
143+
'pool_size': 40,
144+
'pool_keepalive': 41,
145+
'pool_lifetime': 42,
146+
},
147+
{
148+
'user': 'uid=readonly_user,ou=system,dc=example,dc=com',
149+
'password': 'password',
150+
'auto_bind': AUTO_BIND_NO_TLS,
151+
'strategy_type': MOCK_SYNC,
152+
'read_only': True,
153+
'version': 3,
154+
'pool_size': 40,
155+
'pool_keepalive': 41,
156+
'pool_lifetime': 42,
157+
},
158+
),
159+
]
160+
)
161+
def test_connection_config(self, config, connection_attributes):
162+
ldapAttributeStore = LdapAttributeStore({'default': config},
163+
name="test_ldap_attribute_store",
164+
base_url="https://satosa.example.com")
165+
connection = ldapAttributeStore.config['default']['connection']
166+
167+
for k, v in connection_attributes.items():
168+
assert getattr(connection, k) == v

0 commit comments

Comments
 (0)