Skip to content

Commit

Permalink
feat(ignore-errors): introduce --skip-errors flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Morelly authored and FalcoSuessgott committed Oct 26, 2023
1 parent 014cf7f commit 7df6740
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ vault: clean ## set up a development vault server and write kv secrets

.PHONY: vault-ent
vault-ent: clean ## set up a development vault enterprise server and write kv secrets
nohup vault-ent server -dev -dev-root-token-id=root 2> /dev/null &
nohup vault server -dev -dev-root-token-id=root 2> /dev/null &
sleep 3

./scripts/prepare-vault.sh
Expand Down
6 changes: 5 additions & 1 deletion cmd/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type exportOptions struct {
ShowMetadata bool `env:"SHOW_METADATA" envDefault:"true"`
MaxValueLength int `env:"MAX_VALUE_LENGTH" envDefault:"12"`

SkipErrors bool `env:"SKIP_ERRORS" envDefault:"false"`

TemplateFile string `env:"TEMPLATE_FILE"`
TemplateString string `env:"TEMPLATE_STRING"`

Expand Down Expand Up @@ -82,6 +84,7 @@ func NewExportCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Command {
)

// prepare map

m, err := o.buildMap(vaultClient)
if err != nil {
return err
Expand All @@ -103,6 +106,7 @@ func NewExportCmd(writer io.Writer, vaultClient *vault.Vault) *cobra.Command {
// Input
cmd.Flags().StringVarP(&o.Path, "path", "p", o.Path, "KVv2 Engine path (env: VKV_EXPORT_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)")

// Modify
cmd.Flags().BoolVar(&o.OnlyKeys, "only-keys", o.OnlyKeys, "show only keys (env: VKV_EXPORT_ONLY_KEYS)")
Expand Down Expand Up @@ -188,7 +192,7 @@ func (o *exportOptions) buildMap(v *vault.Vault) (map[string]interface{}, error)
rootPath, subPath := utils.HandleEnginePath(o.EnginePath, o.Path)

// read recursive all secrets
s, err := v.ListRecursive(rootPath, subPath)
s, err := v.ListRecursive(rootPath, subPath, o.SkipErrors)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/imp/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (o *importOptions) dryRun(v *vault.Vault, secrets map[string]interface{}) e
rootPath, _ := utils.SplitPath(o.Path)
existingSecrets := make(map[string]interface{})

tmp, err := v.ListRecursive(rootPath, "")
tmp, err := v.ListRecursive(rootPath, "", false)
if err == nil {
existingSecrets = utils.PathMap(rootPath, utils.ToMapStringInterface(tmp), false)
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func (o *importOptions) printResult(v *vault.Vault) error {

rootPath, _ := utils.SplitPath(o.Path)

s, err := v.ListRecursive(rootPath, "")
s, err := v.ListRecursive(rootPath, "", false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapshot/snapshot_restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *VaultSuite) TestSnapShotRestoreCommand() {
continue
}

secret, err := s.client.ListRecursive(path.Join(expNS, engine), "")
secret, err := s.client.ListRecursive(path.Join(expNS, engine), "", false)
require.NoError(s.Suite.T(), err)

out, err := fs.ReadFile(path.Join("testdata/vkv-snapshot-export", expNS, strings.TrimSuffix(engine, "/")+".yaml"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/snapshot/snapshot_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (o *snapshotSaveOptions) backupKVEngines(v *vault.Vault, engines map[string
for _, e := range engines[ns] {
enginePath := path.Join(ns, e)

out, err := v.ListRecursive(enginePath, "")
out, err := v.ListRecursive(enginePath, "", false)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"testing"

"github.com/stretchr/testify/assert"
)

Expand Down
11 changes: 8 additions & 3 deletions pkg/vault/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
type Secrets map[string]interface{}

// ListRecursive returns secrets to a path recursive.
func (v *Vault) ListRecursive(rootPath, subPath string) (*Secrets, error) {
func (v *Vault) ListRecursive(rootPath, subPath string, skipErrors bool) (*Secrets, error) {
s := make(Secrets)

keys, err := v.ListKeys(rootPath, subPath)
Expand All @@ -36,18 +36,23 @@ func (v *Vault) ListRecursive(rootPath, subPath string) (*Secrets, error) {

for _, k := range keys {
if strings.HasSuffix(k, utils.Delimiter) {
secrets, err := v.ListRecursive(rootPath, path.Join(subPath, k))
secrets, err := v.ListRecursive(rootPath, path.Join(subPath, k), skipErrors)
if err != nil {
return &s, err
}

(s)[k] = secrets
} else {
secrets, err := v.ReadSecrets(rootPath, path.Join(subPath, k))
if err != nil {
if !skipErrors && err != nil {
return nil, err
}

// do not exit on errors, just an empty map, so json/yaml export still works
if skipErrors && secrets == nil {
secrets = make(Secrets)
}

(s)[k] = secrets
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/vault/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *VaultSuite) TestListRecursive() {

// read secrets
res := make(Secrets)
secrets, err := s.client.ListRecursive(tc.rootPath, tc.subPath)
secrets, err := s.client.ListRecursive(tc.rootPath, tc.subPath, false)
assert.NoError(s.Suite.T(), err)

res[tc.rootPath] = *secrets
Expand Down

0 comments on commit 7df6740

Please sign in to comment.