grype/cmd/root.go

106 lines
3.1 KiB
Go
Raw Normal View History

2020-05-26 14:37:28 +00:00
package cmd
import (
"fmt"
"os"
"github.com/anchore/imgbom/imgbom"
2020-06-02 21:21:29 +00:00
_distro "github.com/anchore/imgbom/imgbom/distro"
2020-05-26 17:31:50 +00:00
"github.com/anchore/imgbom/imgbom/scope"
2020-05-26 14:37:28 +00:00
"github.com/anchore/stereoscope"
"github.com/anchore/vulnscan/internal"
"github.com/anchore/vulnscan/internal/db"
2020-05-26 14:37:28 +00:00
"github.com/anchore/vulnscan/internal/format"
2020-05-28 22:28:29 +00:00
"github.com/anchore/vulnscan/vulnscan"
"github.com/anchore/vulnscan/vulnscan/presenter"
"github.com/anchore/vulnscan/vulnscan/vulnerability"
2020-05-26 14:37:28 +00:00
"github.com/spf13/cobra"
2020-05-26 17:31:50 +00:00
"github.com/spf13/viper"
2020-05-26 14:37:28 +00:00
)
var rootCmd = &cobra.Command{
Use: fmt.Sprintf("%s [IMAGE]", internal.ApplicationName),
Short: "A vulnerability scanner tool", // TODO: add copy, add path-based scans
2020-05-26 17:31:50 +00:00
Long: format.Tprintf(`Supports the following image sources:
2020-05-26 14:37:28 +00:00
{{.appName}} yourrepo/yourimage:tag defaults to using images from a docker daemon
2020-05-26 17:31:50 +00:00
{{.appName}} docker://yourrepo/yourimage:tag explicitly use a docker daemon
2020-05-26 14:37:28 +00:00
{{.appName}} tar://path/to/yourimage.tar use a tarball from disk
`, map[string]interface{}{
"appName": internal.ApplicationName,
}),
Args: cobra.MaximumNArgs(1),
2020-05-26 17:31:50 +00:00
Run: func(cmd *cobra.Command, args []string) {
os.Exit(runDefaultCmd(cmd, args))
},
2020-05-26 14:37:28 +00:00
}
func init() {
2020-05-26 17:31:50 +00:00
// setup CLI options specific to scanning an image
2020-05-26 14:37:28 +00:00
2020-05-26 17:31:50 +00:00
// scan options
flag := "scope"
rootCmd.Flags().StringP(
"scope", "s", scope.AllLayersScope.String(),
fmt.Sprintf("selection of layers to analyze, options=%v", scope.Options))
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
2020-05-26 14:37:28 +00:00
os.Exit(1)
}
2020-05-26 17:31:50 +00:00
// output & formatting options
flag = "output"
rootCmd.Flags().StringP(
flag, "o", "json",
fmt.Sprintf("report output formatter, options=%v", presenter.Options),
2020-05-26 17:31:50 +00:00
)
if err := viper.BindPFlag(flag, rootCmd.Flags().Lookup(flag)); err != nil {
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
os.Exit(1)
}
2020-05-26 14:37:28 +00:00
}
2020-06-02 21:21:29 +00:00
func runDefaultCmd(_ *cobra.Command, args []string) int {
2020-05-26 14:37:28 +00:00
userImageStr := args[0]
log.Infof("Fetching image '%s'", userImageStr)
img, err := stereoscope.GetImage(userImageStr)
if err != nil {
log.Errorf("could not fetch image '%s': %w", userImageStr, err)
return 1
}
defer stereoscope.Cleanup()
log.Info("Cataloging image")
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
if err != nil {
log.Errorf("could not catalog image: %w", err)
return 1
}
osObj := _distro.Identify(img)
if osObj == nil {
// prevent moving forward with unknown distros for now, revisit later
log.Error("unable to detect distro type for accurate vulnerability matching")
return 1
}
2020-05-28 22:28:29 +00:00
store := db.GetStore()
provider := vulnerability.NewProviderFromStore(store)
results := vulnscan.FindAllVulnerabilities(provider, *osObj, catalog)
outputOption := viper.GetString("output")
presenterType := presenter.ParseOption(outputOption)
if presenterType == presenter.UnknownPresenter {
log.Errorf("cannot find an output presenter for option: %s", outputOption)
return 1
}
err = presenter.GetPresenter(presenterType).Present(os.Stdout, catalog, results)
if err != nil {
log.Errorf("could not format catalog results: %w", err)
return 1
}
2020-05-26 14:37:28 +00:00
return 0
}