From 01a52476c804602d28d27a0400b95647047add55 Mon Sep 17 00:00:00 2001
From: Fabrice Gangler <fabrice.gangler@adullact.org>
Date: Fri, 14 Jun 2024 11:55:35 +0200
Subject: [PATCH] feat: display software version

---
 .gitlab/ci/job.php.gitlab-ci.yml                |  3 ++-
 webapp/.env                                     | 15 +++++++++++++++
 webapp/.env.dev                                 | 13 ++++++++++++-
 webapp/.env.prod                                |  7 ++++++-
 webapp/.env.test                                | 13 +++++++++++++
 webapp/config/packages/twig.yaml                |  4 ++++
 webapp/config/services.yaml                     |  5 ++++-
 webapp/src/Controller/HealthCheckController.php | 14 ++++++++++----
 webapp/templates/healthcheck.html.twig          |  8 ++++++++
 9 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/.gitlab/ci/job.php.gitlab-ci.yml b/.gitlab/ci/job.php.gitlab-ci.yml
index a964d98..259b76f 100644
--- a/.gitlab/ci/job.php.gitlab-ci.yml
+++ b/.gitlab/ci/job.php.gitlab-ci.yml
@@ -370,7 +370,8 @@ php8.1_release-build:
         - echo "${CI_PROJECT_TITLE}"     >>   "${PHP_WEBAPP_DIR}/README.txt"
         - echo "${DOC_URL}"              >>   "${PHP_WEBAPP_DIR}/README.txt"
         - echo "APP_ENV=prod" >   "${PHP_WEBAPP_DIR}/.env.local"
-        - sed -i "s/major.minor.patch/${CI_COMMIT_TAG}/" "${PHP_WEBAPP_DIR}/config/services.yaml"
+        - sed -i "s/version_major.minor.patch/${CI_COMMIT_TAG}/" "${PHP_WEBAPP_DIR}/.env"
+#       - sed -i "s/version_major.minor.patch/${CI_COMMIT_TAG}/" "${PHP_WEBAPP_DIR}/config/services.yaml"
         - cat "${PHP_WEBAPP_DIR}/config/services.yaml"
         - mkdir -p "${CI_PROJECT_DIR}/build-result/${BUILD_NAME}"
         - mkdir -p "${CI_PROJECT_DIR}/build-result/${BUILD_NAME}/bin"
diff --git a/webapp/.env b/webapp/.env
index 2cedfe0..e964077 100644
--- a/webapp/.env
+++ b/webapp/.env
@@ -18,6 +18,7 @@
 #    - APP_SECRET                   Application secret to generate CSRF tokens
 #    - DATABASE_URL                 Database configuration (PostgreSQL)
 #    - MAILER_DSN                   SMTP configuration
+########################################################################################################################
 #    - WEBAPP_DEFAULT_URI           Default URI used to generate URLs in a non-HTTP context
 #    - WEBAPP_EMAIL_FROM            Sender's email address to send emails from the web application
 #    - WEBAPP_EMAIL_ALERTING_TO     Email address for alert notifications (web application not working properly, ...)
@@ -30,6 +31,7 @@
 #######################################################################################################################
 #    - WEBAPP_USER_CONFIG_RESET_PASSWORD_TOKEN_LIFETIME   Lifetime of reset password token in seconds (default: 1200 = 20 minutes).
 #    - WEBAPP_USER_CONFIG_MIN_PASSWORD_LENGTH             Minimum user password length (default: 12)
+#    - WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY         Public display of software version (default: true)
 #######################################################################################################################
 
 ###> symfony/framework-bundle ###
@@ -80,6 +82,7 @@ WEBAPP_SESSION_LIFETIME=3600
 WEBAPP_TIMEZONE='Europe/Paris'
 WEBAPP_USER_CONFIG_PASSWORD_RESET_TOKEN_LIFETIME=1200
 WEBAPP_USER_CONFIG_MIN_PASSWORD_LENGTH=12
+WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY=true
 #######################################################################
 
 # Webapp - List of allowed domains and IP to prevent HTTP Host header attacks
