Skip to content

Commit

Permalink
Fix ruff errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydragyrum committed Oct 10, 2024
1 parent fae4b81 commit 14170af
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 107 deletions.
21 changes: 9 additions & 12 deletions dojo/tools/ptart/assessment_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,23 @@ def parse_assessment(self, assessment):
return [self.get_finding(assessment, hit) for hit in hits]

def get_finding(self, assessment, hit):
effort = ptart_tools.parse_ptart_fix_effort(hit.get("fix_complexity"))
finding = Finding(
title=ptart_tools.parse_title_from_hit(hit),
severity=ptart_tools.parse_ptart_severity(hit.get("severity", 5)),
effort_for_fixing=ptart_tools.parse_ptart_fix_effort(
hit.get("fix_complexity", 3)
),
severity=ptart_tools.parse_ptart_severity(hit.get("severity")),
effort_for_fixing=effort,
component_name=assessment.get("title", "Unknown Component"),
date=ptart_tools.parse_date_added_from_hit(hit),
)

# Don't add fields if they are blank
if "body" in hit and hit["body"]:
finding.description = hit["body"]
if hit["body"]:
finding.description = hit.get("body")

if "remediation" in hit and hit["remediation"]:
finding.mitigation = hit["remediation"]
if hit["remediation"]:
finding.mitigation = hit.get("remediation")

if "id" in hit and hit["id"]:
if hit["id"]:
finding.unique_id_from_tool = hit.get("id")

# Clean up and parse the CVSS vector
Expand All @@ -54,8 +53,6 @@ def get_finding(self, assessment, hit):

# Add screenshots to files, and add other attachments as well.
finding.unsaved_files = ptart_tools.parse_screenshots_from_hit(hit)
finding.unsaved_files.extend(
ptart_tools.parse_attachment_from_hit(hit)
)
finding.unsaved_files.extend(ptart_tools.parse_attachment_from_hit(hit))

return finding
11 changes: 6 additions & 5 deletions dojo/tools/ptart/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from dojo.tools.ptart.retest_parser import PTARTRetestParser


class PTARTParser(object):
class PTARTParser:

