Projekt

Obecné

Profil

Stáhnout (3.76 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
@app.route('/api/certificates/<id>/private_key', methods=["GET"])
50
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
51
    return certificate_controller.get_private_key_of_a_certificate(id)
52

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

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

    
66
    modules = [configuration.configure_env_variable, ConnectionProvider]
67
    injector = Injector(modules)
68
    FlaskInjector(app=application, modules=modules)
69

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

    
85

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

    
93
if __name__ == '__main__':
94
    app_host = "0.0.0.0"
95
    app_port = 5000
96

    
97
    # TODO better load this from config.py
98
    if "FLASK_HOST" in os.environ:
99
        app_host = os.environ["FLASK_HOST"]
100

    
101
    if "FLASK_PORT" in os.environ:
102
        app_host = os.environ["FLASK_PORT"]
103

    
104
    app.run(host=app_host, port=app_port)
(6-6/11)