@@ -100,3 +103,15 @@ WEBAPP_TRUSTED_HOSTS=''
     #       Only allow incoming requests for all subdomains of example.org
     #       WEBAPP_TRUSTED_HOSTS='^(.+\.)?example\.org$'
     ##################################################################################################
+
+
+#######################################################################################################################
+#######################################################################################################################
+
+# We strongly recommend leaving the following environment variables unchanged
+SOFTWARE_NAME='Comptoir'
+SOFTWARE_RELEASE='version_major.minor.patch'
+    ##################################################################################################
+    # Do not modify if you have not identified
+    # the technical implications: HTTP headers using theses values, ...
+    ##################################################################################################
diff --git a/webapp/.env.dev b/webapp/.env.dev
index 4d4e575..8df8b80 100644
--- a/webapp/.env.dev
+++ b/webapp/.env.dev
@@ -62,4 +62,15 @@ MAILER_DSN=smtp://127.0.0.1:1025
     #    error 500 ---> Connection could not be established with host "0.0.0.0:9091":
     #                   stream_socket_client(): unable to connect to 0.0.0.0:9091 (Connection refused)
     ##################################################################################################
-#######################################################################
+
+
+
+#######################################################################################################################
+#######################################################################################################################
+
+# We strongly recommend leaving the following environment variables unchanged
+SOFTWARE_NAME='Comptoir-DEV-Software'
+    ##################################################################################################
+    # Do not modify if you have not identified
+    # the technical implications: HTTP headers using this value, ...
+    ##################################################################################################
diff --git a/webapp/.env.prod b/webapp/.env.prod
index 1399c50..87a7179 100644
--- a/webapp/.env.prod
+++ b/webapp/.env.prod
@@ -18,7 +18,8 @@
 #    - APP_SECRET                   Application secret to generate CSRF tokens
 #    - DATABASE_URL                 Database configuration (PostgreSQL)
 #    - MAILER_DSN                   SMTP configuration
-########################################################################################################################    - WEBAPP_DEFAULT_URI           Default URI used to generate URLs in a non-HTTP context
+########################################################################################################################
+#    - WEBAPP_DEFAULT_URI           Default URI used to generate URLs in a non-HTTP context
 #    - WEBAPP_EMAIL_FROM            Sender's email address to send emails from the web application
 #    - WEBAPP_EMAIL_ALERTING_TO     Email address for alert notifications (web application not working properly, ...)
 #    - WEBAPP_NAME                  Name of web application displayed to users
@@ -30,6 +31,7 @@
 #######################################################################################################################
 #    - WEBAPP_USER_CONFIG_RESET_PASSWORD_TOKEN_LIFETIME   Lifetime of reset password token in seconds (default: 1200 = 20 minutes).
 #    - WEBAPP_USER_CONFIG_MIN_PASSWORD_LENGTH             Minimum user password length (default: 12)
+#    - WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY         Public display of software version (default: true)
 #######################################################################################################################
 
 # Environment
@@ -106,4 +108,7 @@ WEBAPP_USER_CONFIG_PASSWORD_RESET_TOKEN_LIFETIME=1200
 # Minimum user password length
 WEBAPP_USER_CONFIG_MIN_PASSWORD_LENGTH=12
 
+# Public display of software version (default: true)
+WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY=true
+
 #######################################################################################################################
diff --git a/webapp/.env.test b/webapp/.env.test
index 96bb211..06c4e2b 100644
--- a/webapp/.env.test
+++ b/webapp/.env.test
@@ -18,3 +18,16 @@ WEBAPP_I18N_DEFAULT_LOCALE='en'
 #######################################################################
 MAILER_DSN=null://null
 #######################################################################
+
+
+
+#######################################################################################################################
+#######################################################################################################################
+
+# We strongly recommend leaving the following environment variables unchanged
+# We strongly recommend leaving the following environment variables unchanged
+SOFTWARE_NAME='Comptoir-TEST-Software'
+    ##################################################################################################
+    # Do not modify if you have not identified
+    # the technical implications: HTTP headers using this value, ...
+    ##################################################################################################
diff --git a/webapp/config/packages/twig.yaml b/webapp/config/packages/twig.yaml
index d203770..c590937 100644
--- a/webapp/config/packages/twig.yaml
+++ b/webapp/config/packages/twig.yaml
@@ -2,6 +2,10 @@ twig:
     file_name_pattern: '*.twig'
     globals:
         app_name: '%env(WEBAPP_NAME)%'
