Skip to content

Commit 8039472

Browse files
committed
Refactor as per review
Signed-off-by: Sampurna Pyne <sampurnapyne1710@gmail.com>
1 parent 0bb18b5 commit 8039472

File tree

3 files changed

+53
-71
lines changed

3 files changed

+53
-71
lines changed
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import json
22
import logging
33
from typing import Iterable
4+
from typing import Mapping
45

5-
from dateutil import parser as date_parser
6-
from django.utils import timezone
6+
from dateutil.parser import parse
77
from packageurl import PackageURL
8+
from pytz import UTC
89
from univers.version_range import GenericVersionRange
910

1011
from vulnerabilities.importer import AdvisoryData
1112
from vulnerabilities.importer import AffectedPackageV2
12-
from vulnerabilities.importer import ReferenceV2
1313
from vulnerabilities.importer import VulnerabilitySeverity
1414
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
1515
from vulnerabilities.severity_systems import GENERIC
@@ -22,27 +22,25 @@ class TuxCareImporterPipeline(VulnerableCodeBaseImporterPipelineV2):
2222
pipeline_id = "tuxcare_importer_v2"
2323
spdx_license_expression = "Apache-2.0"
2424
license_url = "https://tuxcare.com/legal"
25-
url = "https://cve.tuxcare.com/els/download-json?orderBy=updated-desc"
2625

2726
@classmethod
2827
def steps(cls):
29-
return (cls.collect_and_store_advisories,)
28+
return (
29+
cls.fetch,
30+
cls.collect_and_store_advisories,
31+
)
32+
33+
def fetch(self) -> Iterable[Mapping]:
34+
url = "https://cve.tuxcare.com/els/download-json?orderBy=updated-desc"
35+
self.log(f"Fetching `{url}`")
36+
response = fetch_response(url)
37+
self.response = response.json() if response else []
3038

3139
def advisories_count(self) -> int:
32-
response = fetch_response(self.url)
33-
data = response.json() if response else []
34-
return len(data)
40+
return len(self.response)
3541

3642
def collect_advisories(self) -> Iterable[AdvisoryData]:
37-
response = fetch_response(self.url)
38-
if not response:
39-
return
40-
41-
data = response.json()
42-
if not data:
43-
return
44-
45-
for record in data:
43+
for record in self.response:
4644
cve_id = record.get("cve", "").strip()
4745
if not cve_id or not cve_id.startswith("CVE-"):
4846
continue
@@ -52,11 +50,9 @@ def collect_advisories(self) -> Iterable[AdvisoryData]:
5250
version = record.get("version", "").strip()
5351
score = record.get("score", "").strip()
5452
severity = record.get("severity", "").strip()
55-
status = record.get("status", "").strip()
5653
last_updated = record.get("last_updated", "").strip()
5754

58-
safe_os = os_name.replace(" ", "_") if os_name else "unknown"
59-
advisory_id = f"TUXCARE-{cve_id}-{safe_os}-{project_name}"
55+
advisory_id = cve_id
6056

6157
summary = f"TuxCare advisory for {cve_id}"
6258
if project_name:
@@ -67,13 +63,13 @@ def collect_advisories(self) -> Iterable[AdvisoryData]:
6763
affected_packages = []
6864
if project_name:
6965
purl = PackageURL(type="generic", name=project_name)
70-
66+
7167
affected_version_range = None
7268
if version:
7369
try:
7470
affected_version_range = GenericVersionRange.from_versions([version])
75-
except Exception:
76-
pass
71+
except ValueError as e:
72+
logger.warning(f"Failed to parse version {version} for {cve_id}: {e}")
7773

