2022-01-06 10:20:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-04-25 15:34:49 +00:00
|
|
|
"errors"
|
2022-01-06 10:20:59 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-09-29 14:59:06 +00:00
|
|
|
"time"
|
2022-01-06 10:20:59 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
|
|
|
mail "github.com/xhit/go-simple-mail/v2"
|
2024-04-25 15:34:49 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
2022-01-06 10:20:59 +00:00
|
|
|
)
|
|
|
|
|
2022-08-16 16:07:00 +00:00
|
|
|
var baseLogger hclog.Logger = hclog.New(&hclog.LoggerOptions{
|
2022-01-06 10:20:59 +00:00
|
|
|
Name: "email-plugin",
|
2022-03-10 12:56:46 +00:00
|
|
|
Level: hclog.LevelFromString("INFO"),
|
2022-01-06 10:20:59 +00:00
|
|
|
Output: os.Stderr,
|
|
|
|
JSONFormat: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
var AuthStringToType map[string]mail.AuthType = map[string]mail.AuthType{
|
|
|
|
"none": mail.AuthNone,
|
|
|
|
"crammd5": mail.AuthCRAMMD5,
|
|
|
|
"login": mail.AuthLogin,
|
|
|
|
"plain": mail.AuthPlain,
|
|
|
|
}
|
|
|
|
|
|
|
|
var EncryptionStringToType map[string]mail.Encryption = map[string]mail.Encryption{
|
2022-08-26 14:49:31 +00:00
|
|
|
"ssltls": mail.EncryptionSSLTLS,
|
|
|
|
"starttls": mail.EncryptionSTARTTLS,
|
|
|
|
"none": mail.EncryptionNone,
|
2022-01-06 10:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PluginConfig struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
LogLevel *string `yaml:"log_level"`
|
|
|
|
|
|
|
|
SMTPHost string `yaml:"smtp_host"`
|
|
|
|
SMTPPort int `yaml:"smtp_port"`
|
|
|
|
SMTPUsername string `yaml:"smtp_username"`
|
|
|
|
SMTPPassword string `yaml:"smtp_password"`
|
|
|
|
SenderEmail string `yaml:"sender_email"`
|
2022-03-07 09:18:12 +00:00
|
|
|
SenderName string `yaml:"sender_name"`
|
2022-01-06 10:20:59 +00:00
|
|
|
ReceiverEmails []string `yaml:"receiver_emails"`
|
|
|
|
EmailSubject string `yaml:"email_subject"`
|
|
|
|
EncryptionType string `yaml:"encryption_type"`
|
|
|
|
AuthType string `yaml:"auth_type"`
|
2022-10-17 19:42:31 +00:00
|
|
|
HeloHost string `yaml:"helo_host"`
|
2023-09-29 14:59:06 +00:00
|
|
|
ConnectTimeout string `yaml:"connect_timeout"`
|
|
|
|
SendTimeout string `yaml:"send_timeout"`
|
2022-01-06 10:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EmailPlugin struct {
|
|
|
|
ConfigByName map[string]PluginConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
2022-03-07 09:18:12 +00:00
|
|
|
d := PluginConfig{
|
2022-04-27 10:07:54 +00:00
|
|
|
SMTPPort: 25,
|
2022-03-07 09:18:12 +00:00
|
|
|
SenderName: "Crowdsec",
|
|
|
|
EmailSubject: "Crowdsec notification",
|
|
|
|
EncryptionType: "ssltls",
|
|
|
|
AuthType: "login",
|
2022-04-27 10:07:54 +00:00
|
|
|
SenderEmail: "crowdsec@crowdsec.local",
|
2022-10-17 19:42:31 +00:00
|
|
|
HeloHost: "localhost",
|
2022-03-07 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
if err := yaml.Unmarshal(config.Config, &d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-07 09:18:12 +00:00
|
|
|
|
|
|
|
if d.Name == "" {
|
2024-04-25 15:34:49 +00:00
|
|
|
return nil, errors.New("name is required")
|
2022-03-07 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.SMTPHost == "" {
|
2024-04-25 15:34:49 +00:00
|
|
|
return nil, errors.New("SMTP host is not set")
|
2022-03-07 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if d.ReceiverEmails == nil || len(d.ReceiverEmails) == 0 {
|
2024-04-25 15:34:49 +00:00
|
|
|
return nil, errors.New("receiver emails are not set")
|
2022-03-07 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
n.ConfigByName[d.Name] = d
|
2022-09-08 09:41:28 +00:00
|
|
|
baseLogger.Debug(fmt.Sprintf("Email plugin '%s' use SMTP host '%s:%d'", d.Name, d.SMTPHost, d.SMTPPort))
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
return &protobufs.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
|
|
|
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
|
|
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
|
|
|
}
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
cfg := n.ConfigByName[notification.Name]
|
2022-03-10 12:56:46 +00:00
|
|
|
|
2022-08-16 16:07:00 +00:00
|
|
|
logger := baseLogger.Named(cfg.Name)
|
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
|
|
|
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
|
|
|
|
}
|
2022-03-10 12:56:46 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
logger.Debug("got notification")
|
|
|
|
|
|
|
|
server := mail.NewSMTPClient()
|
|
|
|
server.Host = cfg.SMTPHost
|
|
|
|
server.Port = cfg.SMTPPort
|
|
|
|
server.Username = cfg.SMTPUsername
|
|
|
|
server.Password = cfg.SMTPPassword
|
|
|
|
server.Encryption = EncryptionStringToType[cfg.EncryptionType]
|
|
|
|
server.Authentication = AuthStringToType[cfg.AuthType]
|
2022-10-17 19:42:31 +00:00
|
|
|
server.Helo = cfg.HeloHost
|
2022-01-06 10:20:59 +00:00
|
|
|
|
2023-09-29 14:59:06 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if cfg.ConnectTimeout != "" {
|
|
|
|
server.ConnectTimeout, err = time.ParseDuration(cfg.ConnectTimeout)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(fmt.Sprintf("invalid connect timeout '%s', using default '10s'", cfg.ConnectTimeout))
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2023-09-29 14:59:06 +00:00
|
|
|
server.ConnectTimeout = 10 * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.SendTimeout != "" {
|
|
|
|
server.SendTimeout, err = time.ParseDuration(cfg.SendTimeout)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(fmt.Sprintf("invalid send timeout '%s', using default '10s'", cfg.SendTimeout))
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2023-09-29 14:59:06 +00:00
|
|
|
server.SendTimeout = 10 * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
logger.Debug("making smtp connection")
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
smtpClient, err := server.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return &protobufs.Empty{}, err
|
|
|
|
}
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
logger.Debug("smtp connection done")
|
|
|
|
|
|
|
|
email := mail.NewMSG()
|
2022-03-07 09:18:12 +00:00
|
|
|
email.SetFrom(fmt.Sprintf("%s <%s>", cfg.SenderName, cfg.SenderEmail)).
|
2022-01-06 10:20:59 +00:00
|
|
|
AddTo(cfg.ReceiverEmails...).
|
|
|
|
SetSubject(cfg.EmailSubject)
|
|
|
|
email.SetBody(mail.TextHTML, notification.Text)
|
|
|
|
|
|
|
|
err = email.Send(smtpClient)
|
|
|
|
if err != nil {
|
|
|
|
return &protobufs.Empty{}, err
|
|
|
|
}
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-02-01 21:08:06 +00:00
|
|
|
logger.Info(fmt.Sprintf("sent email to %v", cfg.ReceiverEmails))
|
2024-04-25 15:34:49 +00:00
|
|
|
|
2022-01-06 10:20:59 +00:00
|
|
|
return &protobufs.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-04-25 15:34:49 +00:00
|
|
|
handshake := plugin.HandshakeConfig{
|
2022-01-06 10:20:59 +00:00
|
|
|
ProtocolVersion: 1,
|
|
|
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
|
|
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin.Serve(&plugin.ServeConfig{
|
|
|
|
HandshakeConfig: handshake,
|
|
|
|
Plugins: map[string]plugin.Plugin{
|
|
|
|
"email": &protobufs.NotifierPlugin{
|
|
|
|
Impl: &EmailPlugin{ConfigByName: make(map[string]PluginConfig)},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GRPCServer: plugin.DefaultGRPCServer,
|
2022-08-16 16:07:00 +00:00
|
|
|
Logger: baseLogger,
|
2022-01-06 10:20:59 +00:00
|
|
|
})
|
|
|
|
}
|