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