-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
63 lines (57 loc) · 2.02 KB
/
lambda_function.py
File metadata and controls
63 lines (57 loc) · 2.02 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import requests
from collections import defaultdict
req = {
'POST': requests.post,
'GET': requests.get,
'OPTIONS': requests.options,
'PUT': requests.put,
'HEAD': requests.head
}
# any exceptions where the target is http need to be noted.
target_protocol = defaultdict(lambda: 'https')
if 'http_targets' in os.environ:
for http_target in os.environ['http_targets'].split(','):
target_protocol[http_target.lower()] = 'http'
# create (lookup & rev lookup) dict for hosts found in url_map env var
swap_host = dict()
for pair in [tuple(urls.split(':')) for urls in os.environ['url_map'].split(',')]:
swap_host[pair[0].lower()] = pair[1].lower()
swap_host[pair[1].lower()] = pair[0].lower()
print(swap_host)
def lambda_handler(event, context):
headers = {
k: event['multiValueHeaders'][k][0]
for k in event['multiValueHeaders']
}
try:
host = swap_host[headers['host'].lower()]
except KeyError:
return {
"statusCode": 403,
"multiValueHeaders": event['multiValueHeaders'],
"body": "Improper use"
}
http_method = event['httpMethod']
path = event['path']
print(event, f"{target_protocol[host]}://{host}{path}")
response = req[http_method](
f"{target_protocol[host]}://{host}{path}",
headers=headers,
params=event['multiValueQueryStringParameters'],
data=event['body']
)
domain_swapped_list = list()
for h in response.raw.headers.getlist('Set-Cookie'):
domain_swapped_list.append(swap_host[h], 'Domain=.')
header_multi = {h: [response.headers[h], ] for h in response.headers}
header_multi["Set-Cookie"] = domain_swapped_list
if 'Access-Control-Allow-Origin' in header_multi:
header_multi['Access-Control-Allow-Origin'][0] = (
swap_host[header_multi['Access-Control-Allow-Origin'][0]]
)
return {
"statusCode": response.status_code,
"multiValueHeaders": header_multi,
"body": response.text
}