|
1 | 1 | import ldap |
2 | | -import os |
3 | 2 |
|
4 | 3 | from django_auth_ldap.config import LDAPSearch |
5 | 4 | from importlib import import_module |
| 5 | +from os import environ |
6 | 6 |
|
7 | 7 | # Read secret from file |
8 | | -def read_secret(secret_name): |
| 8 | +def _read_secret(secret_name, default=None): |
9 | 9 | try: |
10 | 10 | f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8') |
11 | 11 | except EnvironmentError: |
12 | | - return '' |
| 12 | + return default |
13 | 13 | else: |
14 | 14 | with f: |
15 | 15 | return f.readline().strip() |
16 | 16 |
|
17 | 17 | # 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): |
19 | 19 | mod = import_module('django_auth_ldap.config') |
20 | 20 | try: |
21 | 21 | return getattr(mod, group_type_name)() |
22 | 22 | except: |
23 | 23 | return None |
24 | 24 |
|
25 | 25 | # 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', '') |
27 | 27 |
|
28 | 28 | # The following may be needed if you are binding to Active Directory. |
29 | 29 | AUTH_LDAP_CONNECTION_OPTIONS = { |
30 | 30 | ldap.OPT_REFERRALS: 0 |
31 | 31 | } |
32 | 32 |
|
33 | 33 | # 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 = os.environ.get('AUTH_LDAP_BIND_PASSWORD', read_secret('auth_ldap_bind_password')) |
| 34 | +AUTH_LDAP_BIND_DN = environ.get('AUTH_LDAP_BIND_DN', '') |
| 35 | +AUTH_LDAP_BIND_PASSWORD = _read_secret('auth_ldap_bind_password', environ.get('AUTH_LDAP_BIND_PASSWORD', '')) |
36 | 36 |
|
37 | 37 | # 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) |
39 | 39 |
|
40 | 40 | # 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' |
42 | 42 |
|
43 | 43 | # Include this setting if you want to ignore certificate errors. This might be needed to accept a self-signed cert. |
44 | 44 | # Note that this is a NetBox-specific setting which sets: |
45 | 45 | # 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' |
47 | 47 |
|
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') |
50 | 50 | AUTH_LDAP_USER_SEARCH = LDAPSearch(AUTH_LDAP_USER_SEARCH_BASEDN, |
51 | 51 | ldap.SCOPE_SUBTREE, |
52 | 52 | "(" + AUTH_LDAP_USER_SEARCH_ATTR + "=%(user)s)") |
53 | 53 |
|
54 | 54 | # This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group |
55 | 55 | # 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') |
58 | 58 | AUTH_LDAP_GROUP_SEARCH = LDAPSearch(AUTH_LDAP_GROUP_SEARCH_BASEDN, ldap.SCOPE_SUBTREE, |
59 | 59 | "(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')) |
61 | 61 |
|
62 | 62 | # 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', '') |
64 | 64 |
|
65 | 65 | # Define special user types using groups. Exercise great caution when assigning superuser status. |
66 | 66 | 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', '') |
70 | 70 | } |
71 | 71 |
|
72 | 72 | # 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' |
| 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' |
74 | 75 |
|
75 | 76 | # Cache groups for one hour to reduce LDAP traffic |
76 | | -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)) |
77 | 78 |
|
78 | 79 | # Populate the Django user from the LDAP directory. |
79 | 80 | AUTH_LDAP_USER_ATTR_MAP = { |
80 | | - "first_name": os.environ.get('AUTH_LDAP_ATTR_FIRSTNAME', 'givenName'), |
81 | | - "last_name": os.environ.get('AUTH_LDAP_ATTR_LASTNAME', 'sn'), |
82 | | - "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') |
83 | 84 | } |
0 commit comments