Skip to content

Commit

Permalink
allow other data types to export and import
Browse files Browse the repository at this point in the history
  • Loading branch information
mc-slava committed May 26, 2020
1 parent 298f0bb commit f9d56b6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Import(path, file, ver string) error {

// Write each keypair to vault
for _, item := range wrap.Data {
data := make(map[string]string)
data := make(map[string]interface{})
for _, kv := range item.Pairs {
data[kv.Key] = kv.Value
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type Item struct {
Pairs []Pair `json:"pairs"`
}
type Pair struct {
Key string `json:"key"`
Value string `json:"value"`
Key string `json:"key"`
Value interface{} `json:"value"`
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,5 @@ require (
gopkg.in/yaml.v2 v2.2.1 // indirect
gotest.tools v2.1.0+incompatible // indirect
)

go 1.13
18 changes: 14 additions & 4 deletions it/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ func TestMigrator__integration(t *testing.T) {
data := []struct {
path string
key string
value string
value interface{}
}{
{"secret/foo", "foo", "YmFyCg=="}, // bar
{"secret/bar/baz", "username", "YWRhbQo="}, // adam
{"secret/baz", "integer", 100},
}
os.Setenv("VAULT_ADDR", "http://127.0.0.1:8200")
os.Setenv("VAULT_TOKEN", token)
Expand All @@ -86,9 +87,18 @@ func TestMigrator__integration(t *testing.T) {

// write values
for i := range data {
client.Write(data[i].path, map[string]string{
data[i].key: data[i].value,
}, "1")
m := make(map[string]interface{})

switch d := data[i].value.(type) {
case int:
m[data[i].key] = d
case string:
m[data[i].key] = d
default:
t.Fatal("Unsupported data type")
}
client.Write(data[i].path, m, "1")

kv := client.Read(data[i].path)
if kv[data[i].key] != data[i].value {
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)
Expand Down
18 changes: 13 additions & 5 deletions vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vault

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"

Expand Down Expand Up @@ -78,6 +79,8 @@ func (v *Vault) Read(path string) map[string]interface{} {
}
for k, v := range s.Data {
switch t := v.(type) {
case json.Number:
out[k] = t.(int)
case string:
out[k] = base64.StdEncoding.EncodeToString([]byte(t))
case map[string]interface{}:
Expand All @@ -97,16 +100,21 @@ func (v *Vault) Read(path string) map[string]interface{} {
}

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

// Decode the base64 values
for k, v := range data {
b, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err
stringv, ok := v.(string)
if ok {
b, err := base64.StdEncoding.DecodeString(stringv)
if err != nil {
return err
}
body[k] = string(b)
} else {
body[k] = v
}
body[k] = string(b)
}

var err error
Expand Down

0 comments on commit f9d56b6

Please sign in to comment.