@@ -58,32 +58,40 @@ def is_configured():
58
58
return False
59
59
60
60
61
- def is_service_available ( label , session , url , raise_exceptions ):
61
+ def is_available ( ):
62
62
"""
63
- Base function that checks if a configured integration service is available.
63
+ Returns True if the configured VulnerableCode server is available.
64
64
"""
65
+ if not is_configured ():
66
+ return False
67
+
65
68
try :
66
- response = session .head (url )
69
+ response = session .head (VULNERABLECODE_API_URL )
67
70
response .raise_for_status ()
68
71
except requests .exceptions .RequestException as request_exception :
69
72
logger .debug (f"{ label } is_available() error: { request_exception } " )
70
- if raise_exceptions :
71
- raise
72
73
return False
73
74
74
75
return response .status_code == requests .codes .ok
75
76
76
77
77
- def is_available ( raise_exceptions = False ):
78
+ def get_base_purl ( purl ):
78
79
"""
79
- Returns True if the configured VulnerableCode server is available .
80
+ Returns the `purl` without qualifiers and subpath .
80
81
"""
81
- if not is_configured ():
82
- return False
82
+ return purl .split ("?" )[0 ]
83
83
84
- return is_service_available (
85
- label , session , VULNERABLECODE_API_URL , raise_exceptions
86
- )
84
+
85
+ def get_purls (packages , base = False ):
86
+ """
87
+ Returns the PURLs for the given list of `packages`.
88
+ Do not include qualifiers nor subpath when `base` is provided.
89
+ """
90
+ return [
91
+ get_base_purl (package_url ) if base else package_url
92
+ for package in packages
93
+ if (package_url := package .package_url )
94
+ ]
87
95
88
96
89
97
def request_get (
@@ -123,13 +131,6 @@ def request_post(
123
131
logger .debug (f"{ label } [Exception] { exception } " )
124
132
125
133
126
- def get_base_purl (purl ):
127
- """
128
- Returns the `purl` without the qualifiers and the subpath.
129
- """
130
- return purl .split ("?" )[0 ]
131
-
132
-
133
134
def _get_vulnerabilities (
134
135
url ,
135
136
field_name ,
@@ -213,62 +214,3 @@ def bulk_search_by_cpes(
213
214
214
215
logger .debug (f"VulnerableCode: url={ url } cpes_count={ len (cpes )} " )
215
216
return request_post (url , data , timeout )
216
-
217
-
218
- def get_purls (packages ):
219
- """
220
- Returns the PURLs for the given list of `packages`.
221
- List comprehension is not used on purpose to avoid crafting each
222
- PURL twice.
223
- """
224
- purls = []
225
- for package in packages :
226
- package_url = package .package_url
227
- if package_url :
228
- purls .append (package_url )
229
- return purls
230
-
231
-
232
- def get_vulnerable_purls (packages ):
233
- """
234
- Returns a list of PURLs for which at least one `affected_by_vulnerabilities`
235
- was found in the VulnerableCodeDB for the given list of `packages`.
236
- """
237
- purls = get_purls (packages )
238
-
239
- if not purls :
240
- return []
241
-
242
- search_results = bulk_search_by_purl (purls , timeout = 5 )
243
- if not search_results :
244
- return []
245
-
246
- return [
247
- entry .get ("purl" )
248
- for entry in search_results
249
- if entry .get ("affected_by_vulnerabilities" )
250
- ]
251
-
252
-
253
- def get_vulnerable_cpes (components ):
254
- """
255
- Returns a list of vulnerable CPEs found in the VulnerableCodeDB for the given list
256
- of `components`.
257
- """
258
- cpes = [component .cpe for component in components if component .cpe ]
259
-
260
- if not cpes :
261
- return []
262
-
263
- search_results = bulk_search_by_cpes (cpes , timeout = 5 )
264
- if not search_results :
265
- return []
266
-
267
- vulnerable_cpes = [
268
- reference .get ("reference_id" )
269
- for entry in search_results
270
- for reference in entry .get ("references" )
271
- if reference .get ("reference_id" ).startswith ("cpe" )
272
- ]
273
-
274
- return list (set (vulnerable_cpes ))
0 commit comments