Skip to content
Snippets Groups Projects
geant_acme_web.py 1.79 KiB
#!/usr/bin/env python3
"""
    run flask CGI that will trigger cert2json.py -p <provider>
"""
import subprocess
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')
BASE_URLS = ['/sectigo_ev', '/sectigo_ov', '/letsencrypt',
             '/sectigo_ev/', '/sectigo_ov/', '/letsencrypt/']
OTHER_URLS = ['/letsencrypt/index.html', '/letsencrypt/expiry_date.html',
              '/letsencrypt/letsencrypt.json', '/letsencrypt/letsencrypt_expired.json',
              '/sectigo_ov/index.html', '/sectigo_ov/expiry_date.html',
              '/sectigo_ov/sectigo_ov.json', '/sectigo_ov/sectigo_ov_expired.json',
              '/sectigo_ev/index.html', '/sectigo_ev/expiry_date.html',
              '/sectigo_ev/sectigo_ev.json', '/sectigo_ev/sectigo_ev_expired.json']


@APP.route('/<provider>')
@APP.route('/<provider>/<filename>')
@APP.route('/static/<filename>')
@APP.route('/')
def index(provider=None, filename=None):
    """ run cert2json.py and serve ACME static files """
    url_name = str(request.path)
    host_name = str(request.host_url)

    if url_name == '/':
        return render_template("index.html")

    if url_name.startswith("/static") or url_name.startswith("static"):
        return send_from_directory(APP.static_folder, filename)

    if url_name in OTHER_URLS:
        subprocess.call(["cert2json.sh", provider])
        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


if __name__ == '__main__':

    APP.run(debug=False, threaded=True, host='::', port=8000)