2022-10-10 07:05:55 -05:00
|
|
|
//go:build search
|
|
|
|
// +build search
|
2020-10-14 16:47:20 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-01-19 10:57:10 -05:00
|
|
|
"encoding/json"
|
2020-07-06 17:44:32 -05:00
|
|
|
"fmt"
|
2022-01-19 10:57:10 -05:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2020-07-06 17:44:32 -05:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/briandowns/spinner"
|
|
|
|
"github.com/spf13/cobra"
|
2022-01-19 10:57:10 -05:00
|
|
|
"gopkg.in/resty.v1"
|
2022-10-20 11:39:20 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
zotErrors "zotregistry.io/zot/errors"
|
2022-01-19 10:57:10 -05:00
|
|
|
"zotregistry.io/zot/pkg/api/constants"
|
2020-07-06 17:44:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewCveCommand(searchService SearchService) *cobra.Command {
|
|
|
|
searchCveParams := make(map[string]*string)
|
|
|
|
|
|
|
|
var servURL, user, outputFormat string
|
|
|
|
|
2022-09-23 11:24:01 -05:00
|
|
|
var isSpinner, verifyTLS, fixedFlag, verbose, debug bool
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
cveCmd := &cobra.Command{
|
2020-07-06 17:44:32 -05:00
|
|
|
Use: "cve [config-name]",
|
2022-01-10 20:15:35 -05:00
|
|
|
Short: "Lookup CVEs in images hosted on the zot registry",
|
|
|
|
Long: `List CVEs (Common Vulnerabilities and Exposures) of images hosted on the zot registry`,
|
2020-07-06 17:44:32 -05:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
configPath := path.Join(home + "/.zot")
|
|
|
|
if servURL == "" {
|
|
|
|
if len(args) > 0 {
|
|
|
|
urlFromConfig, err := getConfigValue(configPath, args[0], "url")
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
if urlFromConfig == "" {
|
|
|
|
return zotErrors.ErrNoURLProvided
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
servURL = urlFromConfig
|
|
|
|
} else {
|
|
|
|
return zotErrors.ErrNoURLProvided
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) > 0 {
|
|
|
|
var err error
|
|
|
|
isSpinner, err = parseBooleanConfig(configPath, args[0], showspinnerConfig)
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return err
|
|
|
|
}
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
verifyTLS, err = parseBooleanConfig(configPath, args[0], verifyTLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spin := spinner.New(spinner.CharSets[39], spinnerDuration, spinner.WithWriter(cmd.ErrOrStderr()))
|
2022-01-19 10:57:10 -05:00
|
|
|
spin.Prefix = fmt.Sprintf("Fetching from %s..", servURL)
|
|
|
|
spin.Suffix = "\n\b"
|
2020-07-06 17:44:32 -05:00
|
|
|
|
2021-05-28 11:27:17 -05:00
|
|
|
verbose = false
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
searchConfig := searchConfig{
|
|
|
|
params: searchCveParams,
|
|
|
|
searchService: searchService,
|
|
|
|
servURL: &servURL,
|
|
|
|
user: &user,
|
|
|
|
outputFormat: &outputFormat,
|
|
|
|
fixedFlag: &fixedFlag,
|
|
|
|
verifyTLS: &verifyTLS,
|
2021-05-28 11:27:17 -05:00
|
|
|
verbose: &verbose,
|
2022-09-23 11:24:01 -05:00
|
|
|
debug: &debug,
|
2020-07-06 17:44:32 -05:00
|
|
|
resultWriter: cmd.OutOrStdout(),
|
|
|
|
spinner: spinnerState{spin, isSpinner},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = searchCve(searchConfig)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
cmd.SilenceUsage = true
|
2021-12-13 14:23:31 -05:00
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := cveFlagVariables{
|
|
|
|
searchCveParams: searchCveParams,
|
|
|
|
servURL: &servURL,
|
|
|
|
user: &user,
|
|
|
|
outputFormat: &outputFormat,
|
|
|
|
fixedFlag: &fixedFlag,
|
2022-09-23 11:24:01 -05:00
|
|
|
debug: &debug,
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
setupCveFlags(cveCmd, vars)
|
|
|
|
|
|
|
|
return cveCmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupCveFlags(cveCmd *cobra.Command, variables cveFlagVariables) {
|
|
|
|
variables.searchCveParams["imageName"] = cveCmd.Flags().StringP("image", "I", "", "List CVEs by IMAGENAME[:TAG]")
|
2022-01-19 10:57:10 -05:00
|
|
|
variables.searchCveParams["cveID"] = cveCmd.Flags().StringP("cve-id", "i", "", "List images affected by a CVE")
|
2020-07-06 17:44:32 -05:00
|
|
|
|
|
|
|
cveCmd.Flags().StringVar(variables.servURL, "url", "", "Specify zot server URL if config-name is not mentioned")
|
|
|
|
cveCmd.Flags().StringVarP(variables.user, "user", "u", "", `User Credentials of `+
|
|
|
|
`zot server in USERNAME:PASSWORD format`)
|
|
|
|
cveCmd.Flags().StringVarP(variables.outputFormat, "output", "o", "", "Specify output format [text/json/yaml]."+
|
|
|
|
" JSON and YAML format return all info for CVEs")
|
|
|
|
|
|
|
|
cveCmd.Flags().BoolVar(variables.fixedFlag, "fixed", false, "List tags which have fixed a CVE")
|
2022-09-23 11:24:01 -05:00
|
|
|
cveCmd.Flags().BoolVar(variables.debug, "debug", false, "Show debug output")
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type cveFlagVariables struct {
|
|
|
|
searchCveParams map[string]*string
|
|
|
|
servURL *string
|
|
|
|
user *string
|
|
|
|
outputFormat *string
|
|
|
|
fixedFlag *bool
|
2022-09-23 11:24:01 -05:00
|
|
|
debug *bool
|
2020-07-06 17:44:32 -05:00
|
|
|
}
|
|
|
|
|
2022-01-19 10:57:10 -05:00
|
|
|
type field struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type schemaList struct {
|
|
|
|
Data struct {
|
|
|
|
Schema struct {
|
|
|
|
QueryType struct {
|
|
|
|
Fields []field `json:"fields"`
|
|
|
|
} `json:"queryType"` //nolint:tagliatelle // graphQL schema
|
|
|
|
} `json:"__schema"` //nolint:tagliatelle // graphQL schema
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsGQLQuery(queryList []field, query string) bool {
|
|
|
|
for _, q := range queryList {
|
|
|
|
if q.Name == query {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkExtEndPoint(serverURL string) bool {
|
|
|
|
client := resty.New()
|
|
|
|
|
|
|
|
extEndPoint, err := combineServerAndEndpointURL(serverURL, fmt.Sprintf("%s%s",
|
|
|
|
constants.RoutePrefix, constants.ExtOciDiscoverPrefix))
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-05 05:21:14 -05:00
|
|
|
//nolint: gosec
|
2022-01-19 10:57:10 -05:00
|
|
|
resp, err := client.R().Get(extEndPoint)
|
|
|
|
if err != nil || resp.StatusCode() != http.StatusOK {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-10-18 22:46:06 -05:00
|
|
|
searchEndPoint, _ := combineServerAndEndpointURL(serverURL, constants.FullSearchPrefix)
|
2022-01-19 10:57:10 -05:00
|
|
|
|
|
|
|
query := `
|
|
|
|
{
|
|
|
|
__schema() {
|
|
|
|
queryType {
|
|
|
|
fields {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
resp, err = client.R().Get(searchEndPoint + "?query=" + url.QueryEscape(query))
|
|
|
|
if err != nil || resp.StatusCode() != http.StatusOK {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
queryList := &schemaList{}
|
|
|
|
|
|
|
|
_ = json.Unmarshal(resp.Body(), queryList)
|
|
|
|
|
|
|
|
return containsGQLQuery(queryList.Data.Schema.QueryType.Fields, "ImageList")
|
|
|
|
}
|
|
|
|
|
2020-07-06 17:44:32 -05:00
|
|
|
func searchCve(searchConfig searchConfig) error {
|
2022-01-19 10:57:10 -05:00
|
|
|
var searchers []searcher
|
|
|
|
|
|
|
|
if checkExtEndPoint(*searchConfig.servURL) {
|
|
|
|
searchers = getCveSearchersGQL()
|
|
|
|
} else {
|
|
|
|
searchers = getCveSearchers()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, searcher := range searchers {
|
2020-07-06 17:44:32 -05:00
|
|
|
found, err := searcher.search(searchConfig)
|
|
|
|
if found {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return zotErrors.ErrInvalidFlagsCombination
|
|
|
|
}
|