From 4b5ca3dcf10234eadf06937535dff70ee03107a0 2024-02-15 01:29:37
From: Branko Majic
Date: 2024-02-15 01:29:37
Subject: [PATCH] MAR-197: Implement environment indicator for the web_server role:
- Now the environment indicator will also cover the default landing
page for the web server.
- Tweaked the landing page to look a bit better in terms of
margins (particularly important for the environment indicator).
---
diff --git a/docs/releasenotes.rst b/docs/releasenotes.rst
index 734babe0269a0feeecba794a1005687e24db6aae..9f61caa09eb28ebc0b970ab8b0d419f3ba9546f7 100644
--- a/docs/releasenotes.rst
+++ b/docs/releasenotes.rst
@@ -49,6 +49,16 @@ Dropped support for Debian 10 (Buster).
``/etc/pip_check_requirements_upgrades`` path, in accordance to
changes made in this release to the ``common`` role.
+**New features/improvements**
+
+* ``web_server`` role
+
+ * Added parameter ``environment_indicator`` which is used on the
+ default page to indicate what environment the web server belongs
+ to. Useful for distinguishing between test, staging, and
+ production environments (similar to what is already available in
+ the ``php_website`` and ``wsgi_website`` roles).
+
**Bug fixes:**
* ``common`` role
diff --git a/docs/rolereference.rst b/docs/rolereference.rst
index 95f17efbe44fc45134adca31e59c774c324dc26c..4230cb8c0f9db3bdbab8d128761a3a594563278d 100644
--- a/docs/rolereference.rst
+++ b/docs/rolereference.rst
@@ -1448,6 +1448,28 @@ Parameters
Private key used for TLS for HTTPS service. The file will be stored in
directory ``/etc/ssl/private/`` under name ``{{ ansible_fqdn }}_https.key``.
+**environment_indicator** (dictionary, optional, ``null``)
+ Specify configuration for including environment indicator on all HTML
+ pages. Indicator is a simple strip at bottom of a page with custom background
+ colour, text colour, and text.
+
+ Specifying environment indicator is useful for avoiding mistakes when testing
+ by having better visibility what environment you are in
+ (production/staging/test).
+
+ The following keys need to be specified:
+
+ **background_colour** (string, mandatory)
+ Background colour to use for the strip at bottom. This should be value
+ compatible with CSS ``background-color`` attribute.
+
+ **text_colour** (string, mandatory)
+ Text colour to use for the strip at bottom. This should be value compatible
+ with CSS ``color`` attribute.
+
+ **text** (string, mandatory)
+ Text to show in show in the strip at bottom.
+
**web_default_title** (string, optional, ``Welcome``)
Title for the default web page shown to users (if no other vhosts were matched).
@@ -1605,7 +1627,7 @@ Parameters
Background colour to use for the strip at bottom. This should be value
compatible with CSS ``background-color`` attribute.
- **text_colour** (string, mandatory
+ **text_colour** (string, mandatory)
Text colour to use for the strip at bottom. This should be value compatible
with CSS ``color`` attribute.
@@ -1842,7 +1864,7 @@ Parameters
Background colour to use for the strip at bottom. This should be value
compatible with CSS ``background-color`` attribute.
- **text_colour** (string, mandatory
+ **text_colour** (string, mandatory)
Text colour to use for the strip at bottom. This should be value compatible
with CSS ``color`` attribute.
diff --git a/roles/web_server/defaults/main.yml b/roles/web_server/defaults/main.yml
index 239acd5f6078b0b59783df7f1f15a53374b00d4b..7df4251cfd74b8b0233b713d00b5af2ddbd229ec 100644
--- a/roles/web_server/defaults/main.yml
+++ b/roles/web_server/defaults/main.yml
@@ -1,5 +1,7 @@
---
+environment_indicator: null
+
web_default_title: "Welcome"
web_default_message: "You are attempting to access the web server using a wrong name or an IP address. Please check your URL."
web_server_tls_protocols:
diff --git a/roles/web_server/molecule/default/group_vars/parameters-optional.yml b/roles/web_server/molecule/default/group_vars/parameters-optional.yml
index 693ad456ae8dc6bd597fbee56e33073f40fab540..29285bb38a8f0f3669ed61bf60750f4d38cb8ca1 100644
--- a/roles/web_server/molecule/default/group_vars/parameters-optional.yml
+++ b/roles/web_server/molecule/default/group_vars/parameters-optional.yml
@@ -2,6 +2,10 @@
default_https_tls_certificate: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.cert.pem') }}"
default_https_tls_key: "{{ lookup('file', 'tests/data/x509/server/{{ inventory_hostname }}_https.key.pem') }}"
+environment_indicator:
+ background_colour: "#ff0000"
+ text_colour: "#00ff00"
+ text: "parameters-optional"
web_default_title: "Optional Welcome"
web_default_message: "Welcome to default virtual host."
web_server_tls_protocols:
diff --git a/roles/web_server/molecule/default/tests/test_optional.py b/roles/web_server/molecule/default/tests/test_optional.py
index b42753d69e20dae5add70933b08222f126bcb945..eb43f3856367032a817c80cb2a03a515a0cb08f2 100644
--- a/roles/web_server/molecule/default/tests/test_optional.py
+++ b/roles/web_server/molecule/default/tests/test_optional.py
@@ -63,3 +63,17 @@ def test_default_vhost_index_page(host):
assert "Optional Welcome" in page.stdout
assert "Optional Welcome
" in page.stdout
assert "Welcome to default virtual host.
" in page.stdout
+
+
+def test_environment_indicator(host):
+ """
+ Tests if environment indicator is applied correctly.
+ """
+
+ hostname = host.ansible.get_variables()['inventory_hostname']
+
+ page = host.run('curl https://%s/' % hostname)
+
+ assert page.rc == 0
+ assert "parameters-optional
-{{ web_default_title}}
-{{ web_default_message }}
+
+
{{ web_default_title}}
+
{{ web_default_message }}
+
" in page.stdout
diff --git a/roles/web_server/templates/index.html.j2 b/roles/web_server/templates/index.html.j2
index a9a959175980366ca1ac68d441c626eff0ec1068..d4e86c7400e39a5860712a9280db6dd7a09e6893 100644
--- a/roles/web_server/templates/index.html.j2
+++ b/roles/web_server/templates/index.html.j2
@@ -3,15 +3,23 @@