Skip to content

Commit 62e97fd

Browse files
Jino-TMaffooch
andauthored
Make Twistlock Parser use discoveredDate for Date (#13922)
* added date support using discoveredDate * Apply suggestions from code review --------- Co-authored-by: Cody Maffucci <46459665+Maffooch@users.noreply.github.com>
1 parent d282030 commit 62e97fd

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

dojo/tools/twistlock/parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import textwrap
77
from datetime import datetime
88

9+
import dateutil.parser
10+
from django.utils import timezone
11+
912
from dojo.models import Finding
1013

1114
logger = logging.getLogger(__name__)
@@ -235,6 +238,10 @@ def get_item(vulnerability, test, image_metadata=""):
235238
status = vulnerability.get("status", "There seems to be no fix yet. Please check description field.")
236239
cvssv3_score = vulnerability.get("cvss")
237240
riskFactors = vulnerability.get("riskFactors", "No risk factors.")
241+
try:
242+
date = str(dateutil.parser.parse(vulnerability.get("discoveredDate")).date())
243+
except (ValueError, TypeError, dateutil.parser.ParserError):
244+
date = timezone.now()
238245

239246
# Build impact field combining severity and image metadata which can change between scans, so we add it to the impact field as the description field is sometimes used for hash code calculation
240247
impact_parts = [severity]
@@ -264,6 +271,7 @@ def get_item(vulnerability, test, image_metadata=""):
264271
cvssv3=cvssv3,
265272
cvssv3_score=cvssv3_score,
266273
impact=impact_text,
274+
date=date,
267275
)
268276
finding.unsaved_vulnerability_ids = [vulnerability["id"]] if "id" in vulnerability else None
269277
finding.description = finding.description.strip()

unittests/tools/test_twistlock_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def test_parse_file_with_no_link_no_description(self):
113113
self.assertIsNotNone(finding)
114114
self.assertEqual(1, len(finding.unsaved_vulnerability_ids))
115115
self.assertEqual("PRISMA-2021-0013", finding.unsaved_vulnerability_ids[0])
116+
self.assertEqual("2022-11-16", finding.date)
116117
break
117118

118119
def test_parse_file_with_no_cvss(self):
@@ -141,6 +142,7 @@ def test_parse_file_with_no_cvss(self):
141142
self.assertIn("Image ID:", finding.impact)
142143
self.assertIn("Distribution:", finding.impact)
143144
self.assertIn("Debian GNU/Linux 12", finding.impact)
145+
self.assertEqual("2025-07-08", finding.date)
144146

145147
def test_parse_file_with_many_vulns(self):
146148
testfile = (get_unit_tests_scans_path("twistlock") / "many_vulns.json").open(encoding="utf-8")

0 commit comments

Comments
 (0)