2
0

production.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import os
  2. import random
  3. import string
  4. import dj_database_url
  5. from .base import * # noqa: F403
  6. DEBUG = False
  7. # DJANGO_SECRET_KEY *should* be specified in the environment. If it's not, generate an ephemeral key.
  8. if "DJANGO_SECRET_KEY" in os.environ:
  9. SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
  10. else:
  11. # Use if/else rather than a default value to avoid calculating this if we don't need it
  12. print( # noqa: T201
  13. "WARNING: DJANGO_SECRET_KEY not found in os.environ. Generating ephemeral SECRET_KEY."
  14. )
  15. SECRET_KEY = "".join(
  16. [random.SystemRandom().choice(string.printable) for i in range(50)]
  17. )
  18. # Make sure Django can detect a secure connection properly on Heroku:
  19. SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
  20. # Redirect all requests to HTTPS
  21. SECURE_SSL_REDIRECT = os.getenv("DJANGO_SECURE_SSL_REDIRECT", "off") == "on"
  22. # Accept all hostnames, since we don't know in advance which hostname will be used for any given Heroku instance.
  23. # IMPORTANT: Set this to a real hostname when using this in production!
  24. # See https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts
  25. ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(";")
  26. EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  27. # This is used by Wagtail's email notifications for constructing absolute
  28. # URLs. Please set to the domain that users will access the admin site.
  29. if "PRIMARY_HOST" in os.environ:
  30. WAGTAILADMIN_BASE_URL = "https://{}".format(os.environ["PRIMARY_HOST"])
  31. db_from_env = dj_database_url.config(conn_max_age=500)
  32. DATABASES["default"].update(db_from_env)
  33. # AWS creds may be used for S3 and/or Elasticsearch
  34. AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
  35. AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")
  36. AWS_REGION = os.getenv("AWS_REGION", "")
  37. # Server-side cache settings. Do not confuse with front-end cache.
  38. # https://docs.djangoproject.com/en/stable/topics/cache/
  39. # If the server has a Redis instance exposed via a URL string in the REDIS_URL
  40. # environment variable, prefer that. Otherwise use the database backend. We
  41. # usually use Redis in production and database backend on staging and dev. In
  42. # order to use database cache backend you need to run
  43. # "./manage.py createcachetable" to create a table for the cache.
  44. #
  45. # Do not use the same Redis instance for other things like Celery!
  46. # Prefer the TLS connection URL over non
  47. REDIS_URL = os.environ.get("REDIS_TLS_URL", os.environ.get("REDIS_URL"))
  48. if REDIS_URL:
  49. connection_pool_kwargs = {}
  50. if REDIS_URL.startswith("rediss"):
  51. # Heroku Redis uses self-signed certificates for secure redis connections
  52. # When using TLS, we need to disable certificate validation checks.
  53. connection_pool_kwargs["ssl_cert_reqs"] = None
  54. redis_options = {
  55. "IGNORE_EXCEPTIONS": True,
  56. "SOCKET_CONNECT_TIMEOUT": 2, # seconds
  57. "SOCKET_TIMEOUT": 2, # seconds
  58. "CONNECTION_POOL_KWARGS": connection_pool_kwargs,
  59. }
  60. CACHES = {
  61. "default": {
  62. "BACKEND": "django_redis.cache.RedisCache",
  63. "LOCATION": REDIS_URL + "/0",
  64. "OPTIONS": redis_options,
  65. },
  66. "renditions": {
  67. "BACKEND": "django_redis.cache.RedisCache",
  68. "LOCATION": REDIS_URL + "/1",
  69. "OPTIONS": redis_options,
  70. },
  71. }
  72. DJANGO_REDIS_LOG_IGNORED_EXCEPTIONS = True
  73. else:
  74. CACHES = {
  75. "default": {
  76. "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
  77. "LOCATION": "bakerydemo",
  78. }
  79. }
  80. # Configure Elasticsearch, if present in os.environ
  81. ELASTICSEARCH_ENDPOINT = os.getenv("ELASTICSEARCH_ENDPOINT", "")
  82. if ELASTICSEARCH_ENDPOINT:
  83. from elasticsearch import RequestsHttpConnection
  84. WAGTAILSEARCH_BACKENDS = {
  85. "default": {
  86. "BACKEND": "wagtail.search.backends.elasticsearch5",
  87. "HOSTS": [
  88. {
  89. "host": ELASTICSEARCH_ENDPOINT,
  90. "port": int(os.getenv("ELASTICSEARCH_PORT", "9200")),
  91. "use_ssl": os.getenv("ELASTICSEARCH_USE_SSL", "off") == "on",
  92. "verify_certs": os.getenv("ELASTICSEARCH_VERIFY_CERTS", "off")
  93. == "on",
  94. }
  95. ],
  96. "OPTIONS": {
  97. "connection_class": RequestsHttpConnection,
  98. },
  99. }
  100. }
  101. if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
  102. from aws_requests_auth.aws_auth import AWSRequestsAuth
  103. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0]["http_auth"] = AWSRequestsAuth(
  104. aws_access_key=AWS_ACCESS_KEY_ID,
  105. aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
  106. aws_token=os.getenv("AWS_SESSION_TOKEN", ""),
  107. aws_host=ELASTICSEARCH_ENDPOINT,
  108. aws_region=AWS_REGION,
  109. aws_service="es",
  110. )
  111. elif AWS_REGION:
  112. # No API keys in the environ, so attempt to discover them with Boto instead, per:
  113. # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#configuring-credentials
  114. # This may be useful if your credentials are obtained via EC2 instance meta data.
  115. from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
  116. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0][
  117. "http_auth"
  118. ] = BotoAWSRequestsAuth(
  119. aws_host=ELASTICSEARCH_ENDPOINT,
  120. aws_region=AWS_REGION,
  121. aws_service="es",
  122. )
  123. # Simplified static file serving.
  124. # https://warehouse.python.org/project/whitenoise/
  125. MIDDLEWARE.append("whitenoise.middleware.WhiteNoiseMiddleware")
  126. STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
  127. if "AWS_STORAGE_BUCKET_NAME" in os.environ:
  128. AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")
  129. AWS_QUERYSTRING_AUTH = False
  130. INSTALLED_APPS.append("storages")
  131. DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
  132. AWS_S3_FILE_OVERWRITE = False
  133. AWS_DEFAULT_ACL = "private"
  134. if "GS_BUCKET_NAME" in os.environ:
  135. GS_BUCKET_NAME = os.getenv("GS_BUCKET_NAME")
  136. GS_PROJECT_ID = os.getenv("GS_PROJECT_ID")
  137. GS_DEFAULT_ACL = "publicRead"
  138. GS_AUTO_CREATE_BUCKET = True
  139. INSTALLED_APPS.append("storages")
  140. DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
  141. LOGGING = {
  142. "version": 1,
  143. "disable_existing_loggers": False,
  144. "handlers": {
  145. "console": {
  146. "class": "logging.StreamHandler",
  147. },
  148. },
  149. "loggers": {
  150. "django": {
  151. "handlers": ["console"],
  152. "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
  153. },
  154. },
  155. }
  156. # Front-end cache
  157. # This configuration is used to allow purging pages from cache when they are
  158. # published.
  159. # These settings are usually used only on the production sites.
  160. # This is a configuration of the CDN/front-end cache that is used to cache the
  161. # production websites.
  162. # https://docs.wagtail.org/en/latest/reference/contrib/frontendcache.html
  163. # The backend can be configured to use an account-wide API key, or an API token with
  164. # restricted access.
  165. if (
  166. "FRONTEND_CACHE_CLOUDFLARE_TOKEN" in os.environ
  167. or "FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN" in os.environ
  168. ):
  169. INSTALLED_APPS.append("wagtail.contrib.frontend_cache")
  170. WAGTAILFRONTENDCACHE = {
  171. "default": {
  172. "BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend",
  173. "ZONEID": os.environ["FRONTEND_CACHE_CLOUDFLARE_ZONEID"],
  174. }
  175. }
  176. if "FRONTEND_CACHE_CLOUDFLARE_TOKEN" in os.environ:
  177. # To use an account-wide API key, set the following:
  178. # * $FRONTEND_CACHE_CLOUDFLARE_TOKEN
  179. # * $FRONTEND_CACHE_CLOUDFLARE_EMAIL
  180. # * $FRONTEND_CACHE_CLOUDFLARE_ZONEID
  181. # These can be obtained from a sysadmin.
  182. WAGTAILFRONTENDCACHE["default"].update(
  183. {
  184. "EMAIL": os.environ["FRONTEND_CACHE_CLOUDFLARE_EMAIL"],
  185. "TOKEN": os.environ["FRONTEND_CACHE_CLOUDFLARE_TOKEN"],
  186. }
  187. )
  188. else:
  189. # To use an API token with restricted access, set the following:
  190. # * $FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN
  191. # * $FRONTEND_CACHE_CLOUDFLARE_ZONEID
  192. WAGTAILFRONTENDCACHE["default"].update(
  193. {"BEARER_TOKEN": os.environ["FRONTEND_CACHE_CLOUDFLARE_BEARER_TOKEN"]}
  194. )
  195. # Basic authentication settings
  196. # These are settings to configure the third-party library:
  197. # https://gitlab.com/tmkn/django-basic-auth-ip-whitelist
  198. if os.environ.get("BASIC_AUTH_ENABLED", "false").lower().strip() == "true":
  199. # Insert basic auth as a first middleware to be checked first, before
  200. # anything else.
  201. MIDDLEWARE.insert(0, "baipw.middleware.BasicAuthIPWhitelistMiddleware")
  202. # This is the credentials users will have to use to access the site.
  203. BASIC_AUTH_LOGIN = os.environ.get("BASIC_AUTH_LOGIN", "wagtail")
  204. BASIC_AUTH_PASSWORD = os.environ.get("BASIC_AUTH_PASSWORD", "wagtail")
  205. # Wagtail requires Authorization header to be present for the previews
  206. BASIC_AUTH_DISABLE_CONSUMING_AUTHORIZATION_HEADER = True
  207. # This is the list of hosts that website can be accessed without basic auth
  208. # check.
  209. if "BASIC_AUTH_WHITELISTED_HTTP_HOSTS" in os.environ:
  210. BASIC_AUTH_WHITELISTED_HTTP_HOSTS = os.environ[
  211. "BASIC_AUTH_WHITELISTED_HTTP_HOSTS"
  212. ].split(",")
  213. BASIC_AUTH_RESPONSE_TEMPLATE = "base/basic_auth.html"