Skip to content

Commit 5be7f01

Browse files
committed
feat: support new smart db
1 parent 8d4bea2 commit 5be7f01

File tree

5 files changed

+801
-4
lines changed

5 files changed

+801
-4
lines changed

main.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@ func makeAPIDBConfig() database.Config {
3333
}
3434
}
3535

36-
func makeEcosystemDBConfig(ecosystem internal.Ecosystem) database.Config {
36+
func makeEcosystemDBConfig(ecosystem internal.Ecosystem, beSmart bool) database.Config {
37+
typ := "zip"
38+
if beSmart {
39+
typ = "smart"
40+
}
41+
3742
return database.Config{
3843
Name: string(ecosystem),
39-
Type: "zip",
44+
Type: typ,
4045
URL: fmt.Sprintf("https://osv-vulnerabilities.storage.googleapis.com/%s/all.zip", ecosystem),
4146
}
4247
}
@@ -158,6 +163,15 @@ func describeDB(db database.DB) string {
158163
switch tt := db.(type) {
159164
case *database.APIDB:
160165
return "using batches of " + color.YellowString("%d", tt.BatchSize)
166+
case *database.SmartDB:
167+
count := tt.VulnerabilitiesCount
168+
169+
return fmt.Sprintf(
170+
"%s %s, including withdrawn - last updated %s",
171+
color.YellowString("%d", count),
172+
reporter.Form(count, "vulnerability", "vulnerabilities"),
173+
tt.UpdatedAt,
174+
)
161175
case *database.ZipDB:
162176
count := tt.VulnerabilitiesCount
163177

@@ -372,6 +386,7 @@ func (files lockfileAndConfigOrErrs) adjustExtraDatabases(
372386
removeConfigDatabases bool,
373387
addDefaultAPIDatabase bool,
374388
addEcosystemDatabases bool,
389+
beSmart bool,
375390
) {
376391
for _, file := range files {
377392
if file.err != nil {
@@ -391,7 +406,7 @@ func (files lockfileAndConfigOrErrs) adjustExtraDatabases(
391406
ecosystems := collectEcosystems([]lockfileAndConfigOrErr{file})
392407

393408
for _, ecosystem := range ecosystems {
394-
extraDBConfigs = append(extraDBConfigs, makeEcosystemDBConfig(ecosystem))
409+
extraDBConfigs = append(extraDBConfigs, makeEcosystemDBConfig(ecosystem, beSmart))
395410
}
396411
}
397412

@@ -508,6 +523,7 @@ func run(args []string, stdout, stderr io.Writer) int {
508523
useDatabases := cli.Bool("use-dbs", true, "Use the databases from osv.dev to check for known vulnerabilities")
509524
useAPI := cli.Bool("use-api", false, "Use the osv.dev API to check for known vulnerabilities")
510525
batchSize := cli.Int("batch-size", 1000, "The number of packages to include in each batch when using the api database")
526+
beSmart := cli.Bool("be-smart", false, "Use smart database mode for faster incremental updates")
511527

512528
cli.Var(&globalIgnores, "ignore", `ID of an OSV to ignore when determining exit codes.
513529
This flag can be passed multiple times to ignore different vulnerabilities`)
@@ -589,7 +605,7 @@ This flag can be passed multiple times to ignore different vulnerabilities`)
589605

590606
files := readAllLockfiles(r, pathsToLocksWithParseAs, cli.Args(), loadLocalConfig, &config)
591607

592-
files.adjustExtraDatabases(*noConfigDatabases, *useAPI, *useDatabases)
608+
files.adjustExtraDatabases(*noConfigDatabases, *useAPI, *useDatabases, *beSmart)
593609

594610
dbs, errored := loadDatabases(
595611
r,

pkg/database/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var ErrUnsupportedDatabaseType = errors.New("unsupported database source type")
2929
// Load initializes a new OSV database based on the given Config
3030
func Load(config Config, offline bool, batchSize int) (DB, error) {
3131
switch config.Type {
32+
case "smart":
33+
return NewSmartDB(config, offline)
3234
case "zip":
3335
return NewZippedDB(config, offline)
3436
case "api":

0 commit comments

Comments
 (0)