-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alex Goodman <[email protected]>
- Loading branch information
Showing
7 changed files
with
128 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,129 @@ | ||
package commands | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/olekukonko/tablewriter" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/anchore/clio" | ||
"github.com/anchore/grype/grype" | ||
"github.com/anchore/grype/grype/vulnerability" | ||
"github.com/anchore/grype/internal/bus" | ||
"github.com/anchore/grype/internal/log" | ||
) | ||
|
||
var _ clio.FlagAdder = (*dbQueryOptions)(nil) | ||
|
||
type dbQueryOptions struct { | ||
Output string `yaml:"output" json:"output" mapstructure:"output"` | ||
DBOptions `yaml:",inline" mapstructure:",squash"` | ||
} | ||
|
||
func (c *dbQueryOptions) AddFlags(flags clio.FlagSet) { | ||
flags.StringVarP(&c.Output, "output", "o", "format to display results (available=[table, json])") | ||
} | ||
|
||
func DBSearch(app clio.Application) *cobra.Command { | ||
opts := &dbQueryOptions{ | ||
Output: tableOutputFormat, | ||
DBOptions: *dbOptionsDefault(app.ID()), | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "search", | ||
Short: "search the DB for vulnerabilities or affected packages", | ||
// this is here to support v5 functionality today but will be removed when v6 is the default DB version | ||
Use: "search ID...", | ||
Short: "get information on vulnerabilities from the db", | ||
//Use: "search", | ||
//Short: "search the DB for vulnerabilities or affected packages", | ||
PreRunE: disableUI(app), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if opts.Experimental.DBv6 { | ||
if len(args) > 0 { | ||
// looks like the user attempted to use the search command as if it's v5 -- let them know about the new commands instead | ||
return errors.New("this command is only supported for schema DB v5, please use `grype db search pkg` or `grype db search vuln` for schema DB v6+") | ||
} | ||
// running without args should only show help, not as a runtime error | ||
return cmd.Usage() | ||
} | ||
|
||
// this is v5, do arg handling here. Why not do this earlier in the struct Args field? When v6 functionality is | ||
// enabled we want this command to show usage and exit, so we need to do this check later in processing (here). | ||
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil { | ||
return err | ||
} | ||
return legacyDBSearchPackages(*opts, args) | ||
}, | ||
} | ||
|
||
cmd.AddCommand( | ||
DBSearchPackages(app), | ||
DBSearchVulnerabilities(app), | ||
) | ||
|
||
return cmd | ||
return app.SetupCommand(cmd, opts) | ||
} | ||
|
||
func legacyDBSearchPackages(opts dbQueryOptions, vulnerabilityIDs []string) error { | ||
log.Debug("loading DB") | ||
str, status, err := grype.LoadVulnerabilityDB(opts.DB.ToLegacyCuratorConfig(), opts.DB.AutoUpdate) | ||
err = validateDBLoad(err, status) | ||
if err != nil { | ||
return err | ||
} | ||
defer log.CloseAndLogError(str, status.Location) | ||
|
||
var vulnerabilities []vulnerability.Vulnerability | ||
for _, vulnerabilityID := range vulnerabilityIDs { | ||
vulns, err := str.Get(vulnerabilityID, "") | ||
if err != nil { | ||
return fmt.Errorf("unable to get vulnerability %q: %w", vulnerabilityID, err) | ||
} | ||
vulnerabilities = append(vulnerabilities, vulns...) | ||
} | ||
|
||
if len(vulnerabilities) == 0 { | ||
return errors.New("no affected packages found") | ||
} | ||
|
||
sb := &strings.Builder{} | ||
err = presentLegacyDBSearchPackages(opts.Output, vulnerabilities, sb) | ||
bus.Report(sb.String()) | ||
|
||
return err | ||
} | ||
|
||
func presentLegacyDBSearchPackages(outputFormat string, vulnerabilities []vulnerability.Vulnerability, output io.Writer) error { | ||
if vulnerabilities == nil { | ||
return nil | ||
} | ||
|
||
switch outputFormat { | ||
case tableOutputFormat: | ||
rows := [][]string{} | ||
for _, v := range vulnerabilities { | ||
rows = append(rows, []string{v.ID, v.PackageName, v.Namespace, v.Constraint.String()}) | ||
} | ||
|
||
table := tablewriter.NewWriter(output) | ||
commonTableWriterOptions(table) | ||
|
||
table.SetHeader([]string{"ID", "Package Name", "Namespace", "Version Constraint"}) | ||
table.AppendBulk(rows) | ||
table.Render() | ||
case jsonOutputFormat: | ||
enc := json.NewEncoder(output) | ||
enc.SetEscapeHTML(false) | ||
enc.SetIndent("", " ") | ||
if err := enc.Encode(vulnerabilities); err != nil { | ||
return fmt.Errorf("failed to encode diff information: %+v", err) | ||
} | ||
default: | ||
return fmt.Errorf("unsupported output format: %s", outputFormat) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ type DBSearchOSs struct { | |
} | ||
|
||
func (o *DBSearchOSs) AddFlags(flags clio.FlagSet) { | ||
flags.StringArrayVarP(&o.OSs, "distro", "", "refine to results with the given operating system (format: 'name', 'name@version', '[email protected]', 'name@codename') (for v6+ schemas only)") // consistent with grype flag today | ||
// consistent with grype --distro flag today | ||
flags.StringArrayVarP(&o.OSs, "distro", "", "refine to results with the given operating system (format: 'name', 'name@version', '[email protected]', 'name@codename')") | ||
} | ||
|
||
func (o *DBSearchOSs) PostLoad() error { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters