From 490474a502efd171bf629655a64144e3d85c6ef4 Mon Sep 17 00:00:00 2001 From: Austin Gebauer <34121980+austingebauer@users.noreply.github.com> Date: Fri, 23 Apr 2021 14:07:26 -0700 Subject: [PATCH] secrets/database: Fixes marshalling bug for json.Number types (#11451) --- changelog/11451.txt | 3 +++ sdk/database/dbplugin/v5/grpc_client_test.go | 24 +++++++++++++++++++ sdk/database/dbplugin/v5/marshalling.go | 14 +++++++++++ .../sdk/database/dbplugin/v5/marshalling.go | 14 +++++++++++ 4 files changed, 55 insertions(+) create mode 100644 changelog/11451.txt diff --git a/changelog/11451.txt b/changelog/11451.txt new file mode 100644 index 000000000..b5ea7e2f8 --- /dev/null +++ b/changelog/11451.txt @@ -0,0 +1,3 @@ +```release-note:bug +secrets/database: Fix marshalling to allow providing numeric arguments to external database plugins. +``` diff --git a/sdk/database/dbplugin/v5/grpc_client_test.go b/sdk/database/dbplugin/v5/grpc_client_test.go index 696045c67..b187d736d 100644 --- a/sdk/database/dbplugin/v5/grpc_client_test.go +++ b/sdk/database/dbplugin/v5/grpc_client_test.go @@ -2,6 +2,7 @@ package dbplugin import ( "context" + "encoding/json" "errors" "reflect" "testing" @@ -62,6 +63,29 @@ func TestGRPCClient_Initialize(t *testing.T) { }, assertErr: assertErrNil, }, + "JSON number type in initialize request": { + client: fakeClient{ + initResp: &proto.InitializeResponse{ + ConfigData: marshal(t, map[string]interface{}{ + "foo": "bar", + "max": "10", + }), + }, + }, + req: InitializeRequest{ + Config: map[string]interface{}{ + "foo": "bar", + "max": json.Number("10"), + }, + }, + expectedResp: InitializeResponse{ + Config: map[string]interface{}{ + "foo": "bar", + "max": "10", + }, + }, + assertErr: assertErrNil, + }, } for name, test := range tests { diff --git a/sdk/database/dbplugin/v5/marshalling.go b/sdk/database/dbplugin/v5/marshalling.go index ff8bc7cff..e14a21e58 100644 --- a/sdk/database/dbplugin/v5/marshalling.go +++ b/sdk/database/dbplugin/v5/marshalling.go @@ -1,12 +1,26 @@ package dbplugin import ( + "encoding/json" "math" "google.golang.org/protobuf/types/known/structpb" ) func mapToStruct(m map[string]interface{}) (*structpb.Struct, error) { + // Convert any json.Number typed values to float64, since the + // type does not have a conversion mapping defined in structpb + for k, v := range m { + if n, ok := v.(json.Number); ok { + nf, err := n.Float64() + if err != nil { + return nil, err + } + + m[k] = nf + } + } + return structpb.NewStruct(m) } diff --git a/vendor/github.com/hashicorp/vault/sdk/database/dbplugin/v5/marshalling.go b/vendor/github.com/hashicorp/vault/sdk/database/dbplugin/v5/marshalling.go index ff8bc7cff..e14a21e58 100644 --- a/vendor/github.com/hashicorp/vault/sdk/database/dbplugin/v5/marshalling.go +++ b/vendor/github.com/hashicorp/vault/sdk/database/dbplugin/v5/marshalling.go @@ -1,12 +1,26 @@ package dbplugin import ( + "encoding/json" "math" "google.golang.org/protobuf/types/known/structpb" ) func mapToStruct(m map[string]interface{}) (*structpb.Struct, error) { + // Convert any json.Number typed values to float64, since the + // type does not have a conversion mapping defined in structpb + for k, v := range m { + if n, ok := v.(json.Number); ok { + nf, err := n.Float64() + if err != nil { + return nil, err + } + + m[k] = nf + } + } + return structpb.NewStruct(m) }