Skip to content

Commit 30ec0e2

Browse files
committed
improvements
1 parent f89d858 commit 30ec0e2

File tree

3 files changed

+130
-42
lines changed

3 files changed

+130
-42
lines changed

dojo/tools/wizcli_dir/parser.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,66 @@
11
import json
2+
import logging
23

34
from dojo.tools.wizcli_common_parsers.parsers import WizcliParsers
45

6+
logger = logging.getLogger(__name__)
7+
58

69
class WizcliDirParser:
7-
"""
8-
Wizcli Dir Scan results in JSON file format.
9-
"""
10+
11+
"""Wiz CLI Directory/IaC Scan results in JSON file format."""
1012

1113
def get_scan_types(self):
1214
return ["Wizcli Dir Scan"]
1315

1416
def get_label_for_scan_types(self, scan_type):
15-
return "Wizcli Dir Scan"
17+
return "Wiz CLI Scan (Directory)"
1618

1719
def get_description_for_scan_types(self, scan_type):
18-
return "Wizcli Dir Scan results in JSON file format."
20+
return "Parses Wiz CLI Directory/IaC scan results in JSON format, creating granular findings for vulnerabilities and secrets."
1921

20-
def get_findings(self, filename, test):
21-
scan_data = filename.read()
22+
def get_findings(self, file, test):
23+
"""Processes the JSON report and returns a list of DefectDojo Finding objects."""
2224
try:
23-
data = json.loads(scan_data.decode("utf-8"))
24-
except Exception:
25+
scan_data = file.read()
26+
if isinstance(scan_data, bytes):
27+
# Try decoding common encodings
28+
try:
29+
scan_data = scan_data.decode("utf-8-sig") # Handles BOM
30+
except UnicodeDecodeError:
31+
scan_data = scan_data.decode("utf-8") # Fallback
2532
data = json.loads(scan_data)
33+
except json.JSONDecodeError as e:
34+
msg = f"Invalid JSON format: {e}"
35+
logger.error(msg)
36+
raise ValueError(msg) from e
37+
except Exception as e:
38+
msg = f"Error processing report file: {e}"
39+
logger.error(msg)
40+
raise ValueError(msg) from e
41+
2642
findings = []
27-
results = data.get("result", {})
43+
results_data = data.get("result", {})
44+
45+
if not results_data:
46+
logger.warning("No 'result' key found in the Wiz report. Unable to parse findings.")
47+
return findings
2848

29-
libraries = results.get("libraries", None)
49+
# Parse Libraries (Vulnerabilities)
50+
libraries = results_data.get("libraries")
3051
if libraries:
52+
logger.debug(f"Parsing {len(libraries)} library entries.")
3153
findings.extend(WizcliParsers.parse_libraries(libraries, test))
54+
else:
55+
logger.debug("No 'libraries' data found in results.")
3256

33-
secrets = results.get("secrets", None)
57+
# Parse Secrets
58+
secrets = results_data.get("secrets")
3459
if secrets:
60+
logger.debug(f"Parsing {len(secrets)} secret entries.")
3561
findings.extend(WizcliParsers.parse_secrets(secrets, test))
62+
else:
63+
logger.debug("No 'secrets' data found in results.")
3664

65+
logger.info(f"WizcliDirParser processed {len(findings)} findings.")
3766
return findings

dojo/tools/wizcli_iac/parser.py

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,64 @@
11
import json
2+
import logging
23

3-
from dojo.tools.wizcli_common_parsers.parsers import WizcliParsers
4+
from dojo.tools.wizcli_common_parsers.parsers import WizcliParsers # Adjust import path
45

6+
logger = logging.getLogger(__name__)
57

6-
class WizcliIaCParser:
7-
"""
8-
Wizcli IaC Scan results in JSON file format.
9-
"""
8+
9+
class WizcliIacParser:
10+
11+
"""Wiz CLI IaC Scan results in JSON file format."""
1012

1113
def get_scan_types(self):
1214
return ["Wizcli IaC Scan"]
1315

1416
def get_label_for_scan_types(self, scan_type):
15-
return "Wizcli IaC Scan"
17+
return "Wiz CLI Scan (IaC)"
1618

1719
def get_description_for_scan_types(self, scan_type):
18-
return "Wizcli IaC Scan results in JSON file format."
20+
return "Parses Wiz CLI Infrastructure as Code (IaC) scan results in JSON format."
1921

20-
def get_findings(self, filename, test):
21-
scan_data = filename.read()
22+
def get_findings(self, file, test):
2223
try:
23-
data = json.loads(scan_data.decode("utf-8"))
24-
except Exception:
24+
scan_data = file.read()
25+
if isinstance(scan_data, bytes):
26+
try:
27+
scan_data = scan_data.decode("utf-8-sig")
28+
except UnicodeDecodeError:
29+
scan_data = scan_data.decode("utf-8")
2530
data = json.loads(scan_data)
31+
except json.JSONDecodeError as e:
32+
msg = f"Invalid JSON format: {e}"
33+
logger.error(msg)
34+
raise ValueError(msg) from e
35+
except Exception as e:
36+
msg = f"Error processing report file: {e}"
37+
logger.error(msg)
38+
raise ValueError(msg) from e
39+
2640
findings = []
27-
results = data.get("result", {})
41+
results_data = data.get("result", {})
42+
43+
if not results_data:
44+
logger.warning("No 'result' key found in the Wiz report.")
45+
return findings
2846

