Skip to content

Commit 20820e1

Browse files
committed
Optimize Imports in ldap_config.py
1 parent 553c8ea commit 20820e1

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

configuration/ldap/ldap_config.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import ldap
2-
import os
32

43
from django_auth_ldap.config import LDAPSearch
54
from importlib import import_module
5+
from os import environ
66

77
# Read secret from file
8-
def read_secret(secret_name, default=''):
8+
def _read_secret(secret_name, default=None):
99
try:
1010
f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8')
1111
except EnvironmentError:
@@ -15,70 +15,70 @@ def read_secret(secret_name, default=''):
1515
return f.readline().strip()
1616

1717
# Import and return the group type based on string name
18-
def import_group_type(group_type_name):
18+
def _import_group_type(group_type_name):
1919
mod = import_module('django_auth_ldap.config')
2020
try:
2121
return getattr(mod, group_type_name)()
2222
except:
2323
return None
2424

2525
# Server URI
26-
AUTH_LDAP_SERVER_URI = os.environ.get('AUTH_LDAP_SERVER_URI', '')
26+
AUTH_LDAP_SERVER_URI = environ.get('AUTH_LDAP_SERVER_URI', '')
2727

2828
# The following may be needed if you are binding to Active Directory.
2929
AUTH_LDAP_CONNECTION_OPTIONS = {
3030
ldap.OPT_REFERRALS: 0
3131
}
3232

3333
# Set the DN and password for the NetBox service account.
34-
AUTH_LDAP_BIND_DN = os.environ.get('AUTH_LDAP_BIND_DN', '')
35-
AUTH_LDAP_BIND_PASSWORD = read_secret('auth_ldap_bind_password', os.environ.get('AUTH_LDAP_BIND_PASSWORD', ''))
34+
AUTH_LDAP_BIND_DN = environ.get('AUTH_LDAP_BIND_DN', '')
35+
AUTH_LDAP_BIND_PASSWORD = environ.get('AUTH_LDAP_BIND_PASSWORD', _read_secret('auth_ldap_bind_password'))
3636

3737
# Set a string template that describes any user’s distinguished name based on the username.
38-
AUTH_LDAP_USER_DN_TEMPLATE = os.environ.get('AUTH_LDAP_USER_DN_TEMPLATE', None)
38+
AUTH_LDAP_USER_DN_TEMPLATE = environ.get('AUTH_LDAP_USER_DN_TEMPLATE', None)
3939

4040
# Enable STARTTLS for ldap authentication.
41-
AUTH_LDAP_START_TLS = os.environ.get('AUTH_LDAP_START_TLS', 'False').lower() == 'true'
41+
AUTH_LDAP_START_TLS = environ.get('AUTH_LDAP_START_TLS', 'False').lower() == 'true'
4242

4343
# Include this setting if you want to ignore certificate errors. This might be needed to accept a self-signed cert.
4444
# Note that this is a NetBox-specific setting which sets:
4545
# ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
46-
LDAP_IGNORE_CERT_ERRORS = os.environ.get('LDAP_IGNORE_CERT_ERRORS', 'False').lower() == 'true'
46+
LDAP_IGNORE_CERT_ERRORS = environ.get('LDAP_IGNORE_CERT_ERRORS', 'False').lower() == 'true'
4747

48-
AUTH_LDAP_USER_SEARCH_BASEDN = os.environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', '')
49-
AUTH_LDAP_USER_SEARCH_ATTR = os.environ.get('AUTH_LDAP_USER_SEARCH_ATTR', 'sAMAccountName')
48+
AUTH_LDAP_USER_SEARCH_BASEDN = environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', '')
49+
AUTH_LDAP_USER_SEARCH_ATTR = environ.get('AUTH_LDAP_USER_SEARCH_ATTR', 'sAMAccountName')
5050
AUTH_LDAP_USER_SEARCH = LDAPSearch(AUTH_LDAP_USER_SEARCH_BASEDN,
5151
ldap.SCOPE_SUBTREE,
5252
"(" + AUTH_LDAP_USER_SEARCH_ATTR + "=%(user)s)")
5353