"""
Imports JSON reports from the PTART reporting tool
(https://github.com/certmichelin/PTART)
Expand Down Expand Up @@ -48,13 +49,13 @@ def get_tests(self, scan_type, scan):
# Perhaps in a future version of DefectDojo?
if "start_date" in data:
test.target_start = ptart_tools.parse_date(
data["start_date"],
"%Y-%m-%d"
data["start_date"], "%Y-%m-%d",
)

if "end_date" in data:
test.target_end = ptart_tools.parse_date(data["end_date"],
"%Y-%m-%d")
test.target_end = ptart_tools.parse_date(
data["end_date"], "%Y-%m-%d",
)

findings = self.get_items(data)
test.findings = findings
Expand Down
45 changes: 23 additions & 22 deletions dojo/tools/ptart/ptart_parser_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@

from dojo.models import Endpoint

ATTACHMENT_ERROR = "Attachment data not found"
SCREENSHOT_ERROR = "Screenshot data not found"


def parse_ptart_severity(severity):
severity_mapping = {
1: "Critical",
2: "High",
3: "Medium",
4: "Low"
4: "Low",
}
return severity_mapping.get(severity, "Info") # Default severity

Expand All @@ -20,7 +23,7 @@ def parse_ptart_fix_effort(effort):
effort_mapping = {
1: "High",
2: "Medium",
3: "Low"
3: "Low",
}
return effort_mapping.get(effort, None)

Expand Down Expand Up @@ -67,7 +70,7 @@ def parse_retest_status(status):
"NF": "Not Fixed",
"PF": "Partially Fixed",
"NA": "Not Applicable",
"NT": "Not Tested"
"NT": "Not Tested",
}
return fix_status_mapping.get(status, None)

Expand All @@ -86,32 +89,31 @@ def parse_screenshot_data(screenshot):
data = get_screenshot_data(screenshot)
return {
"title": title,
"data": data
"data": data,
}
except ValueError:
return None


def get_screenshot_title(screenshot):
caption = screenshot.get('caption', 'screenshot') \
if "caption" in screenshot and screenshot["caption"] \
else "screenshot"
title = f"{caption}{get_file_suffix_from_screenshot(screenshot)}"
return title
caption = screenshot.get("caption", "screenshot")
if not caption:
caption = "screenshot"
return f"{caption}{get_file_suffix_from_screenshot(screenshot)}"


def get_screenshot_data(screenshot):
if ("screenshot" not in screenshot
or "data" not in screenshot["screenshot"]
or not screenshot["screenshot"]["data"]):
raise ValueError("Screenshot data not found")
raise ValueError(SCREENSHOT_ERROR)
return screenshot["screenshot"]["data"]


def get_file_suffix_from_screenshot(screenshot):
return pathlib.Path(screenshot['screenshot']['filename']).suffix \
return pathlib.Path(screenshot["screenshot"]["filename"]).suffix \
if ("screenshot" in screenshot
and "filename" in screenshot['screenshot']) \
and "filename" in screenshot["screenshot"]) \
else ""


Expand All @@ -129,7 +131,7 @@ def parse_attachment_data(attachment):
data = get_attachment_data(attachment)
return {
"title": title,
"data": data
"data": data,
}
except ValueError:
# No data in attachment, let's not import this file.
Expand All @@ -138,14 +140,15 @@ def parse_attachment_data(attachment):

def get_attachment_data(attachment):
if "data" not in attachment or not attachment["data"]:
raise ValueError("Attachment data not found")
raise ValueError(ATTACHMENT_ERROR)
return attachment["data"]


def get_attachement_title(attachment):
return attachment.get("title", "attachment") \
if "title" in attachment and attachment["title"] \
else "attachment"
title = attachment.get("title", "attachment")
if not title:
title = "attachment"
return title


def parse_endpoints_from_hit(hit):
Expand All @@ -157,8 +160,6 @@ def parse_endpoints_from_hit(hit):

def generate_test_description_from_report(data):
keys = ["executive_summary", "engagement_overview", "conclusion"]
description = "\n\n".join(data[key]
for key in keys
if key in data and data[key]
)
return description if description else None
clauses = [clause for clause in [data.get(key) for key in keys] if clause]
description = "\n\n".join(clauses)
return description or None
24 changes: 11 additions & 13 deletions dojo/tools/ptart/retest_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dojo.tools.ptart.ptart_parser_tools as ptart_tools
from dojo.models import Finding
from dojo.tools.ptart.ptart_parser_tools import parse_title_from_hit


def generate_retest_hit_title(hit, original_hit):
Expand All @@ -13,8 +12,7 @@ def generate_retest_hit_title(hit, original_hit):
"title": title,
"id": hit_id,
}
finding_title = parse_title_from_hit(fake_retest_hit)
return finding_title
return ptart_tools.parse_title_from_hit(fake_retest_hit)


class PTARTRetestParser:
Expand Down Expand Up @@ -59,31 +57,31 @@ def get_finding(self, retest, hit):
finding = Finding(
title=finding_title,
severity=ptart_tools.parse_ptart_severity(
original_hit.get("severity")
original_hit.get("severity"),
),
effort_for_fixing=ptart_tools.parse_ptart_fix_effort(
original_hit.get("fix_complexity")
original_hit.get("fix_complexity"),
),
component_name=f"Retest: {retest.get('name', 'Retest')}",
date=ptart_tools.parse_date(
retest.get("start_date"),
"%Y-%m-%d"
"%Y-%m-%d",
),
)

# Don't add the fields if they are blank.
if "body" in hit and hit["body"]:
finding.description = hit["body"]
if hit["body"]:
finding.description = hit.get("body")

if "remediation" in original_hit and original_hit["remediation"]:
finding.mitigation = original_hit["remediation"]
if original_hit["remediation"]:
finding.mitigation = original_hit.get("remediation")

if "id" in hit and hit["id"]:
if hit["id"]:
finding.unique_id_from_tool = hit.get("id")

cvss_vector = ptart_tools.parse_cvss_vector(
original_hit,
self.cvss_type
self.cvss_type,
)
if cvss_vector:
finding.cvssv3 = cvss_vector
Expand All @@ -92,7 +90,7 @@ def get_finding(self, retest, hit):
finding.unsaved_tags = original_hit["labels"]

finding.unsaved_endpoints = ptart_tools.parse_endpoints_from_hit(
original_hit
original_hit,
)

# We only have screenshots in a retest. Refer to the original hit for
Expand Down
Loading

0 comments on commit 14170af

Please sign in to comment.