Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DBPW 1/5] Add Database v5 interface with gRPC client & server #9641

Merged
merged 19 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions sdk/database/newdbplugin/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package newdbplugin

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -38,7 +37,7 @@ func (c gRPCClient) Initialize(ctx context.Context, req InitializeRequest) (Init
}

func initReqToProto(req InitializeRequest) (*proto.InitializeRequest, error) {
config, err := json.Marshal(req.Config)
config, err := mapToStruct(req.Config)
if err != nil {
return nil, fmt.Errorf("unable to marshal config: %w", err)
}
Expand All @@ -51,10 +50,7 @@ func initReqToProto(req InitializeRequest) (*proto.InitializeRequest, error) {
}

func initRespFromProto(rpcResp *proto.InitializeResponse) (InitializeResponse, error) {
newConfig, err := parseConfigData(rpcResp.GetConfigData())
if err != nil {
return InitializeResponse{}, fmt.Errorf("unable to parse configuration response: %w", err)
}
newConfig := structToMap(rpcResp.GetConfigData())

resp := InitializeResponse{
Config: newConfig,
Expand Down
13 changes: 0 additions & 13 deletions sdk/database/newdbplugin/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ func TestGRPCClient_Initialize(t *testing.T) {
},
assertErr: assertErrNotNil,
},
"bad returned config": {
client: fakeClient{
initResp: &proto.InitializeResponse{
ConfigData: []byte("baddata"),
},
},
req: InitializeRequest{
Config: map[string]interface{}{
"foo": "bar",
},
},
assertErr: assertErrNotNil,
},
"happy path": {
client: fakeClient{
initResp: &proto.InitializeResponse{
Expand Down
55 changes: 2 additions & 53 deletions sdk/database/newdbplugin/grpc_server.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package newdbplugin

import (
"bytes"
"context"
"encoding/json"
"fmt"
"time"

Expand All @@ -21,11 +19,7 @@ type gRPCServer struct {

// Initialize the database plugin
func (g gRPCServer) Initialize(ctx context.Context, request *proto.InitializeRequest) (*proto.InitializeResponse, error) {
// Parse the config back from JSON to a map[string]interface{}
rawConfig, err := parseConfigData(request.ConfigData)
if err != nil {
return &proto.InitializeResponse{}, status.Errorf(codes.InvalidArgument, "unable to parse config data: %s", err)
}
rawConfig := structToMap(request.ConfigData)

dbReq := InitializeRequest{
Config: rawConfig,
Expand All @@ -37,7 +31,7 @@ func (g gRPCServer) Initialize(ctx context.Context, request *proto.InitializeReq
return &proto.InitializeResponse{}, status.Errorf(codes.Internal, "failed to initialize: %s", err)
}

newConfig, err := json.Marshal(dbResp.Config)
newConfig, err := mapToStruct(dbResp.Config)
if err != nil {
return &proto.InitializeResponse{}, status.Errorf(codes.Internal, "failed to marshal new config to JSON: %s", err)
}
Expand All @@ -49,51 +43,6 @@ func (g gRPCServer) Initialize(ctx context.Context, request *proto.InitializeReq
return resp, nil
}

func parseConfigData(b []byte) (map[string]interface{}, error) {
config := map[string]interface{}{}
if len(b) == 0 {
return config, nil
}
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.UseNumber()
err := decoder.Decode(&config)
if err != nil {
return nil, err
}

cleanNumbers(config)
return config, nil
}

func cleanNumbers(data map[string]interface{}) {
for k, v := range data {
switch val := v.(type) {
case json.Number:
newNum, err := coerceToScalarNumber(val)
if err != nil {
continue
}
data[k] = newNum
case map[string]interface{}:
cleanNumbers(val)
}
}
}

func coerceToScalarNumber(num json.Number) (newVal interface{}, err error) {
intNum, err := num.Int64()
if err == nil {
return intNum, nil
}

floatNum, err := num.Float64()
if err == nil {
return floatNum, nil
}

return nil, fmt.Errorf("unrecognized number: %w", err)
}

func (g gRPCServer) NewUser(ctx context.Context, req *proto.NewUserRequest) (*proto.NewUserResponse, error) {
if req.GetUsernameConfig() == nil {
return &proto.NewUserResponse{}, status.Errorf(codes.InvalidArgument, "missing username config")
Expand Down
80 changes: 14 additions & 66 deletions sdk/database/newdbplugin/grpc_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package newdbplugin

import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"testing"
"time"

"google.golang.org/protobuf/types/known/structpb"

"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/hashicorp/vault/sdk/database/newdbplugin/proto"
Expand All @@ -31,15 +32,6 @@ func TestGRPCServer_Initialize(t *testing.T) {
}

tests := map[string]testCase{
"bad config data": {
db: fakeDatabase{},
req: &proto.InitializeRequest{
ConfigData: []byte("98ythguns"),
},
expectedResp: &proto.InitializeResponse{},
expectErr: true,
expectCode: codes.InvalidArgument,
},
"database errored": {
db: fakeDatabase{
initErr: errors.New("initialization error"),
Expand Down Expand Up @@ -114,7 +106,7 @@ func TestGRPCServer_Initialize(t *testing.T) {
}
}

func TestCleanNumbers(t *testing.T) {
func TestCoerceFloatsToInt(t *testing.T) {
type testCase struct {
input map[string]interface{}
expected map[string]interface{}
Expand All @@ -137,72 +129,28 @@ func TestCleanNumbers(t *testing.T) {
"foo": 42,
},
},
"json.Number integer": {
input: map[string]interface{}{
"foo": json.Number("42"),
},
expected: map[string]interface{}{
"foo": int64(42),
},
},
"json.Number float": {
input: map[string]interface{}{
"foo": json.Number("42.123"),
},
expected: map[string]interface{}{
"foo": float64(42.123),
},
},
"bad json.Number": {
input: map[string]interface{}{
"foo": json.Number("bar"),
},
expected: map[string]interface{}{
"foo": json.Number("bar"),
},
},
"recursive integer": {
input: map[string]interface{}{
"foo": map[string]interface{}{
"bar": json.Number("42"),
},
},
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": int64(42),
},
},
},
"recursive float": {
"floats ": {
input: map[string]interface{}{
"foo": map[string]interface{}{
"bar": json.Number("42.123"),
},
"foo": 42.2,
},
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": float64(42.123),
},
"foo": 42.2,
},
},
"recursive no numbers": {
"floats coerced to ints": {
input: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "baz",
},
"foo": float64(42),
},
expected: map[string]interface{}{
"foo": map[string]interface{}{
"bar": "baz",
},
"foo": int64(42),
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
actual := copyMap(test.input)
cleanNumbers(actual)
coerceFloatsToInt(actual)
if !reflect.DeepEqual(actual, test.expected) {
t.Fatalf("Actual: %#v\nExpected: %#v", actual, test.expected)
}
Expand Down Expand Up @@ -616,14 +564,14 @@ func TestGRPCServer_Close(t *testing.T) {
}
}

func marshal(t *testing.T, val interface{}) []byte {
func marshal(t *testing.T, m map[string]interface{}) *structpb.Struct {
t.Helper()

b, err := json.Marshal(val)
strct, err := mapToStruct(m)
if err != nil {
t.Fatalf("unable to marshal to JSON: %s", err)
t.Fatalf("unable to marshal to protobuf: %s", err)
}
return b
return strct
}

type badJSONValue struct{}
Expand Down
36 changes: 36 additions & 0 deletions sdk/database/newdbplugin/marshalling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package newdbplugin

import (
"math"

"google.golang.org/protobuf/types/known/structpb"
)

func mapToStruct(m map[string]interface{}) (*structpb.Struct, error) {
return structpb.NewStruct(m)
}

func structToMap(strct *structpb.Struct) map[string]interface{} {
m := strct.AsMap()
coerceFloatsToInt(m)
return m
}

// coerceFloatsToInt if the floats can be coerced to an integer without losing data
func coerceFloatsToInt(m map[string]interface{}) {
for k, v := range m {
fVal, ok := v.(float64)
if !ok {
continue
}
if isInt(fVal) {
m[k] = int64(fVal)
}
}
}

// isInt attempts to determine if the given floating point number could be represented as an integer without losing data
// This does not work for very large floats, however in this usage that's okay since we don't expect numbers that large.
func isInt(f float64) bool {
return math.Floor(f) == f
}
Loading