diff --git a/.golangci.yaml b/.golangci.yaml index cc2aac6d..c9a49026 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -18,3 +18,4 @@ linters: - funlen - maintidx - musttag + - nosnakecase diff --git a/api/api.go b/api/api.go index 1fddfbf1..34b2c20d 100644 --- a/api/api.go +++ b/api/api.go @@ -84,6 +84,12 @@ func (a *API) GetPlugins(context.Context, *emptypb.Empty) (*v1.PluginConfigs, er requires[r.Name] = r.Version } } + + hooks := make([]int32, 0) + for _, hook := range plugIn.Hooks { + hooks = append(hooks, int32(hook.Number())) + } + plugins = append(plugins, &v1.PluginConfig{ Id: &v1.PluginID{ Name: pluginID.Name, @@ -96,7 +102,7 @@ func (a *API) GetPlugins(context.Context, *emptypb.Empty) (*v1.PluginConfigs, er License: plugIn.License, ProjectUrl: plugIn.ProjectURL, Config: plugIn.Config, - Hooks: plugIn.Hooks, + Hooks: hooks, Requires: requires, Tags: plugIn.Tags, Categories: plugIn.Categories, diff --git a/api/v1/api.pb.go b/api/v1/api.pb.go index 85dc89da..c671e4ad 100644 --- a/api/v1/api.pb.go +++ b/api/v1/api.pb.go @@ -210,7 +210,7 @@ type PluginConfig struct { // internal and external config options Config map[string]string `protobuf:"bytes,6,rep,name=config,proto3" json:"config,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` // hooks it attaches to - Hooks []string `protobuf:"bytes,7,rep,name=hooks,proto3" json:"hooks,omitempty"` + Hooks []int32 `protobuf:"varint,7,rep,packed,name=hooks,proto3" json:"hooks,omitempty"` // required plugins Requires map[string]string `protobuf:"bytes,8,rep,name=requires,proto3" json:"requires,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` Tags []string `protobuf:"bytes,9,rep,name=tags,proto3" json:"tags,omitempty"` @@ -291,7 +291,7 @@ func (x *PluginConfig) GetConfig() map[string]string { return nil } -func (x *PluginConfig) GetHooks() []string { +func (x *PluginConfig) GetHooks() []int32 { if x != nil { return x.Hooks } @@ -408,7 +408,7 @@ var file_api_v1_api_proto_rawDesc = []byte{ 0x20, 0x03, 0x28, 0x0b, 0x32, 0x20, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x14, - 0x0a, 0x05, 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x09, 0x52, 0x05, 0x68, + 0x0a, 0x05, 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x05, 0x52, 0x05, 0x68, 0x6f, 0x6f, 0x6b, 0x73, 0x12, 0x3e, 0x0a, 0x08, 0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x73, 0x18, 0x08, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6c, 0x75, 0x67, 0x69, 0x6e, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x2e, 0x52, 0x65, 0x71, diff --git a/api/v1/api.proto b/api/v1/api.proto index d860b9c3..1a401a11 100644 --- a/api/v1/api.proto +++ b/api/v1/api.proto @@ -94,7 +94,7 @@ message PluginConfig { // internal and external config options map config = 6; // hooks it attaches to - repeated string hooks = 7; + repeated int32 hooks = 7; // required plugins map requires = 8; repeated string tags = 9; @@ -103,4 +103,4 @@ message PluginConfig { message PluginConfigs { repeated PluginConfig configs = 1; -} \ No newline at end of file +} diff --git a/api/v1/api.swagger.json b/api/v1/api.swagger.json index 91dffb86..1c440532 100644 --- a/api/v1/api.swagger.json +++ b/api/v1/api.swagger.json @@ -255,7 +255,8 @@ "hooks": { "type": "array", "items": { - "type": "string" + "type": "integer", + "format": "int32" }, "title": "hooks it attaches to" }, diff --git a/cmd/run.go b/cmd/run.go index 82418bf2..35dab1be 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -16,6 +16,7 @@ import ( "github.com/NYTimes/gziphandler" sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/api" "github.com/gatewayd-io/gatewayd/config" gerr "github.com/gatewayd-io/gatewayd/errors" @@ -214,7 +215,7 @@ var runCmd = &cobra.Command{ updatedGlobalConfig, err := pluginRegistry.Run( context.Background(), conf.GlobalKoanf.All(), - sdkPlugin.OnConfigLoaded) + v1.HookName_HOOK_NAME_ON_CONFIG_LOADED) if err != nil { logger.Error().Err(err).Msg("Failed to run OnConfigLoaded hooks") span.RecordError(err) @@ -296,7 +297,8 @@ var runCmd = &cobra.Command{ // This is a notification hook, so we don't care about the result. // TODO: Use a context with a timeout if data, ok := conf.GlobalKoanf.Get("loggers").(map[string]interface{}); ok { - _, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewLogger) + _, err = pluginRegistry.Run( + context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_LOGGER) if err != nil { logger.Error().Err(err).Msg("Failed to run OnNewLogger hooks") span.RecordError(err) @@ -364,7 +366,8 @@ var runCmd = &cobra.Command{ "tcpKeepAlive": client.TCPKeepAlive, "tcpKeepAlivePeriod": client.TCPKeepAlivePeriod.String(), } - _, err := pluginRegistry.Run(context.Background(), clientCfg, sdkPlugin.OnNewClient) + _, err := pluginRegistry.Run( + context.Background(), clientCfg, v1.HookName_HOOK_NAME_ON_NEW_CLIENT) if err != nil { logger.Error().Err(err).Msg("Failed to run OnNewClient hooks") span.RecordError(err) @@ -399,7 +402,7 @@ var runCmd = &cobra.Command{ _, err = pluginRegistry.Run( context.Background(), map[string]interface{}{"name": name, "size": cfg.GetSize()}, - sdkPlugin.OnNewPool) + v1.HookName_HOOK_NAME_ON_NEW_POOL) if err != nil { logger.Error().Err(err).Msg("Failed to run OnNewPool hooks") span.RecordError(err) @@ -432,7 +435,8 @@ var runCmd = &cobra.Command{ )) if data, ok := conf.GlobalKoanf.Get("proxy").(map[string]interface{}); ok { - _, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewProxy) + _, err = pluginRegistry.Run( + context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_PROXY) if err != nil { logger.Error().Err(err).Msg("Failed to run OnNewProxy hooks") span.RecordError(err) @@ -507,7 +511,8 @@ var runCmd = &cobra.Command{ )) if data, ok := conf.GlobalKoanf.Get("servers").(map[string]interface{}); ok { - _, err = pluginRegistry.Run(context.Background(), data, sdkPlugin.OnNewServer) + _, err = pluginRegistry.Run( + context.Background(), data, v1.HookName_HOOK_NAME_ON_NEW_SERVER) if err != nil { logger.Error().Err(err).Msg("Failed to run OnNewServer hooks") span.RecordError(err) @@ -617,7 +622,7 @@ var runCmd = &cobra.Command{ _, err := pluginRegistry.Run( context.Background(), map[string]interface{}{"signal": sig.String()}, - sdkPlugin.OnSignal, + v1.HookName_HOOK_NAME_ON_SIGNAL, ) if err != nil { logger.Error().Err(err).Msg("Failed to run OnSignal hooks") diff --git a/config/getters.go b/config/getters.go index 74c38882..2ba03143 100644 --- a/config/getters.go +++ b/config/getters.go @@ -70,7 +70,6 @@ var ( "panic": zerolog.PanicLevel, "disabled": zerolog.Disabled, } - //nolint:nosnakecase rSyslogPriorities = map[string]syslog.Priority{ "emerg": syslog.LOG_EMERG, "alert": syslog.LOG_ALERT, @@ -176,8 +175,6 @@ func (l Logger) GetLevel() zerolog.Level { } // GetSyslogPriority returns the rsyslog facility from config file. -// -//nolint:nosnakecase func (l Logger) GetSyslogPriority() syslog.Priority { if priority, ok := rSyslogPriorities[l.SyslogPriority]; ok { return priority | syslog.LOG_DAEMON diff --git a/go.mod b/go.mod index 95bdc10b..1c7b3707 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/Masterminds/semver/v3 v3.2.0 github.com/NYTimes/gziphandler v1.1.1 github.com/envoyproxy/protoc-gen-validate v0.9.1 - github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9 + github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13 github.com/getsentry/sentry-go v0.19.0 github.com/go-co-op/gocron v1.18.1 github.com/google/go-cmp v0.5.9 @@ -28,7 +28,7 @@ require ( go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 go.opentelemetry.io/otel/sdk v1.14.0 go.opentelemetry.io/otel/trace v1.14.0 - golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6 + golang.org/x/exp v0.0.0-20230307190834-24139beb5833 google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 google.golang.org/grpc v1.53.0 google.golang.org/protobuf v1.28.1 diff --git a/go.sum b/go.sum index 90d58bdf..2dc111dc 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,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.0.9 h1:AHvkO38U6t2GYWFvsU9aL2eymff3rw2UfRjgUlmVVXo= -github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.9/go.mod h1:8ubBVLIi3KM+Xpq3/PHKtD5ev5wtcEobNTSKbvQaUmE= +github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13 h1:XiCVN09Xc/lcVIGcH+wtDU97V2r317Fchiri94HccwY= +github.com/gatewayd-io/gatewayd-plugin-sdk v0.0.13/go.mod h1:8ubBVLIi3KM+Xpq3/PHKtD5ev5wtcEobNTSKbvQaUmE= github.com/getsentry/sentry-go v0.19.0 h1:BcCH3CN5tXt5aML+gwmbFwVptLLQA+eT866fCO9wVOM= github.com/getsentry/sentry-go v0.19.0/go.mod h1:y3+lGEFEFexZtpbG1GUE2WD/f9zGyKYwpEqryTOC/nE= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= @@ -463,8 +463,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= -golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6 h1:3p+wVC0x0TCIPgd3LCQlpgVlEtjziEC5v42w7+B8t8M= -golang.org/x/exp v0.0.0-20230306221820-f0f767cdffd6/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s= +golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/network/proxy.go b/network/proxy.go index d8924f59..b53e4853 100644 --- a/network/proxy.go +++ b/network/proxy.go @@ -4,7 +4,7 @@ import ( "context" "time" - sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/config" gerr "github.com/gatewayd-io/gatewayd/errors" "github.com/gatewayd-io/gatewayd/metrics" @@ -293,7 +293,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError { }, }, origErr), - sdkPlugin.OnTrafficFromClient) + v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT) if err != nil { pr.logger.Error().Err(err).Msg("Error running hook") span.RecordError(err) @@ -337,7 +337,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError { }, }, err), - sdkPlugin.OnTrafficToServer) + v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_SERVER) if err != nil { pr.logger.Error().Err(err).Msg("Error running hook") span.RecordError(err) @@ -398,7 +398,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError { }, }, err), - sdkPlugin.OnTrafficFromServer) + v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_SERVER) if err != nil { pr.logger.Error().Err(err).Msg("Error running hook") span.RecordError(err) @@ -434,7 +434,7 @@ func (pr *Proxy) PassThrough(gconn gnet.Conn) *gerr.GatewayDError { }, err, ), - sdkPlugin.OnTrafficToClient) + v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_CLIENT) if err != nil { pr.logger.Error().Err(err).Msg("Error running hook") span.RecordError(err) diff --git a/network/server.go b/network/server.go index bbe1bb27..317d6d37 100644 --- a/network/server.go +++ b/network/server.go @@ -8,7 +8,7 @@ import ( "os" "time" - sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/config" gerr "github.com/gatewayd-io/gatewayd/errors" "github.com/gatewayd-io/gatewayd/metrics" @@ -49,7 +49,7 @@ func (s *Server) OnBoot(engine gnet.Engine) gnet.Action { _, err := s.pluginRegistry.Run( context.Background(), map[string]interface{}{"status": fmt.Sprint(s.Status)}, - sdkPlugin.OnBooting) + v1.HookName_HOOK_NAME_ON_BOOTING) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnBooting hook") span.RecordError(err) @@ -65,7 +65,7 @@ func (s *Server) OnBoot(engine gnet.Engine) gnet.Action { _, err = s.pluginRegistry.Run( context.Background(), map[string]interface{}{"status": fmt.Sprint(s.Status)}, - sdkPlugin.OnBooted) + v1.HookName_HOOK_NAME_ON_BOOTED) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnBooted hook") span.RecordError(err) @@ -93,7 +93,8 @@ func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) { "remote": gconn.RemoteAddr().String(), }, } - _, err := s.pluginRegistry.Run(context.Background(), onOpeningData, sdkPlugin.OnOpening) + _, err := s.pluginRegistry.Run( + context.Background(), onOpeningData, v1.HookName_HOOK_NAME_ON_OPENING) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnOpening hook") span.RecordError(err) @@ -138,7 +139,8 @@ func (s *Server) OnOpen(gconn gnet.Conn) ([]byte, gnet.Action) { "remote": gconn.RemoteAddr().String(), }, } - _, err = s.pluginRegistry.Run(context.Background(), onOpenedData, sdkPlugin.OnOpened) + _, err = s.pluginRegistry.Run( + context.Background(), onOpenedData, v1.HookName_HOOK_NAME_ON_OPENED) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnOpened hook") span.RecordError(err) @@ -171,7 +173,8 @@ func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action { if err != nil { data["error"] = err.Error() } - _, gatewaydErr := s.pluginRegistry.Run(context.Background(), data, sdkPlugin.OnClosing) + _, gatewaydErr := s.pluginRegistry.Run( + context.Background(), data, v1.HookName_HOOK_NAME_ON_CLOSING) if gatewaydErr != nil { s.logger.Error().Err(gatewaydErr).Msg("Failed to run OnClosing hook") span.RecordError(gatewaydErr) @@ -205,7 +208,8 @@ func (s *Server) OnClose(gconn gnet.Conn, err error) gnet.Action { if err != nil { data["error"] = err.Error() } - _, gatewaydErr = s.pluginRegistry.Run(context.Background(), data, sdkPlugin.OnClosed) + _, gatewaydErr = s.pluginRegistry.Run( + context.Background(), data, v1.HookName_HOOK_NAME_ON_CLOSED) if gatewaydErr != nil { s.logger.Error().Err(gatewaydErr).Msg("Failed to run OnClosed hook") span.RecordError(gatewaydErr) @@ -230,7 +234,8 @@ func (s *Server) OnTraffic(gconn gnet.Conn) gnet.Action { "remote": gconn.RemoteAddr().String(), }, } - _, err := s.pluginRegistry.Run(context.Background(), onTrafficData, sdkPlugin.OnTraffic) + _, err := s.pluginRegistry.Run( + context.Background(), onTrafficData, v1.HookName_HOOK_NAME_ON_TRAFFIC) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnTraffic hook") span.RecordError(err) @@ -271,7 +276,7 @@ func (s *Server) OnShutdown(engine gnet.Engine) { _, err := s.pluginRegistry.Run( context.Background(), map[string]interface{}{"connections": s.engine.CountConnections()}, - sdkPlugin.OnShutdown) + v1.HookName_HOOK_NAME_ON_SHUTDOWN) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnShutdown hook") span.RecordError(err) @@ -298,7 +303,7 @@ func (s *Server) OnTick() (time.Duration, gnet.Action) { _, err := s.pluginRegistry.Run( context.Background(), map[string]interface{}{"connections": s.engine.CountConnections()}, - sdkPlugin.OnTick) + v1.HookName_HOOK_NAME_ON_TICK) if err != nil { s.logger.Error().Err(err).Msg("Failed to run OnTick hook") span.RecordError(err) @@ -334,7 +339,8 @@ func (s *Server) Run() error { if err != nil && err.Unwrap() != nil { onRunData["error"] = err.OriginalError.Error() } - result, err := s.pluginRegistry.Run(context.Background(), onRunData, sdkPlugin.OnRun) + result, err := s.pluginRegistry.Run( + context.Background(), onRunData, v1.HookName_HOOK_NAME_ON_RUN) if err != nil { s.logger.Error().Err(err).Msg("Failed to run the hook") span.RecordError(err) diff --git a/network/server_test.go b/network/server_test.go index b36278b5..ece7e4d0 100644 --- a/network/server_test.go +++ b/network/server_test.go @@ -6,7 +6,7 @@ import ( "errors" "testing" - sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/config" "github.com/gatewayd-io/gatewayd/logging" "github.com/gatewayd-io/gatewayd/plugin" @@ -59,7 +59,7 @@ func TestRunServer(t *testing.T) { assert.Empty(t, paramsMap["error"]) return params, nil } - pluginRegistry.AddHook(sdkPlugin.OnTrafficFromClient, 1, onTrafficFromClient) + pluginRegistry.AddHook(v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT, 1, onTrafficFromClient) onTrafficToServer := func( ctx context.Context, @@ -87,7 +87,7 @@ func TestRunServer(t *testing.T) { assert.Empty(t, paramsMap["error"]) return params, nil } - pluginRegistry.AddHook(sdkPlugin.OnTrafficToServer, 1, onTrafficToServer) + pluginRegistry.AddHook(v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_SERVER, 1, onTrafficToServer) onTrafficFromServer := func( ctx context.Context, @@ -114,7 +114,7 @@ func TestRunServer(t *testing.T) { assert.Empty(t, paramsMap["error"]) return params, nil } - pluginRegistry.AddHook(sdkPlugin.OnTrafficFromServer, 1, onTrafficFromServer) + pluginRegistry.AddHook(v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_SERVER, 1, onTrafficFromServer) onTrafficToClient := func( ctx context.Context, @@ -139,7 +139,7 @@ func TestRunServer(t *testing.T) { assert.Empty(t, paramsMap["error"]) return params, nil } - pluginRegistry.AddHook(sdkPlugin.OnTrafficToClient, 1, onTrafficToClient) + pluginRegistry.AddHook(v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_CLIENT, 1, onTrafficToClient) clientConfig := config.Client{ Network: "tcp", diff --git a/network/utils.go b/network/utils.go index b14c7b3b..f28d047e 100644 --- a/network/utils.go +++ b/network/utils.go @@ -18,7 +18,7 @@ import ( // GetRLimit returns the current system soft and hard limits for the number of open files. func GetRLimit(logger zerolog.Logger) syscall.Rlimit { var limits syscall.Rlimit - if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { //nolint:nosnakecase + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limits); err != nil { logger.Error().Err(err).Msg("Failed to get rlimit") } logger.Debug().Str("value", fmt.Sprint(limits.Cur)).Msg("Current system soft limit") @@ -63,7 +63,7 @@ func Resolve(network, address string, logger zerolog.Logger) (string, *gerr.Gate } } -// trafficData creates the ingress/egress map for the OnIngressTraffic/OnEgresTraffic hooks. +// trafficData creates the ingress/egress map for the traffic hooks. func trafficData( gconn gnet.Conn, client *Client, diff --git a/plugin/plugin_registry.go b/plugin/plugin_registry.go index d185650f..80422823 100644 --- a/plugin/plugin_registry.go +++ b/plugin/plugin_registry.go @@ -22,12 +22,12 @@ import ( ) type IHook interface { - AddHook(hookName string, priority sdkPlugin.Priority, hookMethod sdkPlugin.Method) - Hooks() map[string]map[sdkPlugin.Priority]sdkPlugin.Method + AddHook(hookName v1.HookName, priority sdkPlugin.Priority, hookMethod sdkPlugin.Method) + Hooks() map[v1.HookName]map[sdkPlugin.Priority]sdkPlugin.Method Run( ctx context.Context, args map[string]interface{}, - hookName string, + hookName v1.HookName, opts ...grpc.CallOption, ) (map[string]interface{}, *gerr.GatewayDError) } @@ -52,7 +52,7 @@ type IRegistry interface { type Registry struct { plugins pool.IPool - hooks map[string]map[sdkPlugin.Priority]sdkPlugin.Method + hooks map[v1.HookName]map[sdkPlugin.Priority]sdkPlugin.Method ctx context.Context //nolint:containedctx devMode bool @@ -78,7 +78,7 @@ func NewRegistry( return &Registry{ plugins: pool.NewPool(regCtx, config.EmptyPoolCapacity), - hooks: map[string]map[sdkPlugin.Priority]sdkPlugin.Method{}, + hooks: map[v1.HookName]map[sdkPlugin.Priority]sdkPlugin.Method{}, ctx: regCtx, devMode: devMode, Logger: logger, @@ -219,7 +219,7 @@ func (reg *Registry) Shutdown() { } // Hooks returns the hooks map. -func (reg *Registry) Hooks() map[string]map[sdkPlugin.Priority]sdkPlugin.Method { +func (reg *Registry) Hooks() map[v1.HookName]map[sdkPlugin.Priority]sdkPlugin.Method { _, span := otel.Tracer(config.TracerName).Start(reg.ctx, "Hooks") defer span.End() @@ -227,7 +227,7 @@ func (reg *Registry) Hooks() map[string]map[sdkPlugin.Priority]sdkPlugin.Method } // Add adds a hook with a priority to the hooks map. -func (reg *Registry) AddHook(hookName string, priority sdkPlugin.Priority, hookMethod sdkPlugin.Method) { +func (reg *Registry) AddHook(hookName v1.HookName, priority sdkPlugin.Priority, hookMethod sdkPlugin.Method) { _, span := otel.Tracer(config.TracerName).Start(reg.ctx, "AddHook") defer span.End() @@ -237,7 +237,7 @@ func (reg *Registry) AddHook(hookName string, priority sdkPlugin.Priority, hookM if _, ok := reg.hooks[hookName][priority]; ok { reg.Logger.Warn().Fields( map[string]interface{}{ - "hookName": hookName, + "hookName": hookName.String(), "priority": priority, }, ).Msg("Hook is replaced") @@ -261,7 +261,7 @@ func (reg *Registry) AddHook(hookName string, priority sdkPlugin.Priority, hookM func (reg *Registry) Run( ctx context.Context, args map[string]interface{}, - hookName string, + hookName v1.HookName, opts ...grpc.CallOption, ) (map[string]interface{}, *gerr.GatewayDError) { _, span := otel.Tracer(config.TracerName).Start(reg.ctx, "Run") @@ -314,7 +314,7 @@ func (reg *Registry) Run( if err != nil { reg.Logger.Error().Err(err).Fields( map[string]interface{}{ - "hookName": hookName, + "hookName": hookName.String(), "priority": priority, }, ).Msg("Hook returned an error") @@ -606,58 +606,71 @@ func (reg *Registry) RegisterHooks(ctx context.Context, pluginID sdkPlugin.Ident return } - span.SetAttributes(attribute.StringSlice("hooks", pluginImpl.Hooks)) + reg.Logger.Info().Str("name", pluginImpl.ID.Name).Msg("Registering plugin hooks") + hooks := make([]string, 0) + for _, hook := range pluginImpl.Hooks { + hooks = append(hooks, hook.String()) + } + span.SetAttributes(attribute.StringSlice("hooks", hooks)) + reg.Logger.Debug().Str("name", pluginImpl.ID.Name).Msgf( + "Plugin hooks: %+v", pluginImpl.Hooks) for _, hookName := range pluginImpl.Hooks { var hookMethod sdkPlugin.Method switch hookName { - case sdkPlugin.OnConfigLoaded: + case v1.HookName_HOOK_NAME_UNSPECIFIED: + reg.Logger.Debug().Str("name", pluginImpl.ID.Name).Msg( + "Plugin hook is unspecified or invalid, so it won't work properly") + reg.Logger.Debug().Str("name", pluginImpl.ID.Name).Msg( + "Consider casting the enum value to an int32") + case v1.HookName_HOOK_NAME_ON_CONFIG_LOADED: hookMethod = pluginV1.OnConfigLoaded - case sdkPlugin.OnNewLogger: + case v1.HookName_HOOK_NAME_ON_NEW_LOGGER: hookMethod = pluginV1.OnNewLogger - case sdkPlugin.OnNewPool: + case v1.HookName_HOOK_NAME_ON_NEW_POOL: hookMethod = pluginV1.OnNewPool - case sdkPlugin.OnNewProxy: + case v1.HookName_HOOK_NAME_ON_NEW_CLIENT: + hookMethod = pluginV1.OnNewClient + case v1.HookName_HOOK_NAME_ON_NEW_PROXY: hookMethod = pluginV1.OnNewProxy - case sdkPlugin.OnNewServer: + case v1.HookName_HOOK_NAME_ON_NEW_SERVER: hookMethod = pluginV1.OnNewServer - case sdkPlugin.OnSignal: + case v1.HookName_HOOK_NAME_ON_SIGNAL: hookMethod = pluginV1.OnSignal - case sdkPlugin.OnRun: + case v1.HookName_HOOK_NAME_ON_RUN: hookMethod = pluginV1.OnRun - case sdkPlugin.OnBooting: + case v1.HookName_HOOK_NAME_ON_BOOTING: hookMethod = pluginV1.OnBooting - case sdkPlugin.OnBooted: + case v1.HookName_HOOK_NAME_ON_BOOTED: hookMethod = pluginV1.OnBooted - case sdkPlugin.OnOpening: + case v1.HookName_HOOK_NAME_ON_OPENING: hookMethod = pluginV1.OnOpening - case sdkPlugin.OnOpened: + case v1.HookName_HOOK_NAME_ON_OPENED: hookMethod = pluginV1.OnOpened - case sdkPlugin.OnClosing: + case v1.HookName_HOOK_NAME_ON_CLOSING: hookMethod = pluginV1.OnClosing - case sdkPlugin.OnClosed: + case v1.HookName_HOOK_NAME_ON_CLOSED: hookMethod = pluginV1.OnClosed - case sdkPlugin.OnTraffic: + case v1.HookName_HOOK_NAME_ON_TRAFFIC: hookMethod = pluginV1.OnTraffic - case sdkPlugin.OnTrafficFromClient: + case v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_CLIENT: hookMethod = pluginV1.OnTrafficFromClient - case sdkPlugin.OnTrafficToServer: + case v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_SERVER: hookMethod = pluginV1.OnTrafficToServer - case sdkPlugin.OnTrafficFromServer: + case v1.HookName_HOOK_NAME_ON_TRAFFIC_FROM_SERVER: hookMethod = pluginV1.OnTrafficFromServer - case sdkPlugin.OnTrafficToClient: + case v1.HookName_HOOK_NAME_ON_TRAFFIC_TO_CLIENT: hookMethod = pluginV1.OnTrafficToClient - case sdkPlugin.OnShutdown: + case v1.HookName_HOOK_NAME_ON_SHUTDOWN: hookMethod = pluginV1.OnShutdown - case sdkPlugin.OnTick: + case v1.HookName_HOOK_NAME_ON_TICK: hookMethod = pluginV1.OnTick - case sdkPlugin.OnNewClient: - hookMethod = pluginV1.OnNewClient + case v1.HookName_HOOK_NAME_ON_HOOK: // fallthrough default: switch reg.Acceptance { case config.Reject: reg.Logger.Warn().Fields(map[string]interface{}{ - "hook": hookName, + "hook": hookName.String(), "priority": pluginImpl.Priority, "name": pluginImpl.ID.Name, }).Msg("Unknown hook, skipping") @@ -665,7 +678,7 @@ func (reg *Registry) RegisterHooks(ctx context.Context, pluginID sdkPlugin.Ident default: // Default is to accept custom hooks. reg.Logger.Debug().Fields(map[string]interface{}{ - "hook": hookName, + "hook": hookName.String(), "priority": pluginImpl.Priority, "name": pluginImpl.ID.Name, }).Msg("Registering a custom hook") @@ -676,7 +689,7 @@ func (reg *Registry) RegisterHooks(ctx context.Context, pluginID sdkPlugin.Ident } reg.Logger.Debug().Fields(map[string]interface{}{ - "hook": hookName, + "hook": hookName.String(), "priority": pluginImpl.Priority, "name": pluginImpl.ID.Name, }).Msg("Registering hook") diff --git a/plugin/plugin_registry_test.go b/plugin/plugin_registry_test.go index 107deba6..6c9a9a06 100644 --- a/plugin/plugin_registry_test.go +++ b/plugin/plugin_registry_test.go @@ -5,6 +5,7 @@ import ( "testing" sdkPlugin "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin" + v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1" "github.com/gatewayd-io/gatewayd/config" "github.com/gatewayd-io/gatewayd/logging" "github.com/rs/zerolog" @@ -68,44 +69,44 @@ func Test_PluginRegistry_AddHook(t *testing.T) { } reg := NewPluginRegistry(t) - reg.AddHook(sdkPlugin.OnNewLogger, 0, testFunc) - assert.NotNil(t, reg.Hooks()[sdkPlugin.OnNewLogger][0]) - assert.ObjectsAreEqual(testFunc, reg.Hooks()[sdkPlugin.OnNewLogger][0]) + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, testFunc) + assert.NotNil(t, reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER][0]) + assert.ObjectsAreEqual(testFunc, reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER][0]) } // Test_HookRegistry_Add_Multiple_Hooks tests the Add function with multiple hooks. func Test_PluginRegistry_AddHook_Multiple(t *testing.T) { reg := NewPluginRegistry(t) - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { return args, nil }) - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { return args, nil }) - assert.NotNil(t, reg.Hooks()[sdkPlugin.OnNewLogger][0]) - assert.NotNil(t, reg.Hooks()[sdkPlugin.OnNewLogger][1]) + assert.NotNil(t, reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER][0]) + assert.NotNil(t, reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER][1]) } // Test_HookRegistry_Run tests the Run function. func Test_PluginRegistry_Run(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.Ignore - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { return args, nil }) - result, err := reg.Run(context.Background(), map[string]interface{}{}, sdkPlugin.OnNewLogger) + result, err := reg.Run(context.Background(), map[string]interface{}{}, v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.NotNil(t, result) assert.Nil(t, err) } @@ -115,7 +116,7 @@ func Test_PluginRegistry_Run_PassDown(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.PassDown // The result of the hook will be nil and will be passed down to the next - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -123,7 +124,7 @@ func Test_PluginRegistry_Run_PassDown(t *testing.T) { return nil, nil //nolint:nilnil }) // The consolidated result should be {"test": "test"}. - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -141,7 +142,7 @@ func Test_PluginRegistry_Run_PassDown(t *testing.T) { result, err := reg.Run( context.Background(), map[string]interface{}{"test": "test"}, - sdkPlugin.OnNewLogger) + v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.Nil(t, err) assert.NotNil(t, result) } @@ -151,26 +152,26 @@ func Test_HookRegistry_Run_PassDown_2(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.PassDown // The result of the hook will be nil and will be passed down to the next - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { args.Fields["test1"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ //nolint:nosnakecase + Kind: &structpb.Value_StringValue{ StringValue: "test1", }, } return args, nil }) // The consolidated result should be {"test1": "test1", "test2": "test2"}. - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { args.Fields["test2"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ //nolint:nosnakecase + Kind: &structpb.Value_StringValue{ StringValue: "test2", }, } @@ -182,7 +183,7 @@ func Test_HookRegistry_Run_PassDown_2(t *testing.T) { result, err := reg.Run( context.Background(), map[string]interface{}{"test": "test"}, - sdkPlugin.OnNewLogger) + v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.Nil(t, err) assert.NotNil(t, result) } @@ -192,7 +193,7 @@ func Test_HookRegistry_Run_Ignore(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.Ignore // This should not run, because the return value is not the same as the params - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -200,13 +201,13 @@ func Test_HookRegistry_Run_Ignore(t *testing.T) { return nil, nil //nolint:nilnil }) // This should run, because the return value is the same as the params - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, ) (*structpb.Struct, error) { args.Fields["test"] = &structpb.Value{ - Kind: &structpb.Value_StringValue{ //nolint:nosnakecase + Kind: &structpb.Value_StringValue{ StringValue: "test", }, } @@ -218,7 +219,7 @@ func Test_HookRegistry_Run_Ignore(t *testing.T) { result, err := reg.Run( context.Background(), map[string]interface{}{"test": "test"}, - sdkPlugin.OnNewLogger) + v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.Nil(t, err) assert.NotNil(t, result) } @@ -228,7 +229,7 @@ func Test_HookRegistry_Run_Abort(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.Abort // This should not run, because the return value is not the same as the params - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -236,7 +237,7 @@ func Test_HookRegistry_Run_Abort(t *testing.T) { return nil, nil //nolint:nilnil }) // This should not run, because the first hook returns nil, and its result is ignored. - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -248,7 +249,7 @@ func Test_HookRegistry_Run_Abort(t *testing.T) { return output, nil }) // The first hook returns nil, and it aborts the execution of the rest of the - result, err := reg.Run(context.Background(), map[string]interface{}{}, sdkPlugin.OnNewLogger) + result, err := reg.Run(context.Background(), map[string]interface{}{}, v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.Nil(t, err) assert.Equal(t, map[string]interface{}{}, result) } @@ -258,7 +259,7 @@ func Test_HookRegistry_Run_Remove(t *testing.T) { reg := NewPluginRegistry(t) reg.Verification = config.Remove // This should not run, because the return value is not the same as the params - reg.AddHook(sdkPlugin.OnNewLogger, 0, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 0, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -266,7 +267,7 @@ func Test_HookRegistry_Run_Remove(t *testing.T) { return nil, nil //nolint:nilnil }) // This should not run, because the first hook returns nil, and its result is ignored. - reg.AddHook(sdkPlugin.OnNewLogger, 1, func( + reg.AddHook(v1.HookName_HOOK_NAME_ON_NEW_LOGGER, 1, func( ctx context.Context, args *structpb.Struct, opts ...grpc.CallOption, @@ -280,8 +281,8 @@ func Test_HookRegistry_Run_Remove(t *testing.T) { // The first hook returns nil, and its signature doesn't match the params, // so its result is ignored. The failing hook is removed from the list and // the execution continues with the next hook in the list. - result, err := reg.Run(context.Background(), map[string]interface{}{}, sdkPlugin.OnNewLogger) + result, err := reg.Run(context.Background(), map[string]interface{}{}, v1.HookName_HOOK_NAME_ON_NEW_LOGGER) assert.Nil(t, err) assert.Equal(t, map[string]interface{}{}, result) - assert.Equal(t, 1, len(reg.Hooks()[sdkPlugin.OnNewLogger])) + assert.Equal(t, 1, len(reg.Hooks()[v1.HookName_HOOK_NAME_ON_NEW_LOGGER])) }