Projekt

Obecné

Profil

Stáhnout (5.98 KB) Statistiky
| Větev: | Tag: | Revize:
1
import os
2

    
3
from flask import Flask, redirect, request
4
from flask_injector import FlaskInjector
5
from injector import Injector
6
from werkzeug.exceptions import HTTPException
7

    
8
from src.config import configuration
9
from src.config.configuration import Configuration
10
from src.config.connection_provider import ConnectionProvider
11
from src.controllers.certificates_controller import CertController
12
from src.controllers.crl_ocsp_controller import CrlOcspController
13
from src.exceptions.database_exception import DatabaseException
14
from src.services.cryptography import CryptographyService, CryptographyException
15
from src.utils.logger import Logger
16

    
17
app = Flask(__name__)
18

    
19

    
20
@app.route('/')
21
def index():
22
    return redirect("/static/index.html")
23

    
24

    
25
@app.route('/api/certificates', methods=["POST"])
26
def create_certificate(certificate_controller: CertController):
27
    return certificate_controller.create_certificate()
28

    
29

    
30
@app.route('/api/certificates', methods=["GET"])
31
def get_cert_list(certificate_controller: CertController):
32
    return certificate_controller.get_certificate_list()
33

    
34

    
35
@app.route('/api/certificates/<id>', methods=["GET"])
36
def get_cert(id, certificate_controller: CertController):
37
    return certificate_controller.get_certificate_by_id(id)
38

    
39

    
40
@app.route('/api/certificates/<id>', methods=["PATCH"])
41
def set_certificate_status(id, certificate_controller: CertController):
42
    return certificate_controller.set_certificate_status(id)
43

    
44

    
45
@app.route('/api/certificates/<id>', methods=["DELETE"])
46
def delete_certificate(id, certificate_controller: CertController):
47
    return certificate_controller.delete_certificate(id)
48

    
49

    
50
@app.route('/api/certificates/<id>/details', methods=["GET"])
51
def get_cert_details(id, certificate_controller: CertController):
52
    return certificate_controller.get_certificate_details_by_id(id)
53

    
54

    
55
@app.route('/api/certificates/<id>/root', methods=["GET"])
56
def get_cert_root(id, certificate_controller: CertController):
57
    return certificate_controller.get_certificate_root_by_id(id)
58

    
59

    
60
@app.route('/api/certificates/<id>/chain', methods=["GET"])
61
def get_cert_chain(id, certificate_controller: CertController):
62
    return certificate_controller.get_certificate_trust_chain_by_id(id)
63

    
64

    
65
@app.route('/api/certificates/<id>/privatekey', methods=["GET"])
66
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
67
    return certificate_controller.get_private_key_of_a_certificate(id)
68

    
69

    
70
@app.route('/api/certificates/<id>/publickey', methods=["GET"])
71
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
72
    return certificate_controller.get_public_key_of_a_certificate(id)
73

    
74

    
75
@app.route('/api/crl/<id>', methods=["GET"])
76
def get_crl_of_issuer(id, crl_ocsp_controller: CrlOcspController):
77
    return crl_ocsp_controller.get_crl(id)
78

    
79

    
80
@app.route('/api/ocsp/<id>/<path:ocsp_request>', methods=["GET"])
81
def get_ocsp_of_issuer_get(id, ocsp_request, crl_ocsp_controller: CrlOcspController):
82
    return crl_ocsp_controller.get_ocsp_from_base64(id, ocsp_request)
83

    
84

    
85
@app.route('/api/ocsp/<id>', methods=["POST"])
86
def get_ocsp_of_issuer_post(id, crl_ocsp_controller: CrlOcspController):
87
    return crl_ocsp_controller.get_ocsp_from_der(id, request.data)
88

    
89

    
90
@app.route('/api/certificates/<id>/identity', methods=["POST"])
91
def generate_certificate_pkcs_identity(id, certificate_controller: CertController):
92
    return certificate_controller.generate_certificate_pkcs_identity(id)
93

    
94

    
95
@app.errorhandler(CryptographyException)
96
def cryptography_error(e, certificate_controller: CertController):
97
    return certificate_controller.handle_cryptography_error(e)
98

    
99

    
100
@app.errorhandler(DatabaseException)
101
def database_error(e, certificate_controller: CertController):
102
    return certificate_controller.handle_database_error(e)
103

    
104

    
105
@app.errorhandler(Exception)
106
def generic_exception(e, certificate_controller: CertController):
107
    if isinstance(e, HTTPException):
108
        Logger.warning(f""" HTTPException occurred: "{str(e)}" """)
109
        return str(e), e.code
110
    return certificate_controller.handle_generic_exception(e)
111

    
112

    
113
def initialize_app(application) -> bool:
114
    """
115
    Initializes the application
116
        -   configure dependency injection
117
        -   check whether OpenSSL is on the system
118
    :param application Flask Application to be initialized.
119
    :return: boolean flag indicating whether initialization was successful or not
120
    """
121
    modules = [configuration.configure_env_variable, ConnectionProvider]
122
    injector = Injector(modules)
123
    FlaskInjector(app=application, modules=modules)
124

    
125
    config = injector.get(Configuration)
126
    configuration.configure_logging(config)
127

    
128
    Logger.info(f"Using configuration file: {config.config_file}")
129

    
130
    # There's a little dependency on the CryptoService, which is not a pretty thing from
131
    # architectural point of view. However it is only a minimal piece of code and
132
    # it makes sense to do it in this way instead of trying to run openssl via subprocess here
133
    cryptography_service = injector.get(CryptographyService)
134
    try:
135
        # if version string is returned, OpenSSL is present on the system
136
        Logger.info(f"Using {cryptography_service.get_openssl_version()}")
137
        return True
138
    except CryptographyException:
139
        # If getting the version string throws an exception the OpenSSL is not available
140
        print("OpenSSL was not located on the system. Application will now exit.")
141
        Logger.error(f"OpenSSL was not located on the system. Application will now exit.")
142
        return False
143

    
144

    
145
# app initialization must follow endpoint declaration (after all Flask decoration)
146
with app.app_context():
147
    if not initialize_app(app):
148
        print("Failed to initialize app, aborting...")
149
        Logger.error(f"Failed to initialize app, aborting...")
150
        exit(-1)
151

    
152
if __name__ == '__main__':
153
    app_host = "0.0.0.0"
154
    app_port = 5000
155

    
156
    # TODO better load this from config.py
157
    if "FLASK_HOST" in os.environ:
158
        app_host = os.environ["FLASK_HOST"]
159

    
160
    if "FLASK_PORT" in os.environ:
161
        app_host = os.environ["FLASK_PORT"]
162

    
163
    app.run(host=app_host, port=app_port)
(7-7/12)