Skip to content

Commit 5210b32

Browse files
committed
fix: Add --update-only flag for database updates without scanning
1 parent a2978f4 commit 5210b32

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

cve_bin_tool/cli.py

+28-1
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,12 @@ def main(argv=None):
486486
)
487487

488488
database_group = parser.add_argument_group("Database Management")
489+
database_group.add_argument(
490+
"--update-only",
491+
action="store_true",
492+
help="update the database without scanning any files or directories",
493+
default=False,
494+
)
489495
database_group.add_argument(
490496
"--import-json",
491497
action="store",
@@ -908,6 +914,11 @@ def main(argv=None):
908914
"Consult the documentation at https://cve-bin-tool.readthedocs.io/en/latest/how_to_guides/offline.html to find out how to setup offline operation."
909915
)
910916
return ERROR_CODES[CVEDBOutdatedSchema]
917+
918+
# If running in update-only mode, exit now
919+
if args.get("update_only", False):
920+
LOGGER.info("Database update completed. Exiting without scanning.")
921+
return 0
911922

912923
# CVE Database validation
913924
if not cvedb_orig.check_cve_entries():
@@ -965,6 +976,22 @@ def main(argv=None):
965976
"Use -F --filter only when you want to filter out intermediate reports on the basis of tag"
966977
)
967978

979+
# Handle --update-only flag
980+
if args.get("update_only", False):
981+
LOGGER.info("Running in update-only mode")
982+
# Force the update regardless of update setting
983+
db_update = "now"
984+
# Skip input validation
985+
if (
986+
not args["directory"]
987+
and not args["input_file"]
988+
and not args["package_list"]
989+
and not args["merge"]
990+
and not args["sbom_file"]
991+
and not args["vex_file"]
992+
):
993+
args["directory"] = "" # Set a dummy value to pass validation
994+
968995
# Input validation
969996
if (
970997
not args["directory"]
@@ -1099,7 +1126,7 @@ def main(argv=None):
10991126
LOGGER.info(f"Number of checkers: {version_scanner.number_of_checkers()}")
11001127
version_scanner.print_checkers()
11011128
LOGGER.debug(
1102-
"If the checkers arent loading properly: https://cve-bin-tool.readthedocs.io/en/latest/CONTRIBUTING.html#help-my-checkers-aren-t-loading"
1129+
"If the checkers aren't loading properly: https://cve-bin-tool.readthedocs.io/en/latest/CONTRIBUTING.html#help-my-checkers-aren-t-loading"
11031130
)
11041131
LOGGER.info(
11051132
f"Number of language checkers: {version_scanner.number_of_language_checkers()}"

doc/MANUAL.md

+16
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@
7474
- [Database Management](#database-management)
7575
- [--export EXPORT](#--export-export)
7676
- [--import IMPORT](#--import-import)
77+
- [--update-only update the database without scanning any files or directories](#--update-only-update-the-database-without-scanning-any-files-or-directories)
78+
- [--import-json IMPORT_JSON](#--import-json-import_json)
79+
import database from json files chopped by years
7780
- [Deprecated Arguments](#deprecated-arguments)
7881
- [-x, --extract](#-x---extract)
7982
- [--report](#--report)
@@ -216,6 +219,11 @@ which is useful if you're trying the latest code from
216219
-r RUNS, --runs RUNS comma-separated list of checkers to enable
217220

218221
Database Management:
222+
--export EXPORT
223+
make a copy of the database
224+
--import IMPORT
225+
import a copy of the database
226+
--update-only update the database without scanning any files or directories
219227
--import-json IMPORT_JSON
220228
import database from json files chopped by years
221229
--ignore-sig do not verify PGP signature while importing json data
@@ -1487,6 +1495,14 @@ This option allows you to make a copy of the database. This is typically require
14871495

14881496
This option allows you to import a copy of the database (typically created using the `--export` option). If the specified file does not exist, this operation has no effect.
14891497

1498+
### --update-only update the database without scanning any files or directories
1499+
1500+
This option allows you to update the database without scanning any files or directories. This is useful when you want to update the database without running any scans.
1501+
1502+
### --import-json IMPORT_JSON
1503+
1504+
This option allows you to import a copy of the database from JSON files chopped by years. This is useful when you want to import a pre-existing database from JSON files.
1505+
14901506
## Deprecated Arguments
14911507

14921508
### -x, --extract

doc/how_to_guides/multiple_scans_at_once.md

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ To update (without scanning) you can use the following command:
1313
cve-bin-tool -u now
1414
```
1515

16+
Alternatively, you can use the dedicated update-only flag:
17+
18+
```
19+
cve-bin-tool --update-only
20+
```
21+
1622
We recommend once per day, but this can be more frequently or less frequently depending on your needs. Ideally, you want to be sure this completes before you kick off any other scans, so that you aren't checking against a partial database.
1723

1824
## Step 2: Scan

test/test_cli.py

+22
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,28 @@ def test_update(self, caplog):
275275
) in caplog.record_tuples
276276
caplog.clear()
277277

278+
@pytest.mark.skipif(not LONG_TESTS(), reason="Update-only flag tests are long tests")
279+
def test_update_only(self, caplog):
280+
"""Test the --update-only flag"""
281+
with caplog.at_level(logging.INFO):
282+
main(["cve-bin-tool", "--update-only"])
283+
284+
# Check that we see the update-only mode message
285+
assert (
286+
"cve_bin_tool",
287+
logging.INFO,
288+
"Running in update-only mode",
289+
) in caplog.record_tuples
290+
291+
# Check that we see the completion message
292+
assert (
293+
"cve_bin_tool",
294+
logging.INFO,
295+
"Database update completed. Exiting without scanning.",
296+
) in caplog.record_tuples
297+
298+
caplog.clear()
299+
278300
def test_unknown_warning(self, caplog):
279301
"""Test that an "UNKNOWN" file generates a log (only in debug mode)"""
280302

0 commit comments

Comments
 (0)