Skip to content

Commit

Permalink
tmp: started new interface
Browse files Browse the repository at this point in the history
  • Loading branch information
FalcoSuessgott committed Jun 3, 2024
1 parent fa1f05f commit 9ceea0d
Show file tree
Hide file tree
Showing 42 changed files with 1,262 additions and 2,548 deletions.
251 changes: 117 additions & 134 deletions cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package cmd

import (
"errors"
"fmt"
"log"
"path"
"strings"

prt "github.com/FalcoSuessgott/vkv/pkg/printer/secret"
prt "github.com/FalcoSuessgott/vkv/pkg/printer"
"github.com/FalcoSuessgott/vkv/pkg/utils"
"github.com/FalcoSuessgott/vkv/pkg/vault"
"github.com/k0kubun/pp/v3"
"github.com/spf13/cobra"
)

Expand All @@ -33,9 +31,7 @@ type exportOptions struct {
TemplateFile string `env:"TEMPLATE_FILE"`
TemplateString string `env:"TEMPLATE_STRING"`

FormatString string `env:"FORMAT" envDefault:"base"`

outputFormat prt.OutputFormat
FormatString string `env:"FORMAT" envDefault:"default"`
}

// NewExportCmd export subcommand.
Expand All @@ -53,46 +49,39 @@ func NewExportCmd() *cobra.Command {
Short: "recursively list secrets from Vaults KV2 engine in various formats",
SilenceUsage: true,
SilenceErrors: true,
PreRunE: o.validateFlags,
// PreRunE: o.validateFlags,
RunE: func(cmd *cobra.Command, args []string) error {
enginePath, _ := utils.HandleEnginePath(o.EnginePath, o.Path)

printer = prt.NewSecretPrinter(
prt.OnlyKeys(o.OnlyKeys),
prt.OnlyPaths(o.OnlyPaths),
prt.CustomValueLength(o.MaxValueLength),
prt.ShowValues(o.ShowValues),
prt.WithTemplate(o.TemplateString, o.TemplateFile),
prt.ToFormat(o.outputFormat),
prt.WithVaultClient(vaultClient),
prt.WithWriter(writer),
prt.ShowVersion(o.ShowVersion),
prt.ShowMetadata(o.ShowMetadata),
prt.WithHyperLinks(o.WithHyperLink),
prt.WithEnginePath(enginePath),
)

rootPath, subPath := utils.HandleEnginePath(o.EnginePath, o.Path)

engine, err := vault.NewEngine(vaultClient, rootPath)
kv, err := vault.NewKVSecrets(vaultClient, rootPath, subPath, o.SkipErrors, o.AllVersions)
if err != nil {
return err
}

err = engine.ListRecursive2(rootPath, subPath, false, o.AllVersions)
if err != nil {
return err
}
opts := prt.DefaultPrinterOptions()
opts.Format = o.FormatString

secrets := make(map[string]interface{})
// pp.Println(kv.Secrets)
pp.Println("")

for path, secret := range engine.Secrets {
m := utils.PathMap2(path, secret, true)
// example for masking secrets
// s := func(i interface{}) error {
// for _, secrets := range engine.Secrets {
// for _, secret := range secrets {
// if secret != nil {
// for k := range secret.Data {
// secret.Data[k] = "***"
// }
// }
// }
// }

secrets = utils.DeepMergeMaps(secrets, m)
}
// return nil
// }

printer.Out(secrets)
if err := prt.Print(kv.PrinterFuncs(), opts); err != nil {
return err
}

return nil
},
Expand All @@ -103,7 +92,7 @@ func NewExportCmd() *cobra.Command {
// Input
cmd.Flags().StringVarP(&o.Path, "path", "p", o.Path, fmt.Sprintf("KV Engine path (env: %s)", envVarExportPrefix+"_PATH"))
cmd.Flags().StringVarP(&o.EnginePath, "engine-path", "e", o.EnginePath, "engine path in case your KV-engine contains special characters such as \"/\", the path value will then be appended if specified (\"<engine-path>/<path>\") (env: VKV_EXPORT_ENGINE_PATH)")
cmd.Flags().BoolVar(&o.SkipErrors, "skip-errors", o.SkipErrors, "dont exit on errors (permission denied, deleted secrets) (env: VKV_EXPORT_SKIP_ERRORS)")
cmd.Flags().BoolVar(&o.SkipErrors, "skip-errors", o.SkipErrors, "don't exit on errors (permission denied, deleted secrets) (env: VKV_EXPORT_SKIP_ERRORS)")

cmd.Flags().BoolVarP(&o.AllVersions, "all-versions", "v", o.AllVersions, "prints out all secrets versions) (env: VKV_EXPORT_ALL_VERSIONS)")

Expand All @@ -130,100 +119,94 @@ func NewExportCmd() *cobra.Command {
return cmd
}

func prepMap(m map[string]interface{}, k string) map[string]interface{} {
m[k] = map[string]interface{}{}

return m
}

// nolint: cyclop, goconst
func (o *exportOptions) validateFlags(cmd *cobra.Command, args []string) error {
switch {
case (o.OnlyKeys && o.ShowValues), (o.OnlyPaths && o.ShowValues), (o.OnlyKeys && o.OnlyPaths):
return errInvalidFlagCombination
case o.EnginePath == "" && o.Path == "":
return errors.New("no KV-paths given. Either --engine-path / -e or --path / -p needs to be specified")
case o.EnginePath != "" && o.Path != "":
return errors.New("cannot specify both engine-path and path")
case true:
switch strings.ToLower(o.FormatString) {
case "yaml", "yml":
o.outputFormat = prt.YAML
o.OnlyKeys = false
o.OnlyPaths = false
o.MaxValueLength = -1
o.ShowValues = true
case "json":
o.outputFormat = prt.JSON
o.OnlyKeys = false
o.OnlyPaths = false
o.MaxValueLength = -1
o.ShowValues = true
case "export":
o.outputFormat = prt.Export
o.OnlyKeys = false
o.OnlyPaths = false
o.ShowValues = true
o.MaxValueLength = -1
case "markdown":
o.outputFormat = prt.Markdown
case "base":
o.outputFormat = prt.Base
case "policy":
o.outputFormat = prt.Policy
o.OnlyKeys = false
o.OnlyPaths = false
o.ShowValues = true
case "template", "tmpl":
o.outputFormat = prt.Template
o.OnlyKeys = false
o.OnlyPaths = false
o.MaxValueLength = -1

if o.TemplateFile != "" && o.TemplateString != "" {
return fmt.Errorf("%w: %s", errInvalidFlagCombination, "cannot specify both --template-file and --template-string")
}

if o.TemplateFile == "" && o.TemplateString == "" {
return fmt.Errorf("%w: %s", errInvalidFlagCombination, "either --template-file or --template-string is required")
}
default:
return prt.ErrInvalidFormat
}
}

return nil
}

func (o *exportOptions) buildMap() (map[string]interface{}, error) {
var isSecretPath bool

rootPath, subPath := utils.HandleEnginePath(o.EnginePath, o.Path)

// read recursive all secrets
s, err := vaultClient.ListRecursive(rootPath, subPath, o.SkipErrors)
if err != nil {
return nil, err
}

// check if path is a directory or secret path
if _, isSecret := vaultClient.ReadSecrets(rootPath, subPath); isSecret == nil {
isSecretPath = true
}

path := path.Join(rootPath, subPath)
if o.EnginePath != "" {
path = subPath
}

// prepare the output map
pathMap := utils.PathMap(path, utils.ToMapStringInterface(s), isSecretPath)

if o.EnginePath != "" {
return map[string]interface{}{
o.EnginePath: pathMap,
}, nil
}

return pathMap, nil
}
// // nolint: cyclop, goconst
// func (o *exportOptions) validateFlags(cmd *cobra.Command, args []string) error {
// switch {
// case (o.OnlyKeys && o.ShowValues), (o.OnlyPaths && o.ShowValues), (o.OnlyKeys && o.OnlyPaths):
// return errInvalidFlagCombination
// case o.EnginePath == "" && o.Path == "":
// return errors.New("no KV-paths given. Either --engine-path / -e or --path / -p needs to be specified")
// case o.EnginePath != "" && o.Path != "":
// return errors.New("cannot specify both engine-path and path")
// case true:
// switch strings.ToLower(o.FormatString) {
// case "yaml", "yml":
// o.outputFormat = prt.YAML
// o.OnlyKeys = false
// o.OnlyPaths = false
// o.MaxValueLength = -1
// o.ShowValues = true
// case "json":
// o.outputFormat = prt.JSON
// o.OnlyKeys = false
// o.OnlyPaths = false
// o.MaxValueLength = -1
// o.ShowValues = true
// case "export":
// o.outputFormat = prt.Export
// o.OnlyKeys = false
// o.OnlyPaths = false
// o.ShowValues = true
// o.MaxValueLength = -1
// case "markdown":
// o.outputFormat = prt.Markdown
// case "base":
// o.outputFormat = prt.Base
// case "policy":
// o.outputFormat = prt.Policy
// o.OnlyKeys = false
// o.OnlyPaths = false
// o.ShowValues = true
// case "template", "tmpl":
// o.outputFormat = prt.Template
// o.OnlyKeys = false
// o.OnlyPaths = false
// o.MaxValueLength = -1

// if o.TemplateFile != "" && o.TemplateString != "" {
// return fmt.Errorf("%w: %s", errInvalidFlagCombination, "cannot specify both --template-file and --template-string")
// }

// if o.TemplateFile == "" && o.TemplateString == "" {
// return fmt.Errorf("%w: %s", errInvalidFlagCombination, "either --template-file or --template-string is required")
// }
// default:
// return prt.ErrInvalidFormat
// }
// }

// return nil
// }

// func (o *exportOptions) buildMap() (map[string]interface{}, error) {
// var isSecretPath bool

// rootPath, subPath := utils.HandleEnginePath(o.EnginePath, o.Path)

// // read recursive all secrets
// s, err := vaultClient.ListRecursive(rootPath, subPath, o.SkipErrors)
// if err != nil {
// return nil, err
// }

// // check if path is a directory or secret path
// if _, isSecret := vaultClient.ReadSecrets(rootPath, subPath); isSecret == nil {
// isSecretPath = true
// }

// path := path.Join(rootPath, subPath)
// if o.EnginePath != "" {
// path = subPath
// }

// // prepare the output map
// pathMap := utils.PathMap(path, utils.ToMapStringInterface(s), isSecretPath)

// if o.EnginePath != "" {
// return map[string]interface{}{
// o.EnginePath: pathMap,
// }, nil
// }

// return pathMap, nil
// }
Loading

0 comments on commit 9ceea0d

Please sign in to comment.