Projekt

Obecné

Profil

Stáhnout (4.35 KB) Statistiky
| Větev: | Tag: | Revize:
1 62d64d21 Stanislav Král
import os
2 1fa243ca Jan Pašek
3
from flask import Flask, redirect
4
from injector import Injector
5 0cf35f70 Stanislav Král
from flask_injector import FlaskInjector
6 1fa243ca Jan Pašek
7
from src.config import configuration
8
from src.config.connection_provider import ConnectionProvider
9 5b57121e Captain_Trojan
from src.controllers.certificates_controller import CertController
10 b1fa358f Jan Pašek
from src.controllers.crl_ocsp_controller import CrlOcspController
11 81dbb479 Jan Pašek
from src.services.cryptography import CryptographyService, CryptographyException
12 6e333f54 Stanislav Král
13 61535019 Stanislav Král
app = Flask(__name__)
14 6e333f54 Stanislav Král
15
16
@app.route('/')
17
def index():
18 2e646e3b Jan Pašek
    return redirect("/static/index.html")
19 6e333f54 Stanislav Král
20
21 5b57121e Captain_Trojan
@app.route('/api/certificates', methods=["POST"])
22 0cf35f70 Stanislav Král
def create_certificate(certificate_controller: CertController):
23 1fa243ca Jan Pašek
    return certificate_controller.create_certificate()
24 5b57121e Captain_Trojan
25
26
@app.route('/api/certificates', methods=["GET"])
27 0cf35f70 Stanislav Král
def get_cert_list(certificate_controller: CertController):
28 1fa243ca Jan Pašek
    return certificate_controller.get_certificate_list()
29 5b57121e Captain_Trojan
30
31 fb987403 Captain_Trojan
@app.route('/api/certificates/<id>', methods=["GET"])
32 0cf35f70 Stanislav Král
def get_cert(id, certificate_controller: CertController):
33 1fa243ca Jan Pašek
    return certificate_controller.get_certificate_by_id(id)
34 fb987403 Captain_Trojan
35
36 2cecaf70 Jan Pašek
@app.route('/api/certificates/<id>', methods=["PATCH"])
37
def set_certificate_status(id, certificate_controller: CertController):
38
    return certificate_controller.set_certificate_status(id)
39
40
41 f808fd93 Captain_Trojan
@app.route('/api/certificates/<id>', methods=["DELETE"])
42
def delete_certificate(id, certificate_controller: CertController):
43
    return certificate_controller.delete_certificate(id)
44
45
46 5b6d9513 Captain_Trojan
@app.route('/api/certificates/<id>/details', methods=["GET"])
47 0cf35f70 Stanislav Král
def get_cert_details(id, certificate_controller: CertController):
48 1fa243ca Jan Pašek
    return certificate_controller.get_certificate_details_by_id(id)
49 5b6d9513 Captain_Trojan
50
51 d53c2fdc Captain_Trojan
@app.route('/api/certificates/<id>/root', methods=["GET"])
52 0cf35f70 Stanislav Král
def get_cert_root(id, certificate_controller: CertController):
53 1fa243ca Jan Pašek
    return certificate_controller.get_certificate_root_by_id(id)
54 d53c2fdc Captain_Trojan
55
56 aa740737 Captain_Trojan
@app.route('/api/certificates/<id>/chain', methods=["GET"])
57 0cf35f70 Stanislav Král
def get_cert_chain(id, certificate_controller: CertController):
58 1fa243ca Jan Pašek
    return certificate_controller.get_certificate_trust_chain_by_id(id)
59
60 f808fd93 Captain_Trojan
61 ce8b9aaf Stanislav Král
@app.route('/api/certificates/<id>/private_key', methods=["GET"])
62
def get_private_key_of_a_certificate(id, certificate_controller: CertController):
63
    return certificate_controller.get_private_key_of_a_certificate(id)
64 1fa243ca Jan Pašek
65 f808fd93 Captain_Trojan
66 cfda1725 Stanislav Král
@app.route('/api/certificates/<id>/public_key', methods=["GET"])
67
def get_public_key_of_a_certificate(id, certificate_controller: CertController):
68
    return certificate_controller.get_public_key_of_a_certificate(id)
69
70 f808fd93 Captain_Trojan
71 b1fa358f Jan Pašek
@app.route('/api/crl/<id>', methods=["GET"])
72
def get_crl_of_issuer(id, crl_ocsp_controller: CrlOcspController):
73
    return crl_ocsp_controller.get_crl(id)
74
75
76 61535019 Stanislav Král
def initialize_app(application) -> bool:
77 81dbb479 Jan Pašek
    """
78
    Initializes the application
79
        -   configure dependency injection
80
        -   check whether OpenSSL is on the system
81 61535019 Stanislav Král
    :param application Flask Application to be initialized.
82
    :return: boolean flag indicating whether initialization was successful or not
83 81dbb479 Jan Pašek
    """
84 1fa243ca Jan Pašek
85 0cf35f70 Stanislav Král
    modules = [configuration.configure_env_variable, ConnectionProvider]
86
    injector = Injector(modules)
87 61535019 Stanislav Král
    FlaskInjector(app=application, modules=modules)
88 aa740737 Captain_Trojan
89 81dbb479 Jan Pašek
    # There's a little dependency on the CryptoService, which is not a pretty thing from
90
    # architectural point of view. However it is only a minimal piece of code and
91
    # it makes sense to do it in this way instead of trying to run openssl via subprocess here
92
    cryptography_service = injector.get(CryptographyService)
93
    try:
94
        # if version string is returned, OpenSSL is present on the system
95
        print(f"Using {cryptography_service.get_openssl_version()}")
96
        # TODO log the version instead of prining it out
97
        return True
98
    except CryptographyException:
99
        # If getting the version string throws an exception the OpenSSL is not available
100
        print("OpenSSL was not located on the system. Application will now exit.")
101
        # TODO add logging here
102
        return False
103
104 aa740737 Captain_Trojan
105 61535019 Stanislav Král
# app initialization must follow endpoint declaration (after all Flask decoration)
106
with app.app_context():
107
    if not initialize_app(app):
108
        # TODO log this
109
        print("Failed to initialize app, aborting...")
110
        exit(-1)
111
112 6e333f54 Stanislav Král
if __name__ == '__main__':
113 1fa243ca Jan Pašek
    app_host = "0.0.0.0"
114
    app_port = 5000
115 62d64d21 Stanislav Král
116 9832694f Stanislav Král
    # TODO better load this from config.py
117 62d64d21 Stanislav Král
    if "FLASK_HOST" in os.environ:
118 1fa243ca Jan Pašek
        app_host = os.environ["FLASK_HOST"]
119 62d64d21 Stanislav Král
120
    if "FLASK_PORT" in os.environ:
121 1fa243ca Jan Pašek
        app_host = os.environ["FLASK_PORT"]
122 62d64d21 Stanislav Král
123 1fa243ca Jan Pašek
    app.run(host=app_host, port=app_port)