forked from splunk-soar-connectors/digitalshadows
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathds_incidents_connector.py
191 lines (168 loc) · 9.61 KB
/
ds_incidents_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
# File: ds_incidents_connector.py
#
# Licensed under Apache 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
#
import phantom.app as phantom
from phantom.action_result import ActionResult
from digital_shadows_consts import (DS_API_KEY_CFG, DS_API_SECRET_KEY_CFG, DS_BP_SUBTYPE, DS_DL_SUBTYPE, DS_GET_INCIDENT_SUCCESS,
DS_INFR_SUBTYPE, DS_PS_SUBTYPE, DS_SMC_SUBTYPE, INCIDENT_ID_KEY, SERVICE_ERROR_MESSAGE)
from dsapi.service.incident_service import IncidentService
from exception_handling_functions import ExceptionHandling
class DSIncidentsConnector(object):
def __init__(self, connector):
"""
:param connector: DigitalShadowsConnector
"""
self._connector = connector
config = connector.get_config()
self._handle_exception_object = ExceptionHandling(connector)
self._ds_api_key = config[DS_API_KEY_CFG]
self._ds_api_secret_key = config[DS_API_SECRET_KEY_CFG]
def get_incident_by_id(self, param):
action_result = ActionResult(dict(param))
self._connector.add_action_result(action_result)
try:
incident_service = IncidentService(self._ds_api_key, self._ds_api_secret_key)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "{0} {1}".format(SERVICE_ERROR_MESSAGE, error_message))
incident_id = param['incident_id']
# validate 'incident_id' action parameter
ret_val, incident_id = self._handle_exception_object.validate_integer(action_result, incident_id, INCIDENT_ID_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
incident = incident_service.find_incident_by_id(incident_id)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error Connecting to server. {0}".format(error_message))
if 'id' in incident:
summary = {
'incident_found': True
}
action_result.update_summary(summary)
action_result.add_data(incident)
action_result.set_status(phantom.APP_SUCCESS, DS_GET_INCIDENT_SUCCESS)
return action_result.get_status()
def get_incident_review_by_id(self, param):
action_result = ActionResult(dict(param))
self._connector.add_action_result(action_result)
incident_id = param['incident_id']
# validate 'incident_id' action parameter
ret_val, incident_id = self._handle_exception_object.validate_integer(action_result, incident_id, INCIDENT_ID_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
try:
incident_service = IncidentService(self._ds_api_key, self._ds_api_secret_key)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "{0} {1}".format(SERVICE_ERROR_MESSAGE, error_message))
try:
incident_reviews = incident_service.find_all_reviews(incident_id)
incident_reviews_total = len(incident_reviews)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error Connecting to server. {0}".format(error_message))
if incident_reviews_total > 0:
summary = {
'incident_reviews_count': incident_reviews_total,
'incident_reviews_found': True
}
action_result.update_summary(summary)
for incident_review in incident_reviews:
action_result.add_data(incident_review)
action_result.set_status(phantom.APP_SUCCESS, "Digital Shadows incident reviews fetched for the Incident ID: {}".format(incident_id))
return action_result.get_status()
def get_incident_list(self, param):
action_result = ActionResult(dict(param))
self._connector.add_action_result(action_result)
# interval_startdate = date.today() - timedelta(int(param['date_range']))
incident_types = []
if param.get('incident_types') is not None:
param_incident_types = str(param['incident_types']).split(',')
for inc_type in param_incident_types:
if inc_type == "DATA_LEAKAGE":
incident_types.append({'type': 'DATA_LEAKAGE', 'subTypes': DS_DL_SUBTYPE})
if inc_type == "BRAND_PROTECTION":
incident_types.append({'type': 'BRAND_PROTECTION', 'subTypes': DS_BP_SUBTYPE})
if inc_type == "INFRASTRUCTURE":
incident_types.append({'type': 'INFRASTRUCTURE', 'subTypes': DS_INFR_SUBTYPE})
if inc_type == "PHYSICAL_SECURITY":
incident_types.append({'type': 'PHYSICAL_SECURITY', 'subTypes': DS_PS_SUBTYPE})
if inc_type == "SOCIAL_MEDIA_COMPLIANCE":
incident_types.append({'type': 'SOCIAL_MEDIA_COMPLIANCE', 'subTypes': DS_SMC_SUBTYPE})
if inc_type == "CYBER_THREAT":
incident_types.append({'type': 'CYBER_THREAT'})
else:
param_incident_types = None
try:
incident_service = IncidentService(self._ds_api_key, self._ds_api_secret_key)
incident_view = IncidentService.incidents_view(
date_range=param.get('date_range'),
date_range_field='published', types=incident_types)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "{0} {1}".format(SERVICE_ERROR_MESSAGE, error_message))
self._connector.save_progress("incident view: {}".format(incident_view))
try:
incident_pages = incident_service.find_all_pages(view=incident_view)
self._connector.save_progress("incident_pages next: {}".format(incident_pages))
incident_total = len(incident_pages)
except StopIteration:
error_message = 'No Incident objects retrieved from the Digital Shadows API in page groups'
return action_result.set_status(phantom.APP_ERROR, "Error Details: {0}".format(error_message))
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error Connecting to server. {0}".format(error_message))
if incident_total > 0:
summary = {
'incident_count': incident_total,
'incident_found': True
}
action_result.update_summary(summary)
self._connector.save_progress("incident_pages: {}".format(incident_pages))
for incident_page in incident_pages:
for incident in incident_page:
self._connector.save_progress('incident: {}'.format(incident.payload))
action_result.add_data(incident.payload)
action_result.set_status(phantom.APP_SUCCESS, DS_GET_INCIDENT_SUCCESS)
return action_result.get_status()
def post_incident_review(self, param):
action_result = ActionResult(dict(param))
self._connector.add_action_result(action_result)
try:
incident_service = IncidentService(self._ds_api_key, self._ds_api_secret_key)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "{0} {1}".format(SERVICE_ERROR_MESSAGE, error_message))
incident_id = param.get('incident_id')
# validate 'incident_id' action parameter
ret_val, incident_id = self._handle_exception_object.validate_integer(action_result, incident_id, INCIDENT_ID_KEY)
if phantom.is_fail(ret_val):
return action_result.get_status()
post_data = {
'note': param.get('review_note'),
'status': param.get('review_status')
}
self._connector.save_progress("post_data: {}".format(post_data))
try:
response = incident_service.post_incident_review(post_data, incident_id=incident_id)
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error Connecting to server. {0}".format(error_message))
self._connector.save_progress("response: {}".format(response))
try:
summary = {
'incident_reviews_status_code': response['status'],
'incident_reviews_message': response['message']
}
action_result.update_summary(summary)
action_result.add_data(response['content'][0])
if response['message'] == "SUCCESS":
action_result.set_status(phantom.APP_SUCCESS, "Digital Shadows Incident review posted successfully")
else:
action_result.set_status(phantom.APP_SUCCESS, "Error in incident review post request")
except Exception as e:
error_message = self._handle_exception_object.get_error_message_from_exception(e)
return action_result.set_status(phantom.APP_ERROR, "Error occurred while fetching data from response. {}".format(error_message))
return action_result.get_status()