Files
notifications-admin/app/config.py

202 lines
7.4 KiB
Python
Raw Normal View History

import os
if os.environ.get('VCAP_APPLICATION'):
# on cloudfoundry, config is a json blob in VCAP_APPLICATION - unpack it, and populate
2016-12-08 16:50:37 +00:00
# standard environment variables from it
from app.cloudfoundry_config import extract_cloudfoundry_config
extract_cloudfoundry_config()
2015-11-24 09:40:14 +00:00
class Config(object):
ADMIN_CLIENT_SECRET = os.environ.get('ADMIN_CLIENT_SECRET')
API_HOST_NAME = os.environ.get('API_HOST_NAME')
SECRET_KEY = os.environ.get('SECRET_KEY')
DANGEROUS_SALT = os.environ.get('DANGEROUS_SALT')
ZENDESK_API_KEY = os.environ.get('ZENDESK_API_KEY')
2016-12-08 16:50:37 +00:00
# if we're not on cloudfoundry, we can get to this app from localhost. but on cloudfoundry its different
ADMIN_BASE_URL = os.environ.get('ADMIN_BASE_URL', 'http://localhost:6012')
TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013')
TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key')
# Logging
2016-12-08 16:50:37 +00:00
DEBUG = False
2017-08-09 16:53:56 +01:00
NOTIFY_LOG_PATH = os.getenv('NOTIFY_LOG_PATH')
2016-12-08 16:50:37 +00:00
2016-09-07 10:31:33 +01:00
ADMIN_CLIENT_USER_NAME = 'notify-admin'
ANTIVIRUS_API_HOST = os.environ.get('ANTIVIRUS_API_HOST')
ANTIVIRUS_API_KEY = os.environ.get('ANTIVIRUS_API_KEY')
2015-11-24 09:40:14 +00:00
ASSETS_DEBUG = False
AWS_REGION = 'eu-west-1'
DEFAULT_SERVICE_LIMIT = 50
DEFAULT_FREE_SMS_FRAGMENT_LIMITS = {
'central': 150_000,
'local': 25_000,
'nhs_central': 150_000,
'nhs_local': 25_000,
'nhs_gp': 10_000,
'emergency_service': 25_000,
'school_or_college': 10_000,
'other': 10_000,
}
EMAIL_EXPIRY_SECONDS = 3600 # 1 hour
INVITATION_EXPIRY_SECONDS = 3600 * 24 * 2 # 2 days - also set on api
EMAIL_2FA_EXPIRY_SECONDS = 1800 # 30 Minutes
HEADER_COLOUR = '#81878b' # mix(govuk-colour("dark-grey"), govuk-colour("mid-grey"))
HTTP_PROTOCOL = 'http'
2016-01-07 16:24:10 +00:00
NOTIFY_APP_NAME = 'admin'
NOTIFY_LOG_LEVEL = 'DEBUG'
PERMANENT_SESSION_LIFETIME = 20 * 60 * 60 # 20 hours
SEND_FILE_MAX_AGE_DEFAULT = 365 * 24 * 60 * 60 # 1 year
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_NAME = 'notify_admin_session'
SESSION_COOKIE_SECURE = True
2019-11-28 14:39:30 +00:00
# don't send back the cookie if it hasn't been modified by the request. this means that the expiry time won't be
# updated unless the session is changed - but it's generally refreshed by `save_service_or_org_after_request`
# every time anyway, except for specific endpoints (png/pdfs generally) where we've disabled that handler.
SESSION_REFRESH_EACH_REQUEST = False
WTF_CSRF_ENABLED = True
WTF_CSRF_TIME_LIMIT = None
2016-04-07 17:33:30 +01:00
CSV_UPLOAD_BUCKET_NAME = 'local-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'local-contact-list'
ACTIVITY_STATS_LIMIT_DAYS = 7
TEST_MESSAGE_FILENAME = 'Report'
REPLY_TO_EMAIL_ADDRESS_VALIDATION_TIMEOUT = 45
NOTIFY_ENVIRONMENT = 'development'
2017-08-02 11:05:17 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-local'
MOU_BUCKET_NAME = 'local-mou'
TRANSIENT_UPLOADED_LETTERS = 'local-transient-uploaded-letters'
2017-11-15 12:05:05 +00:00
ROUTE_SECRET_KEY_1 = os.environ.get('ROUTE_SECRET_KEY_1', '')
ROUTE_SECRET_KEY_2 = os.environ.get('ROUTE_SECRET_KEY_2', '')
CHECK_PROXY_HEADER = False
ANTIVIRUS_ENABLED = True
2017-07-24 15:20:40 +01:00
Add Redis cache between admin and API Most of the time spent by the admin app to generate a page is spent waiting for the API. This is slow for three reasons: 1. Talking to the API means going out to the internet, then through nginx, the Flask app, SQLAlchemy, down to the database, and then serialising the result to JSON and making it into a HTTP response 2. Each call to the API is synchronous, therefore if a page needs 3 API calls to render then the second API call won’t be made until the first has finished, and the third won’t start until the second has finished 3. Every request for a service page in the admin app makes a minimum of two requests to the API (`GET /service/…` and `GET /user/…`) Hitting the database will always be the slowest part of an app like Notify. But this slowness is exacerbated by 2. and 3. Conversely every speedup made to 1. is multiplied by 2. and 3. So this pull request aims to make 1. a _lot_ faster by taking nginx, Flask, SQLAlchemy and the database out of the equation. It replaces them with Redis, which as an in-memory key/value store is a lot faster than Postgres. There is still the overhead of going across the network to talk to Redis, but the net improvement is vast. This commit only caches the `GET /service` response, but is written in such a way that we can easily expand to caching other responses down the line. The tradeoff here is that our code is more complex, and we risk introducing edge cases where a cache becomes stale. The mitigations against this are: - invalidating all caches after 24h so a stale cache doesn’t remain around indefinitely - being careful when we add new stuff to the service response --- Some indicative numbers, based on: - `GET http://localhost:6012/services/<service_id>/template/<template_id>` - with the admin app running locally - talking to Redis running locally - also talking to the API running locally, itself talking to a local Postgres instance - times measured with Chrome web inspector, average of 10 requests ╲ | No cache | Cache service | Cache service and user | Cache service, user and template -- | -- | -- | -- | -- **Request time** | 136ms | 97ms | 73ms | 37ms **Improvement** | 0% | 41% | 88% | 265% --- Estimates of how much storage this requires: - Services: 1,942 on production × 2kb = 4Mb - Users: 4,534 on production × 2kb = 9Mb - Templates: 7,079 on production × 4kb = 28Mb
2018-04-06 13:37:49 +01:00
REDIS_URL = os.environ.get('REDIS_URL')
2020-08-11 09:58:34 +01:00
REDIS_ENABLED = os.environ.get('REDIS_ENABLED') == '1'
Add Redis cache between admin and API Most of the time spent by the admin app to generate a page is spent waiting for the API. This is slow for three reasons: 1. Talking to the API means going out to the internet, then through nginx, the Flask app, SQLAlchemy, down to the database, and then serialising the result to JSON and making it into a HTTP response 2. Each call to the API is synchronous, therefore if a page needs 3 API calls to render then the second API call won’t be made until the first has finished, and the third won’t start until the second has finished 3. Every request for a service page in the admin app makes a minimum of two requests to the API (`GET /service/…` and `GET /user/…`) Hitting the database will always be the slowest part of an app like Notify. But this slowness is exacerbated by 2. and 3. Conversely every speedup made to 1. is multiplied by 2. and 3. So this pull request aims to make 1. a _lot_ faster by taking nginx, Flask, SQLAlchemy and the database out of the equation. It replaces them with Redis, which as an in-memory key/value store is a lot faster than Postgres. There is still the overhead of going across the network to talk to Redis, but the net improvement is vast. This commit only caches the `GET /service` response, but is written in such a way that we can easily expand to caching other responses down the line. The tradeoff here is that our code is more complex, and we risk introducing edge cases where a cache becomes stale. The mitigations against this are: - invalidating all caches after 24h so a stale cache doesn’t remain around indefinitely - being careful when we add new stuff to the service response --- Some indicative numbers, based on: - `GET http://localhost:6012/services/<service_id>/template/<template_id>` - with the admin app running locally - talking to Redis running locally - also talking to the API running locally, itself talking to a local Postgres instance - times measured with Chrome web inspector, average of 10 requests ╲ | No cache | Cache service | Cache service and user | Cache service, user and template -- | -- | -- | -- | -- **Request time** | 136ms | 97ms | 73ms | 37ms **Improvement** | 0% | 41% | 88% | 265% --- Estimates of how much storage this requires: - Services: 1,942 on production × 2kb = 4Mb - Users: 4,534 on production × 2kb = 9Mb - Templates: 7,079 on production × 4kb = 28Mb
2018-04-06 13:37:49 +01:00
ASSET_DOMAIN = ''
ASSET_PATH = '/static/'
# as defined in api db migration 0331_add_broadcast_org.py
BROADCAST_ORGANISATION_ID = '38e4bf69-93b0-445d-acee-53ea53fe02df'
2019-05-13 10:17:20 +01:00
NOTIFY_SERVICE_ID = 'd6aa2c68-a2d9-4437-ab19-3ae8eb202553'
2015-11-24 09:40:14 +00:00
class Development(Config):
NOTIFY_LOG_PATH = 'application.log'
2015-11-24 09:40:14 +00:00
DEBUG = True
SESSION_COOKIE_SECURE = False
2016-05-04 13:01:55 +01:00
SESSION_PROTECTION = None
2016-04-07 17:33:30 +01:00
CSV_UPLOAD_BUCKET_NAME = 'development-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'development-contact-list'
2017-07-28 15:17:50 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-tools'
MOU_BUCKET_NAME = 'notify.tools-mou'
TRANSIENT_UPLOADED_LETTERS = 'development-transient-uploaded-letters'
2015-11-24 09:40:14 +00:00
ADMIN_CLIENT_SECRET = 'dev-notify-secret-key'
API_HOST_NAME = 'http://localhost:6011'
DANGEROUS_SALT = 'dev-notify-salt'
SECRET_KEY = 'dev-notify-secret-key'
ANTIVIRUS_API_HOST = 'http://localhost:6016'
ANTIVIRUS_API_KEY = 'test-key'
ANTIVIRUS_ENABLED = os.getenv('ANTIVIRUS_ENABLED') == '1'
ASSET_PATH = '/static/'
REDIS_URL = 'redis://localhost:6379/0'
2015-11-24 09:40:14 +00:00
class Test(Development):
2016-03-23 15:06:17 +00:00
DEBUG = True
TESTING = True
WTF_CSRF_ENABLED = False
2016-04-07 17:33:30 +01:00
CSV_UPLOAD_BUCKET_NAME = 'test-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'test-contact-list'
2017-08-02 11:05:17 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-test'
MOU_BUCKET_NAME = 'test-mou'
TRANSIENT_UPLOADED_LETTERS = 'test-transient-uploaded-letters'
NOTIFY_ENVIRONMENT = 'test'
API_HOST_NAME = 'http://you-forgot-to-mock-an-api-call-to'
TEMPLATE_PREVIEW_API_HOST = 'http://localhost:9999'
ANTIVIRUS_API_HOST = 'https://test-antivirus'
ANTIVIRUS_API_KEY = 'test-antivirus-secret'
ANTIVIRUS_ENABLED = True
2015-11-24 09:40:14 +00:00
ASSET_DOMAIN = 'static.example.com'
ASSET_PATH = 'https://static.example.com/'
2015-11-30 14:32:58 +00:00
class Preview(Config):
2015-11-30 14:32:58 +00:00
HTTP_PROTOCOL = 'https'
HEADER_COLOUR = '#F499BE' # $baby-pink
2016-04-07 17:33:30 +01:00
CSV_UPLOAD_BUCKET_NAME = 'preview-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'preview-contact-list'
2017-08-02 11:05:17 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-preview'
MOU_BUCKET_NAME = 'notify.works-mou'
TRANSIENT_UPLOADED_LETTERS = 'preview-transient-uploaded-letters'
NOTIFY_ENVIRONMENT = 'preview'
CHECK_PROXY_HEADER = False
ASSET_DOMAIN = 'static.notify.works'
ASSET_PATH = 'https://static.notify.works/'
# On preview, extend the validation timeout to allow more leniency when running functional tests
REPLY_TO_EMAIL_ADDRESS_VALIDATION_TIMEOUT = 120
class Staging(Config):
HTTP_PROTOCOL = 'https'
HEADER_COLOUR = '#6F72AF' # $mauve
CSV_UPLOAD_BUCKET_NAME = 'staging-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'staging-contact-list'
2017-08-02 11:05:17 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-staging'
MOU_BUCKET_NAME = 'staging-notify.works-mou'
TRANSIENT_UPLOADED_LETTERS = 'staging-transient-uploaded-letters'
NOTIFY_ENVIRONMENT = 'staging'
CHECK_PROXY_HEADER = False
ASSET_DOMAIN = 'static.staging-notify.works'
ASSET_PATH = 'https://static.staging-notify.works/'
class Live(Config):
HEADER_COLOUR = '#005EA5' # $govuk-blue
HTTP_PROTOCOL = 'https'
2016-07-05 17:00:29 +01:00
CSV_UPLOAD_BUCKET_NAME = 'live-notifications-csv-upload'
CONTACT_LIST_UPLOAD_BUCKET_NAME = 'production-contact-list'
LOGO_UPLOAD_BUCKET_NAME = 'public-logos-production'
MOU_BUCKET_NAME = 'notifications.service.gov.uk-mou'
TRANSIENT_UPLOADED_LETTERS = 'production-transient-uploaded-letters'
NOTIFY_ENVIRONMENT = 'live'
CHECK_PROXY_HEADER = False
ASSET_DOMAIN = 'static.notifications.service.gov.uk'
ASSET_PATH = 'https://static.notifications.service.gov.uk/'
2016-12-08 16:50:37 +00:00
class CloudFoundryConfig(Config):
pass
2016-12-08 16:50:37 +00:00
# CloudFoundry sandbox
class Sandbox(CloudFoundryConfig):
HTTP_PROTOCOL = 'https'
HEADER_COLOUR = '#F499BE' # $baby-pink
CSV_UPLOAD_BUCKET_NAME = 'cf-sandbox-notifications-csv-upload'
2017-07-24 15:20:40 +01:00
LOGO_UPLOAD_BUCKET_NAME = 'cf-sandbox-notifications-logo-upload'
2016-12-08 16:50:37 +00:00
NOTIFY_ENVIRONMENT = 'sandbox'
2015-11-24 09:40:14 +00:00
configs = {
'development': Development,
'test': Test,
'preview': Preview,
'staging': Staging,
2016-12-08 16:50:37 +00:00
'live': Live,
'production': Live,
2016-12-08 16:50:37 +00:00
'sandbox': Sandbox
2015-11-24 09:40:14 +00:00
}