-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlambda_function.py
123 lines (101 loc) · 3.26 KB
/
lambda_function.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import base64
import boto3
import json
import os
import re
import requests
from aws_requests_auth.aws_auth import AWSRequestsAuth
from aws_requests_auth.aws_auth import sign
HOST = os.environ.get('ES_HOST')
STAGE = os.environ.get('API_STAGE')
def get_aws4auth(HOST, path, opts):
session = boto3.Session()
creds = session.get_credentials().get_frozen_credentials()
service = 'es'
awsauth = AWSRequestsAuth(
aws_access_key=creds.access_key,
aws_secret_access_key=creds.secret_key,
aws_token=creds.token,
aws_host=opts['HOST'],
aws_region=session.region_name,
aws_service=service
)
req = requests.Request(
opts['method'],
opts['url'],
headers=opts['headers'],
data=opts['body'],
params=opts['params']
)
prepped = req.prepare()
auth = awsauth.get_aws_request_headers(prepped,
aws_access_key=creds.access_key,
aws_secret_access_key=creds.secret_key,
aws_token=creds.token)
return auth
def format_response(response):
resp_body = []
headers = {}
isBase64Encoded = False
rewrite = {
'"_dashboards"': '"{}/_dashboards"'.format(API_STAGE),
'/_dashboards': '/{}/_dashboards'.format(API_STAGE)
}
h = dict(response.headers)
del h['Connection']
del h['Content-Length']
if 'content-encoding' in h:
del h['content-encoding']
for k, v in h.items():
headers[k.lower()] = v
for content in response.iter_content(1024):
if content:
resp_body.append(content)
resp_body = b''.join(resp_body)
if any(h in headers['content-type'] for h in ['text', 'javascript']):
resp_body = str(resp_body, 'utf-8')
elif 'json' in headers['content-type']:
resp_body = resp_body.decode('utf8').replace("'", '"')
else:
isBase64Encoded = True
resp_body = str(base64.b64encode(resp_body))
for k, v in rewrite.items():
resp_body = resp_body.replace(k, v)
return (headers, resp_body, isBase64Encoded)
def lambda_handler(event, context):
path = event.get('path')
method = event.get('httpMethod')
body = event.get('body')
query_params = event.get('queryStringParameters')
headers = event.get('headers', {})
req_headers = {}
for k, v in headers.items():
if any(h in k for h in ['content-type', 'cookie', 'kbn-', 'osd-']):
req_headers[k] = v
opts = {
'method': method.upper(),
'HOST': HOST,
'url': 'https://{}{}'.format(HOST, path),
'service': 'es',
'region': 'us-east-1',
'headers': req_headers,
'body': body,
'params': query_params
}
auth = get_aws4auth(HOST, path, opts)
req_headers.update(auth)
response = requests.request(
method=method,
url='https://{}{}'.format(HOST, path),
headers=req_headers,
data=body,
params=query_params,
stream=True
)
(headers, body, isBase64Encoded) = format_response(response)
return {
'statusCode': response.status_code,
'body': body,
'headers': headers,
'isBase64Encoded': isBase64Encoded
}