Skip to content

Commit cbcf174

Browse files
authored
Simplify v6 distribution material (#2277)
* adjust enumerations Signed-off-by: Alex Goodman <[email protected]> * simplify distribution artifacts and plumb to import Signed-off-by: Alex Goodman <[email protected]> * make assigner plural Signed-off-by: Alex Goodman <[email protected]> * fix tests Signed-off-by: Alex Goodman <[email protected]> * incorporate review comments Signed-off-by: Alex Goodman <[email protected]> * remove advisory reference tag Signed-off-by: Alex Goodman <[email protected]> --------- Signed-off-by: Alex Goodman <[email protected]>
1 parent 4c95018 commit cbcf174

26 files changed

+322
-296
lines changed

Taskfile.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ tasks:
5757
- task: check-licenses
5858
- task: lint
5959
- task: validate-cyclonedx-schema
60-
- task: validate-grype-db-schema
60+
# TODO: while developing v6, we need to disable this check (since v5 and v6 are imported in the same codebase)
61+
# - task: validate-grype-db-schema
6162

6263
test:
6364
desc: Run all levels of test

cmd/grype/cli/commands/db_check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func DBCheck(app clio.Application) *cobra.Command {
3434
}
3535

3636
func runDBCheck(opts options.Database) error {
37-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
37+
dbCurator, err := distribution.NewCurator(opts.ToLegacyCuratorConfig())
3838
if err != nil {
3939
return err
4040
}

cmd/grype/cli/commands/db_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func DBDelete(app clio.Application) *cobra.Command {
2525
}
2626

2727
func runDBDelete(opts options.Database) error {
28-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
28+
dbCurator, err := distribution.NewCurator(opts.ToLegacyCuratorConfig())
2929
if err != nil {
3030
return err
3131
}

cmd/grype/cli/commands/db_diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func DBDiff(app clio.Application) *cobra.Command {
6565
}
6666

6767
func runDBDiff(opts *dbDiffOptions, base string, target string) (errs error) {
68-
d, err := differ.NewDiffer(opts.DB.ToCuratorConfig())
68+
d, err := differ.NewDiffer(opts.DB.ToLegacyCuratorConfig())
6969
if err != nil {
7070
return err
7171
}
@@ -104,7 +104,7 @@ func runDBDiff(opts *dbDiffOptions, base string, target string) (errs error) {
104104
}
105105

106106
func getDefaultURLs(opts options.Database) (baseURL string, targetURL string, err error) {
107-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
107+
dbCurator, err := distribution.NewCurator(opts.ToLegacyCuratorConfig())
108108
if err != nil {
109109
return "", "", err
110110
}

cmd/grype/cli/commands/db_import.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package commands
22

33
import (
44
"fmt"
5+
"path/filepath"
6+
"strings"
57

68
"github.com/spf13/cobra"
79

810
"github.com/anchore/clio"
911
"github.com/anchore/grype/cmd/grype/cli/options"
10-
"github.com/anchore/grype/grype/db/legacy/distribution"
12+
legacyDistribution "github.com/anchore/grype/grype/db/legacy/distribution"
13+
"github.com/anchore/grype/grype/db/v6/distribution"
14+
"github.com/anchore/grype/grype/db/v6/installation"
1115
"github.com/anchore/grype/internal"
1216
)
1317

@@ -27,14 +31,44 @@ func DBImport(app clio.Application) *cobra.Command {
2731
}
2832

2933
func runDBImport(opts options.Database, dbArchivePath string) error {
30-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
34+
// TODO: tui update? better logging?
35+
36+
// TODO: we will only support v6 after development is complete
37+
if isV6DB(dbArchivePath) {
38+
return importDB(opts, dbArchivePath)
39+
}
40+
return legacyDBImport(opts, dbArchivePath)
41+
}
42+
43+
func importDB(opts options.Database, dbArchivePath string) error {
44+
client, err := distribution.NewClient(opts.ToClientConfig())
45+
if err != nil {
46+
return fmt.Errorf("unable to create distribution client: %w", err)
47+
}
48+
c, err := installation.NewCurator(opts.ToCuratorConfig(), client)
49+
if err != nil {
50+
return fmt.Errorf("unable to create curator: %w", err)
51+
}
52+
53+
if err := c.Import(dbArchivePath); err != nil {
54+
return fmt.Errorf("unable to import vulnerability database: %w", err)
55+
}
56+
return stderrPrintLnf("Vulnerability database imported")
57+
}
58+
59+
func legacyDBImport(opts options.Database, dbArchivePath string) error {
60+
dbCurator, err := legacyDistribution.NewCurator(opts.ToLegacyCuratorConfig())
3161
if err != nil {
3262
return err
3363
}
3464

3565
if err := dbCurator.ImportFrom(dbArchivePath); err != nil {
36-
return fmt.Errorf("unable to import vulnerability database: %+v", err)
66+
return fmt.Errorf("unable to import vulnerability database: %w", err)
3767
}
3868

3969
return stderrPrintLnf("Vulnerability database imported")
4070
}
71+
72+
func isV6DB(path string) bool {
73+
return strings.Contains(filepath.Base(path), "vulnerability-db_v6")
74+
}

cmd/grype/cli/commands/db_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func DBList(app clio.Application) *cobra.Command {
4040
}
4141

4242
func runDBList(opts *dbListOptions) error {
43-
dbCurator, err := distribution.NewCurator(opts.DB.ToCuratorConfig())
43+
dbCurator, err := distribution.NewCurator(opts.DB.ToLegacyCuratorConfig())
4444
if err != nil {
4545
return err
4646
}

cmd/grype/cli/commands/db_providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func runDBProviders(opts *dbProvidersOptions, app clio.Application) error {
8383
}
8484

8585
func getMetadataFileLocation(app clio.Application) (*string, error) {
86-
dbCurator, err := distribution.NewCurator(dbOptionsDefault(app.ID()).DB.ToCuratorConfig())
86+
dbCurator, err := distribution.NewCurator(dbOptionsDefault(app.ID()).DB.ToLegacyCuratorConfig())
8787
if err != nil {
8888
return nil, err
8989
}

cmd/grype/cli/commands/db_search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func DBSearch(app clio.Application) *cobra.Command {
4646

4747
func runDBSearch(opts *dbQueryOptions, vulnerabilityID string) error {
4848
log.Debug("loading DB")
49-
str, status, dbCloser, err := grype.LoadVulnerabilityDB(opts.DB.ToCuratorConfig(), opts.DB.AutoUpdate)
49+
str, status, dbCloser, err := grype.LoadVulnerabilityDB(opts.DB.ToLegacyCuratorConfig(), opts.DB.AutoUpdate)
5050
err = validateDBLoad(err, status)
5151
if err != nil {
5252
return err

cmd/grype/cli/commands/db_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func DBStatus(app clio.Application) *cobra.Command {
2525
}
2626

2727
func runDBStatus(opts options.Database) error {
28-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
28+
dbCurator, err := distribution.NewCurator(opts.ToLegacyCuratorConfig())
2929
if err != nil {
3030
return err
3131
}

cmd/grype/cli/commands/db_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func DBUpdate(app clio.Application) *cobra.Command {
3131
}
3232

3333
func runDBUpdate(opts options.Database) error {
34-
dbCurator, err := distribution.NewCurator(opts.ToCuratorConfig())
34+
dbCurator, err := distribution.NewCurator(opts.ToLegacyCuratorConfig())
3535
if err != nil {
3636
return err
3737
}

0 commit comments

Comments
 (0)