2021-08-25 09:43:29 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2021-09-03 10:24:59 +00:00
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
2021-08-25 09:43:29 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
|
|
|
|
"github.com/slack-go/slack"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PluginConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Webhook string `yaml:"webhook"`
|
|
|
|
LogLevel *string `yaml:"log_level"`
|
|
|
|
}
|
|
|
|
type Notify struct {
|
|
|
|
ConfigByName map[string]PluginConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
|
|
|
|
Name: "slack-plugin",
|
2022-03-10 12:56:46 +00:00
|
|
|
Level: hclog.LevelFromString("INFO"),
|
2021-08-25 09:43:29 +00:00
|
|
|
Output: os.Stderr,
|
|
|
|
JSONFormat: true,
|
|
|
|
})
|
|
|
|
|
2021-09-03 10:24:59 +00:00
|
|
|
func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
2021-08-25 09:43:29 +00:00
|
|
|
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
|
|
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
|
|
|
}
|
|
|
|
cfg := n.ConfigByName[notification.Name]
|
2022-03-10 12:56:46 +00:00
|
|
|
|
2021-08-25 09:43:29 +00:00
|
|
|
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
|
|
|
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info(fmt.Sprintf("found notify signal for %s config", notification.Name))
|
|
|
|
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.Text))
|
|
|
|
err := slack.PostWebhook(n.ConfigByName[notification.Name].Webhook, &slack.WebhookMessage{
|
|
|
|
Text: notification.Text,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:24:59 +00:00
|
|
|
return &protobufs.Empty{}, err
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 10:24:59 +00:00
|
|
|
func (n *Notify) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
2021-08-25 09:43:29 +00:00
|
|
|
d := PluginConfig{}
|
|
|
|
if err := yaml.Unmarshal(config.Config, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
n.ConfigByName[d.Name] = d
|
2022-09-08 09:41:28 +00:00
|
|
|
logger.Debug(fmt.Sprintf("Slack plugin '%s' use URL '%s'", d.Name, d.Webhook))
|
2021-09-03 10:24:59 +00:00
|
|
|
return &protobufs.Empty{}, nil
|
2021-08-25 09:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var handshake = plugin.HandshakeConfig{
|
|
|
|
ProtocolVersion: 1,
|
|
|
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
|
|
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
|
|
HandshakeConfig: handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
2021-09-03 10:24:59 +00:00
|
|
|
"slack": &protobufs.NotifierPlugin{
|
2021-08-25 09:43:29 +00:00
|
|
|
Impl: &Notify{ConfigByName: make(map[string]PluginConfig)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
|
|
|
Logger: logger,
|
|
|
|
})
|
|
|
|
}
|