From 342f5743e25c00c7794f21cd463ec89d604d5824 Mon Sep 17 00:00:00 2001 From: Moshe-Rappaport-CA Date: Wed, 26 Oct 2022 16:29:55 +0300 Subject: [PATCH] Fix when running with include or exclude namespace scanning only namespaced scope --- cmd/scan/scan.go | 2 +- core/pkg/resourcehandler/fieldselector.go | 22 ++++++++++++++++++++++ core/pkg/resourcehandler/k8sresources.go | 10 +++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/cmd/scan/scan.go b/cmd/scan/scan.go index 75121bb9..adc5236c 100644 --- a/cmd/scan/scan.go +++ b/cmd/scan/scan.go @@ -71,7 +71,7 @@ func GetScanCommand(ks meta.IKubescape) *cobra.Command { scanCmd.PersistentFlags().StringVar(&scanInfo.ControlsInputs, "controls-config", "", "Path to an controls-config obj. If not set will download controls-config from ARMO management portal") scanCmd.PersistentFlags().StringVar(&scanInfo.UseExceptions, "exceptions", "", "Path to an exceptions obj. If not set will download exceptions from ARMO management portal") scanCmd.PersistentFlags().StringVar(&scanInfo.UseArtifactsFrom, "use-artifacts-from", "", "Load artifacts from local directory. If not used will download them") - scanCmd.PersistentFlags().StringVarP(&scanInfo.ExcludedNamespaces, "exclude-namespaces", "e", "", "Namespaces to exclude from scanning. Recommended: kube-system,kube-public") + scanCmd.PersistentFlags().StringVarP(&scanInfo.ExcludedNamespaces, "exclude-namespaces", "e", "", "Namespaces to exclude from scanning. Notice, when running with `exclude-namespace` kubescape does not scan cluster-scoped objects.") scanCmd.PersistentFlags().Float32VarP(&scanInfo.FailThreshold, "fail-threshold", "t", 100, "Failure threshold is the percent above which the command fails and returns exit code 1") diff --git a/core/pkg/resourcehandler/fieldselector.go b/core/pkg/resourcehandler/fieldselector.go index 269e9f1b..cd2cda90 100644 --- a/core/pkg/resourcehandler/fieldselector.go +++ b/core/pkg/resourcehandler/fieldselector.go @@ -10,6 +10,7 @@ import ( type IFieldSelector interface { GetNamespacesSelectors(*schema.GroupVersionResource) []string + GetClusterScope(*schema.GroupVersionResource) bool } type EmptySelector struct { @@ -19,6 +20,10 @@ func (es *EmptySelector) GetNamespacesSelectors(resource *schema.GroupVersionRes return []string{""} // } +func (es *EmptySelector) GetClusterScope(*schema.GroupVersionResource) bool { + return true +} + type ExcludeSelector struct { namespace string } @@ -27,6 +32,14 @@ func NewExcludeSelector(ns string) *ExcludeSelector { return &ExcludeSelector{namespace: ns} } +func (es *ExcludeSelector) GetClusterScope(resource *schema.GroupVersionResource) bool { + // for selector, 'namespace' is in Namespaced scope + if resource.Resource == "namespaces" { + return true + } + return false +} + type IncludeSelector struct { namespace string } @@ -34,6 +47,15 @@ type IncludeSelector struct { func NewIncludeSelector(ns string) *IncludeSelector { return &IncludeSelector{namespace: ns} } + +func (is *IncludeSelector) GetClusterScope(resource *schema.GroupVersionResource) bool { + // for selector, 'namespace' is in Namespaced scope + if resource.Resource == "namespaces" { + return true + } + return false +} + func (es *ExcludeSelector) GetNamespacesSelectors(resource *schema.GroupVersionResource) []string { fieldSelectors := "" for _, n := range strings.Split(es.namespace, ",") { diff --git a/core/pkg/resourcehandler/k8sresources.go b/core/pkg/resourcehandler/k8sresources.go index 70e4f9a7..846a43f3 100644 --- a/core/pkg/resourcehandler/k8sresources.go +++ b/core/pkg/resourcehandler/k8sresources.go @@ -230,10 +230,14 @@ func (k8sHandler *K8sResourceHandler) pullSingleResource(resource *schema.GroupV // set dynamic object var clientResource dynamic.ResourceInterface - if namespace != "" && k8sinterface.IsNamespaceScope(resource) { - clientResource = k8sHandler.k8s.DynamicClient.Resource(*resource).Namespace(namespace) - } else { + if namespace != "" { clientResource = k8sHandler.k8s.DynamicClient.Resource(*resource) + } else if k8sinterface.IsNamespaceScope(resource) { + clientResource = k8sHandler.k8s.DynamicClient.Resource(*resource).Namespace(namespace) + } else if k8sHandler.fieldSelector.GetClusterScope(*&resource) { + clientResource = k8sHandler.k8s.DynamicClient.Resource(*resource) + } else { + continue } // list resources