77# See https://aboutcode.org for more information about nexB OSS projects.
88#
99import re
10+ import shutil
1011from pathlib import Path
1112
1213from 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
1718from 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 ()
0 commit comments