Skip to content

Commit 3ac7de2

Browse files
committed
Refactor ProwlerParser to improve error handling and remove redundant code
- Removed JSONDecodeError import and simplified error handling for unsupported file formats - Enhanced get_findings method to raise an Error with a clear message for unsupported file types - Eliminated unnecessary content decoding in _parse_json and _parse_csv methods, as content is already decoded in get_findings - Added debug logging for skipped findings without required fields in _parse_json_findings
1 parent b7a41f7 commit 3ac7de2

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

dojo/tools/prowler/parser.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import json
33
import logging
44
from io import StringIO
5-
from json.decoder import JSONDecodeError
65

76
from dojo.models import Finding
87

@@ -44,27 +43,20 @@ def get_findings(self, file, test):
4443
csv_data = self._parse_csv(content)
4544
findings = self._parse_csv_findings(csv_data, test, file_name=file_name)
4645
else:
47-
# Try to detect format from content if extension not recognized
48-
try:
49-
data = self._parse_json(content)
50-
findings = self._parse_json_findings(data, test, file_name=file_name)
51-
except (JSONDecodeError, ValueError):
52-
csv_data = self._parse_csv(content)
53-
findings = self._parse_csv_findings(csv_data, test, file_name=file_name)
46+
# If file type can't be determined from extension, throw an error
47+
error_message = f"Unsupported file format. Prowler parser only supports JSON and CSV files. File name: {file_name}"
48+
raise ValueError(error_message)
5449

5550
return findings
5651

5752
def _parse_json(self, content):
5853
"""Safely parse JSON content"""
59-
if isinstance(content, bytes):
60-
content = content.decode("utf-8")
54+
# Content is already decoded in get_findings method
6155
return json.loads(content)
6256

6357
def _parse_csv(self, content):
6458
"""Parse CSV content"""
65-
if isinstance(content, bytes):
66-
content = content.decode("utf-8")
67-
59+
# Content is already decoded in get_findings method
6860
f = StringIO(content)
6961
csv_reader = csv.DictReader(f, delimiter=";")
7062
results = list(csv_reader)
@@ -107,6 +99,7 @@ def _parse_json_findings(self, data, test, *, file_name=""):
10799
for item in data:
108100
# Skip items without required fields
109101
if not isinstance(item, dict) or "message" not in item:
102+
logger.debug(f"Skipping Prowler finding because it's not a dict or missing 'message' field: {item}")
110103
continue
111104

112105
# Get basic information

0 commit comments

Comments
 (0)