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

Add option pool_lifetime option to ldap #395

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ config:
# pool_keepalive: seconds to wait between calls to server to keep the
# connection alive; default: 10
pool_keepalive: 10
# pool_lifetime: number of seconds before recreating a new connection
# in a pooled connection strategy.
pool_lifetime: None

# Attributes to return from LDAP query.
query_return_attributes:
Expand Down
6 changes: 6 additions & 0 deletions src/satosa/micro_services/ldap_attribute_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class LdapAttributeStore(ResponseMicroService):
"client_strategy": "REUSABLE",
"pool_size": 10,
"pool_keepalive": 10,
"pool_lifetime": None,
}

def __init__(self, config, *args, **kwargs):
Expand Down Expand Up @@ -307,13 +308,17 @@ def _ldap_connection_factory(self, config):

pool_size = config["pool_size"]
pool_keepalive = config["pool_keepalive"]
pool_lifetime = config["pool_lifetime"]
pool_name = ''.join(random.sample(string.ascii_lowercase, 6))

if client_strategy == ldap3.REUSABLE:
msg = "Using pool size {}".format(pool_size)
logger.debug(msg)
msg = "Using pool keep alive {}".format(pool_keepalive)
logger.debug(msg)
if pool_lifetime:
msg = "Using pool lifetime {}".format(pool_lifetime)
logger.debug(msg)

try:
connection = ldap3.Connection(
Expand All @@ -327,6 +332,7 @@ def _ldap_connection_factory(self, config):
pool_name=pool_name,
pool_size=pool_size,
pool_keepalive=pool_keepalive,
pool_lifetime=pool_lifetime,
)
msg = "Successfully connected to LDAP server"
logger.debug(msg)
Expand Down
59 changes: 59 additions & 0 deletions tests/satosa/micro_services/test_ldap_attribute_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from copy import deepcopy

from ldap3 import AUTO_BIND_NO_TLS, MOCK_SYNC

from satosa.internal import AuthenticationInformation
from satosa.internal import InternalData
from satosa.micro_services.ldap_attribute_store import LdapAttributeStore
Expand Down Expand Up @@ -107,3 +109,60 @@ def test_attributes_general(self, ldap_attribute_store):
internal_attr = ldap_to_internal_map[ldap_attr]
response_attr = response.attributes[internal_attr]
assert(ldap_value in response_attr)

@pytest.mark.parametrize(
'config,connection_attributes',
[
(
{
'auto_bind': 'AUTO_BIND_NO_TLS',
'client_strategy': 'MOCK_SYNC',
'ldap_url': 'ldap://satosa.example.com',
'bind_dn': 'uid=readonly_user,ou=system,dc=example,dc=com',
'bind_password': 'password',
},
{
'user': 'uid=readonly_user,ou=system,dc=example,dc=com',
'password': 'password',
'auto_bind': AUTO_BIND_NO_TLS,
'strategy_type': MOCK_SYNC,
'read_only': True,
'version': 3,
'pool_size': 10,
'pool_keepalive': 10,
'pool_lifetime': None,
},
),
(
{
'auto_bind': 'AUTO_BIND_NO_TLS',
'client_strategy': 'MOCK_SYNC',
'ldap_url': 'ldap://satosa.example.com',
'bind_dn': 'uid=readonly_user,ou=system,dc=example,dc=com',
'bind_password': 'password',
'pool_size': 40,
'pool_keepalive': 41,
'pool_lifetime': 42,
},
{
'user': 'uid=readonly_user,ou=system,dc=example,dc=com',
'password': 'password',
'auto_bind': AUTO_BIND_NO_TLS,
'strategy_type': MOCK_SYNC,
'read_only': True,
'version': 3,
'pool_size': 40,
'pool_keepalive': 41,
'pool_lifetime': 42,
},
),
]
)
def test_connection_config(self, config, connection_attributes):
ldapAttributeStore = LdapAttributeStore({'default': config},
name="test_ldap_attribute_store",
base_url="https://satosa.example.com")
connection = ldapAttributeStore.config['default']['connection']

for k, v in connection_attributes.items():
assert getattr(connection, k) == v