settings.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. """
  2. Django settings for GlitchTip project.
  3. For more information on this file, see
  4. https://docs.djangoproject.com/en/dev/topics/settings/
  5. For the full list of settings and their values, see
  6. https://docs.djangoproject.com/en/dev/ref/settings/
  7. """
  8. import logging
  9. import os
  10. import sys
  11. import warnings
  12. from datetime import timedelta
  13. import environ
  14. import sentry_sdk
  15. from celery.schedules import crontab
  16. from corsheaders.defaults import default_headers
  17. from django.conf import global_settings
  18. from django.core.exceptions import ImproperlyConfigured
  19. from django.http import UnreadablePostError
  20. from sentry_sdk.integrations.django import DjangoIntegration
  21. env = environ.FileAwareEnv(
  22. ALLOWED_HOSTS=(list, ["*"]),
  23. DEFAULT_FILE_STORAGE=(str, global_settings.STORAGES["default"]["BACKEND"]),
  24. AWS_ACCESS_KEY_ID=(str, None),
  25. AWS_SECRET_ACCESS_KEY=(str, None),
  26. AWS_STORAGE_BUCKET_NAME=(str, None),
  27. AWS_S3_ENDPOINT_URL=(str, None),
  28. AWS_LOCATION=(str, ""),
  29. AZURE_ACCOUNT_NAME=(str, None),
  30. AZURE_ACCOUNT_KEY=(str, None),
  31. AZURE_CONTAINER=(str, None),
  32. AZURE_URL_EXPIRATION_SECS=(int, None),
  33. IS_LOAD_TEST=(bool, False),
  34. GS_BUCKET_NAME=(str, None),
  35. GS_PROJECT_ID=(str, None),
  36. DEBUG=(bool, False),
  37. DEBUG_TOOLBAR=(bool, False),
  38. STATIC_URL=(str, "/"),
  39. ENABLE_OBSERVABILITY_API=(bool, False),
  40. )
  41. path = environ.Path()
  42. # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
  43. BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  44. # Quick-start development settings - unsuitable for production
  45. # See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/
  46. # SECURITY WARNING: keep the secret key used in production secret!
  47. SECRET_KEY = env.str("SECRET_KEY", "change_me")
  48. # SECURITY WARNING: don't run with debug turned on in production!
  49. DEBUG = env("DEBUG")
  50. # Enable only for running end to end testing. Debug must be True to use.
  51. ENABLE_TEST_API = env.bool("ENABLE_TEST_API", False)
  52. if DEBUG is False:
  53. ENABLE_TEST_API = False
  54. if DEBUG and ENABLE_TEST_API:
  55. ACCOUNT_RATE_LIMITS = False # Disable for e2e tests
  56. ALLOWED_HOSTS = env("ALLOWED_HOSTS")
  57. # Necessary for kubernetes health checks
  58. POD_IP = env.str("POD_IP", default=None)
  59. if POD_IP:
  60. ALLOWED_HOSTS.append(POD_IP)
  61. ENVIRONMENT = env.str("ENVIRONMENT", None)
  62. GLITCHTIP_VERSION = env.str("GLITCHTIP_VERSION", "0.0.0-unknown")
  63. # Used in email and DSN generation. Set to full domain such as https://glitchtip.example.com
  64. default_url = env.str(
  65. "APP_URL", env.str("GLITCHTIP_DOMAIN", "http://localhost:8000")
  66. ) # DigitalOcean App Platform uses APP_URL
  67. GLITCHTIP_URL = env.url("GLITCHTIP_URL", default_url)
  68. if GLITCHTIP_URL.scheme not in ["http", "https"]:
  69. raise ImproperlyConfigured("GLITCHTIP_DOMAIN must start with http or https")
  70. # Is running unit test
  71. TESTING = len(sys.argv) > 1 and sys.argv[1] == "test"
  72. DATA_UPLOAD_MAX_MEMORY_SIZE = 4294967295 # TMP REMOVE THIS
  73. DATA_UPLOAD_MAX_NUMBER_FIELDS = env.int(
  74. "DATA_UPLOAD_MAX_NUMBER_FIELDS",
  75. default=global_settings.DATA_UPLOAD_MAX_NUMBER_FIELDS,
  76. )
  77. # Limits size (in bytes) of uncompressed event payloads. Mitigates DOS risk.
  78. GLITCHTIP_MAX_UNZIPPED_PAYLOAD_SIZE = env.int(
  79. "GLITCHTIP_MAX_UNZIPPED_PAYLOAD_SIZE", global_settings.DATA_UPLOAD_MAX_MEMORY_SIZE
  80. )
  81. # Events and associated data older than this will be deleted from the database
  82. GLITCHTIP_MAX_EVENT_LIFE_DAYS = env.int("GLITCHTIP_MAX_EVENT_LIFE_DAYS", default=90)
  83. GLITCHTIP_MAX_UPTIME_CHECK_LIFE_DAYS = env.int(
  84. "GLITCHTIP_MAX_UPTIME_CHECK_LIFE_DAYS", default=GLITCHTIP_MAX_EVENT_LIFE_DAYS
  85. )
  86. GLITCHTIP_MAX_TRANSACTION_EVENT_LIFE_DAYS = env.int(
  87. "GLITCHTIP_MAX_TRANSACTION_EVENT_LIFE_DAYS", default=GLITCHTIP_MAX_EVENT_LIFE_DAYS
  88. )
  89. GLITCHTIP_MAX_FILE_LIFE_DAYS = env.int(
  90. "GLITCHTIP_MAX_EVENT_LIFE_DAYS", default=GLITCHTIP_MAX_EVENT_LIFE_DAYS
  91. )
  92. # Check if a throttle is needed 1 out of every 5000 event requests
  93. GLITCHTIP_THROTTLE_CHECK_INTERVAL = env.int("GLITCHTIP_THROTTLE_CHECK_INTERVAL", 5000)
  94. # Freezes acceptance of new events, for use during db maintenance
  95. MAINTENANCE_EVENT_FREEZE = env.bool("MAINTENANCE_EVENT_FREEZE", False)
  96. # For development purposes only, prints out inbound event store json
  97. EVENT_STORE_DEBUG = env.bool("EVENT_STORE_DEBUG", False)
  98. STATIC_URL = "static/"
  99. # Base HREF, such as example.com/glitchtip/ where BASE_PATH would be "/glitchtip"
  100. if "BASE_PATH" in os.environ or "FORCE_SCRIPT_NAME" in os.environ:
  101. FORCE_SCRIPT_NAME = env.str("BASE_PATH", env.str("FORCE_SCRIPT_NAME", ""))
  102. # GlitchTip can track GlitchTip's own errors.
  103. # If enabling this, use a different server to avoid infinite loops.
  104. def before_send(event, hint):
  105. """Don't log useless, inactionable errors in Sentry."""
  106. if "log_record" in hint:
  107. if hint["log_record"].name == "django.security.DisallowedHost":
  108. return None
  109. if "exc_info" in hint:
  110. _, exc_value, _ = hint["exc_info"]
  111. if isinstance(exc_value, UnreadablePostError):
  112. return None
  113. return event
  114. SENTRY_DSN = env.str("SENTRY_DSN", None)
  115. # Optionally allow a different DSN for the frontend
  116. SENTRY_FRONTEND_DSN = env.str("SENTRY_FRONTEND_DSN", SENTRY_DSN)
  117. # Set sample_rate to 1.0 to capture 100%.
  118. SENTRY_SAMPLE_RATE = env.float("SENTRY_SAMPLE_RATE", 1.0)
  119. # Set traces_sample_rate to 1.0 to capture 100%. Recommended to keep this value low.
  120. SENTRY_TRACES_SAMPLE_RATE = env.float("SENTRY_TRACES_SAMPLE_RATE", 0.01)
  121. # Ignore whitenoise served static routes
  122. def traces_sampler(sampling_context):
  123. if (
  124. sampling_context.get("wsgi_environ", {})
  125. .get("PATH_INFO", "")
  126. .startswith(STATIC_URL)
  127. ):
  128. return 0.0
  129. return SENTRY_TRACES_SAMPLE_RATE
  130. if SENTRY_DSN:
  131. release = "glitchtip@" + GLITCHTIP_VERSION if GLITCHTIP_VERSION else None
  132. sentry_sdk.init(
  133. dsn=SENTRY_DSN,
  134. integrations=[DjangoIntegration()],
  135. before_send=before_send,
  136. release=release,
  137. environment=ENVIRONMENT,
  138. auto_session_tracking=False,
  139. send_client_reports=False,
  140. sample_rate=SENTRY_SAMPLE_RATE,
  141. traces_sample_rate=SENTRY_TRACES_SAMPLE_RATE,
  142. traces_sampler=traces_sampler,
  143. )
  144. def show_toolbar(request):
  145. return env("DEBUG_TOOLBAR")
  146. DEBUG_TOOLBAR = env("DEBUG_TOOLBAR")
  147. DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": show_toolbar}
  148. DEBUG_TOOLBAR_PANELS = [
  149. "debug_toolbar.panels.versions.VersionsPanel",
  150. "debug_toolbar.panels.timer.TimerPanel",
  151. "debug_toolbar.panels.settings.SettingsPanel",
  152. "debug_toolbar.panels.headers.HeadersPanel",
  153. "debug_toolbar.panels.request.RequestPanel",
  154. "debug_toolbar.panels.sql.SQLPanel",
  155. # "debug_toolbar.panels.history.HistoryPanel",
  156. # "debug_toolbar.panels.profiling.ProfilingPanel",
  157. ]
  158. # Application definition
  159. # Conditionally load to workaround unnecessary memory usage in celery/beat
  160. WEB_INSTALLED_APPS = [
  161. "django.contrib.admin",
  162. "django.contrib.messages",
  163. "django.contrib.staticfiles",
  164. "ninja",
  165. ]
  166. INSTALLED_APPS = [
  167. "django.contrib.auth",
  168. "django.contrib.contenttypes",
  169. "django.contrib.humanize",
  170. "psqlextra",
  171. "django_prometheus",
  172. "allauth",
  173. "allauth.account",
  174. "allauth.headless",
  175. "allauth.mfa",
  176. "allauth.socialaccount",
  177. "allauth.socialaccount.providers.digitalocean",
  178. "allauth.socialaccount.providers.gitea",
  179. "allauth.socialaccount.providers.github",
  180. "allauth.socialaccount.providers.gitlab",
  181. "allauth.socialaccount.providers.google",
  182. "allauth.socialaccount.providers.microsoft",
  183. "allauth.socialaccount.providers.nextcloud",
  184. "allauth.socialaccount.providers.openid_connect",
  185. "allauth.socialaccount.providers.okta",
  186. "anymail",
  187. "django_rest_mfa",
  188. "corsheaders",
  189. "django_extensions",
  190. ]
  191. if DEBUG_TOOLBAR:
  192. INSTALLED_APPS.append("debug_toolbar")
  193. INSTALLED_APPS += [
  194. "storages",
  195. "glitchtip",
  196. "apps.alerts",
  197. "apps.environments",
  198. "apps.organizations_ext",
  199. "events",
  200. "issues",
  201. "apps.users",
  202. "apps.importer",
  203. "apps.uptime",
  204. "apps.performance",
  205. "apps.projects",
  206. "apps.teams",
  207. "apps.releases",
  208. "apps.sourcecode",
  209. "apps.difs",
  210. "apps.api_tokens",
  211. "apps.files",
  212. "apps.issue_events",
  213. "apps.event_ingest",
  214. "import_export", # Contains import management command, keep under apps.importer
  215. ]
  216. IS_CELERY = env.bool("IS_CELERY", False)
  217. if not IS_CELERY:
  218. INSTALLED_APPS = WEB_INSTALLED_APPS + INSTALLED_APPS
  219. # Ensure no one uses runsslserver in production
  220. if SECRET_KEY == "change_me" and DEBUG is True:
  221. INSTALLED_APPS += ["sslserver"]
  222. ENABLE_OBSERVABILITY_API = env("ENABLE_OBSERVABILITY_API")
  223. # Workaround https://github.com/korfuri/django-prometheus/issues/34
  224. PROMETHEUS_EXPORT_MIGRATIONS = False
  225. # https://github.com/korfuri/django-prometheus/blob/master/documentation/exports.md#exporting-metrics-in-a-wsgi-application-with-multiple-processes-per-process
  226. if start_port := env.int("METRICS_START_PORT", None):
  227. PROMETHEUS_METRICS_EXPORT_PORT_RANGE = range(
  228. start_port, start_port + env.int("UWSGI_WORKERS", 1)
  229. )
  230. MIDDLEWARE = [
  231. "django.middleware.security.SecurityMiddleware",
  232. "django.contrib.sessions.middleware.SessionMiddleware",
  233. "corsheaders.middleware.CorsMiddleware",
  234. "csp.middleware.CSPMiddleware",
  235. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  236. "whitenoise.middleware.WhiteNoiseMiddleware",
  237. ]
  238. if DEBUG_TOOLBAR:
  239. MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")
  240. MIDDLEWARE += [
  241. "django.middleware.common.CommonMiddleware",
  242. "django.middleware.csrf.CsrfViewMiddleware",
  243. "django.contrib.auth.middleware.AuthenticationMiddleware",
  244. "django.contrib.messages.middleware.MessageMiddleware",
  245. "django.middleware.clickjacking.XFrameOptionsMiddleware",
  246. "sentry.middleware.proxy.DecompressBodyMiddleware",
  247. "django.middleware.locale.LocaleMiddleware",
  248. "allauth.account.middleware.AccountMiddleware",
  249. ]
  250. if ENABLE_OBSERVABILITY_API:
  251. MIDDLEWARE.insert(0, "django_prometheus.middleware.PrometheusBeforeMiddleware")
  252. MIDDLEWARE.append("django_prometheus.middleware.PrometheusAfterMiddleware")
  253. ROOT_URLCONF = "glitchtip.urls"
  254. TEMPLATES = [
  255. {
  256. "BACKEND": "django.template.backends.django.DjangoTemplates",
  257. "DIRS": [path("dist"), path("templates")],
  258. "APP_DIRS": True,
  259. "OPTIONS": {
  260. "context_processors": [
  261. "django.template.context_processors.debug",
  262. "django.template.context_processors.request",
  263. "django.contrib.auth.context_processors.auth",
  264. "django.contrib.messages.context_processors.messages",
  265. ],
  266. },
  267. },
  268. ]
  269. WSGI_APPLICATION = "glitchtip.wsgi.application"
  270. CORS_ORIGIN_ALLOW_ALL = env.bool("CORS_ORIGIN_ALLOW_ALL", True)
  271. CORS_ORIGIN_WHITELIST = env.tuple("CORS_ORIGIN_WHITELIST", str, default=())
  272. CORS_ALLOW_HEADERS = list(default_headers) + [
  273. "x-sentry-auth",
  274. "baggage",
  275. "sentry-trace",
  276. ]
  277. BILLING_ENABLED = False
  278. if env.str("STRIPE_TEST_PUBLIC_KEY", None) or env.str("STRIPE_LIVE_PUBLIC_KEY", None):
  279. BILLING_ENABLED = True
  280. # Set to chatwoot website token to enable live help widget. Assumes app.chatwoot.com.
  281. CHATWOOT_WEBSITE_TOKEN = env.str("CHATWOOT_WEBSITE_TOKEN", None)
  282. CHATWOOT_IDENTITY_TOKEN = env.str("CHATWOOT_IDENTITY_TOKEN", None)
  283. CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", str, [])
  284. SECURE_BROWSER_XSS_FILTER = True
  285. CSP_DEFAULT_SRC = env.list("CSP_DEFAULT_SRC", str, ["'self'"])
  286. if "CSP_STYLE_SRC" in os.environ:
  287. CSP_STYLE_SRC = env.list("CSP_STYLE_SRC", str)
  288. if "CSP_STYLE_SRC_ELEM" in os.environ:
  289. CSP_STYLE_SRC_ELEM = env.list("CSP_STYLE_SRC_ELEM", str)
  290. CSP_FONT_SRC = env.list("CSP_FONT_SRC", str, ["'self'", "data:"])
  291. if "CSP_WORKER_SRC" in os.environ:
  292. CSP_WORKER_SRC = env.list("CSP_WORKER_SRC", str)
  293. # Enable Chatwoot only when configured
  294. default_connect_src = ["'self'", "https://*.glitchtip.com"]
  295. if CHATWOOT_WEBSITE_TOKEN:
  296. default_connect_src.append("https://app.chatwoot.com")
  297. CSP_CONNECT_SRC = env.list("CSP_CONNECT_SRC", str, default_connect_src)
  298. # Enable stripe by default only when configured
  299. stripe_domain = "https://js.stripe.com"
  300. default_script_src = [
  301. "'self'",
  302. "https://*.glitchtip.com",
  303. "'sha256-iRcDQ27XiXX4k+jbJ8nGeQFBnBOjmII7FdMlixb6QE4='", # Theme picker inline JS
  304. ]
  305. default_frame_src = ["'self'"]
  306. if BILLING_ENABLED:
  307. default_script_src.append(stripe_domain)
  308. default_frame_src.append(stripe_domain)
  309. CSP_SCRIPT_SRC = env.list("CSP_SCRIPT_SRC", str, default_script_src)
  310. CSP_IMG_SRC = env.list("CSP_IMG_SRC", str, ["'self'"])
  311. CSP_FRAME_SRC = env.list("CSP_FRAME_SRC", str, default_frame_src)
  312. # Consider tracking CSP reports with GlitchTip itself
  313. CSP_REPORT_URI = env.tuple("CSP_REPORT_URI", str, None)
  314. CSP_REPORT_ONLY = env.bool("CSP_REPORT_ONLY", False)
  315. SECURE_HSTS_SECONDS = env.int("SECURE_HSTS_SECONDS", 0)
  316. SECURE_HSTS_PRELOAD = env.bool("SECURE_HSTS_PRELOAD", False)
  317. SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool("SECURE_HSTS_INCLUDE_SUBDOMAINS", False)
  318. SESSION_COOKIE_SECURE = env.bool("SESSION_COOKIE_SECURE", False)
  319. SESSION_COOKIE_SAMESITE = env.str("SESSION_COOKIE_SAMESITE", "Lax")
  320. DEFAULT_FROM_EMAIL = env.str("DEFAULT_FROM_EMAIL", "webmaster@localhost")
  321. ANYMAIL_SETTINGS = [
  322. "MAILGUN_API_KEY",
  323. "MAILGUN_SENDER_DOMAIN",
  324. "MAILGUN_API_URL",
  325. "MAILGUN_WEBHOOK_SIGNING_KEY",
  326. "SENDGRID_API_KEY",
  327. "SENDGRID_API_URL",
  328. "POSTMARK_SERVER_TOKEN",
  329. "POSTMARK_API_URL",
  330. "MANDRILL_API_KEY",
  331. "MANDRILL_WEBHOOK_KEY",
  332. "MANDRILL_WEBHOOK_URL",
  333. "MANDRILL_API_URL",
  334. "SENDINBLUE_API_KEY",
  335. "SENDINBLUE_API_URL",
  336. "MAILJET_API_KEY",
  337. "MAILJET_SECRET_KEY",
  338. "MAILJET_API_URL",
  339. "POSTAL_API_KEY",
  340. "POSTAL_API_URL",
  341. "POSTAL_WEBHOOK_KEY",
  342. "SPARKPOST_API_KEY",
  343. "SPARKPOST_API_URL",
  344. "SPARKPOST_TRACK_INITIAL_OPEN_AS_OPENED",
  345. ]
  346. ANYMAIL = {
  347. anymail_var: env.str(anymail_var)
  348. for anymail_var in ANYMAIL_SETTINGS
  349. if anymail_var in os.environ
  350. }
  351. ACCOUNT_EMAIL_SUBJECT_PREFIX = env.str("ACCOUNT_EMAIL_SUBJECT_PREFIX", "")
  352. # Database
  353. # https://docs.djangoproject.com/en/dev/ref/settings/#databases
  354. DATABASES = {
  355. "default": env.db(default="postgres://postgres:postgres@postgres:5432/postgres")
  356. }
  357. # Support setting DATABASES in parts in order to get values from the postgresql helm chart
  358. DATABASE_HOST = env.str("DATABASE_HOST", None)
  359. DATABASE_PASSWORD = env.str("DATABASE_PASSWORD", None)
  360. if DATABASE_HOST and DATABASE_PASSWORD:
  361. DATABASES["default"] = {
  362. "NAME": env.str("DATABASE_NAME", "postgres"),
  363. "USER": env.str("DATABASE_USER", "postgres"),
  364. "PASSWORD": DATABASE_PASSWORD,
  365. "HOST": DATABASE_HOST,
  366. "PORT": env.str("DATABASE_PORT", "5432"),
  367. "CONN_MAX_AGE": env.int("DATABASE_CONN_MAX_AGE", 0),
  368. "CONN_HEALTH_CHECKS": env.bool("DATABASE_CONN_HEALTH_CHECKS", False),
  369. }
  370. DATABASES["default"]["ENGINE"] = "psqlextra.backend"
  371. if not DATABASES["default"].get("OPTIONS"):
  372. DATABASES["default"]["OPTIONS"] = {}
  373. # Enable pool by default, if there is no conn_max_age
  374. if DATABASES["default"].get("CONN_MAX_AGE", 0) == 0:
  375. if env.bool("DATABASE_POOL", True):
  376. pool_options = {}
  377. min_size = env.int("DATABASE_POOL_MIN_SIZE", 2)
  378. max_size = env.int("DATABASE_POOL_MAX_SIZE", 6)
  379. if min_size:
  380. pool_options["min_size"] = min_size
  381. if max_size:
  382. pool_options["max_size"] = max_size
  383. if pool_options:
  384. DATABASES["default"]["OPTIONS"]["pool"] = pool_options
  385. else:
  386. DATABASES["default"]["OPTIONS"]["pool"] = True
  387. PSQLEXTRA_PARTITIONING_MANAGER = "glitchtip.partitioning.manager"
  388. DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
  389. # We need to support both url and broken out host to support helm redis chart
  390. REDIS_HOST = env.str("REDIS_HOST", None)
  391. if REDIS_HOST:
  392. REDIS_PORT = env.str("REDIS_PORT", "6379")
  393. REDIS_DATABASE = env.str("REDIS_DATABASE", "0")
  394. REDIS_PASSWORD = env.str("REDIS_PASSWORD", None)
  395. if REDIS_PASSWORD:
  396. REDIS_URL = (
  397. f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}/{REDIS_DATABASE}"
  398. )
  399. else:
  400. REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DATABASE}"
  401. else:
  402. REDIS_URL = env.str("REDIS_URL", "redis://redis:6379/0")
  403. REDIS_RETRY = env.bool("REDIS_RETRY", True)
  404. REDIS_MAX_CONNECTIONS = env.int("REDIS_MAX_CONNECTIONS", 100)
  405. CELERY_BROKER_URL = env.str("CELERY_BROKER_URL", REDIS_URL)
  406. CELERY_BROKER_TRANSPORT_OPTIONS = {
  407. "fanout_prefix": True,
  408. "fanout_patterns": True,
  409. "retry_on_timeout": REDIS_RETRY,
  410. "max_connections": REDIS_MAX_CONNECTIONS,
  411. }
  412. CELERY_REDIS_RETRY_ON_TIMEOUT = REDIS_RETRY
  413. CELERY_REDIS_MAX_CONNECTIONS = REDIS_MAX_CONNECTIONS
  414. if CELERY_BROKER_URL.startswith("sentinel"):
  415. CELERY_BROKER_TRANSPORT_OPTIONS["master_name"] = env.str(
  416. "CELERY_BROKER_MASTER_NAME", "mymaster"
  417. )
  418. IS_LOAD_TEST = env("IS_LOAD_TEST")
  419. # GlitchTip doesn't require a celery result backend
  420. if IS_LOAD_TEST:
  421. CELERY_RESULT_BACKEND = REDIS_URL
  422. if socket_timeout := env.int("CELERY_BROKER_SOCKET_TIMEOUT", None):
  423. CELERY_BROKER_TRANSPORT_OPTIONS["socket_timeout"] = socket_timeout
  424. if broker_sentinel_password := env.str("CELERY_BROKER_SENTINEL_KWARGS_PASSWORD", None):
  425. CELERY_BROKER_TRANSPORT_OPTIONS["sentinel_kwargs"] = {
  426. "password": broker_sentinel_password
  427. }
  428. # Time in seconds to debounce some frequently run tasks
  429. TASK_DEBOUNCE_DELAY = env.int("TASK_DEBOUNCE_DELAY", 30)
  430. UPTIME_CHECK_INTERVAL = 10
  431. ALERT_NOTIFICATION_INTERVAL = env.int("ALERT_NOTIFICATION_INTERVAL", 60)
  432. CELERY_BEAT_SCHEDULE = {
  433. "send-alert-notifications": {
  434. "task": "apps.alerts.tasks.process_event_alerts",
  435. "schedule": ALERT_NOTIFICATION_INTERVAL,
  436. },
  437. "perform-maintenance": {
  438. "task": "glitchtip.tasks.perform_maintenance",
  439. "schedule": crontab(hour=5, minute=0),
  440. },
  441. "uptime-dispatch-checks": {
  442. "task": "apps.uptime.tasks.dispatch_checks",
  443. "schedule": UPTIME_CHECK_INTERVAL,
  444. },
  445. }
  446. # Maximum number of issues send in a single alert payload
  447. MAX_ISSUES_PER_ALERT = env.int("MAX_ISSUES_PER_ALERT", 3)
  448. if os.environ.get("CACHE_URL"):
  449. CACHES = {
  450. "default": env.cache(),
  451. }
  452. else: # Default to REDIS when unset
  453. CACHES = {
  454. "default": {
  455. "BACKEND": "django_redis.cache.RedisCache",
  456. "LOCATION": REDIS_URL,
  457. "PARSER_CLASS": "redis.connection.HiredisParser",
  458. "OPTIONS": {
  459. "CONNECTION_POOL_KWARGS": {
  460. "retry_on_timeout": REDIS_RETRY,
  461. "max_connections": REDIS_MAX_CONNECTIONS,
  462. }
  463. },
  464. }
  465. }
  466. if cache_sentinel_url := env.str("CACHE_SENTINEL_URL", None):
  467. try:
  468. cache_sentinel_host, cache_sentinel_port = cache_sentinel_url.split(":")
  469. SENTINELS = [(cache_sentinel_host, int(cache_sentinel_port))]
  470. except ValueError as err:
  471. raise ImproperlyConfigured(
  472. "Invalid cache redis sentinel url, format is host:port"
  473. ) from err
  474. DJANGO_REDIS_CONNECTION_FACTORY = "django_redis.pool.SentinelConnectionFactory"
  475. CACHES["default"]["OPTIONS"]["SENTINELS"] = SENTINELS
  476. if cache_sentinel_password := env.str("CACHE_SENTINEL_PASSWORD", None):
  477. CACHES["default"]["OPTIONS"]["SENTINEL_KWARGS"] = {
  478. "password": cache_sentinel_password
  479. }
  480. SESSION_ENGINE = "django.contrib.sessions.backends.cache"
  481. SESSION_COOKIE_AGE = env.int("SESSION_COOKIE_AGE", global_settings.SESSION_COOKIE_AGE)
  482. # Password validation
  483. # https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators
  484. AUTH_PASSWORD_VALIDATORS = [
  485. {
  486. "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
  487. },
  488. {
  489. "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
  490. },
  491. {
  492. "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
  493. },
  494. {
  495. "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
  496. },
  497. ]
  498. # Internationalization
  499. # https://docs.djangoproject.com/en/dev/topics/i18n/
  500. LANGUAGE_CODE = "en-us"
  501. TIME_ZONE = "UTC"
  502. USE_I18N = True
  503. USE_TZ = True
  504. STORAGES = {
  505. "default": {
  506. "BACKEND": env("DEFAULT_FILE_STORAGE"),
  507. },
  508. "staticfiles": {
  509. "BACKEND": env.str(
  510. "STATICFILES_STORAGE",
  511. "whitenoise.storage.CompressedManifestStaticFilesStorage",
  512. )
  513. },
  514. }
  515. AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID")
  516. AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY")
  517. AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME")
  518. AWS_S3_ENDPOINT_URL = env("AWS_S3_ENDPOINT_URL")
  519. AWS_LOCATION = env("AWS_LOCATION")
  520. AZURE_ACCOUNT_NAME = env("AZURE_ACCOUNT_NAME")
  521. AZURE_ACCOUNT_KEY = env("AZURE_ACCOUNT_KEY")
  522. AZURE_CONTAINER = env("AZURE_CONTAINER")
  523. AZURE_URL_EXPIRATION_SECS = env("AZURE_URL_EXPIRATION_SECS")
  524. GS_BUCKET_NAME = env("GS_BUCKET_NAME")
  525. GS_PROJECT_ID = env("GS_PROJECT_ID")
  526. if AWS_S3_ENDPOINT_URL:
  527. MEDIA_URL = env.str(
  528. "MEDIA_URL", "https://%s/%s/" % (AWS_S3_ENDPOINT_URL, AWS_LOCATION)
  529. )
  530. STORAGES["default"] = {"BACKEND": "storages.backends.s3boto3.S3Boto3Storage"}
  531. else:
  532. MEDIA_URL = "media/"
  533. MEDIA_ROOT = env.str("MEDIA_ROOT", "")
  534. STATICFILES_DIRS = [
  535. "assets",
  536. "dist",
  537. ]
  538. STATIC_ROOT = path("static/")
  539. EMAIL_BACKEND = env.str(
  540. "EMAIL_BACKEND", default="django.core.mail.backends.smtp.EmailBackend"
  541. )
  542. if os.getenv("EMAIL_HOST_USER"):
  543. EMAIL_HOST_USER = env.str("EMAIL_HOST_USER")
  544. if os.getenv("EMAIL_HOST_PASSWORD"):
  545. EMAIL_HOST_PASSWORD = env.str("EMAIL_HOST_PASSWORD")
  546. if os.getenv("EMAIL_HOST"):
  547. EMAIL_HOST = env.str("EMAIL_HOST")
  548. if os.getenv("EMAIL_PORT"):
  549. EMAIL_PORT = env.str("EMAIL_PORT")
  550. if os.getenv("EMAIL_USE_TLS"):
  551. EMAIL_USE_TLS = env.bool("EMAIL_USE_TLS")
  552. if os.getenv("EMAIL_USE_SSL"):
  553. EMAIL_USE_SSL = env.bool("EMAIL_USE_SSL")
  554. if os.getenv("EMAIL_TIMEOUT"):
  555. EMAIL_TIMEOUT = env.int("EMAIL_TIMEOUT")
  556. if os.getenv("EMAIL_FILE_PATH"):
  557. EMAIL_FILE_PATH = env.str("EMAIL_FILE_PATH")
  558. if os.getenv(
  559. "EMAIL_URL"
  560. ): # Careful, this will override most EMAIL_*** settings. Set them all individually, or use EMAIL_URL to set them all at once, but don't do both.
  561. EMAIL_CONFIG = env.email_url("EMAIL_URL")
  562. vars().update(EMAIL_CONFIG)
  563. AUTH_USER_MODEL = "users.User"
  564. ACCOUNT_ADAPTER = "glitchtip.adapters.CustomDefaultAccountAdapter"
  565. ACCOUNT_AUTHENTICATION_METHOD = "email"
  566. ACCOUNT_EMAIL_REQUIRED = True
  567. ACCOUNT_USERNAME_REQUIRED = False
  568. ACCOUNT_USER_MODEL_USERNAME_FIELD = None
  569. ACCOUNT_REAUTHENTICATION_TIMEOUT = SESSION_COOKIE_AGE # Disabled for now
  570. LOGIN_REDIRECT_URL = "/"
  571. LOGIN_URL = "/login"
  572. # This config will later default to True and then be removed
  573. USE_NEW_SOCIAL_CALLBACKS = env.bool("USE_NEW_SOCIAL_CALLBACKS", False)
  574. HEADLESS_ONLY = True
  575. HEADLESS_FRONTEND_URLS = {
  576. "account_signup": "/login",
  577. "account_reset_password": "/reset-password",
  578. "account_confirm_email": "/profile/confirm-email/{key}/",
  579. "account_reset_password_from_key": "/reset-password/set-new-password/{key}",
  580. "socialaccount_login_error": "/login?socialLoginError=true",
  581. }
  582. MFA_TOTP_ISSUER = GLITCHTIP_URL.hostname
  583. MFA_TOTP_TOLERANCE = 1
  584. MFA_SUPPORTED_TYPES = ["totp", "webauthn", "recovery_codes"]
  585. MFA_PASSKEY_LOGIN_ENABLED = True
  586. MFA_WEBAUTHN_ALLOW_INSECURE_ORIGIN = DEBUG
  587. SOCIALACCOUNT_ADAPTER = "glitchtip.adapters.CustomSocialAccountAdapter"
  588. INVITATION_BACKEND = "apps.organizations_ext.invitation_backend.InvitationBackend"
  589. SOCIALACCOUNT_PROVIDERS = {}
  590. if GITLAB_URL := env.url("SOCIALACCOUNT_PROVIDERS_gitlab_GITLAB_URL", None):
  591. SOCIALACCOUNT_PROVIDERS["gitlab"] = {"GITLAB_URL": GITLAB_URL.geturl()}
  592. if GITEA_URL := env.url("SOCIALACCOUNT_PROVIDERS_gitea_GITEA_URL", None):
  593. SOCIALACCOUNT_PROVIDERS["gitea"] = {"GITEA_URL": GITEA_URL.geturl()}
  594. if NEXTCLOUD_URL := env.url("SOCIALACCOUNT_PROVIDERS_nextcloud_SERVER", None):
  595. SOCIALACCOUNT_PROVIDERS["nextcloud"] = {"SERVER": NEXTCLOUD_URL.geturl()}
  596. if MICROSOFT_TENANT := env.str("SOCIALACCOUNT_PROVIDERS_microsoft_TENANT", None):
  597. SOCIALACCOUNT_PROVIDERS["microsoft"] = {"TENANT": MICROSOFT_TENANT}
  598. ENABLE_USER_REGISTRATION = env.bool("ENABLE_USER_REGISTRATION", True)
  599. ENABLE_ORGANIZATION_CREATION = env.bool(
  600. "ENABLE_OPEN_USER_REGISTRATION", env.bool("ENABLE_ORGANIZATION_CREATION", False)
  601. )
  602. AUTHENTICATION_BACKENDS = (
  603. # Needed to login by username in Django admin, regardless of `allauth`
  604. "django.contrib.auth.backends.ModelBackend",
  605. # `allauth` specific authentication methods, such as login by e-mail
  606. "allauth.account.auth_backends.AuthenticationBackend",
  607. )
  608. NINJA_PAGINATION_CLASS = "glitchtip.api.pagination.AsyncLinkHeaderPagination"
  609. NINJA_PAGINATION_PER_PAGE = 50
  610. LOGGING_HANDLER_CLASS = env.str("DJANGO_LOGGING_HANDLER_CLASS", "logging.StreamHandler")
  611. LOGGING = {
  612. "version": 1,
  613. "disable_existing_loggers": False,
  614. "handlers": {
  615. "null": {
  616. "class": "logging.NullHandler",
  617. },
  618. "console": {
  619. "class": LOGGING_HANDLER_CLASS,
  620. },
  621. },
  622. "loggers": {
  623. "django.security.DisallowedHost": {
  624. "handlers": ["null"],
  625. "propagate": False,
  626. },
  627. },
  628. "root": {"handlers": ["console"]},
  629. }
  630. if LOGGING_HANDLER_CLASS is not logging.StreamHandler:
  631. from celery.signals import after_setup_logger, after_setup_task_logger
  632. @after_setup_logger.connect
  633. @after_setup_task_logger.connect
  634. def setup_celery_logging(logger, **kwargs):
  635. from django.utils.module_loading import import_string
  636. handler = import_string(LOGGING_HANDLER_CLASS)
  637. for h in logger.handlers:
  638. logger.removeHandler(h)
  639. logger.addHandler(handler())
  640. def organization_request_callback(request):
  641. raise ImproperlyConfigured(
  642. "Organization request callback required by dj-stripe but not used."
  643. )
  644. # Set to track activity with Plausible
  645. PLAUSIBLE_URL = env.str("PLAUSIBLE_URL", default=None)
  646. PLAUSIBLE_DOMAIN = env.str("PLAUSIBLE_DOMAIN", default=None)
  647. # See https://liberapay.com/GlitchTip/donate - suggested self-host donation is $5/month/user.
  648. # Support plans available. Email info@burkesoftware.com for more info.
  649. I_PAID_FOR_GLITCHTIP = env.bool("I_PAID_FOR_GLITCHTIP", False)
  650. DJSTRIPE_SUBSCRIBER_MODEL = "organizations_ext.Organization"
  651. DJSTRIPE_SUBSCRIBER_MODEL_REQUEST_CALLBACK = organization_request_callback
  652. DJSTRIPE_USE_NATIVE_JSONFIELD = True
  653. DJSTRIPE_FOREIGN_KEY_TO_FIELD = "djstripe_id"
  654. MARKETING_URL = "https://glitchtip.com"
  655. STRIPE_AUTOMATIC_TAX = env.bool("STRIPE_AUTOMATIC_TAX", False)
  656. STRIPE_LIVE_MODE = env.bool("STRIPE_LIVE_MODE", False)
  657. if BILLING_ENABLED:
  658. I_PAID_FOR_GLITCHTIP = True
  659. INSTALLED_APPS.append("djstripe")
  660. INSTALLED_APPS.append("apps.djstripe_ext")
  661. STRIPE_TEST_PUBLIC_KEY = env.str("STRIPE_TEST_PUBLIC_KEY", None)
  662. STRIPE_TEST_SECRET_KEY = env.str("STRIPE_TEST_SECRET_KEY", None)
  663. STRIPE_LIVE_PUBLIC_KEY = env.str("STRIPE_LIVE_PUBLIC_KEY", None)
  664. STRIPE_LIVE_SECRET_KEY = env.str("STRIPE_LIVE_SECRET_KEY", None)
  665. DJSTRIPE_WEBHOOK_SECRET = env.str("DJSTRIPE_WEBHOOK_SECRET", None)
  666. CELERY_BEAT_SCHEDULE["check-all-organizations-throttle"] = {
  667. "task": "apps.organizations_ext.tasks.check_all_organizations_throttle",
  668. "schedule": timedelta(hours=4),
  669. }
  670. elif TESTING:
  671. # Must run tests with djstripe enabled
  672. BILLING_ENABLED = True
  673. INSTALLED_APPS.append("djstripe")
  674. INSTALLED_APPS.append("apps.djstripe_ext")
  675. STRIPE_TEST_PUBLIC_KEY = "fake"
  676. STRIPE_TEST_SECRET_KEY = "sk_test_fake" # nosec
  677. DJSTRIPE_WEBHOOK_SECRET = "whsec_fake" # nosec
  678. logging.disable(logging.WARNING)
  679. CELERY_TASK_ALWAYS_EAGER = env.bool("CELERY_TASK_ALWAYS_EAGER", False)
  680. if TESTING:
  681. TEST_RUNNER = "glitchtip.test_runner.TimedTestRunner"
  682. # Optimization
  683. PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
  684. DATABASES["default"]["CONN_MAX_AGE"] = None
  685. DATABASES["default"]["OPTIONS"]["pool"] = False
  686. CELERY_TASK_ALWAYS_EAGER = True
  687. SESSION_ENGINE = "django.contrib.sessions.backends.cache"
  688. STORAGES = global_settings.STORAGES
  689. # https://github.com/evansd/whitenoise/issues/215
  690. warnings.filterwarnings(
  691. "ignore", message="No directory at", module="whitenoise.base"
  692. )
  693. if CELERY_TASK_ALWAYS_EAGER:
  694. CACHES = {
  695. "default": {
  696. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  697. }
  698. }
  699. CACHE_IS_REDIS = CACHES["default"]["BACKEND"] == "django_redis.cache.RedisCache"
  700. warnings.filterwarnings(
  701. "ignore", message="No directory at", module="django.core.handlers.base"
  702. )