@@ -41,18 +41,17 @@ def datasource_advisory(self, purl) -> Iterable[VendorData]:
4141 VendorData instance containing the advisory information for the package.
4242 """
4343 package_slug = get_package_slug (purl )
44- location = download_subtree (package_slug , speculative_execution = True )
45- if not location :
46- clear_download (location )
44+ directory_files = fetch_directory_contents (package_slug )
45+ if not directory_files :
4746 path = self .supported_ecosystem ()[purl .type ]
4847 casesensitive_package_slug = get_casesensitive_slug (path , package_slug )
49- location = download_subtree (casesensitive_package_slug )
50- if location :
51- interesting_advisories = parse_interesting_advisories (
52- location , purl , delete_download = True
53- )
48+ directory_files = fetch_directory_contents (casesensitive_package_slug )
49+
50+ if directory_files :
51+ yml_files = [file for file in directory_files if file ["name" ].endswith (".yml" )]
52+
53+ interesting_advisories = parse_interesting_advisories (yml_files , purl )
5454 return interesting_advisories
55- clear_download (location )
5655
5756 @classmethod
5857 def supported_ecosystem (cls ):
@@ -68,6 +67,21 @@ def supported_ecosystem(cls):
6867 }
6968
7069
70+ def fetch_directory_contents (package_slug ):
71+ url = f"https://gitlab.com/api/v4/projects/12006272/repository/tree?path={ package_slug } "
72+ response = requests .get (url )
73+ if response .status_code == 200 :
74+ return response .json ()
75+
76+
77+ def fetch_yaml (file_path ):
78+ response = requests .get (
79+ f"https://gitlab.com/gitlab-org/security-products/gemnasium-db/-/raw/master/{ file_path } "
80+ )
81+ if response .status_code == 200 :
82+ return response .text
83+
84+
7185def get_package_slug (purl ):
7286 """
7387 Constructs a package slug from a given purl.
@@ -92,43 +106,6 @@ def get_package_slug(purl):
92106 return f"{ ecosystem } /{ package_name } "
93107
94108
95- def download_subtree (package_slug : str , speculative_execution = False ):
96- """
97- Downloads and extracts a tar file from a given package slug.
98-
99- Parameters:
100- package_slug: A string representing the package slug to query.
101- speculative_execution: A boolean indicating whether to log errors or not.
102-
103- Returns:
104- A Path object representing the extracted location, or None if an error occurs.
105- """
106- url = f"https://gitlab.com/gitlab-org/security-products/gemnasium-db/-/archive/master/gemnasium-db-master.tar.gz?path={ package_slug } "
107- response = fetch (url )
108- if os .path .getsize (response .location ) > 0 :
109- extracted_location = Path (response .location ).parent .joinpath (
110- "temp_vulntotal_gitlab_datasource"
111- )
112- with tarfile .open (response .location , "r" ) as file_obj :
113- file_obj .extractall (extracted_location )
114- os .remove (response .location )
115- return extracted_location
116- if not speculative_execution :
117- logger .error (f"{ package_slug } doesn't exist" )
118- os .remove (response .location )
119-
120-
121- def clear_download (location ):
122- """
123- Deletes a directory and its contents.
124-
125- Parameters:
126- location: A Path object representing the directory to delete.
127- """
128- if location :
129- shutil .rmtree (location )
130-
131-
132109def get_casesensitive_slug (path , package_slug ):
133110 payload = [
134111 {
@@ -186,26 +163,22 @@ def get_casesensitive_slug(path, package_slug):
186163 has_next = paginated_tree ["pageInfo" ]["hasNextPage" ]
187164
188165
189- def parse_interesting_advisories (location , purl , delete_download = False ) -> Iterable [VendorData ]:
166+ def parse_interesting_advisories (yml_files , purl ) -> Iterable [VendorData ]:
190167 """
191168 Parses advisories from YAML files in a given location that match a given version.
192169
193170 Parameters:
194- location: A Path object representing the location of the YAML files.
171+ yml_files: An array having the paths of yml files to parse .
195172 purl: PURL for the advisory.
196- version: A string representing the version to check against the affected range.
197- delete_download: A boolean indicating whether to delete the downloaded files after parsing.
198173
199174 Yields:
200175 VendorData instance containing the advisory information for the package.
201176 """
202177 version = purl .version
203- path = Path (location )
204- pattern = "**/*.yml"
205- files = [p for p in path .glob (pattern ) if p .is_file ()]
206- for file in sorted (files ):
207- with open (file ) as f :
208- gitlab_advisory = saneyaml .load (f )
178+
179+ for file in yml_files :
180+ yml_data = fetch_yaml (file ["path" ])
181+ gitlab_advisory = saneyaml .load (yml_data )
209182 affected_range = gitlab_advisory ["affected_range" ]
210183 if gitlab_constraints_satisfied (affected_range , version ):
211184 yield VendorData (
@@ -214,5 +187,3 @@ def parse_interesting_advisories(location, purl, delete_download=False) -> Itera
214187 affected_versions = [affected_range ],
215188 fixed_versions = gitlab_advisory ["fixed_versions" ],
216189 )
217- if delete_download :
218- clear_download (location )
0 commit comments