1 |
62d64d21
|
Stanislav Král
|
import os
|
2 |
1fa243ca
|
Jan Pašek
|
|
3 |
b543de69
|
Captain_Trojan
|
from flask import Flask, redirect, request
|
4 |
1fa243ca
|
Jan Pašek
|
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 |
b1fa358f
|
Jan Pašek
|
from src.controllers.crl_ocsp_controller import CrlOcspController
|
11 |
81dbb479
|
Jan Pašek
|
from src.services.cryptography import CryptographyService, CryptographyException
|
12 |
6e333f54
|
Stanislav Král
|
|
13 |
61535019
|
Stanislav Král
|
app = Flask(__name__)
|
14 |
6e333f54
|
Stanislav Král
|
|
15 |
|
|
|
16 |
|
|
@app.route('/')
|
17 |
|
|
def index():
|
18 |
2e646e3b
|
Jan Pašek
|
return redirect("/static/index.html")
|
19 |
6e333f54
|
Stanislav Král
|
|
20 |
|
|
|
21 |
5b57121e
|
Captain_Trojan
|
@app.route('/api/certificates', methods=["POST"])
|
22 |
0cf35f70
|
Stanislav Král
|
def create_certificate(certificate_controller: CertController):
|
23 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.create_certificate()
|
24 |
5b57121e
|
Captain_Trojan
|
|
25 |
|
|
|
26 |
|
|
@app.route('/api/certificates', methods=["GET"])
|
27 |
0cf35f70
|
Stanislav Král
|
def get_cert_list(certificate_controller: CertController):
|
28 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_list()
|
29 |
5b57121e
|
Captain_Trojan
|
|
30 |
|
|
|
31 |
fb987403
|
Captain_Trojan
|
@app.route('/api/certificates/<id>', methods=["GET"])
|
32 |
0cf35f70
|
Stanislav Král
|
def get_cert(id, certificate_controller: CertController):
|
33 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_by_id(id)
|
34 |
fb987403
|
Captain_Trojan
|
|
35 |
|
|
|
36 |
2cecaf70
|
Jan Pašek
|
@app.route('/api/certificates/<id>', methods=["PATCH"])
|
37 |
|
|
def set_certificate_status(id, certificate_controller: CertController):
|
38 |
|
|
return certificate_controller.set_certificate_status(id)
|
39 |
|
|
|
40 |
|
|
|
41 |
f808fd93
|
Captain_Trojan
|
@app.route('/api/certificates/<id>', methods=["DELETE"])
|
42 |
|
|
def delete_certificate(id, certificate_controller: CertController):
|
43 |
|
|
return certificate_controller.delete_certificate(id)
|
44 |
|
|
|
45 |
|
|
|
46 |
5b6d9513
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/details', methods=["GET"])
|
47 |
0cf35f70
|
Stanislav Král
|
def get_cert_details(id, certificate_controller: CertController):
|
48 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_details_by_id(id)
|
49 |
5b6d9513
|
Captain_Trojan
|
|
50 |
|
|
|
51 |
d53c2fdc
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/root', methods=["GET"])
|
52 |
0cf35f70
|
Stanislav Král
|
def get_cert_root(id, certificate_controller: CertController):
|
53 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_root_by_id(id)
|
54 |
d53c2fdc
|
Captain_Trojan
|
|
55 |
|
|
|
56 |
aa740737
|
Captain_Trojan
|
@app.route('/api/certificates/<id>/chain', methods=["GET"])
|
57 |
0cf35f70
|
Stanislav Král
|
def get_cert_chain(id, certificate_controller: CertController):
|
58 |
1fa243ca
|
Jan Pašek
|
return certificate_controller.get_certificate_trust_chain_by_id(id)
|
59 |
|
|
|
60 |
f808fd93
|
Captain_Trojan
|
|
61 |
780c6d9c
|
Jan Pašek
|
@app.route('/api/certificates/<id>/privatekey', methods=["GET"])
|
62 |
ce8b9aaf
|
Stanislav Král
|
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
|
63 |
|
|
return certificate_controller.get_private_key_of_a_certificate(id)
|
64 |
1fa243ca
|
Jan Pašek
|
|
65 |
f808fd93
|
Captain_Trojan
|
|
66 |
780c6d9c
|
Jan Pašek
|
@app.route('/api/certificates/<id>/publickey', methods=["GET"])
|
67 |
cfda1725
|
Stanislav Král
|
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
|
68 |
|
|
return certificate_controller.get_public_key_of_a_certificate(id)
|
69 |
|
|
|
70 |
f808fd93
|
Captain_Trojan
|
|
71 |
b1fa358f
|
Jan Pašek
|
@app.route('/api/crl/<id>', methods=["GET"])
|
72 |
|
|
def get_crl_of_issuer(id, crl_ocsp_controller: CrlOcspController):
|
73 |
|
|
return crl_ocsp_controller.get_crl(id)
|
74 |
|
|
|
75 |
|
|
|
76 |
b543de69
|
Captain_Trojan
|
@app.route('/api/ocsp/<id>/<path:ocsp_request>', methods=["GET"])
|
77 |
|
|
def get_ocsp_of_issuer_get(id, ocsp_request, crl_ocsp_controller: CrlOcspController):
|
78 |
|
|
return crl_ocsp_controller.get_ocsp_from_base64(id, ocsp_request)
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
@app.route('/api/ocsp/<id>', methods=["POST"])
|
82 |
|
|
def get_ocsp_of_issuer_post(id, crl_ocsp_controller: CrlOcspController):
|
83 |
|
|
return crl_ocsp_controller.get_ocsp_from_der(id, request.data)
|
84 |
|
|
|
85 |
|
|
|
86 |
61535019
|
Stanislav Král
|
def initialize_app(application) -> bool:
|
87 |
81dbb479
|
Jan Pašek
|
"""
|
88 |
|
|
Initializes the application
|
89 |
|
|
- configure dependency injection
|
90 |
|
|
- check whether OpenSSL is on the system
|
91 |
61535019
|
Stanislav Král
|
:param application Flask Application to be initialized.
|
92 |
|
|
:return: boolean flag indicating whether initialization was successful or not
|
93 |
81dbb479
|
Jan Pašek
|
"""
|
94 |
1fa243ca
|
Jan Pašek
|
|
95 |
0cf35f70
|
Stanislav Král
|
modules = [configuration.configure_env_variable, ConnectionProvider]
|
96 |
|
|
injector = Injector(modules)
|
97 |
61535019
|
Stanislav Král
|
FlaskInjector(app=application, modules=modules)
|
98 |
aa740737
|
Captain_Trojan
|
|
99 |
81dbb479
|
Jan Pašek
|
# There's a little dependency on the CryptoService, which is not a pretty thing from
|
100 |
|
|
# architectural point of view. However it is only a minimal piece of code and
|
101 |
|
|
# it makes sense to do it in this way instead of trying to run openssl via subprocess here
|
102 |
|
|
cryptography_service = injector.get(CryptographyService)
|
103 |
|
|
try:
|
104 |
|
|
# if version string is returned, OpenSSL is present on the system
|
105 |
|
|
print(f"Using {cryptography_service.get_openssl_version()}")
|
106 |
|
|
# TODO log the version instead of prining it out
|
107 |
|
|
return True
|
108 |
|
|
except CryptographyException:
|
109 |
|
|
# If getting the version string throws an exception the OpenSSL is not available
|
110 |
|
|
print("OpenSSL was not located on the system. Application will now exit.")
|
111 |
|
|
# TODO add logging here
|
112 |
|
|
return False
|
113 |
|
|
|
114 |
aa740737
|
Captain_Trojan
|
|
115 |
61535019
|
Stanislav Král
|
# app initialization must follow endpoint declaration (after all Flask decoration)
|
116 |
|
|
with app.app_context():
|
117 |
|
|
if not initialize_app(app):
|
118 |
|
|
# TODO log this
|
119 |
|
|
print("Failed to initialize app, aborting...")
|
120 |
|
|
exit(-1)
|
121 |
|
|
|
122 |
6e333f54
|
Stanislav Král
|
if __name__ == '__main__':
|
123 |
1fa243ca
|
Jan Pašek
|
app_host = "0.0.0.0"
|
124 |
|
|
app_port = 5000
|
125 |
62d64d21
|
Stanislav Král
|
|
126 |
9832694f
|
Stanislav Král
|
# TODO better load this from config.py
|
127 |
62d64d21
|
Stanislav Král
|
if "FLASK_HOST" in os.environ:
|
128 |
1fa243ca
|
Jan Pašek
|
app_host = os.environ["FLASK_HOST"]
|
129 |
62d64d21
|
Stanislav Král
|
|
130 |
|
|
if "FLASK_PORT" in os.environ:
|
131 |
1fa243ca
|
Jan Pašek
|
app_host = os.environ["FLASK_PORT"]
|
132 |
62d64d21
|
Stanislav Král
|
|
133 |
1fa243ca
|
Jan Pašek
|
app.run(host=app_host, port=app_port)
|