Skip to content

Commit 3991370

Browse files
authored
fix: remove LegacyVersion, upgrade database schema automatically (#2432)
* fixes #2428 * fixes #2433 * fix: remove LegacyVersion * fix: check for cve_range schema changes and upgrade database schema
1 parent 6f9669f commit 3991370

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

cve_bin_tool/cve_scanner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from logging import Logger
99
from pathlib import Path
1010
from string import ascii_lowercase
11-
from typing import DefaultDict, Dict, List, Tuple, Union
11+
from typing import DefaultDict, Dict, List, Tuple
1212

13-
from packaging.version import LegacyVersion, Version
13+
from packaging.version import Version
1414
from packaging.version import parse as parse_version
1515
from rich.console import Console
1616

@@ -307,7 +307,7 @@ def letter_convert(self, version: str) -> str:
307307
version = f"{version[:-1]}.{self.ALPHA_TO_NUM[last_char]}"
308308
return version
309309

310-
VersionType = Union[Version, LegacyVersion]
310+
VersionType = Version
311311

312312
def canonical_convert(
313313
self, product_info: ProductInfo

cve_bin_tool/cvedb.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,35 +149,40 @@ def get_cvelist_if_stale(self) -> None:
149149
self.LOGGER.info(
150150
"Using cached CVE data (<24h old). Use -u now to update immediately."
151151
)
152-
if not self.latest_schema():
152+
severity_schema, range_schema = self.table_schemas()
153+
if not self.latest_schema(
154+
"cve_severity", severity_schema
155+
) or not self.latest_schema("cve_range", range_schema):
153156
self.refresh_cache_and_update_db()
154157
self.time_of_last_update = datetime.datetime.today()
155158

156-
def latest_schema(self, cursor: sqlite3.Cursor | None = None) -> bool:
159+
def latest_schema(
160+
self, table_name: str, table_schema: str, cursor: sqlite3.Cursor | None = None
161+
) -> bool:
157162
"""Check database is using latest schema"""
158163
self.LOGGER.debug("Check database is using latest schema")
159164
cursor = self.db_open_and_get_cursor()
160-
schema_check = "SELECT * FROM cve_severity WHERE 1=0"
165+
schema_check = f"SELECT * FROM {table_name} WHERE 1=0"
161166
result = cursor.execute(schema_check)
162167
schema_latest = False
163168

164169
if not cursor:
165170
self.db_close()
166171

167-
severity, _ = self.table_schemas()
168-
169172
# getting schema from command
170-
lines = severity.split("(")[1].split(",")
173+
lines = table_schema.split("(")[1].split(",")
171174

172-
severity_schema = [x.split("\n")[1].strip().split(" ")[0] for x in lines]
173-
severity_schema.pop()
175+
table_schema = [x.split("\n")[1].strip().split(" ")[0] for x in lines]
176+
table_schema.pop()
174177

175178
# getting current schema from cve_severity
176179
current_schema = [x[0] for x in result.description]
177180

178-
if severity_schema == current_schema:
181+
if table_schema == current_schema:
179182
schema_latest = True
180183

184+
# check for cve_
185+
181186
return schema_latest
182187

183188
def check_cve_entries(self) -> bool:
@@ -256,11 +261,20 @@ def init_database(self) -> None:
256261
cursor.execute(version_range_create)
257262
cursor.execute(index_range)
258263

259-
if not self.latest_schema(cursor):
264+
severity_schema, range_schema = self.table_schemas()
265+
# Check schema on cve_severity
266+
if not self.latest_schema("cve_severity", severity_schema, cursor):
260267
# Recreate table using latest schema
261-
self.LOGGER.info("Upgrading database to latest schema")
268+
self.LOGGER.info("Upgrading database cve_severity to latest schema")
262269
cursor.execute("DROP TABLE cve_severity")
263270
cursor.execute(cve_data_create)
271+
272+
# Check schema on cve_range
273+
if not self.latest_schema("cve_range", range_schema, cursor):
274+
self.LOGGER.info("Upgrading database cve_range to latest schema")
275+
cursor.execute("DROP TABLE cve_range")
276+
cursor.execute(version_range_create)
277+
264278
if self.connection is not None:
265279
self.connection.commit()
266280

0 commit comments

Comments
 (0)