Skip to content

Commit c34801d

Browse files
authored
Merge pull request #8 from avoidik/master
Add KV version 2 support
2 parents 450e2ec + e61f8c9 commit c34801d

File tree

6 files changed

+67
-109
lines changed

6 files changed

+67
-109
lines changed

cmd/export.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/adamdecaf/vault-backend-migrator/vault"
87
"io/ioutil"
98
"os"
109
"path"
1110
"path/filepath"
1211
"strings"
12+
13+
"github.com/adamdecaf/vault-backend-migrator/vault"
1314
)
1415

1516
const (
1617
OutputFileMode = 0644
1718
)
1819

19-
func Export(path, file string) error {
20+
func Export(path, file, metad, ver string) error {
2021
v, err := vault.NewClient()
2122
if v == nil || err != nil {
2223
if err != nil {
@@ -30,10 +31,21 @@ func Export(path, file string) error {
3031
path = path + "/"
3132
}
3233

34+
if ver == "2" {
35+
if !strings.HasSuffix(metad, "/") {
36+
metad = metad + "/"
37+
}
38+
}
39+
3340
// Get all nested keys
3441
fmt.Printf("Reading all keys under %s\n", path)
3542
var all []string
36-
accumulate(&all, *v, path)
43+
44+
if ver == "2" {
45+
accumulate(&all, *v, metad, path)
46+
} else {
47+
accumulate(&all, *v, path, path)
48+
}
3749

3850
// Read each key's value
3951
fmt.Println("Reading all secrets")
@@ -47,7 +59,9 @@ func Export(path, file string) error {
4759

4860
var pairs []Pair
4961
for k, v := range kvs {
50-
pairs = append(pairs, Pair{Key: k, Value: v})
62+
if str, ok := v.(string); ok {
63+
pairs = append(pairs, Pair{Key: k, Value: str})
64+
}
5165
}
5266
items = append(items, Item{Path: p, Pairs: pairs})
5367
}
@@ -82,13 +96,13 @@ func Export(path, file string) error {
8296
return nil
8397
}
8498

85-
func accumulate(acc *[]string, v vault.Vault, p string) {
86-
res := v.List(p)
99+
func accumulate(acc *[]string, v vault.Vault, basep string, accump string) {
100+
res := v.List(basep)
87101
if res == nil { // We ran into a leaf
88-
*acc = append(*acc, p)
102+
*acc = append(*acc, accump)
89103
return
90104
}
91105
for _, k := range res {
92-
accumulate(acc, v, path.Join(p, k))
106+
accumulate(acc, v, path.Join(basep, k), path.Join(accump, k))
93107
}
94108
}

cmd/import.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7-
"github.com/adamdecaf/vault-backend-migrator/vault"
87
"io/ioutil"
98
"os"
109
"path/filepath"
10+
11+
"github.com/adamdecaf/vault-backend-migrator/vault"
1112
)
1213

13-
func Import(path, file string) error {
14+
func Import(path, file, ver string) error {
1415
abs, err := filepath.Abs(file)
1516
if err != nil {
1617
return err
@@ -54,7 +55,9 @@ func Import(path, file string) error {
5455
data[kv.Key] = kv.Value
5556
}
5657
fmt.Printf("Writing %s\n", item.Path)
57-
v.Write(item.Path, data)
58+
if err := v.Write(item.Path, data, ver); err != nil {
59+
fmt.Printf("Error %s\n", err)
60+
}
5861
}
5962

6063
return nil

0 commit comments

Comments
 (0)