From df035ea5fc5b3fafc90dd80b34b1f91670a3dd4f Mon Sep 17 00:00:00 2001 From: Daniel Grunberger <84905812+Daniel-GrunbergerCA@users.noreply.github.com> Date: Sun, 3 Sep 2023 17:22:14 +0300 Subject: [PATCH] Fix scan command (#1369) * bump version Signed-off-by: Daniel Grunberger * bump version for httphandler Signed-off-by: Daniel Grunberger * fix args validation Signed-off-by: Daniel Grunberger * errors as const Signed-off-by: Daniel Grunberger --------- Signed-off-by: Daniel Grunberger Co-authored-by: Daniel Grunberger --- cmd/scan/framework.go | 18 +++++++++++++----- cmd/scan/scan.go | 13 ++----------- cmd/scan/validators_test.go | 10 ++++++++++ 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/cmd/scan/framework.go b/cmd/scan/framework.go index 780c5634..b6704730 100644 --- a/cmd/scan/framework.go +++ b/cmd/scan/framework.go @@ -42,7 +42,11 @@ var ( Run '%[1]s list frameworks' for the list of supported frameworks `, cautils.ExecName()) - ErrUnknownSeverity = errors.New("unknown severity") + ErrUnknownSeverity = errors.New("unknown severity") + ErrSecurityViewNotSupported = errors.New("security view is not supported for framework scan") + ErrBadThreshold = errors.New("bad argument: out of range threshold") + ErrKeepLocalOrSubmit = errors.New("you can use `keep-local` or `submit`, but not both") + ErrOmitRawResourcesOrSubmit = errors.New("you can use `omit-raw-resources` or `submit`, but not both") ) func getFrameworkCmd(ks meta.IKubescape, scanInfo *cautils.ScanInfo) *cobra.Command { @@ -208,17 +212,21 @@ func validateSeverity(severity string) error { // validateFrameworkScanInfo validates the scan info struct for the `scan framework` command func validateFrameworkScanInfo(scanInfo *cautils.ScanInfo) error { + if scanInfo.View == string(cautils.SecurityViewType) { + return ErrSecurityViewNotSupported + } + if scanInfo.Submit && scanInfo.Local { - return fmt.Errorf("you can use `keep-local` or `submit`, but not both") + return ErrKeepLocalOrSubmit } if 100 < scanInfo.ComplianceThreshold || 0 > scanInfo.ComplianceThreshold { - return fmt.Errorf("bad argument: out of range threshold") + return ErrBadThreshold } if 100 < scanInfo.FailThreshold || 0 > scanInfo.FailThreshold { - return fmt.Errorf("bad argument: out of range threshold") + return ErrBadThreshold } if scanInfo.Submit && scanInfo.OmitRawResources { - return fmt.Errorf("you can use `omit-raw-resources` or `submit`, but not both") + return ErrOmitRawResourcesOrSubmit } severity := scanInfo.FailThresholdSeverity if err := validateSeverity(severity); severity != "" && err != nil { diff --git a/cmd/scan/scan.go b/cmd/scan/scan.go index d9a6a73a..a849ca0e 100644 --- a/cmd/scan/scan.go +++ b/cmd/scan/scan.go @@ -41,15 +41,6 @@ func GetScanCommand(ks meta.IKubescape) *cobra.Command { Short: "Scan a Kubernetes cluster or YAML files for image vulnerabilities and misconfigurations", Long: `The action you want to perform`, Example: scanCmdExamples, - Args: func(cmd *cobra.Command, args []string) error { - // setting input patterns for framework scan is only relevancy for non-security view - if len(args) > 0 && scanInfo.View != string(cautils.SecurityViewType) { - if args[0] != "framework" && args[0] != "control" { - return getFrameworkCmd(ks, &scanInfo).RunE(cmd, append([]string{strings.Join(getter.NativeFrameworks, ",")}, args...)) - } - } - return nil - }, RunE: func(cmd *cobra.Command, args []string) error { if scanInfo.View == string(cautils.SecurityViewType) { setSecurityViewScanInfo(args, &scanInfo) @@ -57,8 +48,8 @@ func GetScanCommand(ks meta.IKubescape) *cobra.Command { return securityScan(scanInfo, ks) } - if len(args) == 0 { - return getFrameworkCmd(ks, &scanInfo).RunE(cmd, []string{strings.Join(getter.NativeFrameworks, ",")}) + if len(args) == 0 || (args[0] != "framework" && args[0] != "control") { + return getFrameworkCmd(ks, &scanInfo).RunE(cmd, append([]string{strings.Join(getter.NativeFrameworks, ",")}, args...)) } return nil }, diff --git a/cmd/scan/validators_test.go b/cmd/scan/validators_test.go index 8ea31f8e..f3f5315c 100644 --- a/cmd/scan/validators_test.go +++ b/cmd/scan/validators_test.go @@ -68,6 +68,16 @@ func Test_validateFrameworkScanInfo(t *testing.T) { &cautils.ScanInfo{FailThresholdSeverity: "Unknown"}, ErrUnknownSeverity, }, + { + "Security view should be invalid for scan info", + &cautils.ScanInfo{View: string(cautils.SecurityViewType)}, + ErrSecurityViewNotSupported, + }, + { + "Empty view should be valid for scan info", + &cautils.ScanInfo{}, + nil, + }, } for _, tc := range testCases {