Projekt

Obecné

Profil

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

    
3
from flask import Flask, redirect
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.services.cryptography import CryptographyService, CryptographyException
11

    
12

    
13
class X509ManagementApp(Flask):
14

    
15
    def run(self, host=None, port=None, debug=None, load_dotenv=True, **options):
16
        with self.app_context():
17
            if not initialize_app():
18
                return
19
        super(X509ManagementApp, self).run(host=host, port=port, debug=debug, load_dotenv=load_dotenv, **options)
20

    
21

    
22
app = X509ManagementApp(__name__)
23

    
24

    
25
@app.route('/')
26
def index():
27
    return redirect("/static/index.html")
28

    
29

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

    
34

    
35
@app.route('/api/certificates', methods=["GET"])
36
def get_cert_list(certificate_controller: CertController):
37
    return certificate_controller.get_certificate_list()
38

    
39

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

    
44

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

    
49

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

    
54

    
55
@app.route('/api/certificates/<id>/chain', methods=["GET"])
56
def get_cert_chain(id, certificate_controller: CertController):
57
    print("Get cert chain via flask injector")
58
    return certificate_controller.get_certificate_trust_chain_by_id(id)
59

    
60

    
61
def initialize_app() -> bool:
62
    """
63
    Initializes the application
64
        -   configure dependency injection
65
        -   check whether OpenSSL is on the system
66
    :return: boolean flag indicating whether initialization was successfull or not
67
    """
68

    
69
    modules = [configuration.configure_env_variable, ConnectionProvider]
70
    injector = Injector(modules)
71
    FlaskInjector(app=app, modules=modules)
72

    
73
    # There's a little dependency on the CryptoService, which is not a pretty thing from
74
    # architectural point of view. However it is only a minimal piece of code and
75
    # it makes sense to do it in this way instead of trying to run openssl via subprocess here
76
    cryptography_service = injector.get(CryptographyService)
77
    try:
78
        # if version string is returned, OpenSSL is present on the system
79
        print(f"Using {cryptography_service.get_openssl_version()}")
80
        # TODO log the version instead of prining it out
81
        return True
82
    except CryptographyException:
83
        # If getting the version string throws an exception the OpenSSL is not available
84
        print("OpenSSL was not located on the system. Application will now exit.")
85
        # TODO add logging here
86
        return False
87

    
88

    
89
if __name__ == '__main__':
90
    app_host = "0.0.0.0"
91
    app_port = 5000
92

    
93
    # TODO better load this from config.py
94
    if "FLASK_HOST" in os.environ:
95
        app_host = os.environ["FLASK_HOST"]
96

    
97
    if "FLASK_PORT" in os.environ:
98
        app_host = os.environ["FLASK_PORT"]
99

    
100
    app.run(host=app_host, port=app_port)
(6-6/10)