From 7362e275b048f3b7b74132ed566d32d9174dab0e Mon Sep 17 00:00:00 2001 From: Matthew Yauch Date: Tue, 30 Apr 2019 15:55:44 -0700 Subject: [PATCH] Add AUTH_LDAP_GROUP_TYPE env variable support Dynamically imports the correct class/subclass from django_auth_ldap.config based on the AUTH_LDAP_GROUP_TYPE environment variable. --- configuration/ldap_config.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/configuration/ldap_config.py b/configuration/ldap_config.py index b1b88d8..39fc894 100644 --- a/configuration/ldap_config.py +++ b/configuration/ldap_config.py @@ -1,7 +1,8 @@ import ldap import os -from django_auth_ldap.config import LDAPSearch, GroupOfNamesType +from django_auth_ldap.config import LDAPSearch +from importlib import import_module # Read secret from file def read_secret(secret_name): @@ -13,6 +14,14 @@ def read_secret(secret_name): with f: return f.readline().strip() +# Import and return the group type based on string name +def import_group_type(group_type_name): + mod = import_module('django_auth_ldap.config') + try: + return getattr(mod, group_type_name)() + except: + return None + # Server URI AUTH_LDAP_SERVER_URI = os.environ.get('AUTH_LDAP_SERVER_URI', '') @@ -45,7 +54,7 @@ AUTH_LDAP_GROUP_SEARCH_BASEDN = os.environ.get('AUTH_LDAP_GROUP_SEARCH_BASEDN', AUTH_LDAP_GROUP_SEARCH_CLASS = os.environ.get('AUTH_LDAP_GROUP_SEARCH_CLASS', 'group') AUTH_LDAP_GROUP_SEARCH = LDAPSearch(AUTH_LDAP_GROUP_SEARCH_BASEDN, ldap.SCOPE_SUBTREE, "(objectClass=" + AUTH_LDAP_GROUP_SEARCH_CLASS + ")") -AUTH_LDAP_GROUP_TYPE = GroupOfNamesType() +AUTH_LDAP_GROUP_TYPE = import_group_type(os.environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType')) # Define a group required to login. AUTH_LDAP_REQUIRE_GROUP = os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', '')