1919from fetchcode import fetch
2020from packageurl import PackageURL
2121
22+ from vulntotal .datasources .gitlab_api import fetch_gitlab_advisories_for_purl
23+ from vulntotal .datasources .gitlab_api import fetch_yaml
2224from vulntotal .validator import DataSource
2325from vulntotal .validator import VendorData
2426from vulntotal .vulntotal_utils import gitlab_constraints_satisfied
@@ -40,18 +42,12 @@ def datasource_advisory(self, purl) -> Iterable[VendorData]:
4042 Yields:
4143 VendorData instance containing the advisory information for the package.
4244 """
43- package_slug = get_package_slug (purl )
44- directory_files = fetch_directory_contents (package_slug )
45- if not directory_files :
46- path = self .supported_ecosystem ()[purl .type ]
47- casesensitive_package_slug = get_casesensitive_slug (path , package_slug )
48- directory_files = fetch_directory_contents (casesensitive_package_slug )
45+ advisories = fetch_gitlab_advisories_for_purl (
46+ purl , self .supported_ecosystem (), get_casesensitive_slug
47+ )
4948
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 )
54- return interesting_advisories
49+ if advisories :
50+ return parse_interesting_advisories (advisories , purl )
5551
5652 @classmethod
5753 def supported_ecosystem (cls ):
@@ -67,21 +63,6 @@ def supported_ecosystem(cls):
6763 }
6864
6965
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-
8566def get_package_slug (purl ):
8667 """
8768 Constructs a package slug from a given purl.
@@ -163,27 +144,25 @@ def get_casesensitive_slug(path, package_slug):
163144 has_next = paginated_tree ["pageInfo" ]["hasNextPage" ]
164145
165146
166- def parse_interesting_advisories (yml_files , purl ) -> Iterable [VendorData ]:
147+ def parse_interesting_advisories (advisories , purl ) -> Iterable [VendorData ]:
167148 """
168149 Parses advisories from YAML files in a given location that match a given version.
169150
170151 Parameters:
171- yml_files: An array having the paths of yml files to parse .
152+ advisories: A list of advisory dictionaries fetched from the GitLab API .
172153 purl: PURL for the advisory.
173154
174155 Yields:
175156 VendorData instance containing the advisory information for the package.
176157 """
177158 version = purl .version
178159
179- for file in yml_files :
180- yml_data = fetch_yaml (file ["path" ])
181- gitlab_advisory = saneyaml .load (yml_data )
182- affected_range = gitlab_advisory ["affected_range" ]
160+ for advisory in advisories :
161+ affected_range = advisory .get ("affected_range" )
183162 if gitlab_constraints_satisfied (affected_range , version ):
184163 yield VendorData (
185164 purl = PackageURL (purl .type , purl .namespace , purl .name ),
186- aliases = gitlab_advisory [ "identifiers" ] ,
165+ aliases = advisory . get ( "identifiers" , []) ,
187166 affected_versions = [affected_range ],
188- fixed_versions = gitlab_advisory [ "fixed_versions" ] ,
167+ fixed_versions = advisory . get ( "fixed_versions" , []) ,
189168 )
0 commit comments