forked from splunk-soar-connectors/zscaler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzscaler_connector.py
1148 lines (916 loc) · 45.8 KB
/
zscaler_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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# File: zscaler_connector.py
#
# Copyright (c) 2017-2023 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 json
import re
import time
import phantom.app as phantom
import phantom.rules as phantom_rules
import requests
from bs4 import BeautifulSoup
from phantom.action_result import ActionResult
from phantom.base_connector import BaseConnector
from zscaler_consts import *
class RetVal(tuple):
def __new__(cls, val1, val2):
return tuple.__new__(RetVal, (val1, val2))
class ZscalerConnector(BaseConnector):
def __init__(self):
# Call the BaseConnectors init first
super(ZscalerConnector, self).__init__()
self._state = None
self._base_url = None
self._response = None # The most recent response object
self._headers = None
self._category = None
self._retry_rest_call = None # Retry rest call when get status_code 409 or 429
def _get_err_msg_from_exception(self, e):
"""
Get appropriate error message from the exception.
:param e: Exception object
:return: error message
"""
err_code = None
err_msg = ZSCALER_ERR_MSG_UNAVAILABLE
self.error_print("Error occurred: ", e)
try:
if hasattr(e, "args"):
if len(e.args) > 1:
err_code = e.args[0]
err_msg = e.args[1]
elif len(e.args) == 1:
err_msg = e.args[0]
except Exception as e:
self.debug_print("Error occurred while getting message from response. Error : {}".format(e))
if not err_code:
err_text = "Error Message: {}".format(err_msg)
else:
err_text = "Error Code: {}. Error Message: {}".format(err_code, err_msg)
return err_text
def _validate_integer(self, action_result, parameter, key, allow_zero=False):
"""
Validate an integer.
:param action_result: Action result or BaseConnector object
:param parameter: input parameter
:param key: input parameter message key
:allow_zero: whether zero should be considered as valid value or not
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS, integer value of the parameter or None in case of failure
"""
if parameter is not None:
try:
if not float(parameter).is_integer():
return action_result.set_status(phantom.APP_ERROR, ZSCALER_VALID_INTEGER_MSG.format(param=key)), None
parameter = int(parameter)
except Exception:
return action_result.set_status(phantom.APP_ERROR, ZSCALER_VALID_INTEGER_MSG.format(param=key)), None
if parameter < 0:
return action_result.set_status(phantom.APP_ERROR, ZSCALER_NON_NEGATIVE_INTEGER_MSG.format(param=key)), None
if not allow_zero and parameter == 0:
return action_result.set_status(phantom.APP_ERROR, ZSCALER_POSITIVE_INTEGER_MSG.format(param=key)), None
return phantom.APP_SUCCESS, parameter
def _process_empty_response(self, response, action_result):
if response.status_code == 200 or response.status_code == 204:
return RetVal(phantom.APP_SUCCESS, {})
return RetVal(action_result.set_status(
phantom.APP_ERROR,
"Status code : {}. Empty response and no information in the header".format(response.status_code)),
None
)
def _process_html_response(self, response, action_result):
# An html response, treat it like an error
status_code = response.status_code
try:
soup = BeautifulSoup(response.text, "html.parser")
# Remove the script, style, footer and navigation part from the HTML message
for element in soup(["script", "style", "footer", "nav"]):
element.extract()
err_text = soup.text
split_lines = err_text.split('\n')
split_lines = [x.strip() for x in split_lines if x.strip()]
err_text = '\n'.join(split_lines)
except Exception as e:
err_text = "Cannot parse err details"
self.debug_print("{}. Error: {}".format(err_text, e))
err_text = err_text
msg = "Please check the asset configuration parameters (the base_url should not end with "\
"/api/v1 e.g. https://admin.zscaler_instance.net)."
if len(err_text) <= 500:
msg += "Status Code: {0}. Data from server:\n{1}\n".format(status_code, err_text)
msg = msg.replace('{', '{{').replace('}', '}}')
return RetVal(action_result.set_status(phantom.APP_ERROR, msg), None)
def _process_json_response(self, r, action_result):
# Try a json parse
try:
resp_json = r.json()
except Exception as e:
return RetVal(action_result.set_status(phantom.APP_ERROR, "Unable to parse JSON response. Error: {0}"
.format(self._get_err_msg_from_exception(e))), None)
# Please specify the status codes here
if 200 <= r.status_code < 399:
return RetVal(phantom.APP_SUCCESS, resp_json)
# You should process the error returned in the json
try:
msg = resp_json['message']
except Exception:
msg = "Error from server. Status Code: {0} Data from server: {1}".format(
r.status_code, r.text.replace('{', '{{').replace('}', '}}')
)
return RetVal(action_result.set_status(phantom.APP_ERROR, msg), None)
def _process_response(self, r, action_result):
# store the r_text in debug data, it will get dumped in the logs if the action fails
if hasattr(action_result, 'add_debug_data'):
action_result.add_debug_data({'r_status_code': r.status_code})
action_result.add_debug_data({'r_text': r.text})
action_result.add_debug_data({'r_headers': r.headers})
# Process each 'Content-Type' of response separately
# Process a json response
if 'json' in r.headers.get('Content-Type', ''):
return self._process_json_response(r, action_result)
# Process an HTML response, Do this no matter what the api talks.
# There is a high chance of a PROXY in between phantom and the rest of
# world, in case of errors, PROXY's return HTML, this function parses
# the error and adds it to the action_result.
if 'html' in r.headers.get('Content-Type', ''):
return self._process_html_response(r, action_result)
# it's not content-type that is to be parsed, handle an empty response
if not r.text:
return self._process_empty_response(r, action_result)
# everything else is actually an error at this point
msg = "Can't process response from server. Status Code: {0} Data from server: {1}".format(
r.status_code, r.text.replace('{', '{{').replace('}', '}}')
)
return RetVal(action_result.set_status(phantom.APP_ERROR, msg), None)
def _is_ip(self, input_ip_address):
""" Function that checks given address and return True if address is valid IPv4 or IPV6 address.
:param input_ip_address: IP address
:return: status (success/failure)
"""
ip_address_input = input_ip_address
try:
try:
ipaddress.ip_address(unicode(ip_address_input))
except NameError:
ipaddress.ip_address(str(ip_address_input))
except Exception:
return False
return True
def _make_rest_call(self, endpoint, action_result, headers=None, params=None,
data=None, method="get", use_json=True, timeout=ZSCALER_DEFAULT_TIMEOUT):
resp_json = None
if headers is None:
headers = {}
if self.get_action_identifier() != 'submit_file':
headers.update(self._headers)
try:
request_func = getattr(requests, method)
except AttributeError:
return RetVal(action_result.set_status(phantom.APP_ERROR, "Invalid method: {0}".format(method)), resp_json)
# Create a URL to connect to
url = '{}{}'.format(self._base_url, endpoint)
try:
if use_json:
r = request_func(
url,
json=data,
headers=headers,
params=params,
timeout=timeout
)
else:
r = request_func(
url,
data=data,
headers=headers,
params=params,
timeout=timeout
)
except Exception as e:
error_message = self._get_err_msg_from_exception(e)
error_message = re.sub(ZSCALER_MATCH_REGEX, ZSCALER_REPLACE_REGEX, error_message)
return RetVal(action_result.set_status(phantom.APP_ERROR, "Error Connecting to Zscaler server. {}"
.format(error_message)), resp_json)
self._response = r
return self._process_response(r, action_result)
def _parse_retry_time(self, retry_time):
# Instead of just giving a second value, "retry-time" will return a string like "0 seconds"
# I don't know if the second unit can be not seconds
parts = retry_time.split()
if parts[1].lower() == "seconds":
return int(parts[0])
if parts[1].lower() == "minutes":
return int(parts[0]) * 60
else:
return None
def _make_rest_call_helper(self, *args, **kwargs):
# There are two rate limits
# 1. There is a maximum limit of requests per second, depending on if its a GET / POST / PUT / DELETE
# 2. There is a maximum number of requests per hour
# Regardless, the response will include a try-after value, which we can use to sleep
ret_val, response = self._make_rest_call(*args, **kwargs)
if phantom.is_fail(ret_val):
if self._response is None:
return ret_val, response
if self._response.status_code == 409 and self._retry_rest_call: # Lock not available
# This basically just means we need to try again
self.debug_print("Error 409: Lock not available")
self.send_progress("Error 409: Lock not available: Retrying in 1 second")
time.sleep(1)
self._retry_rest_call = False # make it to false to avoid extra rest call
return self._make_rest_call_helper(*args, **kwargs)
if self._response.status_code == 429 and self._retry_rest_call: # Rate limit exceeded
try:
retry_time = self._response.json()['Retry-After']
except KeyError:
self.debug_print("KeyError")
return ret_val, response
self.debug_print("Retry Time: {}".format(retry_time))
seconds_to_wait = self._parse_retry_time(retry_time)
if seconds_to_wait is None or seconds_to_wait < 0:
return retry_time, response
self.send_progress("Exceeded rate limit: Retrying after {}".format(retry_time))
time.sleep(seconds_to_wait)
self._retry_rest_call = False # make it to false to avoid extra rest call
return self._make_rest_call_helper(*args, **kwargs)
return ret_val, response
def _obfuscate_api_key(self, api_key):
now = str(int(time.time() * 1000))
n = now[-6:]
r = str(int(n) >> 1).zfill(6)
key = ""
for i in range(0, len(n), 1):
key += api_key[int(n[i])]
for j in range(0, len(r), 1):
key += api_key[int(r[j]) + 2]
return now, key
def _init_session(self):
username = self._username
password = self._password
api_key = self._api_key
try:
timestamp, obf_api_key = self._obfuscate_api_key(api_key)
except Exception:
return self.set_status(
phantom.APP_ERROR,
"Error obfuscating API key"
)
body = {
'apiKey': obf_api_key,
'username': username,
'password': password,
'timestamp': timestamp
}
action_result = ActionResult()
ret_val, _ = self._make_rest_call_helper(
'/api/v1/authenticatedSession',
action_result, data=body,
method='post'
)
if phantom.is_fail(ret_val):
self.debug_print('Error starting Zscaler session: {}'.format(action_result.get_message()))
return self.set_status(
phantom.APP_ERROR,
'Error starting Zscaler session: {}'.format(action_result.get_message())
)
else:
self.save_progress('Successfully started Zscaler session')
self._headers = {
'cookie': self._response.headers['Set-Cookie'].split(';')[0].strip()
}
return phantom.APP_SUCCESS
def _deinit_session(self):
action_result = ActionResult()
config = self.get_config()
self._base_url = config['base_url'].rstrip('/')
ret_val, response = self._make_rest_call_helper('/api/v1/authenticatedSession', action_result, method='delete')
if phantom.is_fail(ret_val):
self.debug_print("Deleting the authenticated session failed on the ZScaler server.")
self.debug_print("Marking the action as successful run.")
return phantom.APP_SUCCESS
def _handle_test_connectivity(self, param):
# If we are here we have successfully initialized a session
self.save_progress("Test Connectivity Passed")
self.debug_print("Test Connectivity Passed.")
return self.set_status(phantom.APP_SUCCESS)
def _filter_endpoints(self, action_result, to_add, existing, action, name):
if action == "REMOVE_FROM_LIST":
msg = "{} contains none of these endpoints".format(name)
endpoints = list(set(existing) - (set(existing) - set(to_add)))
else:
msg = "{} contains all of these endpoints".format(name)
endpoints = list(set(to_add) - set(existing))
if not endpoints:
summary = action_result.set_summary({})
summary['updated'] = []
summary['ignored'] = to_add
return RetVal(action_result.set_status(phantom.APP_SUCCESS, msg), None)
return RetVal(phantom.APP_SUCCESS, endpoints)
def _get_blocklist(self, action_result):
return self._make_rest_call_helper('/api/v1/security/advanced', action_result)
def _check_blocklist(self, action_result, endpoints, action):
ret_val, response = self._get_blocklist(action_result)
if phantom.is_fail(ret_val):
return RetVal(ret_val, None)
blocklist = response.get('blacklistUrls', [])
return self._filter_endpoints(action_result, endpoints, blocklist, action, 'Blocklist')
def _amend_blocklist(self, action_result, endpoints, action):
ret_val, filtered_endpoints = self._check_blocklist(action_result, endpoints, action)
if phantom.is_fail(ret_val) or filtered_endpoints is None:
return ret_val
params = {'action': action}
data = {
"blacklistUrls": filtered_endpoints
}
ret_val, response = self._make_rest_call_helper(
'/api/v1/security/advanced/blacklistUrls', action_result, params=params,
data=data, method="post"
)
if phantom.is_fail(ret_val) and self._response.status_code != 204:
return ret_val
summary = action_result.set_summary({})
summary['updated'] = filtered_endpoints
summary['ignored'] = list(set(endpoints) - set(filtered_endpoints))
# Encode the unicode IP or URL strings
summary['updated'] = [element for element in summary['updated']]
summary['ignored'] = [element for element in summary['ignored']]
return action_result.set_status(phantom.APP_SUCCESS)
def _get_allowlist(self, action_result):
return self._make_rest_call_helper('/api/v1/security', action_result)
def _check_allowlist(self, action_result, endpoints, action):
ret_val, response = self._get_allowlist(action_result)
if phantom.is_fail(ret_val):
return RetVal(ret_val, None)
allowlist = response.get('whitelistUrls', [])
self._allowlist = allowlist
return self._filter_endpoints(action_result, endpoints, allowlist, action, 'Allowlist')
def _amend_allowlist(self, action_result, endpoints, action):
ret_val, filtered_endpoints = self._check_allowlist(action_result, endpoints, action)
if phantom.is_fail(ret_val) or filtered_endpoints is None:
return ret_val
if action == "ADD_TO_LIST":
to_add_endpoints = list(set(self._allowlist + filtered_endpoints))
else:
to_add_endpoints = list(set(self._allowlist) - set(filtered_endpoints))
data = {
"whitelistUrls": to_add_endpoints
}
ret_val, response = self._make_rest_call_helper(
'/api/v1/security', action_result,
data=data, method='put'
)
if phantom.is_fail(ret_val):
return ret_val
action_result.add_data(response)
summary = action_result.set_summary({})
summary['updated'] = filtered_endpoints
summary['ignored'] = list(set(endpoints) - set(filtered_endpoints))
# Encode the unicode IP or URL strings
summary['updated'] = [element for element in summary['updated']]
summary['ignored'] = [element for element in summary['ignored']]
return action_result.set_status(phantom.APP_SUCCESS)
def _get_category(self, action_result, category):
ret_val, response = self._make_rest_call_helper('/api/v1/urlCategories', action_result)
if phantom.is_fail(ret_val):
return ret_val, response
for cat in response:
if cat.get('configuredName', None) == category:
return RetVal(phantom.APP_SUCCESS, cat)
for cat in response:
if cat['id'] == category:
return RetVal(phantom.APP_SUCCESS, cat)
return RetVal(
action_result.set_status(
phantom.APP_ERROR, "Unable to find category"
),
None
)
def _check_category(self, action_result, endpoints, category, action):
ret_val, response = self._get_category(action_result, category)
if phantom.is_fail(ret_val):
return ret_val, response
self._category = response
urls = response.get('dbCategorizedUrls', [])
return self._filter_endpoints(action_result, endpoints, urls, action, 'Category')
def _amend_category(self, action_result, endpoints, category, action):
ret_val, filtered_endpoints = self._check_category(action_result, endpoints, category, action)
if phantom.is_fail(ret_val) or filtered_endpoints is None:
return ret_val
params = {'action': action }
data = {
"configuredName": self._category.get('configuredName'),
"keywordsRetainingParentCategory": self._category.get("keywordsRetainingParentCategory", []),
"urls": [],
"dbCategorizedUrls": filtered_endpoints
}
ret_val, response = self._make_rest_call_helper(
'/api/v1/urlCategories/{}'.format(self._category['id']),
action_result, data=data, method='put', params=params, timeout=None
)
if phantom.is_fail(ret_val):
return ret_val
action_result.add_data(response)
summary = action_result.set_summary({})
summary['updated'] = filtered_endpoints
summary['ignored'] = list(set(endpoints) - set(filtered_endpoints))
# Encode the unicode IP or URL strings
summary['updated'] = [element for element in summary['updated']]
summary['ignored'] = [element for element in summary['ignored']]
return action_result.set_status(phantom.APP_SUCCESS)
def _block_endpoint(self, action_result, endpoints, category):
list_endpoints = list()
list_endpoints = [x.strip() for x in endpoints.split(',')]
endpoints = list(filter(None, list_endpoints))
endpoints = self._truncate_protocol(endpoints)
if self.get_action_identifier() in ['block_url']:
ret_val = self._check_for_overlength(action_result, endpoints)
if phantom.is_fail(ret_val):
return ret_val
if category is None:
return self._amend_blocklist(action_result, endpoints, 'ADD_TO_LIST')
else:
return self._amend_category(action_result, endpoints, category, 'ADD_TO_LIST')
def _unblock_endpoint(self, action_result, endpoints, category):
list_endpoints = list()
list_endpoints = [x.strip() for x in endpoints.split(',')]
endpoints = list(filter(None, list_endpoints))
endpoints = self._truncate_protocol(endpoints)
if self.get_action_identifier() in ['unblock_url']:
ret_val = self._check_for_overlength(action_result, endpoints)
if phantom.is_fail(ret_val):
return ret_val
if category is None:
return self._amend_blocklist(action_result, endpoints, 'REMOVE_FROM_LIST')
else:
return self._amend_category(action_result, endpoints, category, 'REMOVE_FROM_LIST')
def _handle_block_ip(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._block_endpoint(action_result, param['ip'], param.get('url_category'))
def _handle_block_url(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._block_endpoint(action_result, param['url'], param.get('url_category'))
def _handle_unblock_ip(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._unblock_endpoint(action_result, param['ip'], param.get('url_category'))
def _handle_unblock_url(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._unblock_endpoint(action_result, param['url'], param.get('url_category'))
def _allowlist_endpoint(self, action_result, endpoints, category):
list_endpoints = list()
list_endpoints = [x.strip() for x in endpoints.split(',')]
endpoints = list(filter(None, list_endpoints))
endpoints = self._truncate_protocol(endpoints)
if self.get_action_identifier() in ['allow_url']:
ret_val = self._check_for_overlength(action_result, endpoints)
if phantom.is_fail(ret_val):
return ret_val
if category is None:
return self._amend_allowlist(action_result, endpoints, 'ADD_TO_LIST')
else:
return self._amend_category(action_result, endpoints, category, 'ADD_TO_LIST')
def _unallow_endpoint(self, action_result, endpoints, category):
list_endpoints = list()
list_endpoints = [x.strip() for x in endpoints.split(',')]
endpoints = list(filter(None, list_endpoints))
endpoints = self._truncate_protocol(endpoints)
if self.get_action_identifier() in ['unallow_url']:
ret_val = self._check_for_overlength(action_result, endpoints)
if phantom.is_fail(ret_val):
return ret_val
if category is None:
return self._amend_allowlist(action_result, endpoints, 'REMOVE_FROM_LIST')
else:
return self._amend_category(action_result, endpoints, category, 'REMOVE_FROM_LIST')
def _handle_allow_ip(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._allowlist_endpoint(action_result, param['ip'], param.get('url_category'))
def _handle_allow_url(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._allowlist_endpoint(action_result, param['url'], param.get('url_category'))
def _handle_unallow_ip(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._unallow_endpoint(action_result, param['ip'], param.get('url_category'))
def _handle_unallow_url(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
return self._unallow_endpoint(action_result, param['url'], param.get('url_category'))
def _lookup_endpoint(self, action_result, endpoints):
if not endpoints:
action_result.set_status(phantom.APP_ERROR, "Please provide valid list of URL(s)")
ret_val, response = self._make_rest_call_helper(
'/api/v1/urlLookup', action_result,
data=endpoints, method='post'
)
if phantom.is_fail(ret_val):
return ret_val
ret_val, blocklist_response = self._make_rest_call_helper(
'/api/v1/security/advanced', action_result, method='get'
)
if phantom.is_fail(ret_val):
return ret_val
for e in endpoints:
if e in blocklist_response.get('blacklistUrls', []):
[response[i].update({"blocklisted": True}) for i, item in enumerate(response) if item['url'] == e]
else:
[response[i].update({"blocklisted": False}) for i, item in enumerate(response) if item['url'] == e]
action_result.update_data(response)
return action_result.set_status(phantom.APP_SUCCESS, "Successfully completed lookup")
def _handle_get_report(self, param):
"""
This action is used to retrieve a sandbox report of provided md5 file hash
:param file_hash: md5Hash of file
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
action_result = self.add_action_result(ActionResult(dict(param)))
file_hash = param['file_hash']
ret_val, sandbox_report = self._make_rest_call_helper('/api/v1/sandbox/report/{0}?details=full'
.format(file_hash), action_result)
if phantom.is_fail(ret_val):
return action_result.get_status()
if sandbox_report.get(ZSCALER_JSON_FULL_DETAILS) and ZSCLAER_ERR_MD5_UNKNOWN_MSG in sandbox_report.get(
ZSCALER_JSON_FULL_DETAILS):
return action_result.set_status(phantom.APP_ERROR, sandbox_report.get(ZSCALER_JSON_FULL_DETAILS))
action_result.add_data(sandbox_report)
return action_result.set_status(phantom.APP_SUCCESS, ZSCALER_SANDBOX_GET_REPORT_MSG)
def _handle_submit_file(self, param):
"""
This action is used to retrieve a sandbox report of provided md5 file hash
:param file_hash: md5Hash of file
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
action_result = self.add_action_result(ActionResult(dict(param)))
if not (self._sandbox_api_token and self._sandbox_base_url):
return action_result.set_status(
phantom.APP_ERROR, "Please provide ZScaler Sandbox Base URL and API token to submit the file to Sandbox")
self._base_url = self._sandbox_base_url
try:
file_id = param['vault_id']
success, msg, file_info = phantom_rules.vault_info(vault_id=file_id)
file_info = list(file_info)[0]
except IndexError:
return action_result.set_status(phantom.APP_ERROR, 'Vault file could not be found with supplied Vault ID')
except Exception as e:
err_msg = self._get_err_msg_from_exception(e)
self.debug_print("Vault ID not valid. Error: {}".format(err_msg))
return action_result.set_status(phantom.APP_ERROR, 'Vault ID not valid')
params = {
'force': 1 if param.get('force', False) else 0,
'api_token': self._sandbox_api_token
}
with open(file_info.get('path'), 'rb') as f:
data = f.read()
ret_val, resp_json = self._make_rest_call_helper('/zscsb/submit',
action_result, params=params, data=data, method='post', use_json=False)
if phantom.is_fail(ret_val):
return action_result.get_status()
if resp_json.get('code') != 200:
return action_result.set_status(phantom.APP_ERROR,
"Status code: {} Details: {}. Please make sure ZScaler Sandbox Base URL and API token are configured correctly"
.format(resp_json.get('code'), resp_json.get('message')))
action_result.add_data(resp_json)
if resp_json.get('message') == '/submit response OK':
msg = ZSCALER_SANDBOX_SUBMIT_FILE_MSG
else:
if resp_json.get('message').lower() != resp_json.get('sandboxSubmission').lower():
msg = 'Status Code: {}. Data from server: {}. {}.'.format(resp_json.get('code'), resp_json.get('sandboxSubmission'),
resp_json.get('message'))
else:
msg = 'Status Code: {}. Data from server: {}'.format(resp_json.get('code'), resp_json.get('message'))
return action_result.set_status(phantom.APP_SUCCESS, msg)
def _handle_list_url_categories(self, param):
"""
This action is used to fetch all the URL categories
:param: No parameters
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
action_result = self.add_action_result(ActionResult(dict(param)))
ret_val, list_url_categories = self._make_rest_call_helper('/api/v1/urlCategories', action_result)
if phantom.is_fail(ret_val):
return action_result.get_status()
for url_category in list_url_categories:
action_result.add_data(url_category)
summary = action_result.update_summary({})
summary['total_url_categories'] = action_result.get_data_size()
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_lookup_ip(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
list_endpoints = list()
list_endpoints = [x.strip() for x in param['ip'].split(',')]
endpoints = list(filter(None, list_endpoints))
return self._lookup_endpoint(action_result, endpoints)
def _handle_lookup_url(self, param):
action_result = self.add_action_result(ActionResult(dict(param)))
list_endpoints = list()
list_endpoints = [x.strip() for x in param['url'].split(',')]
endpoints = list(filter(None, list_endpoints))
endpoints = self._truncate_protocol(endpoints)
ret_val = self._check_for_overlength(action_result, endpoints)
if phantom.is_fail(ret_val):
return ret_val
return self._lookup_endpoint(action_result, endpoints)
def _truncate_protocol(self, endpoints):
"""
This function truncates the protocol from the list of URLs if present
:param: endpoints: list of URLs
:return: updated list of url
"""
for i in range(len(endpoints)):
if endpoints[i].startswith("http://"):
endpoints[i] = endpoints[i][(len("http://")):]
elif endpoints[i].startswith("https://"):
endpoints[i] = endpoints[i][(len("https://")):]
return endpoints
def _check_for_overlength(self, action_result, endpoints):
"""This function checks whether the length of each url is not more
than 1024
:param: :endpoints: list of URLs
"""
for url in endpoints:
if len(url) > 1024:
return action_result.set_status(phantom.APP_ERROR,
"Please provide valid comma-separated values in the action parameter. Max allowed length for each value is 1024.")
return phantom.APP_SUCCESS
def _handle_get_admin_users(self, param):
"""
This action is used to fetch all admin users
:param: No parameters
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
action_result = self.add_action_result(ActionResult(dict(param)))
ret_val, limit = self._validate_integer(action_result, param.get('limit', ZSCALER_MAX_PAGESIZE), ZSCALER_LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
params = {}
admin_users = []
params['page'] = 1
while True:
if limit < ZSCALER_MAX_PAGESIZE:
params['pageSize'] = limit
else:
params['pageSize'] = ZSCALER_MAX_PAGESIZE
ret_val, get_admin_users = self._make_rest_call_helper('/api/v1/adminUsers', action_result, params=params)
if phantom.is_fail(ret_val):
return action_result.get_status()
for admin_user in get_admin_users:
admin_users.append(admin_user)
limit = limit - params['pageSize']
if limit <= 0 or len(get_admin_users) == 0:
break
params['page'] += 1
for user in admin_users:
action_result.add_data(user)
summary = action_result.update_summary({})
summary['total_admin_users'] = action_result.get_data_size()
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_get_users(self, param):
"""
This action is used to fetch all users
:param name: User name
:param dept: User department
:param group: User group
:param limit: Max number of users to retrieve
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
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 param:
return action_result.set_status(phantom.APP_ERROR, "No filters provided")
ret_val, limit = self._validate_integer(action_result, param.get('limit', ZSCALER_MAX_PAGESIZE), ZSCALER_LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
params = {
"name": param.get('name'),
"dept": param.get('dept'),
"group": param.get('group'),
'page': 1
}
users = []
while True:
params['pageSize'] = min(limit, ZSCALER_MAX_PAGESIZE)
ret_val, get_users = self._make_rest_call_helper('/api/v1/users', action_result, params=params, timeout=None)
if phantom.is_fail(ret_val):
return action_result.get_status()
for user in get_users:
users.append(user)
limit = limit - params['pageSize']
if limit <= 0 or len(get_users) == 0:
break
params['page'] += 1
# Add the response into the data section
for user in users:
action_result.add_data(user)
# Add a dictionary that is made up of the most important values from data into the summary
summary = action_result.update_summary({})
summary['total_users'] = action_result.get_data_size()
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_get_groups(self, param):
"""
This action is used to fetch groups based on search parameter
:param search: Search string to match
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
ret_val, limit = self._validate_integer(action_result, param.get('limit', ZSCALER_MAX_PAGESIZE), ZSCALER_LIMIT_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
params = {"search": param.get('search')}
groups = []
params['page'] = 1
while True:
params['pageSize'] = min(limit, ZSCALER_MAX_PAGESIZE)
ret_val, get_groups = self._make_rest_call_helper('/api/v1/groups', action_result, params=params)
if phantom.is_fail(ret_val):
return action_result.get_status()
for group in get_groups:
groups.append(group)
limit = limit - params['pageSize']
if limit <= 0 or len(get_groups) == 0:
break
params['page'] += 1
for group in groups:
action_result.add_data(group)
summary = action_result.update_summary({})
summary['total_groups'] = action_result.get_data_size()
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_add_group_user(self, param):
"""
This action is used to add users to a group based on user id and group id
:param user_id: User ID to add
:param group_id: Group to add user tio
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
user_id = param['user_id']
group_id = param['group_id']
ret_val, user_response = self._make_rest_call_helper(f'/api/v1/users/{user_id}', action_result)
if phantom.is_fail(ret_val):
return action_result.get_status()
ret_val, group_response = self._make_rest_call_helper(f'/api/v1/groups/{group_id}', action_result)
if phantom.is_fail(ret_val):
return action_result.get_status()
summary = action_result.update_summary({})
if group_response in user_response['groups']:
summary['message'] = "User already in group"
action_result.add_data(group_response)
return action_result.set_status(phantom.APP_SUCCESS, "User already in group")
user_response['groups'].append(group_response)
data = user_response
ret_val, response = self._make_rest_call_helper(f'/api/v1/users/{user_id}', action_result, data=data, method='put')
if phantom.is_fail(ret_val):
return action_result.get_status()
action_result.add_data(response)
summary['message'] = "User successfully added to group"
return action_result.set_status(phantom.APP_SUCCESS)
def _handle_remove_group_user(self, param):
"""
This action is used to remove users from a group based on user id and group id
:param user_id: User ID to remove
:param group_id: Group to remove user from
:return: status phantom.APP_ERROR/phantom.APP_SUCCESS(along with appropriate message)
"""
self.save_progress("In action handler for: {0}".format(self.get_action_identifier()))
action_result = self.add_action_result(ActionResult(dict(param)))
user_id = param['user_id']
group_id = param['group_id']
ret_val, user_response = self._make_rest_call_helper(f'/api/v1/users/{user_id}', action_result)
if phantom.is_fail(ret_val):
return action_result.get_status()
summary = action_result.update_summary({})
if group_id not in [item['id'] for item in user_response['groups']]:
summary['message'] = "User already removed from group"
action_result.add_data(user_response)
return action_result.set_status(phantom.APP_SUCCESS, "User already removed from group")
for index, group in enumerate(user_response['groups']):
if group_id == group['id']:
user_response['groups'].pop(index)
data = user_response
ret_val, response = self._make_rest_call_helper(f'/api/v1/users/{user_id}', action_result, data=data, method='put')
if phantom.is_fail(ret_val):
return action_result.get_status()
action_result.add_data(response)
summary['message'] = "User removed from group"
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())