forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_handler.py
95 lines (85 loc) · 3.16 KB
/
csv_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import collections
import csv
import io
class IntSightsCSVParser:
def _parse_csv(self, csv_file) -> [dict]:
"""
Parses entries from the CSV file object into a list of alerts
Args:
csv_file: The JSON file object to parse
Returns:
A list of alerts [dict()]
"""
default_keys = [
"Alert ID",
"Title",
"Description",
"Severity",
"Type",
"Source Date (UTC)",
"Report Date (UTC)",
"Network Type",
"Source URL",
"Source Name",
"Assets",
"Tags",
"Assignees",
"Remediation",
"Status",
"Closed Reason",
"Additional Info",
"Rating",
"Alert Link"
]
# These keys require a value. If one ore more of the values is null or empty, the entire Alert is ignored.
# This is to avoid attempting to import incomplete Findings.
required_keys = ["alert_id", "title", "severity", "status"]
alerts = []
invalid_alerts = []
content = csv_file.read()
if isinstance(content, bytes):
content = content.decode("utf-8")
csv_reader = csv.DictReader(
io.StringIO(content), delimiter=",", quotechar='"'
)
# Don't bother parsing if the keys don't match exactly what's expected
if collections.Counter(default_keys) == collections.Counter(
csv_reader.fieldnames
):
default_valud = "None provided"
for alert in csv_reader:
alert["alert_id"] = alert.pop("Alert ID")
alert["title"] = alert.pop("Title")
alert["description"] = alert.pop("Description")
alert["severity"] = alert.pop("Severity")
alert["type"] = alert.pop(
"Type",
)
alert["source_date"] = alert.pop(
"Source Date (UTC)", default_valud
)
alert["report_date"] = alert.pop(
"Report Date (UTC)", default_valud
)
alert["network_type"] = alert.pop(
"Network Type", default_valud
)
alert["source_url"] = alert.pop("Source URL", default_valud)
alert["assets"] = alert.pop("Assets", default_valud)
alert["tags"] = alert.pop("Tags", default_valud)
alert["status"] = alert.pop("Status", default_valud)
alert["alert_link"] = alert.pop("Alert Link")
alert.pop("Assignees")
alert.pop("Remediation")
alert.pop("Closed Reason")
alert.pop("Rating")
for key in required_keys:
if not alert[key]:
invalid_alerts.append(alert)
if alert not in invalid_alerts:
alerts.append(alert)
else:
self._LOGGER.error(
"The CSV file has one or more missing or unexpected header values"
)
return alerts