-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowflake_connector.py
447 lines (339 loc) · 16.3 KB
/
snowflake_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
# File: snowflake_connector.py
#
# Copyright (c) 2023-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.
#
# Import order matters here - if isort is allowed to put the Snowflake connector
# import later in the file, the connector crashes at runtime.
import snowflake.connector # isort: skip
from snowflake_consts import * # isort: skip
import datetime
import json
import traceback
# Phantom App imports
import phantom.app as phantom
import requests
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
class RetVal(tuple):
def __new__(cls, val1, val2=None):
return tuple.__new__(RetVal, (val1, val2))
class SnowflakeConnector(BaseConnector):
def __init__(self):
# Call the BaseConnectors init first
super(SnowflakeConnector, self).__init__()
self._state = None
self._account = None
self._username = None
self._password = None
def _get_error_msg_from_exception(self, e):
error_code = SNOWFLAKE_ERROR_CODE_UNAVAILABLE
error_msg = SNOWFLAKE_ERROR_MSG_UNAVAILABLE
self.error_print(traceback.format_exc())
try:
if e.args:
if len(e.args) > 1:
error_code = e.args[0]
error_msg = e.args[1]
return "Error Code: {0}. Error Message: {1}".format(error_code, error_msg)
elif len(e.args) == 1:
error_msg = e.args[0]
except Exception:
pass
return "Error Message: {0}".format(error_msg)
def convert_value(self, value):
if isinstance(value, (bytearray, bytes)):
return value.decode("utf-8")
elif isinstance(value, (datetime.datetime, datetime.timedelta, datetime.date)):
return str(value)
else:
return value
def _cleanup_row_values(self, row):
return {k: self.convert_value(v) for k, v in row.items()}
def _handle_test_connectivity(self, param):
self.save_progress(TEST_CONNECTIVITY_PROGRESS_MSG)
action_result = self.add_action_result(ActionResult(dict(param)))
try:
self._connection = self._handle_create_connection()
cursor = self._connection.cursor()
cursor.execute(SNOWFLAKE_VERSION_QUERY)
if cursor:
self.save_progress(TEST_CONNECTIVITY_SUCCESS_MSG)
return action_result.set_status(phantom.APP_SUCCESS)
except Exception as e:
self.save_progress(self._get_error_msg_from_exception(e))
return action_result.set_status(phantom.APP_ERROR, TEST_CONNECTIVITY_ERROR_MSG)
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"]
role = param.get("role")
warehouse = param.get("warehouse")
database = param.get("database")
schema = param.get("schema")
try:
self._connection = self._handle_create_connection(role, warehouse, database, schema)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(query)
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
while len(returned_rows) > 0:
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, "{0}: {1}".format(SQL_QUERY_ERROR_MSG, error_msg))
finally:
if self._connection:
cursor.close()
self._connection.close()
summary = action_result.update_summary({})
if cursor.rowcount > 0:
summary[SNOWFLAKE_TOTAL_ROWS_JSON] = cursor.rowcount
else:
summary[SNOWFLAKE_TOTAL_ROWS_JSON] = 0
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_disable_user(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
database = SNOWFLAKE_DATABASE
username = param["username"]
role = param.get("role")
try:
self._connection = self._handle_create_connection(database=database, role=role)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(DISABLE_SNOWFLAKE_USER_SQL.format(username=username))
row = cursor.fetchone()
action_result.add_data(row)
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, "{0}: {1}".format(DISABLE_USER_ERROR_MSG, error_msg))
finally:
if self._connection:
cursor.close()
self._connection.close()
summary = action_result.update_summary({})
summary["user_status"] = "disabled"
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_show_network_policies(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
database = SNOWFLAKE_DATABASE
role = param.get("role")
try:
self._connection = self._handle_create_connection(database=database, role=role)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(SHOW_NETWORK_POLICIES_SQL)
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
self.debug_print("returned_rows: {}".format(returned_rows))
while len(returned_rows) > 0:
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, error_msg)
finally:
if self._connection:
cursor.close()
self._connection.close()
summary = action_result.update_summary({})
summary["total_policies"] = len(action_result.get_data())
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_describe_network_policy(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
database = SNOWFLAKE_DATABASE
role = param.get("role")
policy_name = param["policy_name"]
try:
self._connection = self._handle_create_connection(database=database, role=role)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(DESCRIBE_NETWORK_POLICY_SQL.format(policy_name=policy_name))
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
self.debug_print("returned_rows: {}".format(returned_rows))
while len(returned_rows) > 0:
returned_rows = cursor.fetchmany(DEFAULT_NUM_ROWS_TO_FETCH)
for row in returned_rows:
action_result.add_data(self._cleanup_row_values(row))
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, error_msg)
finally:
if self._connection:
cursor.close()
self._connection.close()
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_update_network_policy(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
database = SNOWFLAKE_DATABASE
policy_name = param["policy_name"]
role = param.get("role")
# Putting single quotes around each IP address in the list to satisfy SQL formatting. Empty string to clear.
try:
allowed_ip_list = param.get("allowed_ip_list")
if allowed_ip_list:
allowed_ip_list = ",".join(f"'{ip.strip()}'" for ip in allowed_ip_list.split(","))
else:
allowed_ip_list = ""
blocked_ip_list = param.get("blocked_ip_list")
if blocked_ip_list:
blocked_ip_list = ",".join(f"'{ip.strip()}'" for ip in blocked_ip_list.split(","))
else:
blocked_ip_list = ""
comment = param.get("comment")
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, error_msg)
try:
self._connection = self._handle_create_connection(database=database, role=role)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(
UPDATE_NETWORK_POLICY_SQL.format(
policy_name=policy_name, allowed_ip_list=allowed_ip_list, blocked_ip_list=blocked_ip_list, comment=comment
)
)
row = cursor.fetchone()
action_result.add_data(row)
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, error_msg)
finally:
if self._connection:
cursor.close()
self._connection.close()
return action_result.set_status(phantom.APP_SUCCESS, UPDATE_NETWORK_POLICY_SUCCESS_MSG.format(policy_name=policy_name))
def _handle_remove_grants(self, param):
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
database = SNOWFLAKE_DATABASE
username = param["username"]
role_to_remove = param["role_to_remove"]
role = param.get("role")
try:
self._connection = self._handle_create_connection(role=role, database=database)
cursor = self._connection.cursor(snowflake.connector.DictCursor)
cursor.execute(REMOVE_GRANTS_SQL.format(username=username, role_to_remove=role_to_remove))
row = cursor.fetchone()
action_result.add_data(row)
except Exception as e:
error_msg = self._get_error_msg_from_exception(e)
self.save_progress("Error: {}".format(error_msg))
return action_result.set_status(phantom.APP_ERROR, error_msg)
finally:
if self._connection:
cursor.close()
self._connection.close()
return action_result.set_status(phantom.APP_SUCCESS, REMOVE_GRANTS_SUCCESS_MSG.format(role=role_to_remove))
def _handle_create_connection(self, role=None, warehouse=None, database=None, schema=None):
ctx = snowflake.connector.connect(
user=self._username, password=self._password, account=self._account, role=role, warehouse=warehouse, database=database, schema=schema
)
return ctx
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)
if action_id == "run_query":
ret_val = self._handle_run_query(param)
if action_id == "disable_user":
ret_val = self._handle_disable_user(param)
if action_id == "remove_grants":
ret_val = self._handle_remove_grants(param)
if action_id == "show_network_policies":
ret_val = self._handle_show_network_policies(param)
if action_id == "describe_network_policy":
ret_val = self._handle_describe_network_policy(param)
if action_id == "update_network_policy":
ret_val = self._handle_update_network_policy(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._account = config["account"]
self._username = config["username"]
self._password = config["password"]
self._connection = None
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
def 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:
try:
login_url = SnowflakeConnector._get_phantom_base_url() + "/login"
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 = SnowflakeConnector()
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)
if __name__ == "__main__":
main()