Skip to content

Commit 313cb15

Browse files
committed
Update linux kernel to be a importer
Add a test for linux kernel Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent d41b9d5 commit 313cb15

File tree

6 files changed

+9331
-35
lines changed

6 files changed

+9331
-35
lines changed

vulnerabilities/importers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
from vulnerabilities.pipelines.v2_importers import github_osv_importer as github_osv_importer_v2
5151
from vulnerabilities.pipelines.v2_importers import gitlab_importer as gitlab_importer_v2
5252
from vulnerabilities.pipelines.v2_importers import istio_importer as istio_importer_v2
53+
from vulnerabilities.pipelines.v2_importers import linux_kernel_importer as linux_kernel_importer_v2
5354
from vulnerabilities.pipelines.v2_importers import mozilla_importer as mozilla_importer_v2
5455
from vulnerabilities.pipelines.v2_importers import npm_importer as npm_importer_v2
5556
from vulnerabilities.pipelines.v2_importers import nvd_importer as nvd_importer_v2
@@ -81,6 +82,7 @@
8182
mozilla_importer_v2.MozillaImporterPipeline,
8283
github_osv_importer_v2.GithubOSVImporterPipeline,
8384
redhat_importer_v2.RedHatImporterPipeline,
85+
linux_kernel_importer_v2.LinuxKernelPipeline,
8486
nvd_importer.NVDImporterPipeline,
8587
github_importer.GitHubAPIImporterPipeline,
8688
gitlab_importer.GitLabImporterPipeline,

vulnerabilities/improvers/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
from vulnerabilities.pipelines import flag_ghost_packages
2020
from vulnerabilities.pipelines import populate_vulnerability_summary_pipeline
2121
from vulnerabilities.pipelines import remove_duplicate_advisories
22-
from vulnerabilities.pipelines.v2_improvers import (
23-
collect_linux_kernel_cves_commits as collect_linux_kernel_cves_commits_v2,
24-
)
2522
from vulnerabilities.pipelines.v2_improvers import compute_advisory_todo as compute_advisory_todo_v2
2623
from vulnerabilities.pipelines.v2_improvers import compute_package_risk as compute_package_risk_v2
2724
from vulnerabilities.pipelines.v2_improvers import (
@@ -71,6 +68,5 @@
7168
compute_version_rank_v2.ComputeVersionRankPipeline,
7269
compute_advisory_todo_v2.ComputeToDo,
7370
compute_advisory_todo.ComputeToDo,
74-
collect_linux_kernel_cves_commits_v2.CollectFixCommitLinuxKernelPipeline,
7571
]
7672
)

vulnerabilities/pipelines/v2_improvers/collect_linux_kernel_cves_commits.py renamed to vulnerabilities/pipelines/v2_importers/linux_kernel_importer.py

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,48 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99
import re
10+
import shutil
1011
from pathlib import Path
1112

1213
from fetchcode.vcs import fetch_via_vcs
1314

14-
from vulnerabilities.models import AdvisoryV2
15-
from vulnerabilities.models import CodeFixV2
16-
from vulnerabilities.pipelines import VulnerableCodePipeline
15+
from vulnerabilities.importer import AdvisoryData
16+
from vulnerabilities.importer import ReferenceV2
17+
from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2
1718
from vulnerabilities.utils import cve_regex
19+
from vulnerabilities.utils import get_advisory_url
1820

1921

20-
class CollectFixCommitLinuxKernelPipeline(VulnerableCodePipeline):
22+
class LinuxKernelPipeline(VulnerableCodeBaseImporterPipelineV2):
2123
"""
22-
Pipeline to collect fix commits from Linux Kernel:
24+
Pipeline to collect Linux Kernel Pipeline:
2325
"""
2426

2527
pipeline_id = "linux_kernel_cves_fix_commits"
2628
spdx_license_expression = "Apache-2.0"
2729
license_url = "https://github.com/nluedtke/linux_kernel_cves/blob/master/LICENSE"
2830
importer_name = "linux_kernel_cves_fix_commits"
2931
qualified_name = "linux_kernel_cves_fix_commits"
30-
repo_url = "git+https://github.com/nluedtke/linux_kernel_cves"
3132

3233
@classmethod
3334
def steps(cls):
3435
return (
3536
cls.clone,
36-
cls.collect_fix_commits,
37+
cls.collect_and_store_advisories,
38+
cls.clean_downloads,
3739
)
3840

