From d1e02dc298084c80c94c3119a9c303b5cc299e10 Mon Sep 17 00:00:00 2001 From: dwertent Date: Wed, 2 Feb 2022 14:24:53 +0200 Subject: [PATCH] update config command --- build.py | 2 +- cautils/customerloader.go | 4 +- clihandler/cmd/cluster.go | 7 +- clihandler/cmd/cluster_get.go | 7 +- clihandler/cmd/cluster_set.go | 7 +- clihandler/cmd/config.go | 116 ++++++++++++++++++++++++++++++++-- clihandler/cmd/delete.go | 34 ---------- clihandler/cmd/local.go | 7 +- clihandler/cmd/local_get.go | 7 +- clihandler/cmd/local_set.go | 7 +- clihandler/cmd/set.go | 53 ---------------- clihandler/cmd/view.go | 36 ----------- clihandler/initcli.go | 17 +---- go.mod | 2 +- go.sum | 4 +- registryadaptors/README.md | 9 ++- 16 files changed, 151 insertions(+), 168 deletions(-) delete mode 100644 clihandler/cmd/delete.go delete mode 100644 clihandler/cmd/set.go delete mode 100644 clihandler/cmd/view.go diff --git a/build.py b/build.py index ec8a5497..9db7d938 100644 --- a/build.py +++ b/build.py @@ -37,7 +37,7 @@ def main(): print("Building Kubescape") # print environment variables - print(os.environ) + # print(os.environ) # Set some variables packageName = getPackageName() diff --git a/cautils/customerloader.go b/cautils/customerloader.go index bb4ebc0c..d2263a2c 100644 --- a/cautils/customerloader.go +++ b/cautils/customerloader.go @@ -24,12 +24,12 @@ func ConfigFileFullPath() string { return getter.GetDefaultPath(configFileName + type ConfigObj struct { AccountID string `json:"accountID,omitempty"` + ClientID string `json:"clientID,omitempty"` + AccessKey string `json:"accessKey,omitempty"` CustomerGUID string `json:"customerGUID,omitempty"` // Deprecated Token string `json:"invitationParam,omitempty"` CustomerAdminEMail string `json:"adminMail,omitempty"` ClusterName string `json:"clusterName,omitempty"` - ClientID string `json:"clientID,omitempty"` - AccessKey string `json:"accessKey,omitempty"` } // Config - convert ConfigObj to config file diff --git a/clihandler/cmd/cluster.go b/clihandler/cmd/cluster.go index e81ec7d0..30f75f25 100644 --- a/clihandler/cmd/cluster.go +++ b/clihandler/cmd/cluster.go @@ -6,9 +6,10 @@ import ( // clusterCmd represents the cluster command var clusterCmd = &cobra.Command{ - Use: "cluster", - Short: "Set configuration for cluster", - Long: ``, + Use: "cluster", + Short: "Set configuration for cluster", + Long: ``, + Deprecated: "use the 'set' command instead", Run: func(cmd *cobra.Command, args []string) { }, } diff --git a/clihandler/cmd/cluster_get.go b/clihandler/cmd/cluster_get.go index a56ea117..45cab878 100644 --- a/clihandler/cmd/cluster_get.go +++ b/clihandler/cmd/cluster_get.go @@ -11,9 +11,10 @@ import ( ) var getCmd = &cobra.Command{ - Use: "get ", - Short: "Get configuration in cluster", - Long: ``, + Use: "get ", + Short: "Get configuration in cluster", + Long: ``, + Deprecated: "use the 'view' command instead", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 || len(args) > 1 { return fmt.Errorf("requires one argument") diff --git a/clihandler/cmd/cluster_set.go b/clihandler/cmd/cluster_set.go index 5103c7ea..4858e0bb 100644 --- a/clihandler/cmd/cluster_set.go +++ b/clihandler/cmd/cluster_set.go @@ -11,9 +11,10 @@ import ( ) var setClusterCmd = &cobra.Command{ - Use: "set =", - Short: "Set configuration in cluster", - Long: ``, + Use: "set =", + Short: "Set configuration in cluster", + Long: ``, + Deprecated: "use the 'set' command instead", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 || len(args) > 1 { return fmt.Errorf("requires one argument: =") diff --git a/clihandler/cmd/config.go b/clihandler/cmd/config.go index cedcd969..f11f1954 100644 --- a/clihandler/cmd/config.go +++ b/clihandler/cmd/config.go @@ -1,19 +1,127 @@ package cmd import ( + "fmt" + "os" + "strings" + + "github.com/armosec/kubescape/clihandler" + "github.com/armosec/kubescape/clihandler/cliobjects" "github.com/spf13/cobra" ) +var ( + configExample = ` + # View cached configurations + kubescape config view + + # Delete cached configurations + kubescape config delete + + # Set cached configurations + kubescape config set --help +` + setConfigExample = ` + # Set account id + kubescape config set accountID + + # Set client id + kubescape config set clientID + + # Set access key + kubescape config set accessKey +` +) + // configCmd represents the config command var configCmd = &cobra.Command{ - Use: "config", - Short: "Set configuration", - Long: ``, - Deprecated: "use the 'set' command instead", + Use: "config", + Short: "handle cached configurations", + Example: configExample, +} + +var setConfig = cliobjects.SetConfig{} + +// configCmd represents the config command +var configSetCmd = &cobra.Command{ + Use: "set", + Short: fmt.Sprintf("Set configurations, supported: %s", strings.Join(stringKeysToSlice(supportConfigSet), "/")), + Example: setConfigExample, + ValidArgs: stringKeysToSlice(supportConfigSet), + RunE: func(cmd *cobra.Command, args []string) error { + if err := parseSetArgs(args); err != nil { + return err + } + if err := clihandler.CliSetConfig(&setConfig); err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } + return nil + }, +} + +var supportConfigSet = map[string]func(*cliobjects.SetConfig, string){ + "accountID": func(s *cliobjects.SetConfig, account string) { s.Account = account }, + "clientID": func(s *cliobjects.SetConfig, clientID string) { s.ClientID = clientID }, + "accessKey": func(s *cliobjects.SetConfig, accessKey string) { s.AccessKey = accessKey }, +} + +func stringKeysToSlice(m map[string]func(*cliobjects.SetConfig, string)) []string { + l := []string{} + for i := range m { + l = append(l, i) + } + return l +} + +func parseSetArgs(args []string) error { + var key string + var value string + if len(args) == 1 { + if keyValue := strings.Split(args[0], "="); len(keyValue) == 2 { + key = keyValue[0] + value = keyValue[1] + } + } else if len(args) == 2 { + key = args[0] + value = args[1] + } + if setConfigFunc, ok := supportConfigSet[key]; ok { + setConfigFunc(&setConfig, value) + } else { + return fmt.Errorf("key '%s' unknown . supported: %s", key, strings.Join(stringKeysToSlice(supportConfigSet), "/")) + } + return nil +} + +var configDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete cached configurations", + Long: ``, Run: func(cmd *cobra.Command, args []string) { + if err := clihandler.CliDelete(); err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } + }, +} + +// configCmd represents the config command +var configViewCmd = &cobra.Command{ + Use: "view", + Short: "View cached configurations", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + if err := clihandler.CliView(); err != nil { + fmt.Fprintf(os.Stderr, "error: %v\n", err) + os.Exit(1) + } }, } func init() { rootCmd.AddCommand(configCmd) + configCmd.AddCommand(configSetCmd) + configCmd.AddCommand(configDeleteCmd) + configCmd.AddCommand(configViewCmd) } diff --git a/clihandler/cmd/delete.go b/clihandler/cmd/delete.go deleted file mode 100644 index 5caf85e2..00000000 --- a/clihandler/cmd/delete.go +++ /dev/null @@ -1,34 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "github.com/armosec/kubescape/clihandler" - "github.com/spf13/cobra" -) - -var deleteCmd = &cobra.Command{ - Use: "delete", - Short: "Delete cached configurations and other data", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - }, -} - -var deleteConfigCmd = &cobra.Command{ - Use: "config", - Short: "Delete cached configurations", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - if err := clihandler.CliDelete(); err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - os.Exit(1) - } - }, -} - -func init() { - rootCmd.AddCommand(deleteCmd) - deleteCmd.AddCommand(deleteConfigCmd) -} diff --git a/clihandler/cmd/local.go b/clihandler/cmd/local.go index 75e7b680..224de282 100644 --- a/clihandler/cmd/local.go +++ b/clihandler/cmd/local.go @@ -5,9 +5,10 @@ import ( ) var localCmd = &cobra.Command{ - Use: "local", - Short: "Set configuration locally (for config.json)", - Long: ``, + Use: "local", + Short: "Set configuration locally (for config.json)", + Long: ``, + Deprecated: "use the 'set' command instead", Run: func(cmd *cobra.Command, args []string) { }, } diff --git a/clihandler/cmd/local_get.go b/clihandler/cmd/local_get.go index 9807e794..d3bde11b 100644 --- a/clihandler/cmd/local_get.go +++ b/clihandler/cmd/local_get.go @@ -9,9 +9,10 @@ import ( ) var localGetCmd = &cobra.Command{ - Use: "get ", - Short: "Get configuration locally", - Long: ``, + Use: "get ", + Short: "Get configuration locally", + Long: ``, + Deprecated: "use the 'view' command instead", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 || len(args) > 1 { return fmt.Errorf("requires one argument") diff --git a/clihandler/cmd/local_set.go b/clihandler/cmd/local_set.go index e5e8c7c4..0b2cc89a 100644 --- a/clihandler/cmd/local_set.go +++ b/clihandler/cmd/local_set.go @@ -9,9 +9,10 @@ import ( ) var localSetCmd = &cobra.Command{ - Use: "set =", - Short: "Set configuration locally", - Long: ``, + Use: "set =", + Short: "Set configuration locally", + Long: ``, + Deprecated: "use the 'set' command instead", Args: func(cmd *cobra.Command, args []string) error { if len(args) < 1 || len(args) > 1 { return fmt.Errorf("requires one argument: =") diff --git a/clihandler/cmd/set.go b/clihandler/cmd/set.go deleted file mode 100644 index a54d4d2c..00000000 --- a/clihandler/cmd/set.go +++ /dev/null @@ -1,53 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "github.com/armosec/kubescape/clihandler" - "github.com/armosec/kubescape/clihandler/cliobjects" - "github.com/spf13/cobra" -) - -var ( - setConfigExample = ` - # Set account credentials - kubescape set config --account --client-id --access-key -` -) -var setConfig = cliobjects.SetConfig{} - -// configCmd represents the config command -var setCmd = &cobra.Command{ - Use: "set", - Short: "Set configurations and other data", - Long: ``, - Example: setConfigExample, - Run: func(cmd *cobra.Command, args []string) { - }, -} - -// configCmd represents the config command -var setConfigCmd = &cobra.Command{ - Use: "config", - Short: "Set cached configurations", - Long: ``, - Example: setConfigExample, - Run: func(cmd *cobra.Command, args []string) { - if err := clihandler.CliSetConfig(&setConfig); err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - os.Exit(1) - } - }, -} - -func init() { - - setConfigCmd.PersistentFlags().StringVarP(&setConfig.Account, "account", "", "", "Set Armo account ID") - setConfigCmd.PersistentFlags().StringVarP(&setConfig.ClientID, "client-id", "", "", "Set Armo client ID") - setConfigCmd.PersistentFlags().StringVarP(&setConfig.AccessKey, "access-key", "", "", "Set Armo access key") - - rootCmd.AddCommand(setCmd) - setCmd.AddCommand(setConfigCmd) - -} diff --git a/clihandler/cmd/view.go b/clihandler/cmd/view.go deleted file mode 100644 index 77cbdf2c..00000000 --- a/clihandler/cmd/view.go +++ /dev/null @@ -1,36 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "github.com/armosec/kubescape/clihandler" - "github.com/spf13/cobra" -) - -// configCmd represents the config command -var viewCmd = &cobra.Command{ - Use: "view", - Short: "View configurations and other data", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - }, -} - -// configCmd represents the config command -var viewConfigCmd = &cobra.Command{ - Use: "config", - Short: "View cached configurations", - Long: ``, - Run: func(cmd *cobra.Command, args []string) { - if err := clihandler.CliView(); err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - os.Exit(1) - } - }, -} - -func init() { - rootCmd.AddCommand(viewCmd) - viewCmd.AddCommand(viewConfigCmd) -} diff --git a/clihandler/initcli.go b/clihandler/initcli.go index 81d7ddf5..c3ce60c7 100644 --- a/clihandler/initcli.go +++ b/clihandler/initcli.go @@ -11,7 +11,6 @@ import ( // printerv2 "github.com/armosec/kubescape/resultshandling/printer/v2" - "github.com/armosec/armoapi-go/armotypes" "github.com/armosec/kubescape/cautils" "github.com/armosec/kubescape/cautils/getter" "github.com/armosec/kubescape/clihandler/cliinterfaces" @@ -158,19 +157,9 @@ func ScanCliSetup(scanInfo *cautils.ScanInfo) error { } func Scan(policyHandler *policyhandler.PolicyHandler, scanInfo *cautils.ScanInfo) error { - policyNotification := &reporthandling.PolicyNotification{ - NotificationType: reporthandling.TypeExecPostureScan, - Rules: scanInfo.PolicyIdentifier, - Designators: armotypes.PortalDesignator{}, - } - switch policyNotification.NotificationType { - case reporthandling.TypeExecPostureScan: - if err := policyHandler.HandleNotificationRequest(policyNotification, scanInfo); err != nil { - return err - } - - default: - return fmt.Errorf("notification type '%s' Unknown", policyNotification.NotificationType) + policyNotification := &reporthandling.PolicyNotification{Rules: scanInfo.PolicyIdentifier} + if err := policyHandler.HandleNotificationRequest(policyNotification, scanInfo); err != nil { + return err } return nil } diff --git a/go.mod b/go.mod index 9c71297f..d226c90b 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.17 require ( github.com/armosec/armoapi-go v0.0.41 github.com/armosec/k8s-interface v0.0.56 - github.com/armosec/opa-utils v0.0.99 + github.com/armosec/opa-utils v0.0.105 github.com/armosec/rbac-utils v0.0.12 github.com/armosec/utils-go v0.0.3 github.com/armosec/utils-k8s-go v0.0.1 diff --git a/go.sum b/go.sum index c2c78393..0a6a0e4f 100644 --- a/go.sum +++ b/go.sum @@ -95,8 +95,8 @@ github.com/armosec/k8s-interface v0.0.50/go.mod h1:vHxGWqD/uh6+GQb9Sqv7OGMs+Rvc2 github.com/armosec/k8s-interface v0.0.56 h1:7dOgc3qZaI7ReLRZcJa2JZKk0rliyYi05l1vuHc6gcE= github.com/armosec/k8s-interface v0.0.56/go.mod h1:vHxGWqD/uh6+GQb9Sqv7OGMs+Rvc2dsFVc0XtgRh1ZU= github.com/armosec/opa-utils v0.0.64/go.mod h1:6tQP8UDq2EvEfSqh8vrUdr/9QVSCG4sJfju1SXQOn4c= -github.com/armosec/opa-utils v0.0.99 h1:ZuoIPg6vbgO4J09xJZDO/yIRD59odwmK2Bm55uTvkU8= -github.com/armosec/opa-utils v0.0.99/go.mod h1:BNTjeianyXlflJMz3bZM0GimBWqmzirUf1whWR6Os04= +github.com/armosec/opa-utils v0.0.105 h1:HQdP2LpFJtBoJpsfygh4IDXLLukIkQEuETOUePF/Zas= +github.com/armosec/opa-utils v0.0.105/go.mod h1:BNTjeianyXlflJMz3bZM0GimBWqmzirUf1whWR6Os04= github.com/armosec/rbac-utils v0.0.1/go.mod h1:pQ8CBiij8kSKV7aeZm9FMvtZN28VgA7LZcYyTWimq40= github.com/armosec/rbac-utils v0.0.12 h1:uJpMGDyLAX129PrKHp6NPNB6lVRhE0OZIwV6ywzSDrs= github.com/armosec/rbac-utils v0.0.12/go.mod h1:Ex/IdGWhGv9HZq6Hs8N/ApzCKSIvpNe/ETqDfnuyah0= diff --git a/registryadaptors/README.md b/registryadaptors/README.md index e57330dc..ebb90565 100644 --- a/registryadaptors/README.md +++ b/registryadaptors/README.md @@ -11,11 +11,14 @@ For these controls to work properly, it is necasery to 2. Click Profile(top right icon)->"User Management"->"API Tokens" and Generate a token 3. Copy the clientID and accessKey and run: ``` -kubescape set config --access-key <> --client-id <> +kubescape config set clientID <> +``` +``` +kubescape config set accessKey <> ``` 4. Confirm the keys are set ``` -kubescape view config +kubescape config view ``` Expecting: ``` @@ -25,7 +28,7 @@ Expecting: "accessKey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" } ``` -> If you are missing the `accountID` field, set it by running `kubescape set config --account <>` +> If you are missing the `accountID` field, set it by running `kubescape config set accountID <>` For CICD, set environments variables as following: ```