1 |
62d64d21
|
Stanislav Král
|
import os
|
2 |
1fa243ca
|
Jan Pašek
|
|
3 |
|
|
from flask import Flask, redirect
|
4 |
|
|
from injector import Injector
|
5 |
0cf35f70
|
Stanislav Král
|
from flask_injector import FlaskInjector
|
6 |
1fa243ca
|
Jan Pašek
|
|
7 |
|
|
from src.config import configuration
|
8 |
|
|
from src.config.connection_provider import ConnectionProvider
|
9 |
5b57121e
|
Captain_Trojan
|
from src.controllers.certificates_controller import CertController
|
10 |
81dbb479
|
Jan Pašek
|
from src.services.cryptography import CryptographyService, CryptographyException
|
11 |
6e333f54
|
Stanislav Král
|
|
12 |
61535019
|
Stanislav Král
|
app = Flask(__name__)
|
13 |
6e333f54
|
Stanislav Král
|
|
14 |
|
|
|
15 |
|
|
@app.route('/')
|
16 |
|
|
def index():
|
17 |
2e646e3b
|
Jan Pašek
|
return redirect("/static/index.html")
|
18 |
6e333f54
|
Stanislav Král
|
|
19 |
|
|
|
20 |
5b57121e
|
Captain_Trojan
|
@app.route('/api/certificates', methods=["POST"])
|
21 |
0cf35f70
|
Stanislav Král
|
def create_certificate(certificate_controller: CertController):
|
22 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.create_certificate()
|
23 |
5b57121e
|
Captain_Trojan
|
|
24 |
|
|
|
25 |
|
|
@app.route('/api/certificates', methods=["GET"])
|
26 |
0cf35f70
|
Stanislav Král
|
def get_cert_list(certificate_controller: CertController):
|
27 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_list()
|
28 |
5b57121e
|
Captain_Trojan
|
|
29 |
|
|
|
30 |
fb987403
|
Captain_Trojan
|
@app.route('/api/certificates/<id>', methods=["GET"])
|
31 |
0cf35f70
|
Stanislav Král
|
def get_cert(id, certificate_controller: CertController):
|
32 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_by_id(id)
|
33 |
fb987403
|
Captain_Trojan
|
|
34 |
|
|
|
35 |
2cecaf70
|
Jan Pašek
|
@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 |
5b6d9513
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/details', methods=["GET"])
|
41 |
0cf35f70
|
Stanislav Král
|
def get_cert_details(id, certificate_controller: CertController):
|
42 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_details_by_id(id)
|
43 |
5b6d9513
|
Captain_Trojan
|
|
44 |
|
|
|
45 |
d53c2fdc
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/root', methods=["GET"])
|
46 |
0cf35f70
|
Stanislav Král
|
def get_cert_root(id, certificate_controller: CertController):
|
47 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_root_by_id(id)
|
48 |
d53c2fdc
|
Captain_Trojan
|
|
49 |
|
|
|
50 |
aa740737
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/chain', methods=["GET"])
|
51 |
0cf35f70
|
Stanislav Král
|
def get_cert_chain(id, certificate_controller: CertController):
|
52 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_trust_chain_by_id(id)
|
53 |
|
|
|
54 |
ce8b9aaf
|
Stanislav Král
|
@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 |
1fa243ca
|
Jan Pašek
|
|
58 |
cfda1725
|
Stanislav Král
|
@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 |
61535019
|
Stanislav Král
|
def initialize_app(application) -> bool:
|
63 |
81dbb479
|
Jan Pašek
|
"""
|
64 |
|
|
Initializes the application
|
65 |
|
|
- configure dependency injection
|
66 |
|
|
- check whether OpenSSL is on the system
|
67 |
61535019
|
Stanislav Král
|
:param application Flask Application to be initialized.
|
68 |
|
|
:return: boolean flag indicating whether initialization was successful or not
|
69 |
81dbb479
|
Jan Pašek
|
"""
|
70 |
1fa243ca
|
Jan Pašek
|
|
71 |
0cf35f70
|
Stanislav Král
|
modules = [configuration.configure_env_variable, ConnectionProvider]
|
72 |
|
|
injector = Injector(modules)
|
73 |
61535019
|
Stanislav Král
|
FlaskInjector(app=application, modules=modules)
|
74 |
aa740737
|
Captain_Trojan
|
|
75 |
81dbb479
|
Jan Pašek
|
# 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 |
aa740737
|
Captain_Trojan
|
|
91 |
61535019
|
Stanislav Král
|
# 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 |
6e333f54
|
Stanislav Král
|
if __name__ == '__main__':
|
99 |
1fa243ca
|
Jan Pašek
|
app_host = "0.0.0.0"
|
100 |
|
|
app_port = 5000
|
101 |
62d64d21
|
Stanislav Král
|
|
102 |
9832694f
|
Stanislav Král
|
# TODO better load this from config.py
|
103 |
62d64d21
|
Stanislav Král
|
if "FLASK_HOST" in os.environ:
|
104 |
1fa243ca
|
Jan Pašek
|
app_host = os.environ["FLASK_HOST"]
|
105 |
62d64d21
|
Stanislav Král
|
|
106 |
|
|
if "FLASK_PORT" in os.environ:
|
107 |
1fa243ca
|
Jan Pašek
|
app_host = os.environ["FLASK_PORT"]
|
108 |
62d64d21
|
Stanislav Král
|
|
109 |
1fa243ca
|
Jan Pašek
|
app.run(host=app_host, port=app_port)
|