Projekt

Obecné

Profil

Stáhnout (5.64 KB) Statistiky
| Větev: | Tag: | Revize:
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.exceptions.database_exception import DatabaseException
13
from src.services.cryptography import CryptographyService, CryptographyException
14
from src.utils.logger import Logger
15

    
16
app = Flask(__name__)
17

    
18

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

    
23

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

    
28

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

    
33

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

    
38

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

    
43

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

    
48

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

    
53

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

    
58

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

    
63

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

    
68

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

    
73

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

    
78

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

    
83

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

    
88

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

    
93

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

    
98

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

    
103

    
104
def initialize_app(application) -> bool:
105
    """
106
    Initializes the application
107
        -   configure dependency injection
108
        -   check whether OpenSSL is on the system
109
    :param application Flask Application to be initialized.
110
    :return: boolean flag indicating whether initialization was successful or not
111
    """
112
    modules = [configuration.configure_env_variable, ConnectionProvider]
113
    injector = Injector(modules)
114
    FlaskInjector(app=application, modules=modules)
115

    
116
    config = injector.get(Configuration)
117
    configuration.configure_logging(config)
118

    
119
    Logger.info(f"Using configuration file: {config.config_file}")
120

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

    
135

    
136
# app initialization must follow endpoint declaration (after all Flask decoration)
137
with app.app_context():
138
    if not initialize_app(app):
139
        print("Failed to initialize app, aborting...")
140
        Logger.error(f"Failed to initialize app, aborting...")
141
        exit(-1)
142

    
143
if __name__ == '__main__':
144
    app_host = "0.0.0.0"
145
    app_port = 5000
146

    
147
    # TODO better load this from config.py
148
    if "FLASK_HOST" in os.environ:
149
        app_host = os.environ["FLASK_HOST"]
150

    
151
    if "FLASK_PORT" in os.environ:
152
        app_host = os.environ["FLASK_PORT"]
153

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