2021-08-25 09:43:29 +00:00
|
|
|
package csplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
"google.golang.org/grpc"
|
2023-06-22 09:31:41 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
2021-08-25 09:43:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type GRPCClient struct{ client protobufs.NotifierClient }
|
|
|
|
|
|
|
|
func (m *GRPCClient) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
|
|
|
done := make(chan error)
|
|
|
|
go func() {
|
|
|
|
_, err := m.client.Notify(
|
2023-12-04 12:05:26 +00:00
|
|
|
ctx, &protobufs.Notification{Text: notification.Text, Name: notification.Name},
|
2021-08-25 09:43:29 +00:00
|
|
|
)
|
|
|
|
done <- err
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case err := <-done:
|
|
|
|
return &protobufs.Empty{}, err
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
return &protobufs.Empty{}, fmt.Errorf("timeout exceeded")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GRPCClient) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
|
|
|
_, err := m.client.Configure(
|
|
|
|
context.Background(), config,
|
|
|
|
)
|
|
|
|
return &protobufs.Empty{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type GRPCServer struct {
|
|
|
|
Impl Notifier
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifierPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
|
|
|
|
protobufs.RegisterNotifierServer(s, p.Impl)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifierPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
|
|
|
|
return &GRPCClient{client: protobufs.NewNotifierClient(c)}, nil
|
|
|
|
}
|