Skip to content

Commit

Permalink
fix converting a response to json CoreIRApiModule (#34975)
Browse files Browse the repository at this point in the history
* fix converting a response to json

* dict not response

* remove UTs

* RN

* final fix

* UT

* edit UT

* UT

* RN

* pre commit

---------

Co-authored-by: yrosenberg <[email protected]>
  • Loading branch information
rshunim and RosenbergYehuda authored Jun 23, 2024
1 parent 1d17d33 commit 4f55928
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 29 deletions.
5 changes: 5 additions & 0 deletions Packs/ApiModules/ReleaseNotes/2_2_26.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#### Scripts

##### CoreIRApiModule

- Fixed an issue where ***core-retrieve-file-details*** failed parsing the response.
6 changes: 5 additions & 1 deletion Packs/ApiModules/Scripts/CoreIRApiModule/CoreIRApiModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,11 @@ def _http_request(self, method, url_suffix='', full_url=None, headers=None, json
)
if ok_codes and response.get('status') not in ok_codes:
self._handle_error(error_handler, response, with_metrics)
return response['reply'].get('data', {})
try:
return json.loads(response['data'])
except json.JSONDecodeError:
demisto.debug(f"Converting data to json was failed. Return it as is. The data's type is {type(response['data'])}")
return response['data']

def get_incidents(self, incident_id_list=None, lte_modification_time=None, gte_modification_time=None,
lte_creation_time=None, gte_creation_time=None, status=None, starred=None,
Expand Down
33 changes: 9 additions & 24 deletions Packs/ApiModules/Scripts/CoreIRApiModule/CoreIRApiModule_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import demistomock
import demistomock as demisto
from CommonServerPython import Common, tableToMarkdown, pascalToSpace, DemistoException, BaseClient
from CommonServerPython import Common, tableToMarkdown, pascalToSpace, DemistoException
from CoreIRApiModule import CoreClient, handle_outgoing_issue_closure, XSOAR_RESOLVED_STATUS_TO_XDR
from CoreIRApiModule import add_tag_to_endpoints_command, remove_tag_from_endpoints_command, quarantine_files_command, \
isolate_endpoint_command, list_user_groups_command, parse_user_groups, list_users_command, list_roles_command, \
Expand Down Expand Up @@ -4055,14 +4055,19 @@ def test_xsoar_to_xdr_flexible_close_reason_mapping(capfd, mocker, custom_mappin
assert remote_args.delta['status'] == expected_resolved_status[i]


def test_http_request_demisto_call(mocker):
@pytest.mark.parametrize('data, expected_result',
[('{"reply": {"container": ["1.1.1.1"]}}', {"reply": {"container": ["1.1.1.1"]}}),
(b'XXXXXXX', b'XXXXXXX')])
def test_http_request_demisto_call(mocker, data, expected_result):
"""
Given:
- An XSIAM machine with a build version that supports demisto._apiCall() with RBAC validations.
When:
- Calling the http_request method.
Then:
- Make sure demisto._apiCall() is being called and the method returns the expected result.
- converting to json is possible - do it and return json
- converting to json is impossible - catch the error and return the data as is
"""
from CoreIRApiModule import CoreClient
client = CoreClient(
Expand All @@ -4071,27 +4076,7 @@ def test_http_request_demisto_call(mocker):
mocker.patch("CoreIRApiModule.FORWARD_USER_RUN_RBAC", new=True)
mocker.patch.object(demisto, "_apiCall", return_value={'name': '/api/webapp/public_api/v1/distributions/get_versions/',
'status': 200,
'reply': {"data": {"container": ["1.1.1.1"]}}})
'data': data})
res = client._http_request(method="POST",
url_suffix="/distributions/get_versions/")
assert res == {"container": ["1.1.1.1"]}


def test_http_request_base_client(mocker):
"""
Given:
- An XSIAM machine with a build version that supports demisto._apiCall() with RBAC validations.
When
- Calling the http_request method.
Then
- Make sure demisto._apiCall() is being called and the method returns the expected result.
"""
from CoreIRApiModule import CoreClient
client = CoreClient(
base_url=f'{Core_URL}/public_api/v1', headers={}
)
mocker.patch("CoreIRApiModule.FORWARD_USER_RUN_RBAC", new=False)
mocker.patch.object(BaseClient, "_http_request", return_value={'reply': {"data": {"container": ["1.1.1.1"]}}})
res = client._http_request(method="POST",
url_suffix="/distributions/get_versions/")
assert res['reply'] == {"data": {"container": ["1.1.1.1"]}}
assert expected_result == res
2 changes: 1 addition & 1 deletion Packs/ApiModules/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ApiModules",
"description": "API Modules",
"support": "xsoar",
"currentVersion": "2.2.25",
"currentVersion": "2.2.26",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
6 changes: 6 additions & 0 deletions Packs/Core/ReleaseNotes/3_0_39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Integrations

##### Investigation & Response

Fixed an issue in CoreIRApiModule where it was failing to parse a response.
2 changes: 1 addition & 1 deletion Packs/Core/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Core - Investigation and Response",
"description": "Automates incident response",
"support": "xsoar",
"currentVersion": "3.0.38",
"currentVersion": "3.0.39",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
6 changes: 6 additions & 0 deletions Packs/CortexXDR/ReleaseNotes/6_1_46.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Integrations

##### Palo Alto Networks Cortex XDR - Investigation and Response

Fixed an issue in CoreIRApiModule where it was failing to parse a response.
2 changes: 1 addition & 1 deletion Packs/CortexXDR/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Cortex XDR by Palo Alto Networks",
"description": "Automates Cortex XDR incident response, and includes custom Cortex XDR incident views and layouts to aid analyst investigations.",
"support": "xsoar",
"currentVersion": "6.1.45",
"currentVersion": "6.1.46",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down
6 changes: 6 additions & 0 deletions Packs/ctf01/ReleaseNotes/1_0_17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

#### Integrations

##### Cortex XDR - IR CTF

Fixed an issue in CoreIRApiModule where it was failing to parse a response.
2 changes: 1 addition & 1 deletion Packs/ctf01/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Capture The Flag - 01",
"description": "XSOAR's Capture the flag (CTF)",
"support": "xsoar",
"currentVersion": "1.0.16",
"currentVersion": "1.0.17",
"serverMinVersion": "8.2.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
Expand Down

0 comments on commit 4f55928

Please sign in to comment.