-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathathena_connector.py
375 lines (276 loc) · 12.5 KB
/
athena_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
# File: athena_connector.py
#
# Copyright (c) 2017-2025 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 json
import re
import time
import phantom.app as phantom
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
import athena_consts as consts
class RetVal(tuple):
def __new__(cls, val1, val2):
return tuple.__new__(RetVal, (val1, val2))
class AthenaConnector(BaseConnector):
def __init__(self):
# Call the BaseConnectors init first
super(AthenaConnector, self).__init__()
self._state = None
self._client = None
self._region = None
self._access_key = None
self._secret_key = None
self._session_token = None
self._proxy = None
def initialize(self):
self._state = self.load_state()
config = self.get_config()
self._region = config["region"]
if consts.ATHENA_JSON_ACCESS_KEY in config:
self._access_key = config.get(consts.ATHENA_JSON_ACCESS_KEY)
if consts.ATHENA_JSON_SECRET_KEY in config:
self._secret_key = config.get(consts.ATHENA_JSON_SECRET_KEY)
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, "Failed to get EC2 role credentials")
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(consts.ATHENA_JSON_ACCESS_KEY)
self._secret_key = config.get(consts.ATHENA_JSON_SECRET_KEY)
if not (self._access_key and self._secret_key):
return self.set_status(phantom.APP_ERROR, consts.ATHENA_BAD_ASSET_CONFIG_MSG)
return phantom.APP_SUCCESS
def finalize(self):
# Save the state, this data is saved accross actions and app upgrades
self.save_state(self._state)
return phantom.APP_SUCCESS
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=None):
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(
"athena",
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("athena", 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 _make_boto_call(self, action_result, method, **kwargs):
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)
except Exception as e:
return RetVal(
action_result.set_status(phantom.APP_ERROR, "boto3 call to Athena failed", e),
None,
)
return phantom.APP_SUCCESS, resp_json
def _handle_test_connectivity(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
if not self._create_client(action_result, param):
return action_result.get_status()
ret_val, resp_json = self._make_boto_call(action_result, "list_named_queries")
if phantom.is_fail(ret_val):
self.save_progress("Test Connectivity Failed")
return ret_val
self.save_progress("Test Connectivity Passed")
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_list_queries(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
if not self._create_client(action_result, param):
return action_result.get_status()
self.debug_print("Making boto3 call to get list of named queries")
ret_val, resp_json = self._make_boto_call(action_result, "list_named_queries")
if phantom.is_fail(ret_val):
return ret_val
count = 0
for query_id in resp_json.get("NamedQueryIds", []):
ret_val, query_json = self._make_boto_call(action_result, "get_named_query", NamedQueryId=query_id)
if phantom.is_fail(ret_val):
return ret_val
count += 1
action_result.add_data(query_json)
action_result.set_summary({"num_queries": count})
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_run_query(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
query = param["query"].strip()
s3 = param["s3_location"]
encryption = param.get("encryption")
database = param.get("database")
kms_key = self.get_config().get("kms_key")
if not s3.startswith("s3://"):
return action_result.set_status(
phantom.APP_ERROR,
"The S3 location does not appear to be correctly formatted. " "It should start with 's3://'",
)
location_json = {"OutputLocation": s3}
if encryption:
encrypt_config = {}
encrypt_config["EncryptionOption"] = encryption
location_json["EncryptionConfiguration"] = encrypt_config
if encryption in ["SSE_KMS", "CSE_KMS"]:
if not kms_key:
return action_result.set_status(
phantom.APP_ERROR,
"KMS encryption requires asset to have KMS key configured.",
)
encrypt_config["KmsKey"] = kms_key
if not self._create_client(action_result, param):
return action_result.get_status()
reg_exp = re.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
if reg_exp.match(query.lower()):
self.debug_print("Making boto3 call to get named query")
ret_val, query_json = self._make_boto_call(action_result, "get_named_query", NamedQueryId=query)
if phantom.is_fail(ret_val):
return ret_val
query_str = query_json.get("NamedQuery", {}).get("QueryString")
if not query:
return action_result.set_status("Could not find named query - {0}".format(query))
query = query_str
if database:
ret_val, response = self._make_boto_call(
action_result,
"start_query_execution",
QueryString=query,
ResultConfiguration=location_json,
QueryExecutionContext={"Database": database},
)
else:
ret_val, response = self._make_boto_call(
action_result,
"start_query_execution",
QueryString=query,
ResultConfiguration=location_json,
)
if phantom.is_fail(ret_val):
return ret_val
execution_id = response.get("QueryExecutionId")
if not execution_id:
return action_result.set_status(
phantom.APP_ERROR,
"Could not get query execution ID after starting query.",
)
for i in range(0, 60):
ret_val, response = self._make_boto_call(action_result, "get_query_execution", QueryExecutionId=execution_id)
if phantom.is_fail(ret_val):
return ret_val
status = response.get("QueryExecution", {}).get("Status", {}).get("State")
if not status:
return action_result.set_status(
phantom.APP_ERROR,
"Could not get query execution status after starting query.",
)
if status == "FAILED":
return action_result.set_status(
phantom.APP_ERROR,
"Query execution failed: {0}".format(
response.get("QueryExecution", {}).get("Status", {}).get("StateChangeReason", "Unknown error")
),
)
elif status == "CANCELLED":
return action_result.set_status(phantom.APP_ERROR, "Query execution cancelled")
elif status in ["RUNNING", "QUEUED"]:
time.sleep(1)
continue
elif status == "SUCCEEDED":
break
ret_val, response = self._make_boto_call(action_result, "get_query_results", QueryExecutionId=execution_id)
if phantom.is_fail(ret_val):
return ret_val
rows = response.get("ResultSet", {}).get("Rows", [{}])
for row in rows:
action_result.add_data(row.get("Data"))
action_result.set_summary({"num_rows": len(rows) - 1 if len(rows) != 0 else 0})
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 == "list_queries":
ret_val = self._handle_list_queries(param)
elif action_id == "run_query":
ret_val = self._handle_run_query(param)
return ret_val
if __name__ == "__main__":
import sys
# import pudb
# pudb.set_trace()
if len(sys.argv) < 2:
print("No test json specified as input")
sys.exit(0)
with open(sys.argv[1]) as f:
in_json = f.read()
in_json = json.loads(in_json)
print(json.dumps(in_json, indent=4))
connector = AthenaConnector()
connector.print_progress_message = True
ret_val = connector._handle_action(json.dumps(in_json), None)
print(json.dumps(json.loads(ret_val), indent=4))
sys.exit(0)