Skip to content

Commit 77a3a87

Browse files
authored
refactor: encapsulate vendor fetch to CVEDB (#1417)
* refactor: encapsulate vendor fetch to CVEDB * fixes #1416
1 parent 5c9aee0 commit 77a3a87

File tree

4 files changed

+49
-72
lines changed

4 files changed

+49
-72
lines changed

cve_bin_tool/cvedb.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,47 @@ def clear_cached_data(self):
718718
self.LOGGER.warning(f"Deleting old cachedir {OLD_CACHE_DIR}")
719719
shutil.rmtree(OLD_CACHE_DIR)
720720

721+
def get_vendor_product_pairs(self, package_names):
722+
"""
723+
Fetches vendor from the database for packages that doesn't have vendor info for Package List Parser Utility and Universal Python package checker.
724+
"""
725+
self.db_open()
726+
cursor = self.connection.cursor()
727+
vendor_package_pairs = []
728+
query = """
729+
SELECT DISTINCT vendor FROM cve_range
730+
WHERE product=?
731+
"""
732+
733+
# For python package checkers we don't need the progress bar running
734+
if type(package_names) != list:
735+
cursor.execute(query, [package_names])
736+
vendors = list(map(lambda x: x[0], cursor.fetchall()))
737+
for vendor in vendors:
738+
if vendor != "":
739+
vendor_package_pairs.append(
740+
{
741+
"vendor": vendor,
742+
"product": package_names,
743+
}
744+
)
745+
else:
746+
for package_name in track(
747+
package_names, description="Processing the given list...."
748+
):
749+
cursor.execute(query, [package_name["name"].lower()])
750+
vendors = list(map(lambda x: x[0], cursor.fetchall()))
751+
for vendor in vendors:
752+
if vendor != "":
753+
vendor_package_pairs.append(
754+
{
755+
"vendor": vendor,
756+
"product": package_name["name"],
757+
}
758+
)
759+
self.db_close()
760+
return vendor_package_pairs
761+
721762
def db_open(self):
722763
"""Opens connection to sqlite database."""
723764
if not self.connection:

cve_bin_tool/package_list_parser/__init__.py renamed to cve_bin_tool/package_list_parser.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import distro
1212

13+
from cve_bin_tool.cvedb import CVEDB
1314
from cve_bin_tool.error_handler import (
1415
EmptyTxtError,
1516
ErrorHandler,
@@ -19,8 +20,6 @@
1920
from cve_bin_tool.log import LOGGER
2021
from cve_bin_tool.util import ProductInfo, Remarks
2122

22-
from .vendor_fetch import VendorFetch
23-
2423
ROOT_PATH = join(dirname(__file__), "..")
2524
PYPI_CSV = join(ROOT_PATH, "package_list_parser", "pypi_list.csv")
2625

@@ -132,10 +131,10 @@ def parse_list(self):
132131
if package_name in txt_package_names:
133132
self.package_names_without_vendor.append(installed_package)
134133

135-
with VendorFetch() as vendor_fetch:
136-
vendor_package_pairs = vendor_fetch.get_vendor_product_pairs(
137-
self.package_names_without_vendor
138-
)
134+
cve_db = CVEDB()
135+
vendor_package_pairs = cve_db.get_vendor_product_pairs(
136+
self.package_names_without_vendor
137+
)
139138

140139
self.add_vendor(vendor_package_pairs)
141140
self.parse_data()

cve_bin_tool/package_list_parser/vendor_fetch.py

Lines changed: 0 additions & 63 deletions
This file was deleted.

cve_bin_tool/version_scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88

99
import pkg_resources
1010

11+
from cve_bin_tool.cvedb import CVEDB
1112
from cve_bin_tool.egg_updater import IS_DEVELOP, update_egg
1213
from cve_bin_tool.error_handler import ErrorMode
1314
from cve_bin_tool.extractor import Extractor
1415
from cve_bin_tool.file import is_binary
1516
from cve_bin_tool.log import LOGGER
16-
from cve_bin_tool.package_list_parser.vendor_fetch import VendorFetch
1717
from cve_bin_tool.strings import Strings
1818
from cve_bin_tool.util import DirWalk, ProductInfo, inpath
1919

@@ -177,8 +177,8 @@ def run_python_package_checkers(self, filename, lines):
177177
product = search(compile(r"^Name: (.+)$", MULTILINE), lines).group(1)
178178
version = search(compile(r"^Version: (.+)$", MULTILINE), lines).group(1)
179179

180-
with VendorFetch() as vendor_fetch:
181-
vendor_package_pair = vendor_fetch.get_vendor_product_pairs(product)
180+
cve_db = CVEDB()
181+
vendor_package_pair = cve_db.get_vendor_product_pairs(product)
182182

183183
if vendor_package_pair != []:
184184
vendor = vendor_package_pair[0]["vendor"]

0 commit comments

Comments
 (0)