From 07636992aa200f96185b31aabb8effb6fd8d94ad Mon Sep 17 00:00:00 2001
From: Massimiliano Adamo <maxadamo@gmail.com>
Date: Mon, 30 Nov 2020 19:31:43 +0100
Subject: [PATCH] final changes to flask script

---
 files/geant_acme_web.py  | 29 +++++++++++------------------
 templates/index.html.epp |  4 ++--
 2 files changed, 13 insertions(+), 20 deletions(-)

diff --git a/files/geant_acme_web.py b/files/geant_acme_web.py
index fcf8753..21939ac 100644
--- a/files/geant_acme_web.py
+++ b/files/geant_acme_web.py
@@ -3,10 +3,7 @@
     run flask CGI that will trigger cert2json.py -p <provider>
 """
 import subprocess as sp
-from flask import Flask
-from flask import request
-from flask import render_template
-from flask import send_from_directory
+from flask import Flask, request, redirect, render_template, send_from_directory
 
 APP = Flask(__name__, root_path='/var/www/acme_web', template_folder='/var/www/acme_web',
             static_folder='static', static_url_path='/var/www/acme_web')
@@ -14,6 +11,7 @@ BASE_URLS = ['/sectigo_ev', '/sectigo_ov', '/letsencrypt',
              '/sectigo_ev/', '/sectigo_ov/', '/letsencrypt/']
 OTHER_URLS = ['/sectigo_ev/index.html', '/sectigo_ov/index.html',
               '/letsencrypt/index.html', '/sectigo_ev/expiry_date.html',
+              '/sectigo_ov/expiry_date.html', '/letsencrypt/expiry_date.html',
               '/sectigo_ov/sectigo_ov.json', '/sectigo_ov/sectigo_ov_expired.json',
               '/sectigo_ov/sectigo_ev.json', '/sectigo_ov/sectigo_ev_expired.json',
               '/letsencrypt/letsencrypt.json', '/letsencrypt/letsencrypt_expired.json']
@@ -21,17 +19,17 @@ OTHER_URLS = ['/sectigo_ev/index.html', '/sectigo_ov/index.html',
 
 def create_json(provider):
     """ runs cert2json """
-    script_cmd = ["cert2json.sh", provider]
-    sp.Popen(script_cmd, stdout=sp.PIPE, stderr=sp.PIPE)
+    sp.Popen(["cert2json.sh", provider], stdout=sp.PIPE, stderr=sp.PIPE)
 
 
 @APP.route('/<provider>')
+@APP.route('/<provider>/<filename>')
 @APP.route('/static/<filename>')
 @APP.route('/')
 def index(provider=None, filename=None):
-    """ check the git environment and run g10k """
+    """ run cert2json.py and serve ACME static files """
     url_name = str(request.path)
-    print("provider is {}".format(provider))
+    host_name = str(request.host_url)
 
     if url_name == '/':
         return render_template("index.html")
@@ -39,17 +37,12 @@ def index(provider=None, filename=None):
     if url_name.startswith("/static") or url_name.startswith("static"):
         return send_from_directory(APP.static_folder, filename)
 
-    if url_name in BASE_URLS:
-        stripped_url = url_name.replace("/", "")
-        create_json(stripped_url)
-        return render_template("{}/index.html".format(stripped_url))
-
     if url_name in OTHER_URLS:
-        stripped_base_url = url_name.split("/")[0]
-        stripped_url = url_name.split("/")[1]
-        create_json(stripped_url)
-        print("{}/{}".format(stripped_base_url, stripped_url))
-        return render_template("{}/{}".format(stripped_base_url, stripped_url))
+        create_json(filename)
+        return render_template("{}/{}".format(provider, filename))
+
+    if url_name in BASE_URLS:
+        return redirect("{}{}/index.html".format(host_name, provider), code=302)
 
     return "Oh no! NOT Found", 404
 
diff --git a/templates/index.html.epp b/templates/index.html.epp
index a333cf1..f8153c8 100644
--- a/templates/index.html.epp
+++ b/templates/index.html.epp
@@ -24,9 +24,9 @@
         <table class="table" id="patching">
           <thead>
             <tr>
-              <th><a href="<%= $provider %>/index.html">Certificate name</a></th>
+              <th><a href="index.html">Certificate name</a></th>
               <th>Domains (SANs)</th>
-              <th><a href="<%= $provider %>/expiry_date.html">Expiry Date</a></th>
+              <th><a href="expiry_date.html">Expiry Date</a></th>
               <th>Serial Number</th>
               <th>&nbsp;</th>
             </tr>
-- 
GitLab