-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
48 lines (40 loc) · 1.43 KB
/
app.py
File metadata and controls
48 lines (40 loc) · 1.43 KB
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
import json
import os
from datetime import datetime
from flask import request
from flask_api import FlaskAPI, status
VALID_PRODUCT_NAMES = ['teamware']
app = FlaskAPI(__name__)
try:
proxies = int(os.environ.get('REVERSE_PROXY', '0'))
if proxies > 0:
from werkzeug.middleware.proxy_fix import ProxyFix
# https://flask.palletsprojects.com/en/2.2.x/deploying/proxy_fix/
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=proxies, x_proto=proxies, x_host=proxies, x_prefix=proxies
)
except ValueError:
# REVERSE_PROXY environment variable not parseable as a number, assume no proxy
print("WARNING: invalid value for REVERSE_PROXY in environment - ignored")
@app.post("/phone_home")
def answer_phone_home():
payload = request.data
if valid_payload(payload):
successful_call(payload)
return 'Phone home registered', status.HTTP_201_CREATED
else:
return 'Invalid phone home', status.HTTP_401_UNAUTHORIZED
def successful_call(payload: dict):
""" Successful calls are printed to stdout. """
product = payload.pop('product')
print(json.dumps({
'@timestamp': datetime.now().isoformat(),
'clientIp': request.remote_addr,
'product': product,
product: payload,
}))
def valid_payload(payload: dict) -> bool:
if type(payload) == dict:
return payload.get('product') in VALID_PRODUCT_NAMES
else:
return False