settings.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import logging
  2. import os
  3. import socket
  4. import sys
  5. import warnings
  6. from django.contrib.messages import constants as messages
  7. from django.core.exceptions import ImproperlyConfigured
  8. try:
  9. from netbox import configuration
  10. except ImportError:
  11. raise ImproperlyConfigured(
  12. "Configuration file is not present. Please define netbox/netbox/configuration.py per the documentation."
  13. )
  14. # Raise a deprecation warning for Python 2.x
  15. if sys.version_info[0] < 3:
  16. warnings.warn(
  17. "Support for Python 2 will be removed in NetBox v2.5. Please consider migration to Python 3 at your earliest "
  18. "opportunity. Guidance is available in the documentation at http://netbox.readthedocs.io/.",
  19. DeprecationWarning
  20. )
  21. VERSION = '2.4-dev'
  22. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  23. # Import required configuration parameters
  24. ALLOWED_HOSTS = DATABASE = SECRET_KEY = None
  25. for setting in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY']:
  26. try:
  27. globals()[setting] = getattr(configuration, setting)
  28. except AttributeError:
  29. raise ImproperlyConfigured(
  30. "Mandatory setting {} is missing from configuration.py.".format(setting)
  31. )
  32. # Import optional configuration parameters
  33. ADMINS = getattr(configuration, 'ADMINS', [])
  34. BANNER_BOTTOM = getattr(configuration, 'BANNER_BOTTOM', '')
  35. BANNER_LOGIN = getattr(configuration, 'BANNER_LOGIN', '')
  36. BANNER_TOP = getattr(configuration, 'BANNER_TOP', '')
  37. BASE_PATH = getattr(configuration, 'BASE_PATH', '')
  38. if BASE_PATH:
  39. BASE_PATH = BASE_PATH.strip('/') + '/' # Enforce trailing slash only
  40. CORS_ORIGIN_ALLOW_ALL = getattr(configuration, 'CORS_ORIGIN_ALLOW_ALL', False)
  41. CORS_ORIGIN_REGEX_WHITELIST = getattr(configuration, 'CORS_ORIGIN_REGEX_WHITELIST', [])
  42. CORS_ORIGIN_WHITELIST = getattr(configuration, 'CORS_ORIGIN_WHITELIST', [])
  43. DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
  44. DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
  45. DEBUG = getattr(configuration, 'DEBUG', False)
  46. ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False)
  47. EMAIL = getattr(configuration, 'EMAIL', {})
  48. LOGGING = getattr(configuration, 'LOGGING', {})
  49. LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
  50. MAINTENANCE_MODE = getattr(configuration, 'MAINTENANCE_MODE', False)
  51. MAX_PAGE_SIZE = getattr(configuration, 'MAX_PAGE_SIZE', 1000)
  52. MEDIA_ROOT = getattr(configuration, 'MEDIA_ROOT', os.path.join(BASE_DIR, 'media')).rstrip('/')
  53. NAPALM_USERNAME = getattr(configuration, 'NAPALM_USERNAME', '')
  54. NAPALM_PASSWORD = getattr(configuration, 'NAPALM_PASSWORD', '')
  55. NAPALM_TIMEOUT = getattr(configuration, 'NAPALM_TIMEOUT', 30)
  56. NAPALM_ARGS = getattr(configuration, 'NAPALM_ARGS', {})
  57. PAGINATE_COUNT = getattr(configuration, 'PAGINATE_COUNT', 50)
  58. PREFER_IPV4 = getattr(configuration, 'PREFER_IPV4', False)
  59. REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
  60. SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
  61. SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
  62. SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
  63. TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
  64. TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
  65. CSRF_TRUSTED_ORIGINS = ALLOWED_HOSTS
  66. # Attempt to import LDAP configuration if it has been defined
  67. LDAP_IGNORE_CERT_ERRORS = False
  68. try:
  69. from netbox.ldap_config import *
  70. LDAP_CONFIGURED = True
  71. except ImportError:
  72. LDAP_CONFIGURED = False
  73. # LDAP configuration (optional)
  74. if LDAP_CONFIGURED:
  75. try:
  76. import ldap
  77. import django_auth_ldap
  78. # Prepend LDAPBackend to the default ModelBackend
  79. AUTHENTICATION_BACKENDS = [
  80. 'django_auth_ldap.backend.LDAPBackend',
  81. 'django.contrib.auth.backends.ModelBackend',
  82. ]
  83. # Optionally disable strict certificate checking
  84. if LDAP_IGNORE_CERT_ERRORS:
  85. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  86. # Enable logging for django_auth_ldap
  87. ldap_logger = logging.getLogger('django_auth_ldap')
  88. ldap_logger.addHandler(logging.StreamHandler())
  89. ldap_logger.setLevel(logging.DEBUG)
  90. except ImportError:
  91. raise ImproperlyConfigured(
  92. "LDAP authentication has been configured, but django-auth-ldap is not installed. You can remove "
  93. "netbox/ldap_config.py to disable LDAP."
  94. )
  95. # Database
  96. configuration.DATABASE.update({'ENGINE': 'django.db.backends.postgresql'})
  97. DATABASES = {
  98. 'default': configuration.DATABASE,
  99. }
  100. # Email
  101. EMAIL_HOST = EMAIL.get('SERVER')
  102. EMAIL_PORT = EMAIL.get('PORT', 25)
  103. EMAIL_HOST_USER = EMAIL.get('USERNAME')
  104. EMAIL_HOST_PASSWORD = EMAIL.get('PASSWORD')
  105. EMAIL_TIMEOUT = EMAIL.get('TIMEOUT', 10)
  106. SERVER_EMAIL = EMAIL.get('FROM_EMAIL')
  107. EMAIL_SUBJECT_PREFIX = '[NetBox] '
  108. # Installed applications
  109. INSTALLED_APPS = (
  110. 'django.contrib.admin',
  111. 'django.contrib.auth',
  112. 'django.contrib.contenttypes',
  113. 'django.contrib.sessions',
  114. 'django.contrib.messages',
  115. 'django.contrib.staticfiles',
  116. 'django.contrib.humanize',
  117. 'corsheaders',
  118. 'debug_toolbar',
  119. 'django_filters',
  120. 'django_tables2',
  121. 'mptt',
  122. 'rest_framework',
  123. 'taggit',
  124. 'timezone_field',
  125. 'circuits',
  126. 'dcim',
  127. 'ipam',
  128. 'extras',
  129. 'secrets',
  130. 'tenancy',
  131. 'users',
  132. 'utilities',
  133. 'virtualization',
  134. 'drf_yasg',
  135. )
  136. # Middleware
  137. MIDDLEWARE = (
  138. 'debug_toolbar.middleware.DebugToolbarMiddleware',
  139. 'corsheaders.middleware.CorsMiddleware',
  140. 'django.contrib.sessions.middleware.SessionMiddleware',
  141. 'django.middleware.common.CommonMiddleware',
  142. 'django.middleware.csrf.CsrfViewMiddleware',
  143. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  144. 'django.contrib.messages.middleware.MessageMiddleware',
  145. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  146. 'django.middleware.security.SecurityMiddleware',
  147. 'utilities.middleware.ExceptionHandlingMiddleware',
  148. 'utilities.middleware.LoginRequiredMiddleware',
  149. 'utilities.middleware.APIVersionMiddleware',
  150. )
  151. ROOT_URLCONF = 'netbox.urls'
  152. TEMPLATES = [
  153. {
  154. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  155. 'DIRS': [BASE_DIR + '/templates/'],
  156. 'APP_DIRS': True,
  157. 'OPTIONS': {
  158. 'context_processors': [
  159. 'django.template.context_processors.debug',
  160. 'django.template.context_processors.request',
  161. 'django.template.context_processors.media',
  162. 'django.contrib.auth.context_processors.auth',
  163. 'django.contrib.messages.context_processors.messages',
  164. 'utilities.context_processors.settings',
  165. ],
  166. },
  167. },
  168. ]
  169. # WSGI
  170. WSGI_APPLICATION = 'netbox.wsgi.application'
  171. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  172. USE_X_FORWARDED_HOST = True
  173. # Internationalization
  174. LANGUAGE_CODE = 'en-us'
  175. USE_I18N = True
  176. USE_TZ = True
  177. # Static files (CSS, JavaScript, Images)
  178. STATIC_ROOT = BASE_DIR + '/static/'
  179. STATIC_URL = '/{}static/'.format(BASE_PATH)
  180. STATICFILES_DIRS = (
  181. os.path.join(BASE_DIR, "project-static"),
  182. )
  183. # Media
  184. MEDIA_URL = '/{}media/'.format(BASE_PATH)
  185. # Disable default limit of 1000 fields per request. Needed for bulk deletion of objects. (Added in Django 1.10.)
  186. DATA_UPLOAD_MAX_NUMBER_FIELDS = None
  187. # Messages
  188. MESSAGE_TAGS = {
  189. messages.ERROR: 'danger',
  190. }
  191. # Authentication URLs
  192. LOGIN_URL = '/{}login/'.format(BASE_PATH)
  193. # Secrets
  194. SECRETS_MIN_PUBKEY_SIZE = 2048
  195. # Django filters
  196. FILTERS_NULL_CHOICE_LABEL = 'None'
  197. FILTERS_NULL_CHOICE_VALUE = '0' # Must be a string
  198. # Django REST framework (API)
  199. REST_FRAMEWORK_VERSION = VERSION[0:3] # Use major.minor as API version
  200. REST_FRAMEWORK = {
  201. 'ALLOWED_VERSIONS': [REST_FRAMEWORK_VERSION],
  202. 'DEFAULT_AUTHENTICATION_CLASSES': (
  203. 'rest_framework.authentication.SessionAuthentication',
  204. 'netbox.api.TokenAuthentication',
  205. ),
  206. 'DEFAULT_FILTER_BACKENDS': (
  207. 'django_filters.rest_framework.DjangoFilterBackend',
  208. ),
  209. 'DEFAULT_PAGINATION_CLASS': 'netbox.api.OptionalLimitOffsetPagination',
  210. 'DEFAULT_PERMISSION_CLASSES': (
  211. 'netbox.api.TokenPermissions',
  212. ),
  213. 'DEFAULT_RENDERER_CLASSES': (
  214. 'rest_framework.renderers.JSONRenderer',
  215. 'netbox.api.FormlessBrowsableAPIRenderer',
  216. ),
  217. 'DEFAULT_VERSION': REST_FRAMEWORK_VERSION,
  218. 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
  219. 'PAGE_SIZE': PAGINATE_COUNT,
  220. 'VIEW_NAME_FUNCTION': 'netbox.api.get_view_name',
  221. }
  222. # drf_yasg settings for Swagger
  223. SWAGGER_SETTINGS = {
  224. 'DEFAULT_FIELD_INSPECTORS': [
  225. 'utilities.custom_inspectors.NullableBooleanFieldInspector',
  226. 'utilities.custom_inspectors.CustomChoiceFieldInspector',
  227. 'drf_yasg.inspectors.CamelCaseJSONFilter',
  228. 'drf_yasg.inspectors.ReferencingSerializerInspector',
  229. 'drf_yasg.inspectors.RelatedFieldInspector',
  230. 'drf_yasg.inspectors.ChoiceFieldInspector',
  231. 'drf_yasg.inspectors.FileFieldInspector',
  232. 'drf_yasg.inspectors.DictFieldInspector',
  233. 'drf_yasg.inspectors.SimpleFieldInspector',
  234. 'drf_yasg.inspectors.StringDefaultFieldInspector',
  235. ],
  236. 'DEFAULT_FILTER_INSPECTORS': [
  237. 'utilities.custom_inspectors.IdInFilterInspector',
  238. 'drf_yasg.inspectors.CoreAPICompatInspector',
  239. ],
  240. 'DEFAULT_PAGINATOR_INSPECTORS': [
  241. 'utilities.custom_inspectors.NullablePaginatorInspector',
  242. 'drf_yasg.inspectors.DjangoRestResponsePagination',
  243. 'drf_yasg.inspectors.CoreAPICompatInspector',
  244. ]
  245. }
  246. # Django debug toolbar
  247. INTERNAL_IPS = (
  248. '127.0.0.1',
  249. '::1',
  250. )
  251. try:
  252. HOSTNAME = socket.gethostname()
  253. except:
  254. HOSTNAME = 'localhost'