-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
69 lines (57 loc) · 2.78 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
import config
from urllib.parse import parse_qs, urlencode, quote
from requests import session as requests_session
from flask import Flask, Response, request
from aws_auth import AWSAuth
app = Flask(__name__)
PROXY_REQ_HEADERS_WHITELIST = ["content-type"]
PROXY_RESP_HEADERS_BLACKLIST = ["connection", "content-length",
"content-encoding", "transfer-encoding"]
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>", methods=["HEAD", "GET", "POST", "PUT", "DELETE"])
def elastic(path):
proxy_request_headers = {"kbn-xsrf": "reporting"}
requests_response = None
response = Response()
session = requests_session()
session.headers["Connection"] = "close"
auth = AWSAuth(config.aws_credentials, config.aws_region)
query_string = urlencode(
parse_qs(request.query_string.decode("utf-8"), keep_blank_values=True),
quote_via=quote, doseq=True
)
endpoint = config.aws_endpoint + "/" + path + "?" + query_string
for header, value in request.headers.items():
if header.lower() in PROXY_REQ_HEADERS_WHITELIST:
proxy_request_headers[header] = value
if request.method == "HEAD":
session_response = session.head(endpoint, auth=auth,
cookies=request.cookies,
headers=proxy_request_headers)
elif request.method == "GET":
session_response = session.get(endpoint, auth=auth,
cookies=request.cookies,
headers=proxy_request_headers)
elif request.method == "POST":
session_response = session.post(endpoint, cookies=request.cookies,
auth=auth, data=request.get_data(),
headers=proxy_request_headers)
elif request.method == "PUT":
session_response = session.put(endpoint, cookies=request.cookies,
auth=auth, data=request.get_data(),
headers=proxy_request_headers)
elif request.method == "DELETE":
session_response = session.delete(endpoint, auth=auth,
cookies=request.cookies,
headers=proxy_request_headers)
else:
return "Method is not allowed!"
if request.method != "HEAD":
response.set_data(session_response.content)
response.status_code = session_response.status_code
for header, value in session_response.headers.items():
if header.lower() not in PROXY_RESP_HEADERS_BLACKLIST:
response.headers[header] = value
return response
def start():
app.run(host=config.app_host, port=config.app_port, debug=config.is_debug)