-
Notifications
You must be signed in to change notification settings - Fork 0
/
awslambda_connector.py
502 lines (378 loc) · 17.8 KB
/
awslambda_connector.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File: awslambda_connector.py
#
# Copyright (c) 2019-2024 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions
# and limitations under the License.
#
#
# Phantom App imports
import ast
import base64
import json
from datetime import datetime
import botocore.paginate as bp
import botocore.response as br
import phantom.app as phantom
import requests
import six
from boto3 import Session, client
from botocore.config import Config
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
# Usage of the consts file is recommended
from awslambda_consts import *
class RetVal(tuple):
def __new__(cls, val1, val2=None):
return tuple.__new__(RetVal, (val1, val2))
class AwsLambdaConnector(BaseConnector):
def __init__(self):
# Call the BaseConnectors init first
super(AwsLambdaConnector, self).__init__()
self._state = None
self._region = None
self._access_key = None
self._secret_key = None
self._session_token = None
self._proxy = None
def _sanitize_data(self, cur_obj):
try:
json.dumps(cur_obj)
return cur_obj
except:
pass
if isinstance(cur_obj, dict):
new_dict = {}
for k, v in six.iteritems(cur_obj):
if isinstance(v, br.StreamingBody):
content = v.read()
new_dict[k] = json.loads(content)
else:
new_dict[k] = self._sanitize_data(v)
return new_dict
if isinstance(cur_obj, list):
new_list = []
for v in cur_obj:
new_list.append(self._sanitize_data(v))
return new_list
if isinstance(cur_obj, datetime):
return cur_obj.strftime("%Y-%m-%d %H:%M:%S")
if isinstance(cur_obj, bp.PageIterator):
new_dict = dict()
try:
for page in cur_obj:
new_dict.update(page)
return new_dict
except Exception as e:
return {"error": e}
return cur_obj
def _make_boto_call(self, action_result, method, paginate=False, empty_payload=False, **kwargs):
self.debug_print("Making call to {}".format(method))
if paginate is False:
try:
boto_func = getattr(self._client, method)
except AttributeError:
return RetVal(action_result.set_status(phantom.APP_ERROR, "Invalid method: {0}".format(method)), None)
try:
resp_json = boto_func(**kwargs)
if empty_payload:
resp_json["Payload"] = {"body": "", "statusCode": resp_json["StatusCode"]}
except Exception as e:
exception_message = e.args[0].strip()
return RetVal(action_result.set_status(phantom.APP_ERROR, "boto3 call to Lambda failed", exception_message), None)
else:
try:
paginator = self._client.get_paginator(method)
resp_json = paginator.paginate(**kwargs)
except Exception as e:
return RetVal(action_result.set_status(phantom.APP_ERROR, "boto3 call to Lambda failed", e), None)
return phantom.APP_SUCCESS, self._sanitize_data(resp_json)
def _handle_get_ec2_role(self):
session = Session(region_name=self._region)
credentials = session.get_credentials()
return credentials
def _create_client(self, action_result, param):
boto_config = None
if self._proxy:
boto_config = Config(proxies=self._proxy)
# Try getting and using temporary assume role credentials from parameters
temp_credentials = dict()
if param and "credentials" in param:
try:
temp_credentials = ast.literal_eval(param["credentials"])
self._access_key = temp_credentials.get("AccessKeyId", "")
self._secret_key = temp_credentials.get("SecretAccessKey", "")
self._session_token = temp_credentials.get("SessionToken", "")
self.save_progress("Using temporary assume role credentials for action")
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Failed to get temporary credentials:{0}".format(e))
try:
if self._access_key and self._secret_key:
self.debug_print("Creating boto3 client with API keys")
self._client = client(
"lambda",
region_name=self._region,
aws_access_key_id=self._access_key,
aws_secret_access_key=self._secret_key,
aws_session_token=self._session_token,
config=boto_config,
)
else:
self.debug_print("Creating boto3 client without API keys")
self._client = client("lambda", region_name=self._region, config=boto_config)
except Exception as e:
return action_result.set_status(phantom.APP_ERROR, "Could not create boto3 client: {0}".format(e))
return phantom.APP_SUCCESS
def _handle_test_connectivity(self, param):
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
self.save_progress("Querying AWS to check credentials")
if not self._create_client(action_result, param):
return action_result.get_status()
# make boto3 call
ret_val, resp_json = self._make_boto_call(action_result, "list_functions", MaxItems=1)
if phantom.is_fail(ret_val):
self.save_progress("Test Connectivity Failed.")
return action_result.get_status()
# Return success
self.save_progress("Test Connectivity Passed")
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_invoke_lambda(self, param):
# Implement the handler here
# use self.save_progress(...) to send progress messages back to the platform
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
if not self._create_client(action_result, param):
return action_result.get_status()
function_name = param["function_name"]
invocation_type = param.get("invocation_type")
log_type = param.get("log_type")
client_context = param.get("client_context")
payload = param.get("payload")
qualifier = param.get("qualifier")
empty_payload = False
args = {"FunctionName": function_name}
if invocation_type:
args["InvocationType"] = invocation_type
if log_type:
args["LogType"] = log_type
if client_context:
try:
args["ClientContext"] = base64.b64encode(str(client_context)).decode("utf-8")
except TypeError: # py3
args["ClientContext"] = base64.b64encode(client_context.encode("UTF-8")).decode("utf-8")
if payload:
args["Payload"] = payload
if qualifier:
args["Qualifier"] = qualifier
if invocation_type == "Event" or invocation_type == "DryRun":
empty_payload = True
# make boto3 call
ret_val, response = self._make_boto_call(action_result, "invoke", False, empty_payload, **args)
if phantom.is_fail(ret_val):
return action_result.get_status()
# Add the response into the data section
action_result.add_data(response)
# Add a dictionary that is made up of the most important values from data into the summary
summary = action_result.update_summary({})
if response.get("FunctionError"):
summary["status"] = "Lambda invoked and returned FunctionError"
return action_result.set_status(phantom.APP_ERROR, "Lambda invoked and returned FunctionError")
else:
summary["status"] = "Successfully invoked lambda"
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_list_functions(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
if not self._create_client(action_result, param):
return action_result.get_status()
function_version = param.get("function_version")
next_token = param.get("next_token")
max_items = param.get("max_items")
args = {}
if function_version:
args["FunctionVersion"] = function_version
if next_token:
args["Marker"] = next_token
if max_items is not None:
args["MaxItems"] = int(max_items)
# make boto3 call
ret_val, response = self._make_boto_call(action_result, "list_functions", **args)
if phantom.is_fail(ret_val):
return action_result.get_status()
if response.get("error", None) is not None:
return action_result.set_status(phantom.APP_ERROR, "{}".format(response.get("error")))
# Add the response into the data section
action_result.add_data(response)
# Add a dictionary that is made up of the most important values from data into the summary
summary = action_result.update_summary({})
summary["num_functions"] = len(response.get("Functions", {}))
if response.get("NextMarker"):
summary["next_token"] = response.get("NextMarker", "Unavailable")
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_add_permission(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
# Add an action result object to self (BaseConnector) to represent the action for this param
action_result = self.add_action_result(ActionResult(dict(param)))
if not self._create_client(action_result, param):
return action_result.get_status()
function_name = param["function_name"]
statement_id = param["statement_id"]
action = param["action"]
principal = param["principal"]
source_arn = param.get("source_arn")
source_account = param.get("source_account")
event_source_token = param.get("event_source_token")
qualifier = param.get("qualifier")
revision_id = param.get("revision_id")
args = {"FunctionName": function_name, "StatementId": statement_id, "Action": action, "Principal": principal}
if source_arn:
args["SourceArn"] = source_arn
if source_account:
args["SourceAccount"] = source_account
if event_source_token:
args["EventSourceToken"] = event_source_token
if qualifier:
args["Qualifier"] = qualifier
if revision_id:
args["RevisionId"] = revision_id
# make boto3 call
ret_val, response = self._make_boto_call(action_result, "add_permission", **args)
if phantom.is_fail(ret_val):
return action_result.get_status()
# Add the response into the data section
action_result.add_data(response)
# Add a dictionary that is made up of the most important values from data into the summary
summary = action_result.update_summary({})
summary["status"] = "Successfully added permission"
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_remove_permission(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
if not self._create_client(action_result, param):
return action_result.get_status()
function_name = param["function_name"]
statement_id = param["statement_id"]
qualifier = param.get("qualifier")
revision_id = param.get("revision_id")
args = {
"FunctionName": function_name,
"StatementId": statement_id,
}
if qualifier:
args["Qualifier"] = qualifier
if revision_id:
args["RevisionId"] = revision_id
# make boto3 call
ret_val, response = self._make_boto_call(action_result, "remove_permission", **args)
if phantom.is_fail(ret_val):
return action_result.get_status()
# Add the response into the data section
action_result.add_data(response)
# Add a dictionary that is made up of the most important values from data into the summary
summary = action_result.update_summary({})
summary["status"] = "Successfully removed permission"
return action_result.set_status(phantom.APP_SUCCESS)
def handle_action(self, param):
ret_val = phantom.APP_SUCCESS
# Get the action that we are supposed to execute for this App Run
action_id = self.get_action_identifier()
self.debug_print("action_id", self.get_action_identifier())
if action_id == "test_connectivity":
ret_val = self._handle_test_connectivity(param)
elif action_id == "invoke_lambda":
ret_val = self._handle_invoke_lambda(param)
elif action_id == "list_functions":
ret_val = self._handle_list_functions(param)
elif action_id == "add_permission":
ret_val = self._handle_add_permission(param)
elif action_id == "remove_permission":
ret_val = self._handle_remove_permission(param)
return ret_val
def initialize(self):
# Load the state in initialize, use it to store data
# that needs to be accessed across actions
self._state = self.load_state()
# get the asset config
config = self.get_config()
self._region = LAMBDA_REGION_DICT.get(config[LAMBDA_JSON_REGION])
self._proxy = {}
env_vars = config.get("_reserved_environment_variables", {})
if "HTTP_PROXY" in env_vars:
self._proxy["http"] = env_vars["HTTP_PROXY"]["value"]
if "HTTPS_PROXY" in env_vars:
self._proxy["https"] = env_vars["HTTPS_PROXY"]["value"]
if config.get("use_role"):
credentials = self._handle_get_ec2_role()
if not credentials:
return self.set_status(phantom.APP_ERROR, EC2_ROLE_CREDENTIALS_FAILURE_MSG)
self._access_key = credentials.access_key
self._secret_key = credentials.secret_key
self._session_token = credentials.token
return phantom.APP_SUCCESS
self._access_key = config.get(LAMBDA_JSON_ACCESS_KEY)
self._secret_key = config.get(LAMBDA_JSON_SECRET_KEY)
if not (self._access_key and self._secret_key):
return self.set_status(phantom.APP_ERROR, LAMBDA_BAD_ASSET_CONFIG_MSG)
return phantom.APP_SUCCESS
def finalize(self):
# Save the state, this data is saved across actions and app upgrades
self.save_state(self._state)
return phantom.APP_SUCCESS
if __name__ == "__main__":
import argparse
import pudb
pudb.set_trace()
argparser = argparse.ArgumentParser()
argparser.add_argument("input_test_json", help="Input Test JSON file")
argparser.add_argument("-u", "--username", help="username", required=False)
argparser.add_argument("-p", "--password", help="password", required=False)
args = argparser.parse_args()
session_id = None
username = args.username
password = args.password
if username is not None and password is None:
# User specified a username but not a password, so ask
import getpass
password = getpass.getpass("Password: ")
if username and password:
login_url = BaseConnector._get_phantom_base_url() + "login"
try:
print("Accessing the Login page")
r = requests.get(login_url, verify=False)
csrftoken = r.cookies["csrftoken"]
data = dict()
data["username"] = username
data["password"] = password
data["csrfmiddlewaretoken"] = csrftoken
headers = dict()
headers["Cookie"] = "csrftoken=" + csrftoken
headers["Referer"] = login_url
print("Logging into Platform to get the session id")
r2 = requests.post(login_url, verify=False, data=data, headers=headers)
session_id = r2.cookies["sessionid"]
except Exception as e:
print("Unable to get session id from the platform. Error: " + str(e))
exit(1)
with open(args.input_test_json) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=4))
connector = AwsLambdaConnector()
connector.print_progress_message = True
if session_id is not None:
in_json["user_session_token"] = session_id
connector._set_csrf_info(csrftoken, headers["Referer"])
ret_val = connector._handle_action(json.dumps(in_json), None)
print(json.dumps(json.loads(ret_val), indent=4))
exit(0)