5454
# This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group
5555
# heirarchy.
56-
AUTH_LDAP_GROUP_SEARCH_BASEDN = os.environ.get('AUTH_LDAP_GROUP_SEARCH_BASEDN', '')
57-
AUTH_LDAP_GROUP_SEARCH_CLASS = os.environ.get('AUTH_LDAP_GROUP_SEARCH_CLASS', 'group')
56+
AUTH_LDAP_GROUP_SEARCH_BASEDN = environ.get('AUTH_LDAP_GROUP_SEARCH_BASEDN', '')
57+
AUTH_LDAP_GROUP_SEARCH_CLASS = environ.get('AUTH_LDAP_GROUP_SEARCH_CLASS', 'group')
5858
AUTH_LDAP_GROUP_SEARCH = LDAPSearch(AUTH_LDAP_GROUP_SEARCH_BASEDN, ldap.SCOPE_SUBTREE,
5959
"(objectClass=" + AUTH_LDAP_GROUP_SEARCH_CLASS + ")")
60-
AUTH_LDAP_GROUP_TYPE = import_group_type(os.environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType'))
60+
AUTH_LDAP_GROUP_TYPE = _import_group_type(environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType'))
6161

6262
# Define a group required to login.
63-
AUTH_LDAP_REQUIRE_GROUP = os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', '')
63+
AUTH_LDAP_REQUIRE_GROUP = environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', '')
6464

6565
# Define special user types using groups. Exercise great caution when assigning superuser status.
6666
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
67-
"is_active": os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', ''),
68-
"is_staff": os.environ.get('AUTH_LDAP_IS_ADMIN_DN', ''),
69-
"is_superuser": os.environ.get('AUTH_LDAP_IS_SUPERUSER_DN', '')
67+
"is_active": environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', ''),
68+
"is_staff": environ.get('AUTH_LDAP_IS_ADMIN_DN', ''),
69+
"is_superuser": environ.get('AUTH_LDAP_IS_SUPERUSER_DN', '')
7070
}
7171

7272
# For more granular permissions, we can map LDAP groups to Django groups.
73-
AUTH_LDAP_FIND_GROUP_PERMS = os.environ.get('AUTH_LDAP_FIND_GROUP_PERMS', 'True').lower() == 'true'
74-
AUTH_LDAP_MIRROR_GROUPS = os.environ.get('AUTH_LDAP_MIRROR_GROUPS', None).lower() == 'true'
73+
AUTH_LDAP_FIND_GROUP_PERMS = environ.get('AUTH_LDAP_FIND_GROUP_PERMS', 'True').lower() == 'true'
74+
AUTH_LDAP_MIRROR_GROUPS = environ.get('AUTH_LDAP_MIRROR_GROUPS', None).lower() == 'true'
7575

7676
# Cache groups for one hour to reduce LDAP traffic
77-
AUTH_LDAP_CACHE_TIMEOUT = int(os.environ.get('AUTH_LDAP_CACHE_TIMEOUT', 3600))
77+
AUTH_LDAP_CACHE_TIMEOUT = int(environ.get('AUTH_LDAP_CACHE_TIMEOUT', 3600))
7878

7979
# Populate the Django user from the LDAP directory.
8080
AUTH_LDAP_USER_ATTR_MAP = {
81-
"first_name": os.environ.get('AUTH_LDAP_ATTR_FIRSTNAME', 'givenName'),
82-
"last_name": os.environ.get('AUTH_LDAP_ATTR_LASTNAME', 'sn'),
83-
"email": os.environ.get('AUTH_LDAP_ATTR_MAIL', 'mail')
81+
"first_name": environ.get('AUTH_LDAP_ATTR_FIRSTNAME', 'givenName'),
82+
"last_name": environ.get('AUTH_LDAP_ATTR_LASTNAME', 'sn'),
83+
"email": environ.get('AUTH_LDAP_ATTR_MAIL', 'mail')
8484
}

0 commit comments

Comments
 (0)