From 498df7f7dd4617c6ae71d6094e8c53a01e8f527b Mon Sep 17 00:00:00 2001 From: Mostafa Moradian Date: Sat, 2 Sep 2023 20:11:39 +0200 Subject: [PATCH] Implement the new JSON binary protocol --- go.mod | 6 +-- go.sum | 12 +++--- network/proxy.go | 10 +---- network/server_test.go | 60 ++++++++------------------- network/utils.go | 16 +++----- plugin/plugin_registry.go | 19 +++++---- plugin/plugin_registry_test.go | 75 +++++++++++++++++----------------- plugin/utils.go | 4 +- plugin/utils_test.go | 10 ++--- 9 files changed, 88 insertions(+), 124 deletions(-) diff --git a/go.mod b/go.mod index 17ca1bfd..a9e7d2e3 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/NYTimes/gziphandler v1.1.1 github.com/codingsince1985/checksum v1.3.0 github.com/envoyproxy/protoc-gen-validate v1.0.2 - github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.0 + github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.1 github.com/getsentry/sentry-go v0.23.0 github.com/go-co-op/gocron v1.33.1 github.com/google/go-cmp v0.5.9 @@ -80,8 +80,8 @@ require ( golang.org/x/net v0.14.0 // indirect golang.org/x/oauth2 v0.11.0 // indirect golang.org/x/sync v0.3.0 // indirect - golang.org/x/sys v0.11.0 // indirect - golang.org/x/text v0.12.0 // indirect + golang.org/x/sys v0.12.0 // indirect + golang.org/x/text v0.13.0 // indirect google.golang.org/appengine v1.6.7 // indirect google.golang.org/genproto v0.0.0-20230822172742-b8732ec3820d // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect diff --git a/go.sum b/go.sum index 75f74b6d..bb3b32b4 100644 --- a/go.sum +++ b/go.sum @@ -74,8 +74,8 @@ github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= -github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.0 h1:KLxUoh063oMsQmGNsT+Txmyi4M1O673oZbdsk5l1ENs= -github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.0/go.mod h1:B4oWVHf7NeSCs7szN8nrlIO6tkznV1F3ZMqE9VxDtKY= +github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.1 h1:ujlh6TSDFmG2e9VtY6hZk8BW0p7i0AEQL3BpMMLzxwc= +github.com/gatewayd-io/gatewayd-plugin-sdk v0.1.1/go.mod h1:B4oWVHf7NeSCs7szN8nrlIO6tkznV1F3ZMqE9VxDtKY= github.com/getsentry/sentry-go v0.23.0 h1:dn+QRCeJv4pPt9OjVXiMcGIBIefaTJPw/h0bZWO05nE= github.com/getsentry/sentry-go v0.23.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -495,8 +495,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= -golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= @@ -512,8 +512,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc= -golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/network/proxy.go b/network/proxy.go index 323d9257..fbd04e8d 100644 --- a/network/proxy.go +++ b/network/proxy.go @@ -674,11 +674,8 @@ func (pr *Proxy) getPluginModifiedRequest(result map[string]interface{}) []byte defer span.End() // If the hook modified the request, use the modified request. - if modRequest, errMsg, convErr := extractFieldValue(result, "request"); errMsg != "" { + if modRequest, errMsg := extractFieldValue(result, "request"); errMsg != "" { pr.logger.Error().Str("error", errMsg).Msg("Error in hook") - } else if convErr != nil { - pr.logger.Error().Err(convErr).Msg("Error in data conversion") - span.RecordError(convErr) } else if modRequest != nil { return modRequest } @@ -693,11 +690,8 @@ func (pr *Proxy) getPluginModifiedResponse(result map[string]interface{}) ([]byt defer span.End() // If the hook returns a response, use it instead of the original response. - if modResponse, errMsg, convErr := extractFieldValue(result, "response"); errMsg != "" { + if modResponse, errMsg := extractFieldValue(result, "response"); errMsg != "" { pr.logger.Error().Str("error", errMsg).Msg("Error in hook") - } else if convErr != nil { - pr.logger.Error().Err(convErr).Msg("Error in data conversion") - span.RecordError(convErr) } else if modResponse != nil { return modResponse, len(modResponse) } diff --git a/network/server_test.go b/network/server_test.go index f73279b4..3c2f62b4 100644 --- a/network/server_test.go +++ b/network/server_test.go @@ -2,7 +2,6 @@ package network import ( "context" - "encoding/base64" "errors" "testing" @@ -15,7 +14,6 @@ import ( "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "google.golang.org/grpc" - "google.golang.org/protobuf/types/known/structpb" ) // TestRunServer tests an entire server run with a single client connection and hooks. @@ -42,24 +40,17 @@ func TestRunServer(t *testing.T) { onTrafficFromClient := func( ctx context.Context, - params *structpb.Struct, + params *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { paramsMap := params.AsMap() if paramsMap["request"] == nil { errs <- errors.New("request is nil") //nolint:goerr113 } logger.Info().Msg("Ingress traffic") - // Decode the request. - // The request is []byte, but it is base64-encoded as a string - // via using the structpb.NewStruct function. - if req, ok := paramsMap["request"].(string); ok { - if request, err := base64.StdEncoding.DecodeString(req); err == nil { - assert.Equal(t, CreatePgStartupPacket(), request) - } else { - errs <- err - } + if req, ok := paramsMap["request"].([]byte); ok { + assert.Equal(t, CreatePgStartupPacket(), req) } else { errs <- errors.New("request is not a []byte") //nolint:goerr113 } @@ -70,24 +61,17 @@ func TestRunServer(t *testing.T) { onTrafficToServer := func( ctx context.Context, - params *structpb.Struct, + params *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { paramsMap := params.AsMap() if paramsMap["request"] == nil { errs <- errors.New("request is nil") //nolint:goerr113 } logger.Info().Msg("Ingress traffic") - // Decode the request. - // The request is []byte, but it is base64-encoded as a string - // via using the structpb.NewStruct function. - if req, ok := paramsMap["request"].(string); ok { - if request, err := base64.StdEncoding.DecodeString(req); err == nil { - assert.Equal(t, CreatePgStartupPacket(), request) - } else { - errs <- err - } + if req, ok := paramsMap["request"].([]byte); ok { + assert.Equal(t, CreatePgStartupPacket(), req) } else { errs <- errors.New("request is not a []byte") //nolint:goerr113 } @@ -98,23 +82,19 @@ func TestRunServer(t *testing.T) { onTrafficFromServer := func( ctx context.Context, - params *structpb.Struct, + params *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { paramsMap := params.AsMap() if paramsMap["response"] == nil { errs <- errors.New("response is nil") //nolint:goerr113 } logger.Info().Msg("Egress traffic") - if resp, ok := paramsMap["response"].(string); ok { - if response, err := base64.StdEncoding.DecodeString(resp); err == nil { - assert.Equal(t, CreatePostgreSQLPacket('R', []byte{ - 0x0, 0x0, 0x0, 0xa, 0x53, 0x43, 0x52, 0x41, 0x4d, 0x2d, 0x53, 0x48, 0x41, 0x2d, 0x32, 0x35, 0x36, 0x0, 0x0, - }), response) - } else { - errs <- err - } + if resp, ok := paramsMap["response"].([]byte); ok { + assert.Equal(t, CreatePostgreSQLPacket('R', []byte{ + 0x0, 0x0, 0x0, 0xa, 0x53, 0x43, 0x52, 0x41, 0x4d, 0x2d, 0x53, 0x48, 0x41, 0x2d, 0x32, 0x35, 0x36, 0x0, 0x0, + }), resp) } else { errs <- errors.New("response is not a []byte") //nolint:goerr113 } @@ -125,21 +105,17 @@ func TestRunServer(t *testing.T) { onTrafficToClient := func( ctx context.Context, - params *structpb.Struct, + params *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { paramsMap := params.AsMap() if paramsMap["response"] == nil { errs <- errors.New("response is nil") //nolint:goerr113 } logger.Info().Msg("Egress traffic") - if resp, ok := paramsMap["response"].(string); ok { - if response, err := base64.StdEncoding.DecodeString(resp); err == nil { - assert.Equal(t, uint8(0x52), response[0]) - } else { - errs <- err - } + if resp, ok := paramsMap["response"].([]byte); ok { + assert.Equal(t, uint8(0x52), resp[0]) } else { errs <- errors.New("response is not a []byte") //nolint:goerr113 } diff --git a/network/utils.go b/network/utils.go index 0d4a3dac..80af2092 100644 --- a/network/utils.go +++ b/network/utils.go @@ -2,7 +2,6 @@ package network import ( "crypto/sha256" - "encoding/base64" "encoding/hex" "errors" "fmt" @@ -95,25 +94,20 @@ func trafficData( } // extractFieldValue extracts the given field name and error message from the result of the hook. -func extractFieldValue(result map[string]interface{}, fieldName string) ([]byte, string, error) { +func extractFieldValue(result map[string]interface{}, fieldName string) ([]byte, string) { var data []byte var err string - var conversionErr error - //nolint:nestif if result != nil { - if fieldValue, ok := result[fieldName].(string); ok { - if base64Decoded, err := base64.StdEncoding.DecodeString(fieldValue); err == nil { - data = base64Decoded - } else { - conversionErr = err - } + if val, ok := result[fieldName].([]byte); ok { + data = val } + if errMsg, ok := result["error"].(string); ok && errMsg != "" { err = errMsg } } - return data, err, conversionErr + return data, err } // IsConnTimedOut returns true if the error is a timeout error. diff --git a/plugin/plugin_registry.go b/plugin/plugin_registry.go index b192a3bd..b975d86e 100644 --- a/plugin/plugin_registry.go +++ b/plugin/plugin_registry.go @@ -20,7 +20,6 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" "google.golang.org/grpc" - "google.golang.org/protobuf/types/known/structpb" ) type IHook interface { @@ -283,11 +282,11 @@ func (reg *Registry) Run( // Cast custom fields to their primitive types, like time.Duration to float64. args = CastToPrimitiveTypes(args) - // Create structpb.Struct from args. - var params *structpb.Struct + // Create v1.Struct from args. + var params *v1.Struct if len(args) == 0 { - params = &structpb.Struct{} - } else if casted, err := structpb.NewStruct(args); err == nil { + params = &v1.Struct{} + } else if casted, err := v1.NewStruct(args); err == nil { params = casted } else { span.RecordError(err) @@ -304,11 +303,11 @@ func (reg *Registry) Run( }) // Run hooks, passing the result of the previous hook to the next one. - returnVal := &structpb.Struct{} + returnVal := &v1.Struct{} var removeList []sdkPlugin.Priority // The signature of parameters and args MUST be the same for this to work. for idx, priority := range priorities { - var result *structpb.Struct + var result *v1.Struct var err error if idx == 0 { result, err = reg.hooks[hookName][priority](inheritedCtx, params, opts...) @@ -501,7 +500,7 @@ func (reg *Registry) LoadPlugins(ctx context.Context, plugins []config.Plugin) { span.AddEvent("Started plugin") // Load metadata from the plugin. - var metadata *structpb.Struct + var metadata *v1.Struct pluginV1, err := plugin.Dispense() if err != nil { reg.Logger.Debug().Str("name", plugin.ID.Name).Err(err).Msg( @@ -509,13 +508,15 @@ func (reg *Registry) LoadPlugins(ctx context.Context, plugins []config.Plugin) { plugin.Client.Kill() continue } + meta, origErr := pluginV1.GetPluginConfig( //nolint:contextcheck - context.Background(), &structpb.Struct{}) + context.Background(), &v1.Struct{}) if err != nil || meta == nil { reg.Logger.Debug().Str("name", plugin.ID.Name).Err(origErr).Msg( "Failed to get plugin metadata") continue } + metadata = meta span.AddEvent("Fetched plugin metadata") diff --git a/plugin/plugin_registry_test.go b/plugin/plugin_registry_test.go index 642be9fc..19c78ec5 100644 --- a/plugin/plugin_registry_test.go +++ b/plugin/plugin_registry_test.go @@ -11,7 +11,6 @@ import ( "github.com/rs/zerolog" "github.com/stretchr/testify/assert" "google.golang.org/grpc" - "google.golang.org/protobuf/types/known/structpb" ) func NewPluginRegistry(t *testing.T) *Registry { @@ -69,9 +68,9 @@ func TestPluginRegistry(t *testing.T) { func Test_PluginRegistry_AddHook(t *testing.T) { testFunc := func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil } @@ -86,16 +85,16 @@ func Test_PluginRegistry_AddHook_Multiple(t *testing.T) { reg := NewPluginRegistry(t) reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) assert.NotNil(t, reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER][0]) @@ -108,9 +107,9 @@ func Test_PluginRegistry_Run(t *testing.T) { reg.Verification = config.Ignore reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) result, err := reg.Run(context.Background(), map[string]interface{}{}, v1.HookName_HOOK_NAME_ON_NEW_LOGGER) @@ -125,18 +124,18 @@ func Test_PluginRegistry_Run_PassDown(t *testing.T) { // The result of the hook will be nil and will be passed down to the next reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) // The consolidated result should be {"test": "test"}. reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - output, err := structpb.NewStruct(map[string]interface{}{ + ) (*v1.Struct, error) { + output, err := v1.NewStruct(map[string]interface{}{ "test": "test", }) assert.Nil(t, err) @@ -161,11 +160,11 @@ func Test_HookRegistry_Run_PassDown_2(t *testing.T) { // The result of the hook will be nil and will be passed down to the next reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - args.Fields["test1"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ + ) (*v1.Struct, error) { + args.Fields["test1"] = &v1.Value{ + Kind: &v1.Value_StringValue{ StringValue: "test1", }, } @@ -174,11 +173,11 @@ func Test_HookRegistry_Run_PassDown_2(t *testing.T) { // The consolidated result should be {"test1": "test1", "test2": "test2"}. reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - args.Fields["test2"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ + ) (*v1.Struct, error) { + args.Fields["test2"] = &v1.Value{ + Kind: &v1.Value_StringValue{ StringValue: "test2", }, } @@ -202,19 +201,19 @@ func Test_HookRegistry_Run_Ignore(t *testing.T) { // This should not run, because the return value is not the same as the params reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) // This should run, because the return value is the same as the params reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - args.Fields["test"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ + ) (*v1.Struct, error) { + args.Fields["test"] = &v1.Value{ + Kind: &v1.Value_StringValue{ StringValue: "test", }, } @@ -238,18 +237,18 @@ func Test_HookRegistry_Run_Abort(t *testing.T) { // This should not run, because the return value is not the same as the params reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) // This should not run, because the first hook returns nil, and its result is ignored. reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - output, err := structpb.NewStruct(map[string]interface{}{ + ) (*v1.Struct, error) { + output, err := v1.NewStruct(map[string]interface{}{ "test": "test", }) assert.Nil(t, err) @@ -268,18 +267,18 @@ func Test_HookRegistry_Run_Remove(t *testing.T) { // This should not run, because the return value is not the same as the params reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { + ) (*v1.Struct, error) { return args, nil }) // This should not run, because the first hook returns nil, and its result is ignored. reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, - args *structpb.Struct, + args *v1.Struct, opts ...grpc.CallOption, - ) (*structpb.Struct, error) { - output, err := structpb.NewStruct(map[string]interface{}{ + ) (*v1.Struct, error) { + output, err := v1.NewStruct(map[string]interface{}{ "test": "test", }) assert.Nil(t, err) diff --git a/plugin/utils.go b/plugin/utils.go index 5dcb7c74..e7771f4d 100644 --- a/plugin/utils.go +++ b/plugin/utils.go @@ -4,13 +4,13 @@ import ( "os/exec" "time" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" - "google.golang.org/protobuf/types/known/structpb" ) // Verify compares two structs and returns true if they are equal. -func Verify(params, returnVal *structpb.Struct) bool { +func Verify(params, returnVal *v1.Struct) bool { return cmp.Equal(params.AsMap(), returnVal.AsMap(), cmp.Options{ cmpopts.SortMaps(func(a, b string) bool { return a < b diff --git a/plugin/utils_test.go b/plugin/utils_test.go index 3e892e52..0e3e695a 100644 --- a/plugin/utils_test.go +++ b/plugin/utils_test.go @@ -3,20 +3,20 @@ package plugin import ( "testing" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/stretchr/testify/assert" - "google.golang.org/protobuf/types/known/structpb" ) // Test_Verify tests the Verify function. func Test_Verify(t *testing.T) { - params, err := structpb.NewStruct( + params, err := v1.NewStruct( map[string]interface{}{ "test": "test", }, ) assert.Nil(t, err) - returnVal, err := structpb.NewStruct( + returnVal, err := v1.NewStruct( map[string]interface{}{ "test": "test", }, @@ -61,9 +61,9 @@ func Test_Verify_fail(t *testing.T) { } for _, d := range data { - params, err := structpb.NewStruct(d[0]) + params, err := v1.NewStruct(d[0]) assert.Nil(t, err) - returnVal, err := structpb.NewStruct(d[1]) + returnVal, err := v1.NewStruct(d[1]) assert.Nil(t, err) assert.False(t, Verify(params, returnVal)) }