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