Projekt

Obecné

Profil

Stáhnout (3.55 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
app = Flask(__name__)
13

    
14

    
15
@app.route('/')
16
def index():
17
    return redirect("/static/index.html")
18

    
19

    
20
@app.route('/api/certificates', methods=["POST"])
21
def create_certificate(certificate_controller: CertController):
22
    return certificate_controller.create_certificate()
23

    
24

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

    
29

    
30
@app.route('/api/certificates/<id>', methods=["GET"])
31
def get_cert(id, certificate_controller: CertController):
32
    return certificate_controller.get_certificate_by_id(id)
33

    
34

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

    
39

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

    
44

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

    
49

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

    
54
def initialize_app(application) -> bool:
55
    """
56
    Initializes the application
57
        -   configure dependency injection
58
        -   check whether OpenSSL is on the system
59
    :param application Flask Application to be initialized.
60
    :return: boolean flag indicating whether initialization was successful or not
61
    """
62

    
63
    modules = [configuration.configure_env_variable, ConnectionProvider]
64
    injector = Injector(modules)
65
    FlaskInjector(app=application, modules=modules)
66

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

    
82

    
83
# app initialization must follow endpoint declaration (after all Flask decoration)
84
with app.app_context():
85
    if not initialize_app(app):
86
        # TODO log this
87
        print("Failed to initialize app, aborting...")
88
        exit(-1)
89

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

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

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

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