2018-02-22 07:49:38 -06:00
|
|
|
|
import ldap
|
|
|
|
|
import os
|
|
|
|
|
|
2019-04-30 17:55:44 -05:00
|
|
|
|
from django_auth_ldap.config import LDAPSearch
|
|
|
|
|
from importlib import import_module
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
2019-04-30 16:24:22 -05:00
|
|
|
|
# Read secret from file
|
2020-05-13 07:44:41 -05:00
|
|
|
|
def read_secret(secret_name, default=''):
|
2019-04-30 16:24:22 -05:00
|
|
|
|
try:
|
|
|
|
|
f = open('/run/secrets/' + secret_name, 'r', encoding='utf-8')
|
|
|
|
|
except EnvironmentError:
|
2020-05-13 07:44:41 -05:00
|
|
|
|
return default
|
2019-04-30 16:24:22 -05:00
|
|
|
|
else:
|
|
|
|
|
with f:
|
|
|
|
|
return f.readline().strip()
|
|
|
|
|
|
2019-04-30 17:55:44 -05:00
|
|
|
|
# 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
|
|
|
|
|
|
2018-02-22 07:49:38 -06:00
|
|
|
|
# Server URI
|
|
|
|
|
AUTH_LDAP_SERVER_URI = os.environ.get('AUTH_LDAP_SERVER_URI', '')
|
|
|
|
|
|
|
|
|
|
# The following may be needed if you are binding to Active Directory.
|
|
|
|
|
AUTH_LDAP_CONNECTION_OPTIONS = {
|
|
|
|
|
ldap.OPT_REFERRALS: 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Set the DN and password for the NetBox service account.
|
|
|
|
|
AUTH_LDAP_BIND_DN = os.environ.get('AUTH_LDAP_BIND_DN', '')
|
2020-05-13 07:44:41 -05:00
|
|
|
|
AUTH_LDAP_BIND_PASSWORD = read_secret('auth_ldap_bind_password', os.environ.get('AUTH_LDAP_BIND_PASSWORD', ''))
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
2018-07-20 05:24:17 -05:00
|
|
|
|
# Set a string template that describes any user’s distinguished name based on the username.
|
2018-07-20 05:21:47 -05:00
|
|
|
|
AUTH_LDAP_USER_DN_TEMPLATE = os.environ.get('AUTH_LDAP_USER_DN_TEMPLATE', None)
|
2018-07-11 09:50:02 -05:00
|
|
|
|
|
2020-05-14 09:41:42 -05:00
|
|
|
|
# Enable STARTTLS for ldap authentication.
|
|
|
|
|
AUTH_LDAP_START_TLS = os.environ.get('AUTH_LDAP_START_TLS', 'False').lower() == 'true'
|
|
|
|
|
|
2018-02-22 07:49:38 -06:00
|
|
|
|
# Include this setting if you want to ignore certificate errors. This might be needed to accept a self-signed cert.
|
|
|
|
|
# Note that this is a NetBox-specific setting which sets:
|
|
|
|
|
# ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
|
2018-03-26 04:08:09 -05:00
|
|
|
|
LDAP_IGNORE_CERT_ERRORS = os.environ.get('LDAP_IGNORE_CERT_ERRORS', 'False').lower() == 'true'
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
2019-01-30 06:58:23 -06:00
|
|
|
|
AUTH_LDAP_USER_SEARCH_BASEDN = os.environ.get('AUTH_LDAP_USER_SEARCH_BASEDN', '')
|
|
|
|
|
AUTH_LDAP_USER_SEARCH_ATTR = os.environ.get('AUTH_LDAP_USER_SEARCH_ATTR', 'sAMAccountName')
|
|
|
|
|
AUTH_LDAP_USER_SEARCH = LDAPSearch(AUTH_LDAP_USER_SEARCH_BASEDN,
|
|
|
|
|
ldap.SCOPE_SUBTREE,
|
|
|
|
|
"(" + AUTH_LDAP_USER_SEARCH_ATTR + "=%(user)s)")
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
|
|
|
|
# This search ought to return all groups to which the user belongs. django_auth_ldap uses this to determine group
|
|
|
|
|
# heirarchy.
|
2019-01-30 06:58:23 -06:00
|
|
|
|
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 + ")")
|
2019-04-30 17:55:44 -05:00
|
|
|
|
AUTH_LDAP_GROUP_TYPE = import_group_type(os.environ.get('AUTH_LDAP_GROUP_TYPE', 'GroupOfNamesType'))
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
|
|
|
|
# Define a group required to login.
|
|
|
|
|
AUTH_LDAP_REQUIRE_GROUP = os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', '')
|
|
|
|
|
|
|
|
|
|
# Define special user types using groups. Exercise great caution when assigning superuser status.
|
|
|
|
|
AUTH_LDAP_USER_FLAGS_BY_GROUP = {
|
|
|
|
|
"is_active": os.environ.get('AUTH_LDAP_REQUIRE_GROUP_DN', ''),
|
|
|
|
|
"is_staff": os.environ.get('AUTH_LDAP_IS_ADMIN_DN', ''),
|
|
|
|
|
"is_superuser": os.environ.get('AUTH_LDAP_IS_SUPERUSER_DN', '')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# For more granular permissions, we can map LDAP groups to Django groups.
|
2018-03-26 04:08:09 -05:00
|
|
|
|
AUTH_LDAP_FIND_GROUP_PERMS = os.environ.get('AUTH_LDAP_FIND_GROUP_PERMS', 'True').lower() == 'true'
|
2020-10-19 09:50:33 -05:00
|
|
|
|
AUTH_LDAP_MIRROR_GROUPS = os.environ.get('AUTH_LDAP_MIRROR_GROUPS', None).lower() == 'true'
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
|
|
|
|
# Cache groups for one hour to reduce LDAP traffic
|
2020-01-28 17:43:35 -06:00
|
|
|
|
AUTH_LDAP_CACHE_TIMEOUT = int(os.environ.get('AUTH_LDAP_CACHE_TIMEOUT', 3600))
|
2018-02-22 07:49:38 -06:00
|
|
|
|
|
|
|
|
|
# Populate the Django user from the LDAP directory.
|
|
|
|
|
AUTH_LDAP_USER_ATTR_MAP = {
|
|
|
|
|
"first_name": os.environ.get('AUTH_LDAP_ATTR_FIRSTNAME', 'givenName'),
|
|
|
|
|
"last_name": os.environ.get('AUTH_LDAP_ATTR_LASTNAME', 'sn'),
|
|
|
|
|
"email": os.environ.get('AUTH_LDAP_ATTR_MAIL', 'mail')
|
|
|
|
|
}
|