7874
affected_packages.append(
7975
AffectedPackageV2(
@@ -87,28 +83,24 @@ def collect_advisories(self) -> Iterable[AdvisoryData]:
8783
severities.append(
8884
VulnerabilitySeverity(
8985
system=GENERIC,
90-
value=f"{severity} ({score})",
91-
scoring_elements=f"score={score},severity={severity}",
86+
value=score,
87+
scoring_elements=severity,
9288
)
9389
)
9490

9591
date_published = None
9692
if last_updated:
9793
try:
98-
date_published = date_parser.parse(last_updated)
99-
if timezone.is_naive(date_published):
100-
date_published = timezone.make_aware(date_published, timezone=timezone.utc)
101-
except Exception:
102-
pass
94+
date_published = parse(last_updated).replace(tzinfo=UTC)
95+
except ValueError as e:
96+
logger.warning(f"Failed to parse date {last_updated} for {cve_id}: {e}")
10397

10498
yield AdvisoryData(
10599
advisory_id=advisory_id,
106-
aliases=[cve_id],
107100
summary=summary,
108101
affected_packages=affected_packages,
109-
references_v2=[ReferenceV2(url="https://cve.tuxcare.com/")],
110102
severities=severities,
111103
date_published=date_published,
112-
url="https://cve.tuxcare.com/",
104+
url=f"https://cve.tuxcare.com/els/cve/{cve_id}",
113105
original_advisory_text=json.dumps(record, indent=2, ensure_ascii=False),
114106
)

vulnerabilities/tests/pipelines/v2_importers/test_tuxcare_importer_v2.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,17 @@
2222
class TestTuxCareImporterPipeline(TestCase):
2323
@patch("vulnerabilities.pipelines.v2_importers.tuxcare_importer.fetch_response")
2424
def test_collect_advisories(self, mock_fetch):
25-
"""Test collecting and parsing advisories from test data"""
2625
sample_path = TEST_DATA / "data.json"
2726
sample_data = json.loads(sample_path.read_text(encoding="utf-8"))
2827

2928
mock_fetch.return_value = Mock(json=lambda: sample_data)
3029

3130
pipeline = TuxCareImporterPipeline()
31+
pipeline.fetch()
32+
3233
advisories = [data.to_dict() for data in list(pipeline.collect_advisories())]
3334

3435
expected_file = TEST_DATA / "expected.json"
3536
util_tests.check_results_against_json(advisories, expected_file)
3637

37-
@patch("vulnerabilities.pipelines.v2_importers.tuxcare_importer.fetch_response")
38-
def test_advisories_count(self, mock_fetch):
39-
"""Test counting advisories"""
40-
sample_path = TEST_DATA / "data.json"
41-
sample_data = json.loads(sample_path.read_text(encoding="utf-8"))
42-
43-
mock_fetch.return_value = Mock(json=lambda: sample_data)
44-
45-
pipeline = TuxCareImporterPipeline()
46-
count = pipeline.advisories_count()
47-
48-
assert count == 5
38+
assert pipeline.advisories_count() == 5
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
3-
"advisory_id": "TUXCARE-CVE-2023-52922-CloudLinux_7_ELS-squid",
4-
"aliases": ["CVE-2023-52922"],
3+
"advisory_id": "CVE-2023-52922",
4+
"aliases": [],
55
"summary": "TuxCare advisory for CVE-2023-52922 in squid on CloudLinux 7 ELS",
66
"affected_packages": [
77
{
@@ -12,16 +12,16 @@
1212
"fixed_by_commit_patches": []
1313
}
1414
],
15-
"references_v2": [{"reference_id": "", "reference_type": "", "url": "https://cve.tuxcare.com/"}],
15+
"references_v2": [],
1616
"patches": [],
17-
"severities": [{"system": "generic_textual", "value": "HIGH (7.8)", "scoring_elements": "score=7.8,severity=HIGH"}],
17+
"severities": [{"system": "generic_textual", "value": "7.8", "scoring_elements": "HIGH"}],
1818
"date_published": "2025-12-23T10:08:36.423446+00:00",
1919
"weaknesses": [],
20-
"url": "https://cve.tuxcare.com/"
20+
"url": "https://cve.tuxcare.com/els/cve/CVE-2023-52922"
2121
},
2222
{
23-
"advisory_id": "TUXCARE-CVE-2023-52922-Oracle_Linux_7_ELS-squid",
24-
"aliases": ["CVE-2023-52922"],
23+
"advisory_id": "CVE-2023-52922",
24+
"aliases": [],
2525
"summary": "TuxCare advisory for CVE-2023-52922 in squid on Oracle Linux 7 ELS",
2626
"affected_packages": [
2727
{
@@ -32,16 +32,16 @@
3232
"fixed_by_commit_patches": []
3333
}
3434
],
35-
"references_v2": [{"reference_id": "", "reference_type": "", "url": "https://cve.tuxcare.com/"}],
35+
"references_v2": [],
3636
"patches": [],
37-
"severities": [{"system": "generic_textual", "value": "HIGH (7.8)", "scoring_elements": "score=7.8,severity=HIGH"}],
37+
"severities": [{"system": "generic_textual", "value": "7.8", "scoring_elements": "HIGH"}],
3838
"date_published": "2025-12-23T10:08:35.944749+00:00",
3939
"weaknesses": [],
40-
"url": "https://cve.tuxcare.com/"
40+
"url": "https://cve.tuxcare.com/els/cve/CVE-2023-52922"
4141
},
4242
{
43-
"advisory_id": "TUXCARE-CVE-2023-48161-RHEL_7_ELS-java-11-openjdk",
44-
"aliases": ["CVE-2023-48161"],
43+
"advisory_id": "CVE-2023-48161",
44+
"aliases": [],
4545
"summary": "TuxCare advisory for CVE-2023-48161 in java-11-openjdk on RHEL 7 ELS",
4646
"affected_packages": [
4747
{
@@ -52,16 +52,16 @@
5252
"fixed_by_commit_patches": []
5353
}
5454
],
55-
"references_v2": [{"reference_id": "", "reference_type": "", "url": "https://cve.tuxcare.com/"}],
55+
"references_v2": [],
5656
"patches": [],
57-
"severities": [{"system": "generic_textual", "value": "HIGH (7.1)", "scoring_elements": "score=7.1,severity=HIGH"}],
57+
"severities": [{"system": "generic_textual", "value": "7.1", "scoring_elements": "HIGH"}],
5858
"date_published": "2025-12-23T08:55:12.096092+00:00",
5959
"weaknesses": [],
60-
"url": "https://cve.tuxcare.com/"
60+
"url": "https://cve.tuxcare.com/els/cve/CVE-2023-48161"
6161
},
6262
{
63-
"advisory_id": "TUXCARE-CVE-2024-21147-RHEL_7_ELS-java-11-openjdk",
64-
"aliases": ["CVE-2024-21147"],
63+
"advisory_id": "CVE-2024-21147",
64+
"aliases": [],
6565
"summary": "TuxCare advisory for CVE-2024-21147 in java-11-openjdk on RHEL 7 ELS",
6666
"affected_packages": [
6767
{
@@ -72,16 +72,16 @@
7272
"fixed_by_commit_patches": []
7373
}
7474
],
75-
"references_v2": [{"reference_id": "", "reference_type": "", "url": "https://cve.tuxcare.com/"}],
75+
"references_v2": [],
7676
"patches": [],
77-
"severities": [{"system": "generic_textual", "value": "HIGH (7.4)", "scoring_elements": "score=7.4,severity=HIGH"}],
77+
"severities": [{"system": "generic_textual", "value": "7.4", "scoring_elements": "HIGH"}],
7878
"date_published": "2025-12-23T08:55:07.139188+00:00",
7979
"weaknesses": [],
80-
"url": "https://cve.tuxcare.com/"
80+
"url": "https://cve.tuxcare.com/els/cve/CVE-2024-21147"
8181
},
8282
{
83-
"advisory_id": "TUXCARE-CVE-2025-21587-RHEL_7_ELS-java-11-openjdk",
84-
"aliases": ["CVE-2025-21587"],
83+
"advisory_id": "CVE-2025-21587",
84+
"aliases": [],
8585
"summary": "TuxCare advisory for CVE-2025-21587 in java-11-openjdk on RHEL 7 ELS",
8686
"affected_packages": [
8787
{
@@ -92,11 +92,11 @@
9292
"fixed_by_commit_patches": []
9393
}
9494
],
95-
"references_v2": [{"reference_id": "", "reference_type": "", "url": "https://cve.tuxcare.com/"}],
95+
"references_v2": [],
9696
"patches": [],
97-
"severities": [{"system": "generic_textual", "value": "HIGH (7.4)", "scoring_elements": "score=7.4,severity=HIGH"}],
97+
"severities": [{"system": "generic_textual", "value": "7.4", "scoring_elements": "HIGH"}],
9898
"date_published": "2025-12-23T08:55:06.706873+00:00",
9999
"weaknesses": [],
100-
"url": "https://cve.tuxcare.com/"
100+
"url": "https://cve.tuxcare.com/els/cve/CVE-2025-21587"
101101
}
102102
]

0 commit comments

Comments
 (0)