From 297a4fc42bc1eec3ed0542d4dd79ac634628d44e Mon Sep 17 00:00:00 2001 From: danielgrunbergerarmo Date: Thu, 19 Aug 2021 13:42:23 +0300 Subject: [PATCH] fix error handling --- cautils/k8sinterface/k8sconfig.go | 23 ++++++++++++++--------- cautils/opapolicy/datastructures.go | 2 +- inputhandler/clihandler/clihandler.go | 5 ++--- main.go | 2 +- policyhandler/handlepullpolicies.go | 16 +++------------- 5 files changed, 21 insertions(+), 27 deletions(-) diff --git a/cautils/k8sinterface/k8sconfig.go b/cautils/k8sinterface/k8sconfig.go index 5ac1ef9d..15007bdb 100644 --- a/cautils/k8sinterface/k8sconfig.go +++ b/cautils/k8sinterface/k8sconfig.go @@ -27,13 +27,16 @@ type KubernetesApi struct { // NewKubernetesApi - func NewKubernetesApi() *KubernetesApi { + kubernetesClient, err := kubernetes.NewForConfig(GetK8sConfig()) if err != nil { - panic(fmt.Sprintf("kubernetes.NewForConfig - Failed to load config file, reason: %s", err.Error())) + fmt.Printf("Failed to load config file, reason: %s", err.Error()) + os.Exit(1) } - dynamicClient, err := dynamic.NewForConfig(GetK8sConfig()) + dynamicClient, err := dynamic.NewForConfig(K8SConfig) if err != nil { - panic(fmt.Sprintf("dynamic.NewForConfig - Failed to load config file, reason: %s", err.Error())) + fmt.Printf("Failed to load config file, reason: %s", err.Error()) + os.Exit(1) } return &KubernetesApi{ @@ -50,11 +53,11 @@ var RunningIncluster bool func LoadK8sConfig() error { kubeconfig, err := clientcmd.BuildConfigFromFlags("", ConfigPath) if err != nil { - kubeconfig, err = restclient.InClusterConfig() - if err != nil { - return fmt.Errorf("Failed to load kubernetes config from file: '%s', err: %v", ConfigPath, err) - } - RunningIncluster = true + // kubeconfig, err = restclient.InClusterConfig() + // if err != nil { + return fmt.Errorf("Failed to load kubernetes config from file: '%s'.\n", ConfigPath) + // } + // RunningIncluster = true } else { RunningIncluster = false } @@ -66,7 +69,9 @@ func LoadK8sConfig() error { func GetK8sConfig() *restclient.Config { if K8SConfig == nil { if err := LoadK8sConfig(); err != nil { - return nil + // print error + fmt.Printf("%s", err.Error()) + os.Exit(1) } } return K8SConfig diff --git a/cautils/opapolicy/datastructures.go b/cautils/opapolicy/datastructures.go index 3c508f9f..81cd7f08 100644 --- a/cautils/opapolicy/datastructures.go +++ b/cautils/opapolicy/datastructures.go @@ -28,7 +28,7 @@ type RuleResponse struct { type AlertObject struct { K8SApiObjects []map[string]interface{} `json:"k8sApiObjects,omitempty"` - ExternalObjects []map[string]interface{} `json:"externalObjects,omitempty"` + ExternalObjects map[string]interface{} `json:"externalObjects,omitempty"` } type FrameworkReport struct { diff --git a/inputhandler/clihandler/clihandler.go b/inputhandler/clihandler/clihandler.go index 9a6d6bb6..c1fe128a 100644 --- a/inputhandler/clihandler/clihandler.go +++ b/inputhandler/clihandler/clihandler.go @@ -8,8 +8,6 @@ import ( "kube-escape/cautils/armotypes" "kube-escape/cautils/opapolicy" - - "github.com/golang/glog" ) type CLIHandler struct { @@ -43,7 +41,8 @@ func (clihandler *CLIHandler) Scan() error { case opapolicy.TypeExecPostureScan: go func() { if err := clihandler.policyHandler.HandleNotificationRequest(policyNotification); err != nil { - glog.Error(err) + fmt.Printf("%v\n", err) + os.Exit(0) } }() default: diff --git a/main.go b/main.go index 5944ff1c..cc354ede 100644 --- a/main.go +++ b/main.go @@ -24,6 +24,7 @@ func main() { func CliSetup() error { k8s := k8sinterface.NewKubernetesApi() + processNotification := make(chan *cautils.OPASessionObj) reportResults := make(chan *cautils.OPASessionObj) @@ -42,7 +43,6 @@ func CliSetup() error { reporterObj := opaprocessor.NewOPAProcessor(&processNotification, &reportResults) reporterObj.ProcessRulesListenner() }() - p := printer.NewPrinter(&reportResults) p.ActionPrint() diff --git a/policyhandler/handlepullpolicies.go b/policyhandler/handlepullpolicies.go index 93d0ceb9..3d2ae0b2 100644 --- a/policyhandler/handlepullpolicies.go +++ b/policyhandler/handlepullpolicies.go @@ -138,23 +138,13 @@ func (policyHandler *PolicyHandler) GetPoliciesFromBackend(notification *opapoli // backend receivedFrameworks, err := d.OPAFRAMEWORKGet(rule.Name) if err != nil { - errs = fmt.Errorf("%v\nKind: %v, Name: %s, error: %s", errs, rule.Kind, rule.Name, err.Error()) + errs = fmt.Errorf("Could not download framework, please check if this framework exists") } frameworks = append(frameworks, receivedFrameworks...) - case opapolicy.KindControl: - receivedControls := []opapolicy.Control{} //, err := policyHandler.cacli.OPAFRAMEWORKGet(rule.Name, !k8sinterface.RunningIncluster) - // receivedControls, err := policyHandler.cacli.OPACONTROLGet(rule.Name) - // if err != nil { - // errs = fmt.Errorf("%v\nKind: %v, Name: %s, error: %s", errs, rule.Kind, rule.Name, err.Error()) - // } - framework := opapolicy.Framework{ // TODO - wrap control by framework properly - Controls: receivedControls, - } - frameworks = append(frameworks, framework) default: - err := fmt.Errorf("missing rule kind, expected: %s", opapolicy.KindFramework) - errs = fmt.Errorf("%v\nerror: %s", errs, err.Error()) + err := fmt.Errorf("Missing rule kind, expected: %s", opapolicy.KindFramework) + errs = fmt.Errorf("%s", err.Error()) } }