41+
def advisories_count(self):
42+
root = Path(self.vcs_response.dest_dir)
43+
return sum(1 for _ in root.rglob("data/*.txt"))
44+
3945
def clone(self):
46+
self.repo_url = "git+https://github.com/nluedtke/linux_kernel_cves"
4047
self.log(f"Cloning `{self.repo_url}`")
4148
self.vcs_response = fetch_via_vcs(self.repo_url)
4249

43-
def collect_fix_commits(self):
44-
self.log(f"Processing aosp_dataset fix commits.")
50+
def collect_advisories(self):
51+
self.log(f"Processing linux kernel fix commits.")
4552
base_path = Path(self.vcs_response.dest_dir) / "data"
4653
for file_path in base_path.rglob("*.txt"):
4754
if "_CVEs.txt" in file_path.name:
@@ -58,27 +65,25 @@ def collect_fix_commits(self):
5865
if not (vulnerability_id and commit_hash):
5966
continue
6067

61-
try:
62-
advisories = AdvisoryV2.objects.filter(
63-
advisory_id__iendswith=vulnerability_id
68+
references = []
69+
for kernel_url in kernel_urls:
70+
ref = ReferenceV2(
71+
reference_type="commit",
72+
url=kernel_url,
6473
)
65-
except AdvisoryV2.DoesNotExist:
66-
self.log(f"Can't find vulnerability_id: {vulnerability_id}")
67-
continue
74+
references.append(ref)
6875

69-
for advisory in advisories:
70-
for impact in advisory.impacted_packages.all():
71-
for package in impact.affecting_packages.all():
72-
code_fix, created = CodeFixV2.objects.get_or_create(
73-
commits=[kernel_urls],
74-
advisory=advisory,
75-
affected_package=package,
76-
)
76+
advisory_url = get_advisory_url(
77+
file=file_path,
78+
base_path=self.vcs_response.dest_dir,
79+
url="https://github.com/nluedtke/linux_kernel_cves/blob/master/",
80+
)
7781

78-
if created:
79-
self.log(
80-
f"Created CodeFix entry for vulnerability_id: {vulnerability_id} with VCS URL {kernel_urls}"
81-
)
82+
yield AdvisoryData(
83+
advisory_id=vulnerability_id,
84+
references_v2=references,
85+
url=advisory_url,
86+
)
8287

8388
def parse_commits_file(self, file_path):
8489
sha1_pattern = re.compile(r"\b[a-f0-9]{40}\b")
@@ -90,16 +95,21 @@ def parse_commits_file(self, file_path):
9095
continue
9196

9297
cve_match = cve_regex.search(line)
93-
cve = cve_match.group(1) if cve_match else None
98+
if not cve_match:
99+
continue
100+
101+
cve = cve_match.group(0)
94102

95103
sha1_match = sha1_pattern.search(line)
96104
commit_hash = sha1_match.group(0) if sha1_match else None
97105
yield cve, commit_hash
98106

99107
def clean_downloads(self):
100-
if self.vcs_response:
101-
self.log(f"Removing cloned repository")
102-
self.vcs_response.delete()
108+
"""Cleanup any temporary repository data."""
109+
self.log("Cleaning up local repository resources.")
110+
if hasattr(self, "repo") and self.repo.working_dir:
111+
shutil.rmtree(path=self.repo.working_dir)
103112

104113
def on_failure(self):
114+
"""Ensure cleanup is always performed on failure."""
105115
self.clean_downloads()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Copyright (c) nexB Inc. and others. All rights reserved.
3+
# VulnerableCode is a trademark of nexB Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
6+
# See https://github.com/aboutcode-org/vulnerablecode for support or download.
7+
# See https://aboutcode.org for more information about nexB OSS projects.
8+
#
9+
10+
import os
11+
from pathlib import Path
12+
from unittest.mock import Mock
13+
14+
import pytest
15+
16+
from vulnerabilities.pipelines.v2_importers.linux_kernel_importer import LinuxKernelPipeline
17+
from vulnerabilities.tests import util_tests
18+
19+
TEST_DATA = Path(__file__).parent.parent.parent / "test_data" / "linux_kernel"
20+
21+
22+
@pytest.mark.django_db
23+
def test_linux_kernel_advisories():
24+
expected_file = os.path.join(TEST_DATA, "expected-linux-kernel-advisory.json")
25+
pipeline = LinuxKernelPipeline()
26+
pipeline.vcs_response = Mock(dest_dir=TEST_DATA)
27+
result = [adv.to_dict() for adv in pipeline.collect_advisories()]
28+
util_tests.check_results_against_json(result, expected_file)

0 commit comments

Comments
 (0)