settings.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. import hashlib
  2. import importlib
  3. import importlib.util
  4. import os
  5. import platform
  6. import sys
  7. import warnings
  8. from urllib.parse import urlsplit
  9. import django
  10. import sentry_sdk
  11. from django.contrib.messages import constants as messages
  12. from django.core.exceptions import ImproperlyConfigured, ValidationError
  13. from django.core.validators import URLValidator
  14. from django.utils.encoding import force_str
  15. from extras.plugins import PluginConfig
  16. from sentry_sdk.integrations.django import DjangoIntegration
  17. from netbox.config import PARAMS
  18. #
  19. # Environment setup
  20. #
  21. VERSION = '3.4-beta1'
  22. # Hostname
  23. HOSTNAME = platform.node()
  24. # Set the base directory two levels up
  25. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  26. # Validate Python version
  27. if sys.version_info < (3, 8):
  28. raise RuntimeError(
  29. f"NetBox requires Python 3.8 or later. (Currently installed: Python {platform.python_version()})"
  30. )
  31. DEFAULT_SENTRY_DSN = 'https://198cf560b29d4054ab8e583a1d10ea58@o1242133.ingest.sentry.io/6396485'
  32. #
  33. # Configuration import
  34. #
  35. # Import configuration parameters
  36. config_path = os.getenv('NETBOX_CONFIGURATION', 'netbox.configuration')
  37. try:
  38. configuration = importlib.import_module(config_path)
  39. except ModuleNotFoundError as e:
  40. if getattr(e, 'name') == config_path:
  41. raise ImproperlyConfigured(
  42. f"Specified configuration module ({config_path}) not found. Please define netbox/netbox/configuration.py "
  43. f"per the documentation, or specify an alternate module in the NETBOX_CONFIGURATION environment variable."
  44. )
  45. raise
  46. # Enforce required configuration parameters
  47. for parameter in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY', 'REDIS']:
  48. if not hasattr(configuration, parameter):
  49. raise ImproperlyConfigured(f"Required parameter {parameter} is missing from configuration.")
  50. # Set required parameters
  51. ALLOWED_HOSTS = getattr(configuration, 'ALLOWED_HOSTS')
  52. DATABASE = getattr(configuration, 'DATABASE')
  53. REDIS = getattr(configuration, 'REDIS')
  54. SECRET_KEY = getattr(configuration, 'SECRET_KEY')
  55. # Calculate a unique deployment ID from the secret key
  56. DEPLOYMENT_ID = hashlib.sha256(SECRET_KEY.encode('utf-8')).hexdigest()[:16]
  57. # Set static config parameters
  58. ADMINS = getattr(configuration, 'ADMINS', [])
  59. AUTH_PASSWORD_VALIDATORS = getattr(configuration, 'AUTH_PASSWORD_VALIDATORS', [])
  60. BASE_PATH = getattr(configuration, 'BASE_PATH', '')
  61. if BASE_PATH:
  62. BASE_PATH = BASE_PATH.strip('/') + '/' # Enforce trailing slash only
  63. CORS_ORIGIN_ALLOW_ALL = getattr(configuration, 'CORS_ORIGIN_ALLOW_ALL', False)
  64. CORS_ORIGIN_REGEX_WHITELIST = getattr(configuration, 'CORS_ORIGIN_REGEX_WHITELIST', [])
  65. CORS_ORIGIN_WHITELIST = getattr(configuration, 'CORS_ORIGIN_WHITELIST', [])
  66. CSRF_COOKIE_NAME = getattr(configuration, 'CSRF_COOKIE_NAME', 'csrftoken')
  67. CSRF_COOKIE_PATH = BASE_PATH or '/'
  68. CSRF_TRUSTED_ORIGINS = getattr(configuration, 'CSRF_TRUSTED_ORIGINS', [])
  69. DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
  70. DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
  71. DEBUG = getattr(configuration, 'DEBUG', False)
  72. DEVELOPER = getattr(configuration, 'DEVELOPER', False)
  73. DOCS_ROOT = getattr(configuration, 'DOCS_ROOT', os.path.join(os.path.dirname(BASE_DIR), 'docs'))
  74. EMAIL = getattr(configuration, 'EMAIL', {})
  75. EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', [])
  76. FIELD_CHOICES = getattr(configuration, 'FIELD_CHOICES', {})
  77. HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', None)
  78. INTERNAL_IPS = getattr(configuration, 'INTERNAL_IPS', ('127.0.0.1', '::1'))
  79. JINJA2_FILTERS = getattr(configuration, 'JINJA2_FILTERS', {})
  80. LOGGING = getattr(configuration, 'LOGGING', {})
  81. LOGIN_PERSISTENCE = getattr(configuration, 'LOGIN_PERSISTENCE', False)
  82. LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
  83. LOGIN_TIMEOUT = getattr(configuration, 'LOGIN_TIMEOUT', None)
  84. MEDIA_ROOT = getattr(configuration, 'MEDIA_ROOT', os.path.join(BASE_DIR, 'media')).rstrip('/')
  85. METRICS_ENABLED = getattr(configuration, 'METRICS_ENABLED', False)
  86. PLUGINS = getattr(configuration, 'PLUGINS', [])
  87. PLUGINS_CONFIG = getattr(configuration, 'PLUGINS_CONFIG', {})
  88. RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None)
  89. REMOTE_AUTH_AUTO_CREATE_USER = getattr(configuration, 'REMOTE_AUTH_AUTO_CREATE_USER', False)
  90. REMOTE_AUTH_BACKEND = getattr(configuration, 'REMOTE_AUTH_BACKEND', 'netbox.authentication.RemoteUserBackend')
  91. REMOTE_AUTH_DEFAULT_GROUPS = getattr(configuration, 'REMOTE_AUTH_DEFAULT_GROUPS', [])
  92. REMOTE_AUTH_DEFAULT_PERMISSIONS = getattr(configuration, 'REMOTE_AUTH_DEFAULT_PERMISSIONS', {})
  93. REMOTE_AUTH_ENABLED = getattr(configuration, 'REMOTE_AUTH_ENABLED', False)
  94. REMOTE_AUTH_HEADER = getattr(configuration, 'REMOTE_AUTH_HEADER', 'HTTP_REMOTE_USER')
  95. REMOTE_AUTH_GROUP_HEADER = getattr(configuration, 'REMOTE_AUTH_GROUP_HEADER', 'HTTP_REMOTE_USER_GROUP')
  96. REMOTE_AUTH_GROUP_SYNC_ENABLED = getattr(configuration, 'REMOTE_AUTH_GROUP_SYNC_ENABLED', False)
  97. REMOTE_AUTH_SUPERUSER_GROUPS = getattr(configuration, 'REMOTE_AUTH_SUPERUSER_GROUPS', [])
  98. REMOTE_AUTH_SUPERUSERS = getattr(configuration, 'REMOTE_AUTH_SUPERUSERS', [])
  99. REMOTE_AUTH_STAFF_GROUPS = getattr(configuration, 'REMOTE_AUTH_STAFF_GROUPS', [])
  100. REMOTE_AUTH_STAFF_USERS = getattr(configuration, 'REMOTE_AUTH_STAFF_USERS', [])
  101. REMOTE_AUTH_GROUP_SEPARATOR = getattr(configuration, 'REMOTE_AUTH_GROUP_SEPARATOR', '|')
  102. REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
  103. RQ_DEFAULT_TIMEOUT = getattr(configuration, 'RQ_DEFAULT_TIMEOUT', 300)
  104. SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/')
  105. SEARCH_BACKEND = getattr(configuration, 'SEARCH_BACKEND', 'netbox.search.backends.CachedValueSearchBackend')
  106. SENTRY_DSN = getattr(configuration, 'SENTRY_DSN', DEFAULT_SENTRY_DSN)
  107. SENTRY_ENABLED = getattr(configuration, 'SENTRY_ENABLED', False)
  108. SENTRY_SAMPLE_RATE = getattr(configuration, 'SENTRY_SAMPLE_RATE', 1.0)
  109. SENTRY_TRACES_SAMPLE_RATE = getattr(configuration, 'SENTRY_TRACES_SAMPLE_RATE', 0)
  110. SENTRY_TAGS = getattr(configuration, 'SENTRY_TAGS', {})
  111. SESSION_FILE_PATH = getattr(configuration, 'SESSION_FILE_PATH', None)
  112. SESSION_COOKIE_NAME = getattr(configuration, 'SESSION_COOKIE_NAME', 'sessionid')
  113. SESSION_COOKIE_PATH = BASE_PATH or '/'
  114. LANGUAGE_COOKIE_PATH = BASE_PATH or '/'
  115. SHORT_DATE_FORMAT = getattr(configuration, 'SHORT_DATE_FORMAT', 'Y-m-d')
  116. SHORT_DATETIME_FORMAT = getattr(configuration, 'SHORT_DATETIME_FORMAT', 'Y-m-d H:i')
  117. SHORT_TIME_FORMAT = getattr(configuration, 'SHORT_TIME_FORMAT', 'H:i:s')
  118. STORAGE_BACKEND = getattr(configuration, 'STORAGE_BACKEND', None)
  119. STORAGE_CONFIG = getattr(configuration, 'STORAGE_CONFIG', {})
  120. TIME_FORMAT = getattr(configuration, 'TIME_FORMAT', 'g:i a')
  121. TIME_ZONE = getattr(configuration, 'TIME_ZONE', 'UTC')
  122. # Check for hard-coded dynamic config parameters
  123. for param in PARAMS:
  124. if hasattr(configuration, param.name):
  125. globals()[param.name] = getattr(configuration, param.name)
  126. # Validate update repo URL and timeout
  127. if RELEASE_CHECK_URL:
  128. validator = URLValidator(
  129. message=(
  130. "RELEASE_CHECK_URL must be a valid API URL. Example: "
  131. "https://api.github.com/repos/netbox-community/netbox"
  132. )
  133. )
  134. try:
  135. validator(RELEASE_CHECK_URL)
  136. except ValidationError as err:
  137. raise ImproperlyConfigured(str(err))
  138. #
  139. # Database
  140. #
  141. # Only PostgreSQL is supported
  142. if METRICS_ENABLED:
  143. DATABASE.update({
  144. 'ENGINE': 'django_prometheus.db.backends.postgresql'
  145. })
  146. else:
  147. DATABASE.update({
  148. 'ENGINE': 'django.db.backends.postgresql'
  149. })
  150. DATABASES = {
  151. 'default': DATABASE,
  152. }
  153. #
  154. # Media storage
  155. #
  156. if STORAGE_BACKEND is not None:
  157. DEFAULT_FILE_STORAGE = STORAGE_BACKEND
  158. # django-storages
  159. if STORAGE_BACKEND.startswith('storages.'):
  160. try:
  161. import storages.utils # type: ignore
  162. except ModuleNotFoundError as e:
  163. if getattr(e, 'name') == 'storages':
  164. raise ImproperlyConfigured(
  165. f"STORAGE_BACKEND is set to {STORAGE_BACKEND} but django-storages is not present. It can be "
  166. f"installed by running 'pip install django-storages'."
  167. )
  168. raise e
  169. # Monkey-patch django-storages to fetch settings from STORAGE_CONFIG
  170. def _setting(name, default=None):
  171. if name in STORAGE_CONFIG:
  172. return STORAGE_CONFIG[name]
  173. return globals().get(name, default)
  174. storages.utils.setting = _setting
  175. if STORAGE_CONFIG and STORAGE_BACKEND is None:
  176. warnings.warn(
  177. "STORAGE_CONFIG has been set in configuration.py but STORAGE_BACKEND is not defined. STORAGE_CONFIG will be "
  178. "ignored."
  179. )
  180. #
  181. # Redis
  182. #
  183. # Background task queuing
  184. if 'tasks' not in REDIS:
  185. raise ImproperlyConfigured(
  186. "REDIS section in configuration.py is missing the 'tasks' subsection."
  187. )
  188. TASKS_REDIS = REDIS['tasks']
  189. TASKS_REDIS_HOST = TASKS_REDIS.get('HOST', 'localhost')
  190. TASKS_REDIS_PORT = TASKS_REDIS.get('PORT', 6379)
  191. TASKS_REDIS_SENTINELS = TASKS_REDIS.get('SENTINELS', [])
  192. TASKS_REDIS_USING_SENTINEL = all([
  193. isinstance(TASKS_REDIS_SENTINELS, (list, tuple)),
  194. len(TASKS_REDIS_SENTINELS) > 0
  195. ])
  196. TASKS_REDIS_SENTINEL_SERVICE = TASKS_REDIS.get('SENTINEL_SERVICE', 'default')
  197. TASKS_REDIS_SENTINEL_TIMEOUT = TASKS_REDIS.get('SENTINEL_TIMEOUT', 10)
  198. TASKS_REDIS_PASSWORD = TASKS_REDIS.get('PASSWORD', '')
  199. TASKS_REDIS_DATABASE = TASKS_REDIS.get('DATABASE', 0)
  200. TASKS_REDIS_SSL = TASKS_REDIS.get('SSL', False)
  201. TASKS_REDIS_SKIP_TLS_VERIFY = TASKS_REDIS.get('INSECURE_SKIP_TLS_VERIFY', False)
  202. # Caching
  203. if 'caching' not in REDIS:
  204. raise ImproperlyConfigured(
  205. "REDIS section in configuration.py is missing caching subsection."
  206. )
  207. CACHING_REDIS_HOST = REDIS['caching'].get('HOST', 'localhost')
  208. CACHING_REDIS_PORT = REDIS['caching'].get('PORT', 6379)
  209. CACHING_REDIS_DATABASE = REDIS['caching'].get('DATABASE', 0)
  210. CACHING_REDIS_PASSWORD = REDIS['caching'].get('PASSWORD', '')
  211. CACHING_REDIS_SENTINELS = REDIS['caching'].get('SENTINELS', [])
  212. CACHING_REDIS_SENTINEL_SERVICE = REDIS['caching'].get('SENTINEL_SERVICE', 'default')
  213. CACHING_REDIS_PROTO = 'rediss' if REDIS['caching'].get('SSL', False) else 'redis'
  214. CACHING_REDIS_SKIP_TLS_VERIFY = REDIS['caching'].get('INSECURE_SKIP_TLS_VERIFY', False)
  215. CACHES = {
  216. 'default': {
  217. 'BACKEND': 'django_redis.cache.RedisCache',
  218. 'LOCATION': f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_HOST}:{CACHING_REDIS_PORT}/{CACHING_REDIS_DATABASE}',
  219. 'OPTIONS': {
  220. 'CLIENT_CLASS': 'django_redis.client.DefaultClient',
  221. 'PASSWORD': CACHING_REDIS_PASSWORD,
  222. }
  223. }
  224. }
  225. if CACHING_REDIS_SENTINELS:
  226. DJANGO_REDIS_CONNECTION_FACTORY = 'django_redis.pool.SentinelConnectionFactory'
  227. CACHES['default']['LOCATION'] = f'{CACHING_REDIS_PROTO}://{CACHING_REDIS_SENTINEL_SERVICE}/{CACHING_REDIS_DATABASE}'
  228. CACHES['default']['OPTIONS']['CLIENT_CLASS'] = 'django_redis.client.SentinelClient'
  229. CACHES['default']['OPTIONS']['SENTINELS'] = CACHING_REDIS_SENTINELS
  230. if CACHING_REDIS_SKIP_TLS_VERIFY:
  231. CACHES['default']['OPTIONS'].setdefault('CONNECTION_POOL_KWARGS', {})
  232. CACHES['default']['OPTIONS']['CONNECTION_POOL_KWARGS']['ssl_cert_reqs'] = False
  233. #
  234. # Sessions
  235. #
  236. if LOGIN_TIMEOUT is not None:
  237. # Django default is 1209600 seconds (14 days)
  238. SESSION_COOKIE_AGE = LOGIN_TIMEOUT
  239. SESSION_SAVE_EVERY_REQUEST = bool(LOGIN_PERSISTENCE)
  240. if SESSION_FILE_PATH is not None:
  241. SESSION_ENGINE = 'django.contrib.sessions.backends.file'
  242. #
  243. # Email
  244. #
  245. EMAIL_HOST = EMAIL.get('SERVER')
  246. EMAIL_HOST_USER = EMAIL.get('USERNAME')
  247. EMAIL_HOST_PASSWORD = EMAIL.get('PASSWORD')
  248. EMAIL_PORT = EMAIL.get('PORT', 25)
  249. EMAIL_SSL_CERTFILE = EMAIL.get('SSL_CERTFILE')
  250. EMAIL_SSL_KEYFILE = EMAIL.get('SSL_KEYFILE')
  251. EMAIL_SUBJECT_PREFIX = '[NetBox] '
  252. EMAIL_USE_SSL = EMAIL.get('USE_SSL', False)
  253. EMAIL_USE_TLS = EMAIL.get('USE_TLS', False)
  254. EMAIL_TIMEOUT = EMAIL.get('TIMEOUT', 10)
  255. SERVER_EMAIL = EMAIL.get('FROM_EMAIL')
  256. #
  257. # Django
  258. #
  259. INSTALLED_APPS = [
  260. 'django.contrib.admin',
  261. 'django.contrib.auth',
  262. 'django.contrib.contenttypes',
  263. 'django.contrib.sessions',
  264. 'django.contrib.messages',
  265. 'django.contrib.staticfiles',
  266. 'django.contrib.humanize',
  267. 'corsheaders',
  268. 'debug_toolbar',
  269. 'graphiql_debug_toolbar',
  270. 'django_filters',
  271. 'django_tables2',
  272. 'django_prometheus',
  273. 'graphene_django',
  274. 'mptt',
  275. 'rest_framework',
  276. 'social_django',
  277. 'taggit',
  278. 'timezone_field',
  279. 'circuits',
  280. 'dcim',
  281. 'ipam',
  282. 'extras',
  283. 'tenancy',
  284. 'users',
  285. 'utilities',
  286. 'virtualization',
  287. 'wireless',
  288. 'django_rq', # Must come after extras to allow overriding management commands
  289. 'drf_yasg',
  290. ]
  291. # Middleware
  292. MIDDLEWARE = [
  293. 'graphiql_debug_toolbar.middleware.DebugToolbarMiddleware',
  294. 'django_prometheus.middleware.PrometheusBeforeMiddleware',
  295. 'corsheaders.middleware.CorsMiddleware',
  296. 'django.contrib.sessions.middleware.SessionMiddleware',
  297. 'django.middleware.common.CommonMiddleware',
  298. 'django.middleware.csrf.CsrfViewMiddleware',
  299. 'django.contrib.auth.middleware.AuthenticationMiddleware',
  300. 'django.contrib.messages.middleware.MessageMiddleware',
  301. 'django.middleware.clickjacking.XFrameOptionsMiddleware',
  302. 'django.middleware.security.SecurityMiddleware',
  303. 'netbox.middleware.ExceptionHandlingMiddleware',
  304. 'netbox.middleware.RemoteUserMiddleware',
  305. 'netbox.middleware.LoginRequiredMiddleware',
  306. 'netbox.middleware.DynamicConfigMiddleware',
  307. 'netbox.middleware.APIVersionMiddleware',
  308. 'netbox.middleware.ObjectChangeMiddleware',
  309. 'django_prometheus.middleware.PrometheusAfterMiddleware',
  310. ]
  311. ROOT_URLCONF = 'netbox.urls'
  312. TEMPLATES_DIR = BASE_DIR + '/templates'
  313. TEMPLATES = [
  314. {
  315. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  316. 'DIRS': [TEMPLATES_DIR],
  317. 'APP_DIRS': True,
  318. 'OPTIONS': {
  319. 'builtins': [
  320. 'utilities.templatetags.builtins.filters',
  321. 'utilities.templatetags.builtins.tags',
  322. ],
  323. 'context_processors': [
  324. 'django.template.context_processors.debug',
  325. 'django.template.context_processors.request',
  326. 'django.template.context_processors.media',
  327. 'django.contrib.auth.context_processors.auth',
  328. 'django.contrib.messages.context_processors.messages',
  329. 'netbox.context_processors.settings_and_registry',
  330. ],
  331. },
  332. },
  333. ]
  334. # Set up authentication backends
  335. AUTHENTICATION_BACKENDS = [
  336. REMOTE_AUTH_BACKEND,
  337. 'netbox.authentication.ObjectPermissionBackend',
  338. ]
  339. # Internationalization
  340. LANGUAGE_CODE = 'en-us'
  341. USE_I18N = True
  342. USE_L10N = False
  343. USE_TZ = True
  344. USE_DEPRECATED_PYTZ = True
  345. # WSGI
  346. WSGI_APPLICATION = 'netbox.wsgi.application'
  347. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  348. USE_X_FORWARDED_HOST = True
  349. X_FRAME_OPTIONS = 'SAMEORIGIN'
  350. # Static files (CSS, JavaScript, Images)
  351. STATIC_ROOT = BASE_DIR + '/static'
  352. STATIC_URL = f'/{BASE_PATH}static/'
  353. STATICFILES_DIRS = (
  354. os.path.join(BASE_DIR, 'project-static', 'dist'),
  355. os.path.join(BASE_DIR, 'project-static', 'img'),
  356. ('docs', os.path.join(BASE_DIR, 'project-static', 'docs')), # Prefix with /docs
  357. )
  358. # Media
  359. MEDIA_URL = '/{}media/'.format(BASE_PATH)
  360. # Disable default limit of 1000 fields per request. Needed for bulk deletion of objects. (Added in Django 1.10.)
  361. DATA_UPLOAD_MAX_NUMBER_FIELDS = None
  362. # Messages
  363. MESSAGE_TAGS = {
  364. messages.ERROR: 'danger',
  365. }
  366. # Authentication URLs
  367. LOGIN_URL = f'/{BASE_PATH}login/'
  368. LOGIN_REDIRECT_URL = f'/{BASE_PATH}'
  369. DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
  370. TEST_RUNNER = "django_rich.test.RichRunner"
  371. # Exclude potentially sensitive models from wildcard view exemption. These may still be exempted
  372. # by specifying the model individually in the EXEMPT_VIEW_PERMISSIONS configuration parameter.
  373. EXEMPT_EXCLUDE_MODELS = (
  374. ('auth', 'group'),
  375. ('auth', 'user'),
  376. ('users', 'objectpermission'),
  377. )
  378. # All URLs starting with a string listed here are exempt from login enforcement
  379. EXEMPT_PATHS = (
  380. f'/{BASE_PATH}api/',
  381. f'/{BASE_PATH}graphql/',
  382. f'/{BASE_PATH}login/',
  383. f'/{BASE_PATH}oauth/',
  384. f'/{BASE_PATH}metrics',
  385. )
  386. #
  387. # Sentry
  388. #
  389. if SENTRY_ENABLED:
  390. if not SENTRY_DSN:
  391. raise ImproperlyConfigured("SENTRY_ENABLED is True but SENTRY_DSN has not been defined.")
  392. # If using the default DSN, force sampling rates
  393. if SENTRY_DSN == DEFAULT_SENTRY_DSN:
  394. SENTRY_SAMPLE_RATE = 1.0
  395. SENTRY_TRACES_SAMPLE_RATE = 0
  396. # Initialize the SDK
  397. sentry_sdk.init(
  398. dsn=SENTRY_DSN,
  399. release=VERSION,
  400. integrations=[DjangoIntegration()],
  401. sample_rate=SENTRY_SAMPLE_RATE,
  402. traces_sample_rate=SENTRY_TRACES_SAMPLE_RATE,
  403. send_default_pii=True,
  404. http_proxy=HTTP_PROXIES.get('http') if HTTP_PROXIES else None,
  405. https_proxy=HTTP_PROXIES.get('https') if HTTP_PROXIES else None
  406. )
  407. # Assign any configured tags
  408. for k, v in SENTRY_TAGS.items():
  409. sentry_sdk.set_tag(k, v)
  410. # If using the default DSN, append a unique deployment ID tag for error correlation
  411. if SENTRY_DSN == DEFAULT_SENTRY_DSN:
  412. sentry_sdk.set_tag('netbox.deployment_id', DEPLOYMENT_ID)
  413. #
  414. # Django social auth
  415. #
  416. SOCIAL_AUTH_PIPELINE = (
  417. 'social_core.pipeline.social_auth.social_details',
  418. 'social_core.pipeline.social_auth.social_uid',
  419. 'social_core.pipeline.social_auth.social_user',
  420. 'social_core.pipeline.user.get_username',
  421. 'social_core.pipeline.social_auth.associate_by_email',
  422. 'social_core.pipeline.user.create_user',
  423. 'social_core.pipeline.social_auth.associate_user',
  424. 'netbox.authentication.user_default_groups_handler',
  425. 'social_core.pipeline.social_auth.load_extra_data',
  426. 'social_core.pipeline.user.user_details',
  427. )
  428. # Load all SOCIAL_AUTH_* settings from the user configuration
  429. for param in dir(configuration):
  430. if param.startswith('SOCIAL_AUTH_'):
  431. globals()[param] = getattr(configuration, param)
  432. # Force usage of PostgreSQL's JSONB field for extra data
  433. SOCIAL_AUTH_JSONFIELD_ENABLED = True
  434. SOCIAL_AUTH_CLEAN_USERNAME_FUNCTION = 'netbox.users.utils.clean_username'
  435. #
  436. # Django Prometheus
  437. #
  438. PROMETHEUS_EXPORT_MIGRATIONS = False
  439. #
  440. # Django filters
  441. #
  442. FILTERS_NULL_CHOICE_LABEL = 'None'
  443. FILTERS_NULL_CHOICE_VALUE = 'null'
  444. #
  445. # Django REST framework (API)
  446. #
  447. REST_FRAMEWORK_VERSION = '.'.join(VERSION.split('-')[0].split('.')[:2]) # Use major.minor as API version
  448. REST_FRAMEWORK = {
  449. 'ALLOWED_VERSIONS': [REST_FRAMEWORK_VERSION],
  450. 'COERCE_DECIMAL_TO_STRING': False,
  451. 'DEFAULT_AUTHENTICATION_CLASSES': (
  452. 'rest_framework.authentication.SessionAuthentication',
  453. 'netbox.api.authentication.TokenAuthentication',
  454. ),
  455. 'DEFAULT_FILTER_BACKENDS': (
  456. 'django_filters.rest_framework.DjangoFilterBackend',
  457. 'rest_framework.filters.OrderingFilter',
  458. ),
  459. 'DEFAULT_METADATA_CLASS': 'netbox.api.metadata.BulkOperationMetadata',
  460. 'DEFAULT_PAGINATION_CLASS': 'netbox.api.pagination.OptionalLimitOffsetPagination',
  461. 'DEFAULT_PARSER_CLASSES': (
  462. 'rest_framework.parsers.JSONParser',
  463. 'rest_framework.parsers.MultiPartParser',
  464. ),
  465. 'DEFAULT_PERMISSION_CLASSES': (
  466. 'netbox.api.authentication.TokenPermissions',
  467. ),
  468. 'DEFAULT_RENDERER_CLASSES': (
  469. 'rest_framework.renderers.JSONRenderer',
  470. 'netbox.api.renderers.FormlessBrowsableAPIRenderer',
  471. ),
  472. 'DEFAULT_VERSION': REST_FRAMEWORK_VERSION,
  473. 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
  474. 'SCHEMA_COERCE_METHOD_NAMES': {
  475. # Default mappings
  476. 'retrieve': 'read',
  477. 'destroy': 'delete',
  478. # Custom operations
  479. 'bulk_destroy': 'bulk_delete',
  480. },
  481. 'VIEW_NAME_FUNCTION': 'utilities.api.get_view_name',
  482. }
  483. #
  484. # Graphene
  485. #
  486. GRAPHENE = {
  487. # Avoids naming collision on models with 'type' field; see
  488. # https://github.com/graphql-python/graphene-django/issues/185
  489. 'DJANGO_CHOICE_FIELD_ENUM_V3_NAMING': True,
  490. }
  491. #
  492. # drf_yasg (OpenAPI/Swagger)
  493. #
  494. SWAGGER_SETTINGS = {
  495. 'DEFAULT_AUTO_SCHEMA_CLASS': 'utilities.custom_inspectors.NetBoxSwaggerAutoSchema',
  496. 'DEFAULT_FIELD_INSPECTORS': [
  497. 'utilities.custom_inspectors.CustomFieldsDataFieldInspector',
  498. 'utilities.custom_inspectors.NullableBooleanFieldInspector',
  499. 'utilities.custom_inspectors.ChoiceFieldInspector',
  500. 'utilities.custom_inspectors.SerializedPKRelatedFieldInspector',
  501. 'drf_yasg.inspectors.CamelCaseJSONFilter',
  502. 'drf_yasg.inspectors.ReferencingSerializerInspector',
  503. 'drf_yasg.inspectors.RelatedFieldInspector',
  504. 'drf_yasg.inspectors.ChoiceFieldInspector',
  505. 'drf_yasg.inspectors.FileFieldInspector',
  506. 'drf_yasg.inspectors.DictFieldInspector',
  507. 'drf_yasg.inspectors.JSONFieldInspector',
  508. 'drf_yasg.inspectors.SerializerMethodFieldInspector',
  509. 'drf_yasg.inspectors.SimpleFieldInspector',
  510. 'drf_yasg.inspectors.StringDefaultFieldInspector',
  511. ],
  512. 'DEFAULT_FILTER_INSPECTORS': [
  513. 'drf_yasg.inspectors.CoreAPICompatInspector',
  514. ],
  515. 'DEFAULT_INFO': 'netbox.urls.openapi_info',
  516. 'DEFAULT_MODEL_DEPTH': 1,
  517. 'DEFAULT_PAGINATOR_INSPECTORS': [
  518. 'utilities.custom_inspectors.NullablePaginatorInspector',
  519. 'drf_yasg.inspectors.DjangoRestResponsePagination',
  520. 'drf_yasg.inspectors.CoreAPICompatInspector',
  521. ],
  522. 'SECURITY_DEFINITIONS': {
  523. 'Bearer': {
  524. 'type': 'apiKey',
  525. 'name': 'Authorization',
  526. 'in': 'header',
  527. }
  528. },
  529. 'VALIDATOR_URL': None,
  530. }
  531. #
  532. # Django RQ (Webhooks backend)
  533. #
  534. if TASKS_REDIS_USING_SENTINEL:
  535. RQ_PARAMS = {
  536. 'SENTINELS': TASKS_REDIS_SENTINELS,
  537. 'MASTER_NAME': TASKS_REDIS_SENTINEL_SERVICE,
  538. 'DB': TASKS_REDIS_DATABASE,
  539. 'PASSWORD': TASKS_REDIS_PASSWORD,
  540. 'SOCKET_TIMEOUT': None,
  541. 'CONNECTION_KWARGS': {
  542. 'socket_connect_timeout': TASKS_REDIS_SENTINEL_TIMEOUT
  543. },
  544. }
  545. else:
  546. RQ_PARAMS = {
  547. 'HOST': TASKS_REDIS_HOST,
  548. 'PORT': TASKS_REDIS_PORT,
  549. 'DB': TASKS_REDIS_DATABASE,
  550. 'PASSWORD': TASKS_REDIS_PASSWORD,
  551. 'SSL': TASKS_REDIS_SSL,
  552. 'SSL_CERT_REQS': None if TASKS_REDIS_SKIP_TLS_VERIFY else 'required',
  553. 'DEFAULT_TIMEOUT': RQ_DEFAULT_TIMEOUT,
  554. }
  555. RQ_QUEUES = {
  556. 'high': RQ_PARAMS,
  557. 'default': RQ_PARAMS,
  558. 'low': RQ_PARAMS,
  559. }
  560. #
  561. # Plugins
  562. #
  563. for plugin_name in PLUGINS:
  564. # Import plugin module
  565. try:
  566. plugin = importlib.import_module(plugin_name)
  567. except ModuleNotFoundError as e:
  568. if getattr(e, 'name') == plugin_name:
  569. raise ImproperlyConfigured(
  570. "Unable to import plugin {}: Module not found. Check that the plugin module has been installed within the "
  571. "correct Python environment.".format(plugin_name)
  572. )
  573. raise e
  574. # Determine plugin config and add to INSTALLED_APPS.
  575. try:
  576. plugin_config: PluginConfig = plugin.config
  577. except AttributeError:
  578. raise ImproperlyConfigured(
  579. "Plugin {} does not provide a 'config' variable. This should be defined in the plugin's __init__.py file "
  580. "and point to the PluginConfig subclass.".format(plugin_name)
  581. )
  582. plugin_module = "{}.{}".format(plugin_config.__module__, plugin_config.__name__) # type: ignore
  583. # Gather additional apps to load alongside this plugin
  584. django_apps = plugin_config.django_apps
  585. if plugin_name in django_apps:
  586. django_apps.pop(plugin_name)
  587. if plugin_module not in django_apps:
  588. django_apps.append(plugin_module)
  589. # Test if we can import all modules (or its parent, for PluginConfigs and AppConfigs)
  590. for app in django_apps:
  591. if "." in app:
  592. parts = app.split(".")
  593. spec = importlib.util.find_spec(".".join(parts[:-1]))
  594. else:
  595. spec = importlib.util.find_spec(app)
  596. if spec is None:
  597. raise ImproperlyConfigured(
  598. f"Failed to load django_apps specified by plugin {plugin_name}: {django_apps} "
  599. f"The module {app} cannot be imported. Check that the necessary package has been "
  600. "installed within the correct Python environment."
  601. )
  602. INSTALLED_APPS.extend(django_apps)
  603. # Preserve uniqueness of the INSTALLED_APPS list, we keep the last occurence
  604. sorted_apps = reversed(list(dict.fromkeys(reversed(INSTALLED_APPS))))
  605. INSTALLED_APPS = list(sorted_apps)
  606. # Validate user-provided configuration settings and assign defaults
  607. if plugin_name not in PLUGINS_CONFIG:
  608. PLUGINS_CONFIG[plugin_name] = {}
  609. plugin_config.validate(PLUGINS_CONFIG[plugin_name], VERSION)
  610. # Add middleware
  611. plugin_middleware = plugin_config.middleware
  612. if plugin_middleware and type(plugin_middleware) in (list, tuple):
  613. MIDDLEWARE.extend(plugin_middleware)
  614. # Create RQ queues dedicated to the plugin
  615. # we use the plugin name as a prefix for queue name's defined in the plugin config
  616. # ex: mysuperplugin.mysuperqueue1
  617. if type(plugin_config.queues) is not list:
  618. raise ImproperlyConfigured(
  619. "Plugin {} queues must be a list.".format(plugin_name)
  620. )
  621. RQ_QUEUES.update({
  622. f"{plugin_name}.{queue}": RQ_PARAMS for queue in plugin_config.queues
  623. })