Skip to content

Commit

Permalink
rest of the owl
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc committed Oct 4, 2024
1 parent bdd98fb commit 14a18df
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 192 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ generate: ## Generate code for the database and APIs
$(GO) generate ./pkg/database/ent
$(GO) generate ./pkg/models
$(GO) generate ./pkg/modelscapi
# TODO: install the protoc-gen-go plugin in CI
# $(GO) generate ./pkg/protobufs

.PHONY: testclean
testclean: bats-clean ## Remove test artifacts
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-dummy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
plugin "github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand All @@ -19,6 +20,7 @@ type PluginConfig struct {
}

type DummyPlugin struct {
protobufs.UnimplementedNotifierServer
PluginConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -84,7 +86,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"dummy": &protobufs.NotifierPlugin{
"dummy": &csplugin.NotifierPlugin{
Impl: sp,
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
mail "github.com/xhit/go-simple-mail/v2"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand Down Expand Up @@ -55,6 +56,7 @@ type PluginConfig struct {
}

type EmailPlugin struct {
protobufs.UnimplementedNotifierServer
ConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -170,7 +172,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"email": &protobufs.NotifierPlugin{
"email": &csplugin.NotifierPlugin{
Impl: &EmailPlugin{ConfigByName: make(map[string]PluginConfig)},
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
plugin "github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand Down Expand Up @@ -52,6 +53,7 @@ type LogRotate struct {
}

type FilePlugin struct {
protobufs.UnimplementedNotifierServer
PluginConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -241,7 +243,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"file": &protobufs.NotifierPlugin{
"file": &csplugin.NotifierPlugin{
Impl: sp,
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
plugin "github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand All @@ -34,6 +35,7 @@ type PluginConfig struct {
}

type HTTPPlugin struct {
protobufs.UnimplementedNotifierServer
PluginConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -190,7 +192,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"http": &protobufs.NotifierPlugin{
"http": &csplugin.NotifierPlugin{
Impl: sp,
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand All @@ -27,6 +28,7 @@ type PluginConfig struct {
}

type SentinelPlugin struct {
protobufs.UnimplementedNotifierServer
PluginConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -122,7 +124,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"sentinel": &protobufs.NotifierPlugin{
"sentinel": &csplugin.NotifierPlugin{
Impl: sp,
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/slack-go/slack"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand All @@ -23,6 +24,7 @@ type PluginConfig struct {
LogLevel *string `yaml:"log_level"`
}
type Notify struct {
protobufs.UnimplementedNotifierServer
ConfigByName map[string]PluginConfig
}

Expand Down Expand Up @@ -84,7 +86,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"slack": &protobufs.NotifierPlugin{
"slack": &csplugin.NotifierPlugin{
Impl: &Notify{ConfigByName: make(map[string]PluginConfig)},
},
},
Expand Down
4 changes: 3 additions & 1 deletion cmd/notification-splunk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
plugin "github.com/hashicorp/go-plugin"
"gopkg.in/yaml.v3"

"github.com/crowdsecurity/crowdsec/pkg/csplugin"
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

Expand All @@ -32,6 +33,7 @@ type PluginConfig struct {
}

type Splunk struct {
protobufs.UnimplementedNotifierServer
PluginConfigByName map[string]PluginConfig
Client http.Client
}
Expand Down Expand Up @@ -117,7 +119,7 @@ func main() {
plugin.Serve(&plugin.ServeConfig{
HandshakeConfig: handshake,
Plugins: map[string]plugin.Plugin{
"splunk": &protobufs.NotifierPlugin{
"splunk": &csplugin.NotifierPlugin{
Impl: sp,
},
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/csplugin/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type PluginBroker struct {
pluginConfigByName map[string]PluginConfig
pluginMap map[string]plugin.Plugin
notificationConfigsByPluginType map[string][][]byte // "slack" -> []{config1, config2}
notificationPluginByName map[string]Notifier
notificationPluginByName map[string]protobufs.NotifierServer
watcher PluginWatcher
pluginKillMethods []func()
pluginProcConfig *csconfig.PluginCfg
Expand Down Expand Up @@ -75,7 +75,7 @@ type ProfileAlert struct {
func (pb *PluginBroker) Init(pluginCfg *csconfig.PluginCfg, profileConfigs []*csconfig.ProfileCfg, configPaths *csconfig.ConfigurationPaths) error {
pb.PluginChannel = make(chan ProfileAlert)
pb.notificationConfigsByPluginType = make(map[string][][]byte)
pb.notificationPluginByName = make(map[string]Notifier)
pb.notificationPluginByName = make(map[string]protobufs.NotifierServer)
pb.pluginMap = make(map[string]plugin.Plugin)
pb.pluginConfigByName = make(map[string]PluginConfig)
pb.alertsByPluginName = make(map[string][]*models.Alert)
Expand Down Expand Up @@ -276,7 +276,7 @@ func (pb *PluginBroker) loadPlugins(path string) error {
return pb.verifyPluginBinaryWithProfile()
}

func (pb *PluginBroker) loadNotificationPlugin(name string, binaryPath string) (Notifier, error) {
func (pb *PluginBroker) loadNotificationPlugin(name string, binaryPath string) (protobufs.NotifierServer, error) {

handshake, err := getHandshake()
if err != nil {
Expand Down Expand Up @@ -313,7 +313,7 @@ func (pb *PluginBroker) loadNotificationPlugin(name string, binaryPath string) (
return nil, err
}
pb.pluginKillMethods = append(pb.pluginKillMethods, c.Kill)
return raw.(Notifier), nil
return raw.(protobufs.NotifierServer), nil
}

func (pb *PluginBroker) pushNotificationsToPlugin(pluginName string, alerts []*models.Alert) error {
Expand Down
14 changes: 6 additions & 8 deletions pkg/csplugin/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ import (
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
)

type Notifier interface {
Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error)
Configure(ctx context.Context, cfg *protobufs.Config) (*protobufs.Empty, error)
}

type NotifierPlugin struct {
plugin.Plugin
Impl Notifier
Impl protobufs.NotifierServer
}

type GRPCClient struct{ client protobufs.NotifierClient }
type GRPCClient struct{
protobufs.UnimplementedNotifierServer
client protobufs.NotifierClient
}

func (m *GRPCClient) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
done := make(chan error)
Expand All @@ -45,7 +43,7 @@ func (m *GRPCClient) Configure(ctx context.Context, config *protobufs.Config) (*
}

type GRPCServer struct {
Impl Notifier
Impl protobufs.NotifierServer
}

func (p *NotifierPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
Expand Down
Loading

0 comments on commit 14a18df

Please sign in to comment.