-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
80 lines (60 loc) · 2.88 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from http import HTTPStatus
from typing import List
from flask import Flask, Request, Response, request
from config_processors.config_processor import ConfigProcessor
from config_processors.config_processors_initializer import \
ConfigProcessorsInitializer
from utils.config_loader import ConfigLoader
from utils.data_validator import validate_data
from utils.signature_validator import SignatureValidator
def set_flask_config_options(app: Flask, app_cfg) -> None:
app.config["HOST"] = app_cfg.get("host", "0.0.0.0")
app.config["PORT"] = app_cfg.get("port", 5000)
def get_flask_app() -> Flask:
processors_cfg = ConfigLoader.load_config()
app = Flask(__name__)
set_flask_config_options(app, processors_cfg.get("flask_settings", {}))
config_processors_initializer = ConfigProcessorsInitializer(processors_cfg)
signature_validator = SignatureValidator(processors_cfg)
config_processors = config_processors_initializer.get_processors()
def get_relevant_config_processors(
request: Request,
) -> List[ConfigProcessor]:
received_hash_id = request.json.get("object", {}).get("id_hash")
relevant_config_processors = []
for config_processor in config_processors:
observed_entity_filters = config_processor.observed_entity_filters
# special case where no entity filters are configured ->
# config_processor is relevant for all entities
if not observed_entity_filters:
relevant_config_processors.append(config_processor)
continue
if received_hash_id in observed_entity_filters:
relevant_config_processors.append(config_processor)
continue
return relevant_config_processors
def update_relevant_configurations(request: Request) -> None:
relevant_config_processors = get_relevant_config_processors(request)
# TODO - handle errors when updating configuration (failed write in
# file etc.)
for config_processor in relevant_config_processors:
config_processor.update_configuration(request)
@app.route("/remote-entity-update", methods=["POST"])
def remote_entity_update():
if not signature_validator.has_valid_signature(request):
return Response(
"Invalid signature on received data",
status=HTTPStatus.UNAUTHORIZED,
)
validator_result = validate_data(request)
if not validator_result.has_valid_data:
return Response(
f"Invalid data: {validator_result.message}",
status=HTTPStatus.BAD_REQUEST,
)
update_relevant_configurations(request)
return Response("Webhook received.", status=HTTPStatus.OK)
return app
if __name__ == "__main__":
app = get_flask_app()
app.run(host=app.config["HOST"], port=app.config["PORT"])