Skip to content

Commit

Permalink
update config command
Browse files Browse the repository at this point in the history
  • Loading branch information
dwertent committed Feb 2, 2022
1 parent 6981403 commit d1e02dc
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 168 deletions.
2 changes: 1 addition & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def main():
print("Building Kubescape")

# print environment variables
print(os.environ)
# print(os.environ)

# Set some variables
packageName = getPackageName()
Expand Down
4 changes: 2 additions & 2 deletions cautils/customerloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions clihandler/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
},
}
Expand Down
7 changes: 4 additions & 3 deletions clihandler/cmd/cluster_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var getCmd = &cobra.Command{
Use: "get <key>",
Short: "Get configuration in cluster",
Long: ``,
Use: "get <key>",
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")
Expand Down
7 changes: 4 additions & 3 deletions clihandler/cmd/cluster_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

var setClusterCmd = &cobra.Command{
Use: "set <key>=<value>",
Short: "Set configuration in cluster",
Long: ``,
Use: "set <key>=<value>",
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: <key>=<value>")
Expand Down
116 changes: 112 additions & 4 deletions clihandler/cmd/config.go
Original file line number Diff line number Diff line change
@@ -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 <account id>
# Set client id
kubescape config set clientID <client id>
# Set access key
kubescape config set accessKey <access key>
`
)

// 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)
}
34 changes: 0 additions & 34 deletions clihandler/cmd/delete.go

This file was deleted.

7 changes: 4 additions & 3 deletions clihandler/cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
},
}
Expand Down
7 changes: 4 additions & 3 deletions clihandler/cmd/local_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var localGetCmd = &cobra.Command{
Use: "get <key>",
Short: "Get configuration locally",
Long: ``,
Use: "get <key>",
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")
Expand Down
7 changes: 4 additions & 3 deletions clihandler/cmd/local_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
)

var localSetCmd = &cobra.Command{
Use: "set <key>=<value>",
Short: "Set configuration locally",
Long: ``,
Use: "set <key>=<value>",
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: <key>=<value>")
Expand Down
53 changes: 0 additions & 53 deletions clihandler/cmd/set.go

This file was deleted.

36 changes: 0 additions & 36 deletions clihandler/cmd/view.go

This file was deleted.

17 changes: 3 additions & 14 deletions clihandler/initcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit d1e02dc

Please sign in to comment.