@@ -4,19 +4,20 @@ import (
4
4
"encoding/json"
5
5
"errors"
6
6
"fmt"
7
- "github.com/adamdecaf/vault-backend-migrator/vault"
8
7
"io/ioutil"
9
8
"os"
10
9
"path"
11
10
"path/filepath"
12
11
"strings"
12
+
13
+ "github.com/adamdecaf/vault-backend-migrator/vault"
13
14
)
14
15
15
16
const (
16
17
OutputFileMode = 0644
17
18
)
18
19
19
- func Export (path , file string ) error {
20
+ func Export (path , file , metad , ver string ) error {
20
21
v , err := vault .NewClient ()
21
22
if v == nil || err != nil {
22
23
if err != nil {
@@ -30,10 +31,21 @@ func Export(path, file string) error {
30
31
path = path + "/"
31
32
}
32
33
34
+ if ver == "2" {
35
+ if ! strings .HasSuffix (metad , "/" ) {
36
+ metad = metad + "/"
37
+ }
38
+ }
39
+
33
40
// Get all nested keys
34
41
fmt .Printf ("Reading all keys under %s\n " , path )
35
42
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
+ }
37
49
38
50
// Read each key's value
39
51
fmt .Println ("Reading all secrets" )
@@ -47,7 +59,9 @@ func Export(path, file string) error {
47
59
48
60
var pairs []Pair
49
61
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
+ }
51
65
}
52
66
items = append (items , Item {Path : p , Pairs : pairs })
53
67
}
@@ -82,13 +96,13 @@ func Export(path, file string) error {
82
96
return nil
83
97
}
84
98
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 )
87
101
if res == nil { // We ran into a leaf
88
- * acc = append (* acc , p )
102
+ * acc = append (* acc , accump )
89
103
return
90
104
}
91
105
for _ , k := range res {
92
- accumulate (acc , v , path .Join (p , k ))
106
+ accumulate (acc , v , path .Join (basep , k ), path . Join ( accump , k ))
93
107
}
94
108
}
0 commit comments