Skip to content

Commit 86455c1

Browse files
feat: Add options to import and export database (fixes #1655) (#1656)
1 parent c6fc6b5 commit 86455c1

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ Usage:
210210
comma-separated list of checkers to disable
211211
-r RUNS, --runs RUNS comma-separated list of checkers to enable
212212

213+
Database Management:
214+
--export EXPORT export database filename
215+
--import IMPORT import database filename
216+
213217
Deprecated:
214218
-x, --extract autoextract compressed files
215219
CVE Binary Tool autoextracts all compressed files by default now

cve_bin_tool/cli.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,20 @@ def main(argv=None):
329329
default="",
330330
)
331331

332+
database_group = parser.add_argument_group("Database Management")
333+
database_group.add_argument(
334+
"--export",
335+
action="store",
336+
help="export database filename",
337+
default="",
338+
)
339+
database_group.add_argument(
340+
"--import",
341+
action="store",
342+
help="import database filename",
343+
default="",
344+
)
345+
332346
exploit_checker_group = parser.add_argument_group("Exploits")
333347
exploit_checker_group.add_argument(
334348
"--exploits",
@@ -473,6 +487,18 @@ def main(argv=None):
473487
)
474488
return -1
475489

490+
# Import database if file exists
491+
if args["import"] and os.path.exists(args["import"]):
492+
LOGGER.info(f'Import database from {args["import"]}')
493+
cvedb_orig.copy_db(filename=args["import"], export=False)
494+
495+
# Export database if database exists
496+
if args["export"] and cvedb_orig.check_db_exists():
497+
LOGGER.info(f'Export database to {args["export"]}')
498+
cvedb_orig.copy_db(filename=args["export"], export=True)
499+
# And terminate operation
500+
return 0
501+
476502
# Clear data if -u now is set
477503
if db_update == "now":
478504
cvedb_orig.clear_cached_data()

cve_bin_tool/cvedb.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,13 @@ def init_database(self) -> None:
439439
self.clear_cached_data()
440440
self.connection.commit()
441441

442+
def copy_db(self, filename, export=True):
443+
self.db_close()
444+
if export:
445+
shutil.copy(self.dbpath, filename)
446+
else:
447+
shutil.copy(filename, self.dbpath)
448+
442449
def populate_db(self) -> None:
443450
"""Function that populates the database from the JSON.
444451

doc/MANUAL.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
- [-t TAG, --tag TAG](#-t-tag---tag-tag)
4545
- [-m INTERMEDIATE_REPORTS, --merge INTERMEDIATE_REPORTS](#-m-intermediate_reports---merge-intermediate_reports)
4646
- [-F TAGS, --filter TAGS](#-f-tags---filter-tags)
47+
- [Database Management](#database-management)
48+
- [--export EXPORT](#--export-export)
49+
- [--import IMPORT](#--import-import)
4750
- [Deprecated Arguments](#deprecated-arguments)
4851
- [-x, --extract](#-x---extract)
4952
- [Feedback & Contributions](#feedback--contributions)
@@ -123,6 +126,10 @@ which is useful if you're trying the latest code from
123126
comma-separated list of checkers to disable
124127
-r RUNS, --runs RUNS comma-separated list of checkers to enable
125128

129+
Database Management:
130+
--export EXPORT export database filename
131+
--import IMPORT import database filename
132+
126133
Deprecated:
127134
-x, --extract autoextract compressed files
128135

@@ -818,6 +825,16 @@ This option allows you to merge intermediate reports created using `-a` or `--ap
818825

819826
This allows you to filter out intermediate reports based on the tag. This can be useful while merging multiple intermediate reports from a single path. See detailed guide on [`filter intermediate reports`](how_to_guides/filter_intermediate_reports.md) for more information.
820827

828+
## Database Management
829+
830+
### --export EXPORT
831+
832+
This option allows you to make a copy of the database. This is typically required as part of setting up offline operation of the tool. If no database exists, this operation has no effect.
833+
834+
### --import IMPORT
835+
836+
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.
837+
821838
## Deprecated Arguments
822839

823840
### -x, --extract

doc/how_to_guides/offline.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,23 @@ $ cve-bin-tool --update now
1111
```
1212
NOTE The tool will error with InsufficientArgs because no directory was specified for a scan. This is expected behaviour.
1313

14+
## Export the database
15+
16+
Run the tool to export the latest version of the vulnerability database.
17+
```
18+
$ cve-bin-tool --export <filename>
19+
```
20+
1421
## Transfer the vulnerability database file into a directory in the offline environment
15-
The way of transfer depends on the environment. The files to be transferred are in "~/.cache/cve-bin-tool"
22+
23+
The way of transfer depends on the environment.
1624

1725
## Import the vulnerability database file on the offline system
18-
The vulnerability database should be copied into ~/.cache/cve-bin-tool.
26+
27+
Run the tool to import the transferred copy of the vulnerability database.
28+
```
29+
$ cve-bin-tool --import <filename>
30+
```
1931

2032
The cve-bin-tool will fail to operate in offline mode if a vulnerability database is not present on the system.
2133

0 commit comments

Comments
 (0)