fix error handling

This commit is contained in:
danielgrunbergerarmo
2021-08-19 13:42:23 +03:00
parent 35449e3d4e
commit 297a4fc42b
5 changed files with 21 additions and 27 deletions
+14 -9
View File
@@ -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
+1 -1
View File
@@ -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 {
+2 -3
View File
@@ -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:
+1 -1
View File
@@ -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()
+3 -13
View File
@@ -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())
}
}