1
|
import os
|
2
|
|
3
|
from flask import Flask, redirect
|
4
|
from injector import Injector
|
5
|
from flask_injector import FlaskInjector
|
6
|
|
7
|
from src.config import configuration
|
8
|
from src.config.connection_provider import ConnectionProvider
|
9
|
from src.controllers.certificates_controller import CertController
|
10
|
from src.services.cryptography import CryptographyService, CryptographyException
|
11
|
|
12
|
app = Flask(__name__)
|
13
|
|
14
|
|
15
|
@app.route('/')
|
16
|
def index():
|
17
|
return redirect("/static/index.html")
|
18
|
|
19
|
|
20
|
@app.route('/api/certificates', methods=["POST"])
|
21
|
def create_certificate(certificate_controller: CertController):
|
22
|
return certificate_controller.create_certificate()
|
23
|
|
24
|
|
25
|
@app.route('/api/certificates', methods=["GET"])
|
26
|
def get_cert_list(certificate_controller: CertController):
|
27
|
return certificate_controller.get_certificate_list()
|
28
|
|
29
|
|
30
|
@app.route('/api/certificates/<id>', methods=["GET"])
|
31
|
def get_cert(id, certificate_controller: CertController):
|
32
|
return certificate_controller.get_certificate_by_id(id)
|
33
|
|
34
|
|
35
|
@app.route('/api/certificates/<id>', methods=["PATCH"])
|
36
|
def set_certificate_status(id, certificate_controller: CertController):
|
37
|
return certificate_controller.set_certificate_status(id)
|
38
|
|
39
|
|
40
|
@app.route('/api/certificates/<id>/details', methods=["GET"])
|
41
|
def get_cert_details(id, certificate_controller: CertController):
|
42
|
return certificate_controller.get_certificate_details_by_id(id)
|
43
|
|
44
|
|
45
|
@app.route('/api/certificates/<id>/root', methods=["GET"])
|
46
|
def get_cert_root(id, certificate_controller: CertController):
|
47
|
return certificate_controller.get_certificate_root_by_id(id)
|
48
|
|
49
|
|
50
|
@app.route('/api/certificates/<id>/chain', methods=["GET"])
|
51
|
def get_cert_chain(id, certificate_controller: CertController):
|
52
|
return certificate_controller.get_certificate_trust_chain_by_id(id)
|
53
|
|
54
|
@app.route('/api/certificates/<id>/private_key', methods=["GET"])
|
55
|
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
|
56
|
return certificate_controller.get_private_key_of_a_certificate(id)
|
57
|
|
58
|
@app.route('/api/certificates/<id>/public_key', methods=["GET"])
|
59
|
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
|
60
|
return certificate_controller.get_public_key_of_a_certificate(id)
|
61
|
|
62
|
def initialize_app(application) -> bool:
|
63
|
"""
|
64
|
Initializes the application
|
65
|
- configure dependency injection
|
66
|
- check whether OpenSSL is on the system
|
67
|
:param application Flask Application to be initialized.
|
68
|
:return: boolean flag indicating whether initialization was successful or not
|
69
|
"""
|
70
|
|
71
|
modules = [configuration.configure_env_variable, ConnectionProvider]
|
72
|
injector = Injector(modules)
|
73
|
FlaskInjector(app=application, modules=modules)
|
74
|
|
75
|
# There's a little dependency on the CryptoService, which is not a pretty thing from
|
76
|
# architectural point of view. However it is only a minimal piece of code and
|
77
|
# it makes sense to do it in this way instead of trying to run openssl via subprocess here
|
78
|
cryptography_service = injector.get(CryptographyService)
|
79
|
try:
|
80
|
# if version string is returned, OpenSSL is present on the system
|
81
|
print(f"Using {cryptography_service.get_openssl_version()}")
|
82
|
# TODO log the version instead of prining it out
|
83
|
return True
|
84
|
except CryptographyException:
|
85
|
# If getting the version string throws an exception the OpenSSL is not available
|
86
|
print("OpenSSL was not located on the system. Application will now exit.")
|
87
|
# TODO add logging here
|
88
|
return False
|
89
|
|
90
|
|
91
|
# app initialization must follow endpoint declaration (after all Flask decoration)
|
92
|
with app.app_context():
|
93
|
if not initialize_app(app):
|
94
|
# TODO log this
|
95
|
print("Failed to initialize app, aborting...")
|
96
|
exit(-1)
|
97
|
|
98
|
if __name__ == '__main__':
|
99
|
app_host = "0.0.0.0"
|
100
|
app_port = 5000
|
101
|
|
102
|
# TODO better load this from config.py
|
103
|
if "FLASK_HOST" in os.environ:
|
104
|
app_host = os.environ["FLASK_HOST"]
|
105
|
|
106
|
if "FLASK_PORT" in os.environ:
|
107
|
app_host = os.environ["FLASK_PORT"]
|
108
|
|
109
|
app.run(host=app_host, port=app_port)
|