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
|
|
13
|
app = Flask(__name__)
|
14
|
|
15
|
|
16
|
@app.route('/')
|
17
|
def index():
|
18
|
return redirect("/static/index.html")
|
19
|
|
20
|
|
21
|
@app.route('/api/certificates', methods=["POST"])
|
22
|
def create_certificate(certificate_controller: CertController):
|
23
|
return certificate_controller.create_certificate()
|
24
|
|
25
|
|
26
|
@app.route('/api/certificates', methods=["GET"])
|
27
|
def get_cert_list(certificate_controller: CertController):
|
28
|
return certificate_controller.get_certificate_list()
|
29
|
|
30
|
|
31
|
@app.route('/api/certificates/<id>', methods=["GET"])
|
32
|
def get_cert(id, certificate_controller: CertController):
|
33
|
return certificate_controller.get_certificate_by_id(id)
|
34
|
|
35
|
|
36
|
@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
|
@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
|
@app.route('/api/certificates/<id>/details', methods=["GET"])
|
47
|
def get_cert_details(id, certificate_controller: CertController):
|
48
|
return certificate_controller.get_certificate_details_by_id(id)
|
49
|
|
50
|
|
51
|
@app.route('/api/certificates/<id>/root', methods=["GET"])
|
52
|
def get_cert_root(id, certificate_controller: CertController):
|
53
|
return certificate_controller.get_certificate_root_by_id(id)
|
54
|
|
55
|
|
56
|
@app.route('/api/certificates/<id>/chain', methods=["GET"])
|
57
|
def get_cert_chain(id, certificate_controller: CertController):
|
58
|
return certificate_controller.get_certificate_trust_chain_by_id(id)
|
59
|
|
60
|
|
61
|
@app.route('/api/certificates/<id>/privatekey', methods=["GET"])
|
62
|
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
|
63
|
return certificate_controller.get_private_key_of_a_certificate(id)
|
64
|
|
65
|
|
66
|
@app.route('/api/certificates/<id>/publickey', methods=["GET"])
|
67
|
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
|
68
|
return certificate_controller.get_public_key_of_a_certificate(id)
|
69
|
|
70
|
|
71
|
@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
|
@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
|
def initialize_app(application) -> bool:
|
87
|
"""
|
88
|
Initializes the application
|
89
|
- configure dependency injection
|
90
|
- check whether OpenSSL is on the system
|
91
|
:param application Flask Application to be initialized.
|
92
|
:return: boolean flag indicating whether initialization was successful or not
|
93
|
"""
|
94
|
|
95
|
modules = [configuration.configure_env_variable, ConnectionProvider]
|
96
|
injector = Injector(modules)
|
97
|
FlaskInjector(app=application, modules=modules)
|
98
|
|
99
|
# 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
|
|
115
|
# 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
|
if __name__ == '__main__':
|
123
|
app_host = "0.0.0.0"
|
124
|
app_port = 5000
|
125
|
|
126
|
# TODO better load this from config.py
|
127
|
if "FLASK_HOST" in os.environ:
|
128
|
app_host = os.environ["FLASK_HOST"]
|
129
|
|
130
|
if "FLASK_PORT" in os.environ:
|
131
|
app_host = os.environ["FLASK_PORT"]
|
132
|
|
133
|
app.run(host=app_host, port=app_port)
|