2020-05-26 14:37:28 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-07-16 19:12:19 +00:00
|
|
|
"runtime/pprof"
|
2020-05-26 14:37:28 +00:00
|
|
|
|
|
|
|
"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/vulnscan/internal"
|
|
|
|
"github.com/anchore/vulnscan/internal/format"
|
2020-05-28 22:28:29 +00:00
|
|
|
"github.com/anchore/vulnscan/vulnscan"
|
2020-06-19 14:12:29 +00:00
|
|
|
"github.com/anchore/vulnscan/vulnscan/db"
|
2020-06-18 14:12:23 +00:00
|
|
|
"github.com/anchore/vulnscan/vulnscan/presenter"
|
2020-06-01 11:21:07 +00:00
|
|
|
"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),
|
2020-06-18 14:12:23 +00:00
|
|
|
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) {
|
2020-07-16 19:12:19 +00:00
|
|
|
if appConfig.Dev.ProfileCPU {
|
|
|
|
f, err := os.Create("cpu.profile")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to create CPU profile: %+v", err)
|
|
|
|
} else {
|
|
|
|
err := pprof.StartCPUProfile(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to start CPU profile: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exitCode := runDefaultCmd(cmd, args)
|
|
|
|
|
|
|
|
if appConfig.Dev.ProfileCPU {
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
2020-05-26 17:31:50 +00:00
|
|
|
},
|
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(
|
2020-06-18 14:12:23 +00:00
|
|
|
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-07-15 11:17:21 +00:00
|
|
|
// nolint:funlen
|
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]
|
2020-07-15 17:24:55 +00:00
|
|
|
scope, cleanup, err := imgbom.NewScope(userImageStr, appConfig.ScopeOpt)
|
2020-05-26 14:37:28 +00:00
|
|
|
if err != nil {
|
2020-07-15 17:24:55 +00:00
|
|
|
log.Errorf("could not produce catalog: %w", err)
|
2020-05-26 14:37:28 +00:00
|
|
|
return 1
|
|
|
|
}
|
2020-07-15 17:24:55 +00:00
|
|
|
defer cleanup()
|
2020-05-26 14:37:28 +00:00
|
|
|
|
2020-07-15 17:24:55 +00:00
|
|
|
log.Info("creating catalog")
|
|
|
|
catalog, err := imgbom.Catalog(scope)
|
2020-05-26 14:37:28 +00:00
|
|
|
if err != nil {
|
2020-07-15 17:24:55 +00:00
|
|
|
log.Errorf("could not produce catalog: %w", err)
|
2020-05-26 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 17:24:55 +00:00
|
|
|
osObj := _distro.Identify(scope)
|
2020-05-28 22:28:29 +00:00
|
|
|
|
2020-06-19 14:12:29 +00:00
|
|
|
dbCurator, err := db.NewCurator(appConfig.Db.ToCuratorConfig())
|
|
|
|
if err != nil {
|
2020-07-16 18:59:35 +00:00
|
|
|
log.Errorf("could not curate database: %+v", err)
|
2020-06-19 14:12:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if appConfig.Db.UpdateOnStartup {
|
|
|
|
updateAvailable, updateEntry, err := dbCurator.IsUpdateAvailable()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: should this be so fatal? we can certainly continue with a warning...
|
|
|
|
log.Errorf("unable to check for vulnerability database update: %+v", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if updateAvailable {
|
|
|
|
err = dbCurator.UpdateTo(updateEntry)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("unable to update vulnerability database: %+v", err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
store, err := dbCurator.GetStore()
|
|
|
|
if err != nil {
|
2020-07-16 18:59:35 +00:00
|
|
|
log.Errorf("failed to load vulnerability database: %+v", err)
|
2020-06-19 14:12:29 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:21:07 +00:00
|
|
|
provider := vulnerability.NewProviderFromStore(store)
|
|
|
|
|
2020-06-18 19:54:58 +00:00
|
|
|
results := vulnscan.FindAllVulnerabilities(provider, *osObj, catalog)
|
2020-07-15 17:24:55 +00:00
|
|
|
|
2020-06-18 14:12:23 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-01 11:21:07 +00:00
|
|
|
|
2020-06-18 14:12:23 +00:00
|
|
|
err = presenter.GetPresenter(presenterType).Present(os.Stdout, catalog, results)
|
|
|
|
if err != nil {
|
2020-07-16 18:59:35 +00:00
|
|
|
log.Errorf("could not format catalog results: %+v", err)
|
2020-06-18 14:12:23 +00:00
|
|
|
return 1
|
2020-06-01 11:21:07 +00:00
|
|
|
}
|
2020-05-26 14:37:28 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|