Skip to content

Commit ace9154

Browse files
authored
Add experimental-download-offline-databases flag (#1039)
Currently flags `experimental-offline` and `experimental-local-db` are confusing sometimes. This PR renames `experimental-local-db` to `experimental-download-database` to make it more explicit whether to download the database or not. For now, `experimental-download-database` only works when `experimental-offline` is set. `internal/local` is also modified to reflect the change in the naming and meaning of this flag.
1 parent 72afdb8 commit ace9154

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed

cmd/osv-scanner/__snapshots__/main_test.snap

+18
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,24 @@ No issues found
13541354

13551355
---
13561356

1357+
[TestRun_LocalDatabases/#12 - 1]
1358+
1359+
---
1360+
1361+
[TestRun_LocalDatabases/#12 - 2]
1362+
databases can only be downloaded when running in offline mode
1363+
1364+
---
1365+
1366+
[TestRun_LocalDatabases/#12 - 3]
1367+
1368+
---
1369+
1370+
[TestRun_LocalDatabases/#12 - 4]
1371+
databases can only be downloaded when running in offline mode
1372+
1373+
---
1374+
13571375
[TestRun_LockfileWithExplicitParseAs/#00 - 1]
13581376

13591377
---

cmd/osv-scanner/main_test.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -480,74 +480,80 @@ func TestRun_LocalDatabases(t *testing.T) {
480480
// one specific supported lockfile
481481
{
482482
name: "",
483-
args: []string{"", "--experimental-local-db", "./fixtures/locks-many/composer.lock"},
483+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "./fixtures/locks-many/composer.lock"},
484484
exit: 0,
485485
},
486486
// one specific supported sbom with vulns
487487
{
488488
name: "",
489-
args: []string{"", "--experimental-local-db", "--config=./fixtures/osv-scanner-empty-config.toml", "./fixtures/sbom-insecure/postgres-stretch.cdx.xml"},
489+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--config=./fixtures/osv-scanner-empty-config.toml", "./fixtures/sbom-insecure/postgres-stretch.cdx.xml"},
490490
exit: 1,
491491
},
492492
// one specific unsupported lockfile
493493
{
494494
name: "",
495-
args: []string{"", "--experimental-local-db", "./fixtures/locks-many/not-a-lockfile.toml"},
495+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "./fixtures/locks-many/not-a-lockfile.toml"},
496496
exit: 128,
497497
},
498498
// all supported lockfiles in the directory should be checked
499499
{
500500
name: "",
501-
args: []string{"", "--experimental-local-db", "./fixtures/locks-many"},
501+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "./fixtures/locks-many"},
502502
exit: 0,
503503
},
504504
// all supported lockfiles in the directory should be checked
505505
{
506506
name: "",
507-
args: []string{"", "--experimental-local-db", "./fixtures/locks-many-with-invalid"},
507+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "./fixtures/locks-many-with-invalid"},
508508
exit: 127,
509509
},
510510
// only the files in the given directories are checked by default (no recursion)
511511
{
512512
name: "",
513-
args: []string{"", "--experimental-local-db", "./fixtures/locks-one-with-nested"},
513+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "./fixtures/locks-one-with-nested"},
514514
exit: 0,
515515
},
516516
// nested directories are checked when `--recursive` is passed
517517
{
518518
name: "",
519-
args: []string{"", "--experimental-local-db", "--recursive", "./fixtures/locks-one-with-nested"},
519+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--recursive", "./fixtures/locks-one-with-nested"},
520520
exit: 0,
521521
},
522522
// .gitignored files
523523
{
524524
name: "",
525-
args: []string{"", "--experimental-local-db", "--recursive", "./fixtures/locks-gitignore"},
525+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--recursive", "./fixtures/locks-gitignore"},
526526
exit: 0,
527527
},
528528
// ignoring .gitignore
529529
{
530530
name: "",
531-
args: []string{"", "--experimental-local-db", "--recursive", "--no-ignore", "./fixtures/locks-gitignore"},
531+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--recursive", "--no-ignore", "./fixtures/locks-gitignore"},
532532
exit: 0,
533533
},
534534
// output with json
535535
{
536536
name: "",
537-
args: []string{"", "--experimental-local-db", "--json", "./fixtures/locks-many/composer.lock"},
537+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--json", "./fixtures/locks-many/composer.lock"},
538538
exit: 0,
539539
},
540540
{
541541
name: "",
542-
args: []string{"", "--experimental-local-db", "--format", "json", "./fixtures/locks-many/composer.lock"},
542+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--format", "json", "./fixtures/locks-many/composer.lock"},
543543
exit: 0,
544544
},
545545
// output format: markdown table
546546
{
547547
name: "",
548-
args: []string{"", "--experimental-local-db", "--format", "markdown", "./fixtures/locks-many/composer.lock"},
548+
args: []string{"", "--experimental-offline", "--experimental-download-offline-databases", "--format", "markdown", "./fixtures/locks-many/composer.lock"},
549549
exit: 0,
550550
},
551+
// database should be downloaded only when offline is set
552+
{
553+
name: "",
554+
args: []string{"", "--experimental-download-offline-databases", "./fixtures/locks-many"},
555+
exit: 127,
556+
},
551557
}
552558

553559
for _, tt := range tests {

cmd/osv-scanner/scan/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,14 @@ func Command(stdout, stderr io.Writer, r *reporter.Reporter) *cli.Command {
101101
Usage: "specify the level of information that should be provided during runtime; value can be: " + strings.Join(reporter.VerbosityLevels(), ", "),
102102
Value: "info",
103103
},
104-
&cli.BoolFlag{
105-
Name: "experimental-local-db",
106-
Usage: "checks for vulnerabilities using local databases",
107-
},
108104
&cli.BoolFlag{
109105
Name: "experimental-offline",
110106
Usage: "checks for vulnerabilities using local databases that are already cached",
111107
},
108+
&cli.BoolFlag{
109+
Name: "experimental-download-offline-databases",
110+
Usage: "downloads vulnerability databases for offline comparison",
111+
},
112112
&cli.StringFlag{
113113
Name: "experimental-local-db-path",
114114
Usage: "sets the path that local databases should be stored",
@@ -210,9 +210,9 @@ func action(context *cli.Context, stdout, stderr io.Writer) (reporter.Reporter,
210210
DirectoryPaths: context.Args().Slice(),
211211
CallAnalysisStates: callAnalysisStates,
212212
ExperimentalScannerActions: osvscanner.ExperimentalScannerActions{
213-
LocalDBPath: context.String("experimental-local-db-path"),
214-
CompareLocally: context.Bool("experimental-local-db"),
215-
CompareOffline: context.Bool("experimental-offline"),
213+
LocalDBPath: context.String("experimental-local-db-path"),
214+
DownloadDatabases: context.Bool("experimental-download-offline-databases"),
215+
CompareOffline: context.Bool("experimental-offline"),
216216
// License summary mode causes all
217217
// packages to appear in the json as
218218
// every package has a license - even

docs/offline-mode.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ If the `OSV_SCANNER_LOCAL_DB_CACHE_DIRECTORY` environment variable is _not_ set,
4949
1. The location returned by [`os.UserCacheDir`](https://pkg.go.dev/os#UserCacheDir)
5050
2. The location returned by [`os.TempDir`](https://pkg.go.dev/os#TempDir)
5151

52-
The database can be [downloaded manually](./experimental.md#manual-database-download) or by using the [`--experimental-local-db` flag](./experimental.md#local-database-option).
52+
The database can be [downloaded manually](./experimental.md#manual-database-download) or by using the [`--experimental-download-offline-databases` flag](./experimental.md#download-databases-option).
5353

5454
## Offline option
5555

@@ -59,17 +59,17 @@ The offline database flag `--experimental-offline` causes OSV-Scanner to scan yo
5959
osv-scanner --experimental-offline ./path/to/your/dir
6060
```
6161

62-
## Local database option
62+
## Download offline databases option
6363

64-
The local database flag `--experimental-local-db` causes OSV-Scanner to download or update your local database and then scan your project against it.
64+
The download offline databases flag `--experimental-download-offline-databases` allows OSV-Scanner to download or update your local database when running in offline mode, to make it easier to get started. This option only works when you also set the offline flag.
6565

6666
```bash
67-
osv-scanner --experimental-local-db ./path/to/your/dir
67+
osv-scanner --experimental-offline --experimental-download-offline-databases ./path/to/your/dir
6868
```
6969

7070
## Manual database download
7171

72-
Instead of using the `--experimental-local-db` flag to download the database, it is possible to manually download the database.
72+
Instead of using the `--experimental-download-offline-databases` flag to download the database, it is possible to manually download the database.
7373

7474
A downloadable copy of the OSV database is stored in a GCS bucket maintained by OSV:
7575
[`gs://osv-vulnerabilities`](https://osv-vulnerabilities.storage.googleapis.com)

pkg/osvscanner/osvscanner.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type ScannerActions struct {
4747
}
4848

4949
type ExperimentalScannerActions struct {
50-
CompareLocally bool
5150
CompareOffline bool
51+
DownloadDatabases bool
5252
ShowAllPackages bool
5353
ScanLicensesSummary bool
5454
ScanLicensesAllowlist []string
@@ -753,17 +753,17 @@ func DoScan(actions ScannerActions, r reporter.Reporter) (models.VulnerabilityRe
753753
}
754754

755755
if actions.CompareOffline {
756-
actions.CompareLocally = true
757-
}
758-
759-
if actions.CompareLocally {
760756
actions.SkipGit = true
761757

762758
if len(actions.ScanLicensesAllowlist) > 0 || actions.ScanLicensesSummary {
763759
return models.VulnerabilityResults{}, errors.New("cannot retrieve licenses locally")
764760
}
765761
}
766762

763+
if !actions.CompareOffline && actions.DownloadDatabases {
764+
return models.VulnerabilityResults{}, errors.New("databases can only be downloaded when running in offline mode")
765+
}
766+
767767
configManager := config.ConfigManager{
768768
DefaultConfig: config.Config{},
769769
ConfigMap: make(map[string]config.Config),
@@ -847,7 +847,7 @@ func DoScan(actions ScannerActions, r reporter.Reporter) (models.VulnerabilityRe
847847

848848
overrideGoVersion(r, filteredScannedPackages, &configManager)
849849

850-
vulnsResp, err := makeRequest(r, filteredScannedPackages, actions.CompareLocally, actions.CompareOffline, actions.LocalDBPath)
850+
vulnsResp, err := makeRequest(r, filteredScannedPackages, actions.CompareOffline, actions.DownloadDatabases, actions.LocalDBPath)
851851
if err != nil {
852852
return models.VulnerabilityResults{}, err
853853
}
@@ -950,8 +950,8 @@ func patchPackageForRequest(pkg scannedPackage) scannedPackage {
950950
func makeRequest(
951951
r reporter.Reporter,
952952
packages []scannedPackage,
953-
compareLocally bool,
954953
compareOffline bool,
954+
downloadDBs bool,
955955
localDBPath string) (*osv.HydratedBatchedResponse, error) {
956956
// Make OSV queries from the packages.
957957
var query osv.BatchedQuery
@@ -974,8 +974,9 @@ func makeRequest(
974974
}
975975
}
976976

977-
if compareLocally {
978-
hydratedResp, err := local.MakeRequest(r, query, compareOffline, localDBPath)
977+
if compareOffline {
978+
// Downloading databases requires network access.
979+
hydratedResp, err := local.MakeRequest(r, query, !downloadDBs, localDBPath)
979980
if err != nil {
980981
return &osv.HydratedBatchedResponse{}, fmt.Errorf("local comparison failed %w", err)
981982
}

0 commit comments

Comments
 (0)