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