@@ -41,11 +41,13 @@ def get_user_id(self, event, context):
41
41
username = identify_user (event , context )
42
42
else :
43
43
try :
44
- rc_identity_id = event ["requestContext" ]["identity" ]["cognitoIdentityId" ]
45
- if rc_identity_id :
46
- username = rc_identity_id
44
+ if 'requestContext' in event and 'identity' in event ["requestContext" ] and 'cognitoIdentityId' in event ["requestContext" ]["identity" ]:
45
+ rc_identity_id = event ["requestContext" ]["identity" ]["cognitoIdentityId" ]
46
+ if rc_identity_id :
47
+ username = rc_identity_id
47
48
except :
48
- print ("can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
49
+ if self .DEBUG :
50
+ print ("can not fetch apiKey from cognitoIdentityId event, setting userId to None." )
49
51
except Exception as e :
50
52
if self .DEBUG :
51
53
print ("can not execute identify_user function, please check moesif settings." )
@@ -70,11 +72,14 @@ def process_request_body(self, raw_request_body):
70
72
req_body = None
71
73
req_transfer_encoding = None
72
74
try :
73
- if raw_request_body ['isBase64Encoded' ]:
75
+ if 'isBase64Encoded' in raw_request_body and 'body' in raw_request_body and raw_request_body ['isBase64Encoded' ] and raw_request_body [ 'body ' ]:
74
76
req_body = raw_request_body ['body' ]
75
77
req_transfer_encoding = 'base64'
76
78
else :
77
- req_body = json .dumps (raw_request_body ['body' ])
79
+ if 'body' in raw_request_body and raw_request_body ['body' ] and isinstance (raw_request_body ['body' ], str ):
80
+ req_body = json .dumps (json .loads (raw_request_body ['body' ]))
81
+ else :
82
+ req_body = json .dumps (raw_request_body ['body' ])
78
83
req_transfer_encoding = 'json'
79
84
except Exception as e :
80
85
if self .DEBUG :
@@ -87,11 +92,14 @@ def process_response_body(self, raw_response_body):
87
92
resp_body = None
88
93
resp_transfer_encoding = None
89
94
try :
90
- if 'isBase64Encoded' in raw_response_body and raw_response_body ['isBase64Encoded' ]:
95
+ if 'isBase64Encoded' in raw_response_body and 'body' in raw_response_body and raw_response_body ['isBase64Encoded' ] and raw_response_body [ 'body ' ]:
91
96
resp_body = raw_response_body ['body' ]
92
97
resp_transfer_encoding = 'base64'
93
98
else :
94
- resp_body = json .dumps (raw_response_body ['body' ])
99
+ if 'body' in raw_response_body and raw_response_body ['body' ] and isinstance (raw_response_body ['body' ], str ):
100
+ resp_body = json .dumps (json .loads (raw_response_body ['body' ]))
101
+ else :
102
+ resp_body = json .dumps (raw_response_body ['body' ])
95
103
resp_transfer_encoding = 'json'
96
104
except Exception as e :
97
105
if self .DEBUG :
@@ -146,11 +154,12 @@ def before(self, event, context):
146
154
self .metadata = get_meta (event , context )
147
155
else :
148
156
try :
149
- self .metadata = {
150
- 'trace_id' : context .aws_request_id ,
151
- 'function_name' : context .function_name ,
152
- 'request_context' : event ['requestContext' ]
153
- }
157
+ if context .aws_request_id and context .function_name and 'requestContext' in event :
158
+ self .metadata = {
159
+ 'trace_id' : str (context .aws_request_id ),
160
+ 'function_name' : context .function_name ,
161
+ 'request_context' : event ['requestContext' ]
162
+ }
154
163
except :
155
164
if self .DEBUG :
156
165
print ("can not fetch default function_name and request_context from aws context, setting metadata to None." )
@@ -172,9 +181,10 @@ def before(self, event, context):
172
181
self .session_token = get_token (event , context )
173
182
else :
174
183
try :
175
- rc_api_key = event ['requestContext' ]['identity' ]['apiKey' ]
176
- if rc_api_key :
177
- self .session_token = rc_api_key
184
+ if 'requestContext' in event and 'identity' in event ['requestContext' ] and 'apiKey' in event ['requestContext' ]['identity' ] and event ['requestContext' ]['identity' ]['apiKey' ]:
185
+ rc_api_key = event ['requestContext' ]['identity' ]['apiKey' ]
186
+ if rc_api_key :
187
+ self .session_token = rc_api_key
178
188
except KeyError :
179
189
if self .DEBUG :
180
190
print ("can not fetch apiKey from aws event, setting session_token to None." )
@@ -191,10 +201,11 @@ def before(self, event, context):
191
201
api_version = get_version (event , context )
192
202
else :
193
203
try :
194
- api_version = context .function_version
204
+ if context .function_version :
205
+ api_version = context .function_version
195
206
except KeyError :
196
207
if self .DEBUG :
197
- print ("can not fetch default function_version from aws context, setting to None." )
208
+ print ("can not fetch default function_version from aws context, setting api_version to None." )
198
209
except Exception as e :
199
210
if self .DEBUG :
200
211
print ("can not execute GET_API_VERSION function, please check moesif settings." )
@@ -203,7 +214,8 @@ def before(self, event, context):
203
214
# IpAddress
204
215
ip_address = None
205
216
try :
206
- ip_address = self .client_ip .get_client_address (event ['headers' ], event ['requestContext' ]['identity' ]['sourceIp' ])
217
+ if 'headers' in event and 'requestContext' in event and 'identity' in event ['requestContext' ] and 'sourceIp' in event ['requestContext' ]['identity' ] and event ['requestContext' ]['identity' ]['sourceIp' ]:
218
+ ip_address = self .client_ip .get_client_address (event ['headers' ], event ['requestContext' ]['identity' ]['sourceIp' ])
207
219
except Exception as e :
208
220
if self .DEBUG :
209
221
print ("Error while fetching Client Ip address, setting Request Ip address to None." )
@@ -296,7 +308,7 @@ def after(self, retval):
296
308
# Send event to Moesif
297
309
event_send = self .api_client .create_event (event_model )
298
310
if self .DEBUG :
299
- print ('Event Send successfully' )
311
+ print ('Event Sent successfully' )
300
312
301
313
# Send response
302
314
return retval
0 commit comments