Skip to content

Commit

Permalink
[PLGN-634] InsightVM - Asset Search | JsonDecode Error (#2185)
Browse files Browse the repository at this point in the history
* Improved error handling ever so slightly

* Changed single letter variables

* Change error to data
  • Loading branch information
cmcnally-r7 committed Dec 13, 2023
1 parent 3155056 commit 54db789
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
2 changes: 1 addition & 1 deletion plugins/rapid7_insightvm/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -3966,7 +3966,7 @@ Example output:

# Version History

* 6.2.0 - `Scan Completion` - New trigger added to retrieve vulnerability information on assets when a scan is completed
* 6.2.0 - `Scan Completion` - New trigger added to retrieve vulnerability information on assets when a scan is completed | Improved error handling across all API calls
* 6.1.1 - Update actions `Update Site Excluded Targets` and `Update Site Included Targets` to prevent error on empty addresses
* 6.1.0 - Add new optional input `override_blackout` in `Scan` action
* 6.0.0 - Fix file output type for `Generate Adhoc SQL Report` | Replace custom output type `file` with `insightvm_file` for each item in the `asset` `files` output in multiple actions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ def __init__(self):

def run(self, params={}):

resource_helper = ResourceRequests(self.connection.session, self.logger)
search_criteria = params.get(Input.SEARCHCRITERIA)
size = params.get(Input.SIZE, 0)
sort_criteria = params.get(Input.SORT_CRITERIA, {})
self.logger.info(f"Performing filtered asset search with criteria {search_criteria}")

self.logger.info(f"Performing filtered asset search with criteria {search_criteria}...")
resource_helper = ResourceRequests(self.connection.session, self.logger)
endpoint = endpoints.Asset.search(self.connection.console_url)

parameters = []

for key, value in sort_criteria.items():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ResourceRequests(object):

# Static headers for all requests
_HEADERS = {"Content-Type": "application/json", "Accept": "application/json"}
_ENSURECONNECTIVITY = (
_ENSURE_CONNECTIVITY = (
"Ensure proper network connectivity between the InsightConnect orchestrator and the InsightVM console"
)

Expand All @@ -50,10 +50,10 @@ class ResourceRequests(object):
requests.HTTPError: "If this issue persists contact support for assistance.",
requests.ConnectionError: "Unable to connect to IVM console."
"If this issue persists contact support for assistance.",
requests.Timeout: _ENSURECONNECTIVITY,
requests.ConnectTimeout: _ENSURECONNECTIVITY,
requests.ReadTimeout: _ENSURECONNECTIVITY,
requests.TooManyRedirects: _ENSURECONNECTIVITY,
requests.Timeout: _ENSURE_CONNECTIVITY,
requests.ConnectTimeout: _ENSURE_CONNECTIVITY,
requests.ReadTimeout: _ENSURE_CONNECTIVITY,
requests.TooManyRedirects: _ENSURE_CONNECTIVITY,
}

# For request exceptions not in REQUEST_EXCEPTIONS
Expand Down Expand Up @@ -103,17 +103,17 @@ def resource_request(
extras = {"json": payload, "params": parameters.params}
try:
response = request_method(url=endpoint, verify=False, **extras)
except requests.RequestException as e:
assistance = self._REQUEST_EXCEPTIONS.get(type(e), self._UNHANDLED_EXCEPTION)
raise PluginException(cause=e, assistance=assistance)
except requests.RequestException as error:
assistance = self._REQUEST_EXCEPTIONS.get(type(error), self._UNHANDLED_EXCEPTION)
raise PluginException(cause=error, assistance=assistance)

resource_request_status_code_check(response.text, response.status_code)

if json_response:
try:
resource = response.json()
except json.decoder.JSONDecodeError:
raise PluginException(preset=PluginException.Preset.INVALID_JSON)
except json.decoder.JSONDecodeError as error:
raise PluginException(preset=PluginException.Preset.INVALID_JSON, data=f"Error returned: {error}")
else:
resource = {"raw": response.text}

Expand Down Expand Up @@ -193,15 +193,15 @@ def get_resource_page(self, endpoint: str, method: str, params: RequestParams, p
extras = {"json": payload, "params": params.params}
try:
response = request_method(url=endpoint, verify=False, **extras)
except requests.RequestException as e:
assistance = self._REQUEST_EXCEPTIONS.get(type(e), self._UNHANDLED_EXCEPTION)
raise PluginException(cause=e, assistance=assistance)
except requests.RequestException as error:
assistance = self._REQUEST_EXCEPTIONS.get(type(error), self._UNHANDLED_EXCEPTION)
raise PluginException(cause=error, assistance=assistance)

resource_request_status_code_check(response.text, response.status_code)
try:
response_json = response.json()
except json.decoder.JSONDecodeError:
raise PluginException(preset=PluginException.Preset.INVALID_JSON)
except json.decoder.JSONDecodeError as error:
raise PluginException(preset=PluginException.Preset.INVALID_JSON, data=f"Error returned: {error}")

result = RequestResult(
page_num=response_json["page"]["number"],
Expand Down

0 comments on commit 54db789

Please sign in to comment.