From 96d2dec08c2e8271ed5935ce69203a22558ad2e8 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Wed, 12 Jun 2024 18:17:47 +0100 Subject: [PATCH] Rework configs and add cookie domain --- README.md | 2 +- app/__init__.py | 9 +-------- app/legal/routes.py | 3 ++- app/templates/base.html | 3 ++- app/templates/macros/meta.html | 3 --- config.py | 5 +---- docker-compose.yml | 2 +- test/main/test_routes.py | 4 ++-- 8 files changed, 10 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7948654c..f4a07ab8 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ In addition to the [base Docker image variables](https://github.com/nationalarch | `DEBUG` | If true, allow debugging[^1] | `False` | | `WAGTAIL_API_URL` | The base URL of the content API, including the `/api/v2` path | _none_ | | `SEARCH_API_URL` | The base URL of the search API | _none_ | -| `DOMAIN` | The domain the site is hosted on | _none_ | +| `COOKIE_DOMAIN` | The domain to save cookie preferences against | _none_ | | `CSP_IMG_SRC` | A comma separated list of CSP rules for `img-src` | `'self'` | | `CSP_SCRIPT_SRC` | A comma separated list of CSP rules for `script-src` | `'self'` | | `CSP_STYLE_SRC` | A comma separated list of CSP rules for `style-src` | `'self'` | diff --git a/app/__init__.py b/app/__init__.py index 699d823e..5e54b700 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -106,14 +106,7 @@ def context_processor(): cookie_preference=cookie_preference, now_iso_8601=now_iso_8601, pretty_date_range=pretty_date_range, - config={ - "DOMAIN": app.config["DOMAIN"], - "BASE_DISCOVERY_URL": app.config["BASE_DISCOVERY_URL"], - "SEARCH_DISCOVERY_URL": app.config["SEARCH_DISCOVERY_URL"], - "SEARCH_WEBSITE_URL": app.config["SEARCH_WEBSITE_URL"], - "ARCHIVE_RECORDS_URL": app.config["ARCHIVE_RECORDS_URL"], - "GA4_ID": app.config["GA4_ID"], - }, + config=app.config, ) from .about import bp as about_bp diff --git a/app/legal/routes.py b/app/legal/routes.py index 42e1c7ec..5f5d144e 100644 --- a/app/legal/routes.py +++ b/app/legal/routes.py @@ -4,7 +4,7 @@ from app.legal import bp from app.lib import cache, cache_key_prefix from app.lib.util import strtobool -from flask import make_response, render_template, request +from flask import make_response, render_template, request, current_app @bp.route("/") @@ -41,6 +41,7 @@ def cookies(): response.set_cookie( "cookies_policy", quote(json.dumps(new_cookies_policy, separators=(",", ":"))), + domain=current_app.config["COOKIE_DOMAIN"] ) return response return render_template("legal/cookies.html") diff --git a/app/templates/base.html b/app/templates/base.html index be2cf7da..75e47f73 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -52,7 +52,8 @@ {# {% if 'cookie_preferences_set' not in request.cookies or not request.cookies.get('cookie_preferences_set') %} #} {{ tnaCookieBanner({ 'serviceName': 'The National Archives', - 'cookiesUrl': '/cookies' + 'cookiesUrl': '/cookies', + 'cookiesDomain': config.COOKIE_DOMAIN }) }} {# {% endif %} #} {% endif %} diff --git a/app/templates/macros/meta.html b/app/templates/macros/meta.html index d8cfaf2b..37cbe762 100644 --- a/app/templates/macros/meta.html +++ b/app/templates/macros/meta.html @@ -81,7 +81,4 @@ {%- if og_image_twitter %} {% endif %} -{% if config.DOMAIN %} - -{% endif %} {% endmacro %} diff --git a/config.py b/config.py index 79ed8316..5ac47273 100644 --- a/config.py +++ b/config.py @@ -11,7 +11,7 @@ class Base(object): WAGTAIL_API_URL = os.environ.get("WAGTAIL_API_URL", "").rstrip("/") SEARCH_API_URL = os.environ.get("SEARCH_API_URL", "").rstrip("/") - DOMAIN = os.environ.get("DOMAIN", "") + COOKIE_DOMAIN = os.environ.get("COOKIE_DOMAIN", "") CSP_IMG_SRC = os.environ.get("CSP_IMG_SRC", "'self'").split(",") CSP_SCRIPT_SRC = os.environ.get("CSP_SCRIPT_SRC", "'self'").split(",") @@ -80,9 +80,6 @@ class Test(Base): WAGTAIL_API_URL = "http://wagtail.test/api/v2" SEARCH_API_URL = "http://search.test/api/v1" - DOMAIN = "http://localhost" - MEDIA_DOMAIN = "http://media.test" - CACHE_TYPE = "SimpleCache" FORCE_HTTPS = False diff --git a/docker-compose.yml b/docker-compose.yml index 7083e26c..04ef3289 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,7 +11,7 @@ services: - NPM_DEVELOP_COMMAND=dev - WAGTAIL_API_URL=http://host.docker.internal:8000/api/v2 - SEARCH_API_URL=http://host.docker.internal:65534/api/v1 - - DOMAIN=http://localhost:65535 + - COOKIE_DOMAIN=localhost:65535 - CSP_IMG_SRC='self',localhost:65535,localhost:8000,*.google-analytics.com,*.googletagmanager.com,googletagmanager.com,*.gstatic.com,*.nationalarchives.gov.uk,*.platformsh.site - CSP_SCRIPT_SRC='self',*.googletagmanager.com,googletagmanager.com,tagmanager.google.com - CSP_STYLE_SRC='self',fonts.googleapis.com,p.typekit.net,use.typekit.net,googletagmanager.com,www.googletagmanager.com,tagmanager.google.com diff --git a/test/main/test_routes.py b/test/main/test_routes.py index e3f5583f..70e2c92a 100644 --- a/test/main/test_routes.py +++ b/test/main/test_routes.py @@ -8,8 +8,8 @@ class MainBlueprintTestCase(unittest.TestCase): def setUp(self): self.app = create_app("config.Test").test_client() - self.domain = self.app.application.config["DOMAIN"] - self.media_domain = self.app.application.config["MEDIA_DOMAIN"] + self.domain = "TEST" + self.media_domain = "TEST_MEDIA" self.mock_api_url = self.app.application.config["WAGTAIL_API_URL"] def test_trailing_slash_redirects(self):