29-
rule_matches = results.get("ruleMatches", None)
47+
# Parse Rule Matches (IaC findings)
48+
rule_matches = results_data.get("ruleMatches")
3049
if rule_matches:
50+
logger.debug(f"Parsing {len(rule_matches)} rule match entries.")
3151
findings.extend(WizcliParsers.parse_rule_matches(rule_matches, test))
52+
else:
53+
logger.debug("No 'ruleMatches' data found in results.")
3254

33-
secrets = results.get("secrets", None)
55+
# Parse Secrets (if present in IaC scans)
56+
secrets = results_data.get("secrets")
3457
if secrets:
58+
logger.debug(f"Parsing {len(secrets)} secret entries.")
3559
findings.extend(WizcliParsers.parse_secrets(secrets, test))
60+
else:
61+
logger.debug("No 'secrets' data found in results.")
3662

63+
logger.info(f"WizcliIacParser processed {len(findings)} findings.")
3764
return findings

dojo/tools/wizcli_img/parser.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,73 @@
11
import json
2+
import logging
23

3-
from dojo.tools.wizcli_common_parsers.parsers import WizcliParsers
4+
from dojo.tools.wizcli_common_parsers.parsers import WizcliParsers # Adjust import path
5+
6+
logger = logging.getLogger(__name__)
47

58

69
class WizcliImgParser:
7-
"""
8-
Wizcli Image Scan results in JSON file format.
9-
"""
10+
11+
"""Wiz CLI Container Image Scan results in JSON file format."""
1012

1113
def get_scan_types(self):
14+
# Use a distinct name for image scans
1215
return ["Wizcli Img Scan"]
1316

1417
def get_label_for_scan_types(self, scan_type):
15-
return "Wizcli Img Scan"
18+
return "Wiz CLI Scan (Image)"
1619

1720
def get_description_for_scan_types(self, scan_type):
18-
return "Wizcli Img report file can be imported in JSON format."
21+
return "Parses Wiz CLI Container Image scan results in JSON format."
1922

20-
def get_findings(self, filename, test):
21-
scan_data = filename.read()
23+
def get_findings(self, file, test):
2224
try:
23-
data = json.loads(scan_data.decode("utf-8"))
24-
except Exception:
25+
scan_data = file.read()
26+
if isinstance(scan_data, bytes):
27+
try:
28+
scan_data = scan_data.decode("utf-8-sig")
29+
except UnicodeDecodeError:
30+
scan_data = scan_data.decode("utf-8")
2531
data = json.loads(scan_data)
32+
except json.JSONDecodeError as e:
33+
msg = f"Invalid JSON format: {e}"
34+
logger.error(msg)
35+
raise ValueError(msg) from e
36+
except Exception as e:
37+
msg = f"Error processing report file: {e}"
38+
logger.error(msg)
39+
raise ValueError(msg) from e
40+
2641
findings = []
27-
results = data.get("result", {})
42+
results_data = data.get("result", {})
43+
44+
if not results_data:
45+
logger.warning("No 'result' key found in the Wiz report.")
46+
return findings
2847

29-
osPackages = results.get("osPackages", None)
30-
if osPackages:
31-
findings.extend(WizcliParsers.parse_os_packages(osPackages, test))
48+
# Parse OS Packages - Key difference for image scans
49+
os_packages = results_data.get("osPackages")
50+
if os_packages:
51+
logger.debug(f"Parsing {len(os_packages)} OS package entries.")
52+
findings.extend(WizcliParsers.parse_os_packages(os_packages, test))
53+
else:
54+
logger.debug("No 'osPackages' data found in results.")
3255

33-
libraries = results.get("libraries", None)
56+
# Parse Libraries (if present in image scans)
57+
libraries = results_data.get("libraries")
3458
if libraries:
59+
logger.debug(f"Parsing {len(libraries)} library entries.")
3560
findings.extend(WizcliParsers.parse_libraries(libraries, test))
61+
else:
62+
logger.debug("No 'libraries' data found in results.")
3663

37-
secrets = results.get("secrets", None)
64+
# Parse Secrets (if present in image scans)
65+
secrets = results_data.get("secrets")
3866
if secrets:
67+
logger.debug(f"Parsing {len(secrets)} secret entries.")
3968
findings.extend(WizcliParsers.parse_secrets(secrets, test))
69+
else:
70+
logger.debug("No 'secrets' data found in results.")
4071

72+
logger.info(f"WizcliImgParser processed {len(findings)} findings.")
4173
return findings

0 commit comments

Comments
 (0)