Skip to content

Commit 161aeec

Browse files
feat: Add offline command line option (#1452) (#1480)
* fixes #1452
1 parent d2c1e27 commit 161aeec

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Usage:
2929
-V, --version show program's version number and exit
3030
--disable-version-check
3131
skips checking for a new version
32+
--offline operate in offline mode
3233

3334
CVE Data Download:
3435
-n {json,api}, --nvd {json,api}
@@ -118,6 +119,8 @@ in the terminal and provide it as an input by running `cve-bin-tool -L pkg-list`
118119
You can use `--config` option to provide configuration file for the tool. You can still override options specified in config file with command line arguments. See our sample config files in the
119120
[test/config](https://github.com/intel/cve-bin-tool/blob/main/test/config/)
120121

122+
Specifying the `--offline` option when running a scan ensures that cve-bin-tool doesn't attempt to download the latest database files or to check for a newer version of the tool.
123+
121124
The 0.3.1 release is intended to be the last release to officially support
122125
python 2.7; please switch to python 3.6+ for future releases and to use the
123126
development tree. You can check [our CI configuration](https://github.com/intel/cve-bin-tool/blob/main/.github/workflows/pythonapp.yml) to see what versions of python we're explicitly testing.

cve_bin_tool/cli.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import platform
2020
import sys
2121
import textwrap
22+
import time
2223
from collections import ChainMap
2324
from typing import Dict
2425

@@ -219,6 +220,11 @@ def main(argv=None):
219220
action="store_true",
220221
help="skips checking for a new version",
221222
)
223+
parser.add_argument(
224+
"--offline",
225+
action="store_true",
226+
help="operate in offline mode",
227+
)
222228

223229
merge_report_group = parser.add_argument_group(
224230
"Merge Report", "Arguments related to Intermediate and Merged Reports"
@@ -303,6 +309,7 @@ def main(argv=None):
303309
"affected_versions": 0,
304310
"sbom": "spdx",
305311
"sbom_file": "",
312+
"offline": False,
306313
}
307314

308315
with ErrorHandler(mode=ErrorMode.NoTrace):
@@ -349,6 +356,15 @@ def main(argv=None):
349356
if int(args["cvss"]) > 0:
350357
score = int(args["cvss"])
351358

359+
# Offline processing
360+
if args["offline"]:
361+
# Override version check and database update arguments
362+
version_check = True
363+
db_update = "never"
364+
else:
365+
version_check = args["disable_version_check"]
366+
db_update = args["update"]
367+
352368
# Check for PDF support
353369
output_format = args["format"]
354370
if output_format == "pdf" and importlib.util.find_spec("reportlab") is None:
@@ -380,12 +396,10 @@ def main(argv=None):
380396
# Database update related settings
381397
# Connect to the database
382398
cvedb_orig = CVEDB(
383-
version_check=not args["disable_version_check"],
399+
version_check=not version_check,
384400
error_mode=error_mode,
385401
nvd_type=args["nvd"],
386-
incremental_update=True
387-
if args["update"] == "latest" and args["nvd"]
388-
else False,
402+
incremental_update=True if db_update == "latest" and args["nvd"] else False,
389403
)
390404

391405
# if OLD_CACHE_DIR (from cvedb.py) exists, print warning
@@ -394,15 +408,23 @@ def main(argv=None):
394408
f"Obsolete cache dir {OLD_CACHE_DIR} is no longer needed and can be removed."
395409
)
396410

411+
# Check database exists if operating in offline mode.
412+
if args["offline"] and not cvedb_orig.check_db_exists():
413+
LOGGER.critical("Database does not exist.")
414+
LOGGER.info(
415+
"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."
416+
)
417+
return -1
418+
397419
# Clear data if -u now is set
398-
if args["update"] == "now":
420+
if db_update == "now":
399421
cvedb_orig.clear_cached_data()
400422

401-
if args["update"] == "latest":
423+
if db_update == "latest":
402424
cvedb_orig.refresh_cache_and_update_db()
403425

404426
# update db if needed
405-
if args["update"] != "never":
427+
if db_update != "never":
406428
cvedb_orig.get_cvelist_if_stale()
407429
else:
408430
if args["nvd"] == "json":
@@ -417,6 +439,12 @@ def main(argv=None):
417439
with ErrorHandler(mode=error_mode, logger=LOGGER):
418440
raise CVEDataMissing("No data in CVE Database")
419441

442+
# Report time of last database update
443+
db_date = time.strftime(
444+
"%d %B %Y at %H:%M:%S", time.localtime(cvedb_orig.get_db_update_date())
445+
)
446+
LOGGER.info(f"CVE database last updated on {db_date}")
447+
420448
cvedb_orig.remove_cache_backup()
421449

422450
# Input validation

cve_bin_tool/cvedb.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ async def nist_fetch_using_api(self):
122122
nvd_api.session = None
123123
return nvd_api.all_cve_entries
124124

125+
def check_db_exists(self):
126+
return os.path.isfile(self.dbpath)
127+
125128
def get_db_update_date(self):
126129
# last time when CVE data was updated
127130
self.time_of_last_update = datetime.datetime.fromtimestamp(

doc/how_to_guides/offline.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ NOTE The tool will error with InsufficientArgs because no directory was specifie
1515
The way of transfer depends on the environment. The files to be transferred are in "~/.cache/cve-bin-tool"
1616

1717
## Import the vulnerability database file on the offline system
18-
The vulnerability database should be copied into ~/.cache/cve-bin-tool
18+
The vulnerability database should be copied into ~/.cache/cve-bin-tool.
1919

20-
## Run cve-bin-tool with --update never and --disable-version-check options
21-
In an offline environment, when running a scan specify `--update never` so that cve-bin-tool doesn't attempt to download the latest database files and `--disable-version-check` so that the cve-bin-tool doesn't attempt to check for a newer version of the tool.
20+
The cve-bin-tool will fail to operate in offline mode if a vulnerability database is not present on the system.
21+
22+
## Run cve-bin-tool with --offline option
23+
In an offline environment, specify the `--offline` option when running a scan so that cve-bin-tool doesn't attempt to download the latest database files or check for a newer version of the tool.
24+
The `--offline` option is equivalent to specifying `--update never` and `--disable-version-check` options.
2225

2326
## Maintenance Updates
24-
In an offline environment, it is important to update the vulnerability database on a regular basis as often as you feel appropriate, so that the scanner can continue to detect recently-identified vulnerabilities. If any changes to CVE data is required (e.g. to remove false positives), you might also want to create and copy over a triage data file for usage.
27+
In an offline environment, it is important to update the vulnerability database on a regular basis as often as you feel appropriate, so that the scanner can continue to detect recently-identified vulnerabilities. If any changes to CVE data is required (e.g. to remove false positives), you might also want to create and copy over a triage data file for usage. The time of the latest database update is reported whenever a scan is performed.
2528

2629
It is important to periodically check if the cve-bin-tool has also been updated as this check cannot be performed within an offline environment.

0 commit comments

Comments
 (0)