From e4a0b508c2d9ffa5cc77eb23aadeb1c59d64dd87 Mon Sep 17 00:00:00 2001 From: "viacheslav.vasilyev" Date: Tue, 26 May 2020 10:25:51 +0300 Subject: [PATCH 1/4] allow other data types to export and import --- cmd/import.go | 2 +- cmd/json.go | 4 ++-- go.mod | 2 ++ it/integration_test.go | 18 ++++++++++++++---- vault/client.go | 39 ++++++++++++++++++++++++++++++++------- 5 files changed, 51 insertions(+), 14 deletions(-) diff --git a/cmd/import.go b/cmd/import.go index f9ce0e4..7d92f70 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -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 } diff --git a/cmd/json.go b/cmd/json.go index 26fe4ae..b4966aa 100644 --- a/cmd/json.go +++ b/cmd/json.go @@ -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"` } diff --git a/go.mod b/go.mod index c8d3331..43b2e16 100644 --- a/go.mod +++ b/go.mod @@ -69,3 +69,5 @@ require ( gopkg.in/yaml.v2 v2.2.1 // indirect gotest.tools v2.1.0+incompatible // indirect ) + +go 1.13 diff --git a/it/integration_test.go b/it/integration_test.go index c73dcc8..e1a6bd2 100644 --- a/it/integration_test.go +++ b/it/integration_test.go @@ -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) @@ -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("Error: 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) diff --git a/vault/client.go b/vault/client.go index 918770c..c8ff813 100644 --- a/vault/client.go +++ b/vault/client.go @@ -2,6 +2,7 @@ package vault import ( "encoding/base64" + "encoding/json" "fmt" "os" @@ -78,13 +79,32 @@ func (v *Vault) Read(path string) map[string]interface{} { } for k, v := range s.Data { switch t := v.(type) { + case json.Number: + if n, err := t.Int64(); err == nil { + out[k] = n + } else if f, err := t.Float64(); err == nil { + out[k] = f + } else { + out[k] = v + } case string: out[k] = base64.StdEncoding.EncodeToString([]byte(t)) case map[string]interface{}: if k == "data" { for x, y := range t { - if z, ok := y.(string); ok { - out[x] = base64.StdEncoding.EncodeToString([]byte(z)) + switch t := y.(type) { + case json.Number: + if n, err := t.Int64(); err == nil { + out[k] = n + } else if f, err := t.Float64(); err == nil { + out[k] = f + } else { + out[k] = y + } + case string: + out[x] = base64.StdEncoding.EncodeToString([]byte(t)) + default: + fmt.Printf("error reading value at %s, key=%s, type=%T\n", path, k, v) } } } @@ -97,16 +117,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 From 2ab0da228d1ed3e4a470ff49962fda0b24aeafe7 Mon Sep 17 00:00:00 2001 From: Mike Oldfield Date: Thu, 30 Jul 2020 23:48:05 +0300 Subject: [PATCH 2/4] allow map data types to export and import via base64 json representation --- vault/client.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/vault/client.go b/vault/client.go index c8ff813..6429873 100644 --- a/vault/client.go +++ b/vault/client.go @@ -1,6 +1,7 @@ package vault import ( + "bytes" "encoding/base64" "encoding/json" "fmt" @@ -103,6 +104,12 @@ func (v *Vault) Read(path string) map[string]interface{} { } case string: out[x] = base64.StdEncoding.EncodeToString([]byte(t)) + case map[string]interface{}: + js, err := json.Marshal(&t) + if err != nil { + fmt.Println(err) + } + out[x] = base64.StdEncoding.EncodeToString(js) default: fmt.Printf("error reading value at %s, key=%s, type=%T\n", path, k, v) } @@ -128,7 +135,14 @@ func (v *Vault) Write(path string, data map[string]interface{}, ver string) erro if err != nil { return err } - body[k] = string(b) + isValid := json.Valid(b) + if isValid { + var mapValue map[string]interface{} + json.Unmarshal(b, &mapValue) + body[k] = mapValue + } else { + body[k] = string(b) + } } else { body[k] = v } @@ -146,3 +160,11 @@ func (v *Vault) Write(path string, data map[string]interface{}, ver string) erro return err } + +func createKeyValuePairs(m map[string]interface{}) string { + b := new(bytes.Buffer) + for key, value := range m { + fmt.Fprintf(b, "%s:\"%s\"\n", key, value) + } + return b.String() +} From 98c2fe67f876753f18e098063bc9ece5acaa4d8c Mon Sep 17 00:00:00 2001 From: Viacheslav Vasilyev Date: Sun, 2 Aug 2020 19:18:23 +0300 Subject: [PATCH 3/4] remove obsolete function --- vault/client.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/vault/client.go b/vault/client.go index 6429873..8a7a4e6 100644 --- a/vault/client.go +++ b/vault/client.go @@ -160,11 +160,3 @@ func (v *Vault) Write(path string, data map[string]interface{}, ver string) erro return err } - -func createKeyValuePairs(m map[string]interface{}) string { - b := new(bytes.Buffer) - for key, value := range m { - fmt.Fprintf(b, "%s:\"%s\"\n", key, value) - } - return b.String() -} From 32f035892c25a5fe0064de6de3db29f7f57059e9 Mon Sep 17 00:00:00 2001 From: Viacheslav Vasilyev Date: Sun, 2 Aug 2020 19:23:31 +0300 Subject: [PATCH 4/4] remote bytes import --- vault/client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/vault/client.go b/vault/client.go index 8a7a4e6..921db0b 100644 --- a/vault/client.go +++ b/vault/client.go @@ -1,7 +1,6 @@ package vault import ( - "bytes" "encoding/base64" "encoding/json" "fmt"