Skip to content

Commit f9d56b6

Browse files
committed
allow other data types to export and import
1 parent 298f0bb commit f9d56b6

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

cmd/import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Import(path, file, ver string) error {
5050

5151
// Write each keypair to vault
5252
for _, item := range wrap.Data {
53-
data := make(map[string]string)
53+
data := make(map[string]interface{})
5454
for _, kv := range item.Pairs {
5555
data[kv.Key] = kv.Value
5656
}

cmd/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ type Item struct {
99
Pairs []Pair `json:"pairs"`
1010
}
1111
type Pair struct {
12-
Key string `json:"key"`
13-
Value string `json:"value"`
12+
Key string `json:"key"`
13+
Value interface{} `json:"value"`
1414
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ require (
6969
gopkg.in/yaml.v2 v2.2.1 // indirect
7070
gotest.tools v2.1.0+incompatible // indirect
7171
)
72+
73+
go 1.13

it/integration_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ func TestMigrator__integration(t *testing.T) {
7272
data := []struct {
7373
path string
7474
key string
75-
value string
75+
value interface{}
7676
}{
7777
{"secret/foo", "foo", "YmFyCg=="}, // bar
7878
{"secret/bar/baz", "username", "YWRhbQo="}, // adam
79+
{"secret/baz", "integer", 100},
7980
}
8081
os.Setenv("VAULT_ADDR", "http://127.0.0.1:8200")
8182
os.Setenv("VAULT_TOKEN", token)
@@ -86,9 +87,18 @@ func TestMigrator__integration(t *testing.T) {
8687

8788
// write values
8889
for i := range data {
89-
client.Write(data[i].path, map[string]string{
90-
data[i].key: data[i].value,
91-
}, "1")
90+
m := make(map[string]interface{})
91+
92+
switch d := data[i].value.(type) {
93+
case int:
94+
m[data[i].key] = d
95+
case string:
96+
m[data[i].key] = d
97+
default:
98+
t.Fatal("Unsupported data type")
99+
}
100+
client.Write(data[i].path, m, "1")
101+
92102
kv := client.Read(data[i].path)
93103
if kv[data[i].key] != data[i].value {
94104
t.Fatalf("path=%q, kv[%s]=%q, value=%q, err=%v", data[i].path, data[i].key, kv[data[i].key], data[i].value, err)

vault/client.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vault
22

33
import (
44
"encoding/base64"
5+
"encoding/json"
56
"fmt"
67
"os"
78

@@ -78,6 +79,8 @@ func (v *Vault) Read(path string) map[string]interface{} {
7879
}
7980
for k, v := range s.Data {
8081
switch t := v.(type) {
82+
case json.Number:
83+
out[k] = t.(int)
8184
case string:
8285
out[k] = base64.StdEncoding.EncodeToString([]byte(t))
8386
case map[string]interface{}:
@@ -97,16 +100,21 @@ func (v *Vault) Read(path string) map[string]interface{} {
97100
}
98101

99102
// Write takes in a vault path and base64 encoded data to be written at that path.
100-
func (v *Vault) Write(path string, data map[string]string, ver string) error {
103+
func (v *Vault) Write(path string, data map[string]interface{}, ver string) error {
101104
body := make(map[string]interface{})
102105

103106
// Decode the base64 values
104107
for k, v := range data {
105-
b, err := base64.StdEncoding.DecodeString(v)
106-
if err != nil {
107-
return err
108+
stringv, ok := v.(string)
109+
if ok {
110+
b, err := base64.StdEncoding.DecodeString(stringv)
111+
if err != nil {
112+
return err
113+
}
114+
body[k] = string(b)
115+
} else {
116+
body[k] = v
108117
}
109-
body[k] = string(b)
110118
}
111119

112120
var err error

0 commit comments

Comments
 (0)