Skip to content

Commit be26c53

Browse files
committed
Sync with main branch and fix latest tests
Signed-off-by: Shivam Sandbhor <[email protected]>
1 parent ae09bbf commit be26c53

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

vulnerabilities/import_runner.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ def process_advisories(data_source: DataSource, create_vulcodes) -> None:
143143
for batch in advisory_batches:
144144
for advisory in batch:
145145
try:
146+
146147
if not advisory.identifier and not create_vulcodes:
147148
continue
148149

@@ -154,9 +155,11 @@ def process_advisories(data_source: DataSource, create_vulcodes) -> None:
154155
reference_id=vuln_ref.reference_id,
155156
)
156157

157-
if vuln_created or not vuln_ref_exists(vuln, vuln_ref.url, vuln_ref.reference_id):
158-
# A vulnerability reference can't exist if the vulnerability is just created so
159-
# insert it
158+
if vuln_created or not vuln_ref_exists(
159+
vuln, vuln_ref.url, vuln_ref.reference_id
160+
):
161+
# A vulnerability reference can't exist if the vulnerability is
162+
# just created, so insert it
160163
bulk_create_vuln_refs.add(ref)
161164

162165
for purl in chain(advisory.impacted_package_urls, advisory.resolved_package_urls):
@@ -176,25 +179,27 @@ def process_advisories(data_source: DataSource, create_vulcodes) -> None:
176179
existing_ref = get_vuln_pkg_refs(vuln, pkg)
177180
if not existing_ref:
178181
bulk_create_vuln_pkg_refs.add(pkg_vuln_ref)
179-
# A vulnerability-package relationship does not exist already if either the
180-
# vulnerability or the package is just created.
182+
# A vulnerability-package relationship does not exist already
183+
# if either the vulnerability or the package is just created.
181184

182185
else:
183-
# insert only if it there is no existing vulnerability-package relationship.
186+
# insert only if it there is no existing vulnerability-package relationship. # nopep8
184187
existing_ref = get_vuln_pkg_refs(vuln, pkg)
185188
if not existing_ref:
186189
bulk_create_vuln_pkg_refs.add(pkg_vuln_ref)
187190

188191
else:
189192
# This handles conflicts between existing data and obtained data
190193
if existing_ref[0].is_vulnerable != pkg_vuln_ref.is_vulnerable:
191-
handle_conflicts([existing_ref[0], pkg_vuln_ref.to_model_object()])
194+
handle_conflicts(
195+
[existing_ref[0], pkg_vuln_ref.to_model_object()]
196+
)
192197
existing_ref.delete()
198+
193199
except Exception:
194200
# TODO: store error but continue
195201
logger.error(
196-
f"Failed to process advisory: {advisory!r}:\n"
197-
+ traceback.format_exc()
202+
f"Failed to process advisory: {advisory!r}:\n" + traceback.format_exc()
198203
)
199204

200205
models.VulnerabilityReference.objects.bulk_create(
@@ -259,17 +264,20 @@ def _get_or_create_vulnerability(
259264
advisory: Advisory,
260265
) -> Tuple[models.Vulnerability, bool]:
261266

262-
vuln, created = models.Vulnerability.objects.get_or_create(identifier=advisory.identifier)
267+
try:
268+
vuln, created = models.Vulnerability.objects.get_or_create(identifier=advisory.identifier)
263269

264-
# Eventually we only want to keep summary from NVD and ignore other descriptions.
265-
if advisory.summary and vuln.summary != advisory.summary:
266-
vuln.summary = advisory.summary
267-
vuln.save()
270+
# Eventually we only want to keep summary from NVD and ignore other descriptions.
271+
if advisory.summary and vuln.summary != advisory.summary:
272+
vuln.summary = advisory.summary
273+
vuln.save()
274+
275+
return vuln, created
268276

269277
except Exception:
270278
logger.error(
271-
f"Failed to _get_or_create_vulnerability: {query_kwargs!r}:\n"
272-
+ traceback.format_exc())
279+
f"Failed to _get_or_create_vulnerability: {query_kwargs!r}:\n" + traceback.format_exc()
280+
)
273281
raise
274282

275283

vulnerabilities/importers/apache_tomcat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def to_advisories(self, apache_tomcat_advisory_html):
116116
summary="",
117117
impacted_package_urls=affected_packages,
118118
resolved_package_urls=fixed_package,
119-
cve_id=cve_id,
119+
identifier=cve_id,
120120
vuln_references=references,
121121
)
122122
)

vulnerabilities/tests/test_apache_tomcat.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_to_advisories(self):
8989
reference_id="",
9090
),
9191
],
92-
cve_id="CVE-2016-0763",
92+
identifier="CVE-2016-0763",
9393
),
9494
Advisory(
9595
summary="",
@@ -127,7 +127,7 @@ def test_to_advisories(self):
127127
reference_id="",
128128
),
129129
],
130-
cve_id="CVE-2015-5351",
130+
identifier="CVE-2015-5351",
131131
),
132132
Advisory(
133133
summary="",
@@ -169,7 +169,7 @@ def test_to_advisories(self):
169169
reference_id="",
170170
),
171171
],
172-
cve_id="CVE-2016-0706",
172+
identifier="CVE-2016-0706",
173173
),
174174
Advisory(
175175
summary="",
@@ -207,16 +207,16 @@ def test_to_advisories(self):
207207
reference_id="",
208208
),
209209
],
210-
cve_id="CVE-2016-0714",
210+
identifier="CVE-2016-0714",
211211
),
212212
],
213-
key=lambda x: x.cve_id,
213+
key=lambda x: x.identifier,
214214
)
215215

216216
with open(TEST_DATA) as f:
217217
found_advisories = self.data_src.to_advisories(f)
218218

219-
found_advisories.sort(key=lambda x: x.cve_id)
219+
found_advisories.sort(key=lambda x: x.identifier)
220220

221221
for i in range(len(found_advisories)):
222222
found_advisories[i].vuln_references.sort(key=lambda x: x.url)

0 commit comments

Comments
 (0)