From 62d37261ea5c5e9e63cb86fa18302ceb3da1533f Mon Sep 17 00:00:00 2001 From: jt-dd <112463504+jt-dd@users.noreply.github.com> Date: Wed, 11 Sep 2024 15:56:15 +0200 Subject: [PATCH] adding config command (#244) * adding config command * PR comment * fix typo --- cmd/kubehound/config.go | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 cmd/kubehound/config.go diff --git a/cmd/kubehound/config.go b/cmd/kubehound/config.go new file mode 100644 index 00000000..42b77069 --- /dev/null +++ b/cmd/kubehound/config.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "os" + + "github.com/DataDog/KubeHound/pkg/cmd" + "github.com/DataDog/KubeHound/pkg/telemetry/log" + "github.com/spf13/cobra" + "gopkg.in/yaml.v2" +) + +var ( + configPath string +) + +var ( + configCmd = &cobra.Command{ + Use: "config", + Short: "Show the current configuration", + Long: `[devOnly] Show the current configuration`, + PreRunE: func(cobraCmd *cobra.Command, args []string) error { + return cmd.InitializeKubehoundConfig(cobraCmd.Context(), "", true, true) + }, + RunE: func(cobraCmd *cobra.Command, args []string) error { + // Adding datadog setup + khCfg, err := cmd.GetConfig() + if err != nil { + return fmt.Errorf("get config: %w", err) + } + + yamlData, err := yaml.Marshal(&khCfg) + + if err != nil { + return fmt.Errorf("marshaling khCfg: %w", err) + } + + if configPath != "" { + f, err := os.Create(configPath) + if err != nil { + return fmt.Errorf("creating file: %w", err) + } + + _, err = f.Write(yamlData) + if err != nil { + return fmt.Errorf("writing to file: %w", err) + } + + log.I.Infof("Configuration saved to %s\n", configPath) + + return nil + } + + fmt.Println("---") //nolint:forbidigo + fmt.Println(string(yamlData)) //nolint:forbidigo + + return nil + }, + } +) + +func init() { + configCmd.Flags().StringVar(&configPath, "path", "", "path to dump current KubeHound configuration") + + rootCmd.AddCommand(configCmd) +}