Projekt

Obecné

Profil

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

    
3
from flask import Flask, redirect
4
from injector import Injector
5

    
6
from src.config import configuration
7
from src.config.connection_provider import ConnectionProvider
8
from src.controllers.certificates_controller import CertController
9
from src.services.cryptography import CryptographyService, CryptographyException
10

    
11

    
12
class X509ManagementApp(Flask):
13

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

    
20

    
21
app = X509ManagementApp(__name__)
22
certificate_controller: CertController = None
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():
32
    return certificate_controller.create_certificate()
33

    
34

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

    
39

    
40
@app.route('/api/certificates/<id>', methods=["GET"])
41
def get_cert(id):
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):
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):
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):
57
    return certificate_controller.get_certificate_trust_chain_by_id(id)
58

    
59

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

    
69
    injector = Injector([configuration.configure_default, ConnectionProvider])
70
    certificate_controller = injector.get(CertController)
71

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

    
87

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

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

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

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