+        app_shortname: '%env(WEBAPP_SHORTNAME)%'
+        software_name: '%env(SOFTWARE_NAME)%'
+        software_release: '%env(SOFTWARE_RELEASE)%'
+        software_release_allow_public_display: '%env(bool:WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY)%'
 
 when@test:
     twig:
diff --git a/webapp/config/services.yaml b/webapp/config/services.yaml
index 15e886a..c7d3e9f 100644
--- a/webapp/config/services.yaml
+++ b/webapp/config/services.yaml
@@ -10,7 +10,10 @@ parameters:
     app.shortname: '%env(WEBAPP_SHORTNAME)%'
     app.email.alerting_to: '%env(WEBAPP_EMAIL_ALERTING_TO)%'
     app.email.from: '%env(WEBAPP_EMAIL_FROM)%'
-    app.software.name: 'Comptoir'
+    app.software.name: '%env(SOFTWARE_NAME)%'
+    app.software.release: '%env(SOFTWARE_RELEASE)%'
+    app.software.release.display.public: '%env(bool:WEBAPP_SOFTWARE_VERSION_DISPLAYED_PUBLICLY)%'
+
 services:
     # default configuration for services in *this* file
     _defaults:
diff --git a/webapp/src/Controller/HealthCheckController.php b/webapp/src/Controller/HealthCheckController.php
index d095ec5..d6b3449 100644
--- a/webapp/src/Controller/HealthCheckController.php
+++ b/webapp/src/Controller/HealthCheckController.php
@@ -37,7 +37,10 @@ class HealthCheckController extends AbstractController
     )]
     public function healthCheck(Request $request, Connection $dbConnection): Response
     {
+        $displayRelease = $this->getParameter('app.software.release.display.public');
+        $softwareRelease = $this->getParameter('app.software.release');
         $softwareName = $this->getParameter('app.software.name');
+
         $httpCode = Response::HTTP_OK;
         $dbStatusCode = "SUCCESSFUL";
         try {
@@ -47,10 +50,13 @@ public function healthCheck(Request $request, Connection $dbConnection): Respons
             $dbStatusCode = "FAILED";
         }
 
-        $slugSoftwareName = strtolower((new AsciiSlugger())->slug("$softwareName")->toString()) ;
-        $headers = [
-            "x-$slugSoftwareName-database-status" => "DB_CONNECTION_$dbStatusCode",
-        ];
+        $slugSoftwareName = strtolower((new AsciiSlugger())->slug("$softwareName")->toString());
+        $headers = [];
+        $headers["x-$slugSoftwareName-database-status"] = "DB_CONNECTION_$dbStatusCode";
+        if($displayRelease === true){
+            $headers["x-$slugSoftwareName-version"] = $softwareRelease;
+        }
+
         if ($request->getMethod() === "HEAD") {
             return new Response(status: $httpCode, headers: $headers);
         } else {
diff --git a/webapp/templates/healthcheck.html.twig b/webapp/templates/healthcheck.html.twig
index 2169c47..e9c9b38 100644
--- a/webapp/templates/healthcheck.html.twig
+++ b/webapp/templates/healthcheck.html.twig
@@ -5,6 +5,7 @@
 
 
 {% block body %}
+
     {% set db_status_cssClass = 'table-danger' %}
     {% set db_status_i18n = 'public.health-check.status.failed'|trans %}
     {% if dbStatusCode == 'SUCCESSFUL' %}
@@ -34,4 +35,11 @@
         </table>
     </div>
 
+
+    <hr>
+
+    {% if software_release_allow_public_display == true %}
+        {{ software_name }} <strong>{{ software_release }} </strong>
+    {% endif %}
+
 {% endblock %}
-- 
GitLab