Skip to content

Commit 7c9bddd

Browse files
refactor(TKT-9082): add environment attribute to Alert and Incident classes
1 parent ee45213 commit 7c9bddd

7 files changed

Lines changed: 58 additions & 30 deletions

File tree

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
1.22.2
2+
-------
3+
- enforce providing an environemnt for alerts and incidents
4+
15
1.22.1
26
-------
37
- Add Get raw data for alerts and incidents

intezer_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.22.1'
1+
__version__ = '1.22.2'

intezer_sdk/_api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,10 @@ def get_alert_by_alert_id(self, alert_id: str, environment: Optional[str] = None
726726

727727
return data_response['result'], data_response['status']
728728

729-
def get_incident_by_id(self, incident_id: str) -> dict:
729+
def get_incident_by_id(self, incident_id: str, environment: Optional[str] = None) -> dict:
730730
data = dict(incident_id=incident_id)
731+
if environment:
732+
data['environment'] = environment
731733
response = self.api.request_with_refresh_expired_access_token(method='GET',
732734
path='/incidents/get-by-id',
733735
data=data)

intezer_sdk/alerts.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ class Alert:
270270

271271
def __init__(self,
272272
alert_id: Optional[str] = None,
273+
environment: Optional[str] = None,
273274
alert_stream: Optional[BinaryIO] = None,
274275
api: IntezerApiClient = None):
275276
"""
@@ -278,6 +279,7 @@ def __init__(self,
278279
instance with the given alert id.
279280
280281
:param alert_id: The alert id.
282+
:param environment: The environment of the alert.
281283
:param api: The API connection to Intezer.
282284
"""
283285
if alert_stream and alert_id:
@@ -294,6 +296,7 @@ def __init__(self,
294296
else:
295297
self.alert_id: str = alert_id
296298

299+
self.environment = environment
297300
self._intezer_api_client = api
298301
self._api = IntezerApi(api or get_global_api())
299302
self._report: Optional[Dict] = None
@@ -320,12 +323,14 @@ def check_status(self) -> AlertStatusCode:
320323
321324
"""
322325
try:
323-
alert, status = self._api.get_alert_by_alert_id(alert_id=self.alert_id)
326+
alert, status = self._api.get_alert_by_alert_id(alert_id=self.alert_id, environment=self.environment)
324327
except requests.HTTPError:
325328
self.status = AlertStatusCode.NOT_FOUND
326329
raise errors.AlertNotFoundError(self.alert_id)
327330

328331
self._report = alert
332+
if not self.environment:
333+
self.environment = alert['environment']
329334

330335
if status in (AlertStatusCode.IN_PROGRESS.value, AlertStatusCode.QUEUED.value):
331336
self.status = AlertStatusCode.IN_PROGRESS
@@ -359,6 +364,7 @@ def result(self) -> dict:
359364
@classmethod
360365
def from_id(cls,
361366
alert_id: str,
367+
environment: Optional[str] = None,
362368
api: IntezerApiClient = None,
363369
fetch_scans: bool = False,
364370
wait: bool = False,
@@ -367,6 +373,7 @@ def from_id(cls,
367373
Create a new Alert instance, and fetch the alert data from the Intezer Analyze API.
368374
369375
:param alert_id: The alert id.
376+
:param environment: The environment of the alert.
370377
:param api: The API connection to Intezer.
371378
:param fetch_scans: Whether to fetch the scans for the alert - this could take some time.
372379
:param wait: Wait for the alert to finish processing before returning.
@@ -375,7 +382,7 @@ def from_id(cls,
375382
:raises intezer_sdk.errors.AlertInProgressError: If the alert is still being processed.
376383
:return: The Alert instance, with the updated alert data.
377384
"""
378-
new_alert = cls(alert_id=alert_id, api=api)
385+
new_alert = cls(alert_id=alert_id, environment=environment or None, api=api)
379386
status = new_alert.check_status()
380387
if status == AlertStatusCode.IN_PROGRESS and not wait:
381388
raise errors.AlertInProgressError(alert_id)
@@ -488,7 +495,7 @@ def send_phishing_email(cls,
488495
send_alert_params = {key: value for key, value in send_alert_params.items() if value is not None}
489496
alert_id = _api.send_binary_alert(**send_alert_params)
490497

491-
alert = cls(alert_id=alert_id, api=api)
498+
alert = cls(alert_id=alert_id, environment=environment, api=api)
492499
if wait:
493500
alert.wait_for_completion(timeout=timeout)
494501
return alert
@@ -550,17 +557,20 @@ def _fetch_scan(scan_: dict,
550557
_fetch_scan(scan, 'url_analysis', UrlAnalysis)
551558

552559
def get_raw_data(self,
553-
environment: str,
560+
environment: Optional[str] = None,
554561
raw_data_type: str = 'raw_alert') -> dict:
555562
"""
556563
Get raw alert data.
557564
558-
:param environment: The environment to get raw data from.
565+
:param environment: The environment to get raw data from. If not provided, the environment will be taken from the alert.
559566
:param raw_data_type: The type of raw data to retrieve. Defaults to 'raw_alert'.
560567
:return: The raw alert data.
561568
"""
569+
if not environment and not self.environment:
570+
raise ValueError('Environment is required to get raw data.')
571+
562572
return self._api.get_raw_alert_data(
563573
alert_id=self.alert_id,
564-
environment=environment,
574+
environment=environment or self.environment,
565575
raw_data_type=raw_data_type
566576
)

intezer_sdk/incidents.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ class Incident:
159159
:vartype intezer_incident_url: str
160160
"""
161161

162-
def __init__(self, incident_id: Optional[str] = None, api: IntezerApiClient = None):
162+
def __init__(
163+
self,
164+
incident_id: Optional[str] = None,
165+
environment: Optional[str] = None,
166+
api: IntezerApiClient = None,
167+
):
163168
"""
164169
Create a new Incident instance with the given incident id.
165170
Please note that this does not query the Intezer Analyze API for the incident data, but rather creates an Incident
@@ -168,10 +173,11 @@ def __init__(self, incident_id: Optional[str] = None, api: IntezerApiClient = No
168173
If you wish to fetch the incident data from the Intezer Analyze API, use the `from_id` class method.
169174
170175
:param incident_id: The incident id.
176+
:param environment: The environment of the incident.
171177
:param api: The API connection to Intezer.
172178
"""
173179
self.incident_id = incident_id
174-
180+
self.environment = environment
175181
self._intezer_api_client = api
176182
self._api = IntezerApi(api or get_global_api())
177183
self._result: Optional[Dict] = None
@@ -192,12 +198,17 @@ def fetch_info(self):
192198
raise ValueError("Incident ID is required to fetch incident info.")
193199

194200
try:
195-
self._result = self._api.get_incident_by_id(self.incident_id)
201+
self._result = self._api.get_incident_by_id(
202+
self.incident_id, self.environment
203+
)
196204
except HTTPError as e:
197205
if e.response.status_code == 404:
198206
raise errors.IncidentNotFoundError(self.incident_id)
199207
raise
200-
208+
209+
if not self.environment:
210+
self.environment = self._result['environment']
211+
201212
self.source = self._result.get('source')
202213
self.sender = self._result.get('sender')
203214
self.name = self._result.get('incident', {}).get('name')
@@ -215,34 +226,35 @@ def result(self) -> Optional[dict]:
215226
return self._result
216227

217228
@classmethod
218-
def from_id(cls, incident_id: str, api: IntezerApiClient = None) -> 'Incident':
229+
def from_id(cls, incident_id: str, environment: Optional[str] = None, api: IntezerApiClient = None) -> 'Incident':
219230
"""
220231
Create a new Incident instance, and fetch the incident data from the Intezer Analyze API.
221232
222233
:param incident_id: The incident id.
234+
:param environment: The environment of the incident.
223235
:param api: The API connection to Intezer.
224236
:raises intezer_sdk.errors.IncidentNotFound: If the incident was not found.
225237
:return: The Incident instance, with the updated incident data.
226238
"""
227-
new_incident = cls(incident_id, api=api)
239+
new_incident = cls(incident_id, environment=environment, api=api)
228240
new_incident.fetch_info()
229241
return new_incident
230242

231-
def get_raw_data(self,
232-
environment: str,
233-
raw_data_type: str = 'raw_incident') -> dict:
243+
def get_raw_data(
244+
self, environment: Optional[str] = None, raw_data_type: str = 'raw_incident'
245+
) -> dict:
234246
"""
235247
Get raw incident data.
236248
237-
:param environment: The environment to get raw data from.
249+
:param environment: The environment to get raw data from. If not provided, the environment will be taken from the incident.
238250
:param raw_data_type: The type of raw data to retrieve. Defaults to 'raw_incident'.
239251
:return: The raw incident data.
240252
"""
241-
if not self.incident_id:
242-
raise ValueError('Incident ID is required to get raw data.')
243-
253+
if not self.incident_id and not (self.environment or environment):
254+
raise ValueError('Incident ID and environment are required to get raw data.')
255+
244256
return self._api.get_raw_incident_data(
245257
incident_id=self.incident_id,
246-
environment=environment,
247-
raw_data_type=raw_data_type
258+
environment=environment or self.environment,
259+
raw_data_type=raw_data_type,
248260
)

tests/unit/test_alerts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def test_alert_from_id(self):
9595
status=HTTPStatus.OK,
9696
json={'result': {}, 'status': 'success'})
9797
# Act
98-
alert = Alert.from_id('alert_id')
98+
alert = Alert.from_id('alert_id', environment='environment')
9999

100100
# Assert
101101
self.assertEqual(alert.alert_id, 'alert_id')
@@ -111,7 +111,7 @@ def test_alert_from_id_waits_from_completion(self):
111111
status=HTTPStatus.OK,
112112
json={'result': {}, 'status': 'success'})
113113
# Act
114-
alert = Alert.from_id('alert_id', wait=True)
114+
alert = Alert.from_id('alert_id', environment='environment', wait=True)
115115

116116
# Assert
117117
self.assertEqual(alert.alert_id, 'alert_id')

tests/unit/test_incidents.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def test_incident_fetch_info_success(self):
142142
}
143143
})
144144

145-
incident = Incident(incident_id)
145+
incident = Incident(incident_id, environment='test_environment')
146146

147147
# Act
148148
incident.fetch_info()
@@ -195,7 +195,7 @@ def test_incident_result_returns_raw_data(self):
195195
status=HTTPStatus.OK,
196196
json={'result': expected_result})
197197

198-
incident = Incident(incident_id)
198+
incident = Incident(incident_id, environment='test_environment')
199199
incident.fetch_info()
200200

201201
# Act
@@ -206,7 +206,7 @@ def test_incident_result_returns_raw_data(self):
206206

207207
def test_incident_result_returns_none_when_no_data_fetched(self):
208208
# Arrange
209-
incident = Incident('test_incident_id')
209+
incident = Incident('test_incident_id', environment='test_environment')
210210

211211
# Act
212212
result = incident.result()
@@ -235,7 +235,7 @@ def test_incident_from_id_success(self):
235235
})
236236

237237
# Act
238-
incident = Incident.from_id(incident_id)
238+
incident = Incident.from_id(incident_id, environment='test_environment')
239239

240240
# Assert
241241
self.assertEqual(incident.incident_id, incident_id)
@@ -260,7 +260,7 @@ def test_get_raw_incident_data(self):
260260
json=expected_raw_data,
261261
status=HTTPStatus.OK)
262262

263-
incident = Incident(incident_id=incident_id)
263+
incident = Incident(incident_id=incident_id, environment=environment)
264264

265265
# Act
266266
result_data = incident.get_raw_data(environment=environment)

0 commit comments

Comments
 (0)