2015-06-04 10:23:46 -05:00
|
|
|
package notifications
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2015-06-05 04:08:19 -05:00
|
|
|
"errors"
|
2015-08-28 06:45:16 -05:00
|
|
|
"fmt"
|
2015-06-05 04:08:19 -05:00
|
|
|
"html/template"
|
2015-08-28 06:45:16 -05:00
|
|
|
"net/url"
|
2015-06-05 04:08:19 -05:00
|
|
|
"path/filepath"
|
2018-04-27 06:01:32 -05:00
|
|
|
"strings"
|
2015-06-04 10:23:46 -05:00
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-06-08 10:56:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/events"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-06-05 04:08:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mailTemplates *template.Template
|
2021-07-19 05:31:51 -05:00
|
|
|
var tmplResetPassword = "reset_password"
|
|
|
|
var tmplSignUpStarted = "signup_started"
|
|
|
|
var tmplWelcomeOnSignUp = "welcome_on_signup"
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
func ProvideService(bus bus.Bus, cfg *setting.Cfg) (*NotificationService, error) {
|
|
|
|
ns := &NotificationService{
|
|
|
|
Bus: bus,
|
|
|
|
Cfg: cfg,
|
|
|
|
log: log.New("notifications"),
|
|
|
|
mailQueue: make(chan *Message, 10),
|
|
|
|
webhookQueue: make(chan *Webhook, 10),
|
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2021-12-28 09:08:07 -06:00
|
|
|
ns.Bus.AddHandler(ns.sendResetPasswordEmail)
|
|
|
|
ns.Bus.AddHandler(ns.validateResetPasswordCode)
|
|
|
|
ns.Bus.AddHandler(ns.sendEmailCommandHandler)
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2021-12-28 09:08:07 -06:00
|
|
|
ns.Bus.AddHandler(ns.sendEmailCommandHandlerSync)
|
|
|
|
ns.Bus.AddHandler(ns.SendWebhookSync)
|
2016-06-15 07:45:05 -05:00
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
ns.Bus.AddEventListener(ns.signUpStartedHandler)
|
|
|
|
ns.Bus.AddEventListener(ns.signUpCompletedHandler)
|
2015-06-08 10:56:56 -05:00
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
mailTemplates = template.New("name")
|
|
|
|
mailTemplates.Funcs(template.FuncMap{
|
|
|
|
"Subject": subjectTemplateFunc,
|
|
|
|
})
|
|
|
|
|
2021-07-19 05:31:51 -05:00
|
|
|
for _, pattern := range ns.Cfg.Smtp.TemplatesPatterns {
|
|
|
|
templatePattern := filepath.Join(ns.Cfg.StaticRootPath, pattern)
|
|
|
|
_, err := mailTemplates.ParseGlob(templatePattern)
|
|
|
|
if err != nil {
|
2021-08-25 08:11:22 -05:00
|
|
|
return nil, err
|
2021-07-19 05:31:51 -05:00
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
}
|
|
|
|
|
2018-04-30 09:21:04 -05:00
|
|
|
if !util.IsEmail(ns.Cfg.Smtp.FromAddress) {
|
2021-08-25 08:11:22 -05:00
|
|
|
return nil, errors.New("invalid email address for SMTP from_address config")
|
2015-06-05 04:08:19 -05:00
|
|
|
}
|
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
if cfg.EmailCodeValidMinutes == 0 {
|
|
|
|
cfg.EmailCodeValidMinutes = 120
|
2015-06-08 03:57:01 -05:00
|
|
|
}
|
2021-08-25 08:11:22 -05:00
|
|
|
return ns, nil
|
|
|
|
}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
type NotificationService struct {
|
|
|
|
Bus bus.Bus
|
|
|
|
Cfg *setting.Cfg
|
|
|
|
|
|
|
|
mailQueue chan *Message
|
|
|
|
webhookQueue chan *Webhook
|
|
|
|
log log.Logger
|
2015-06-05 04:08:19 -05:00
|
|
|
}
|
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
func (ns *NotificationService) Run(ctx context.Context) error {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case webhook := <-ns.webhookQueue:
|
|
|
|
err := ns.sendWebRequestSync(context.Background(), webhook)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ns.log.Error("Failed to send webrequest ", "error", err)
|
|
|
|
}
|
|
|
|
case msg := <-ns.mailQueue:
|
2021-03-18 09:55:11 -05:00
|
|
|
num, err := ns.Send(msg)
|
2018-04-27 06:01:32 -05:00
|
|
|
tos := strings.Join(msg.To, "; ")
|
|
|
|
info := ""
|
|
|
|
if err != nil {
|
|
|
|
if len(msg.Info) > 0 {
|
|
|
|
info = ", info: " + msg.Info
|
|
|
|
}
|
|
|
|
ns.log.Error(fmt.Sprintf("Async sent email %d succeed, not send emails: %s%s err: %s", num, tos, info, err))
|
|
|
|
} else {
|
|
|
|
ns.log.Debug(fmt.Sprintf("Async sent email %d succeed, sent emails: %s%s", num, tos, info))
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func (ns *NotificationService) SendWebhookSync(ctx context.Context, cmd *models.SendWebhookSync) error {
|
2018-04-27 06:01:32 -05:00
|
|
|
return ns.sendWebRequestSync(ctx, &Webhook{
|
2017-03-23 15:53:54 -05:00
|
|
|
Url: cmd.Url,
|
|
|
|
User: cmd.User,
|
|
|
|
Password: cmd.Password,
|
|
|
|
Body: cmd.Body,
|
|
|
|
HttpMethod: cmd.HttpMethod,
|
|
|
|
HttpHeader: cmd.HttpHeader,
|
|
|
|
ContentType: cmd.ContentType,
|
2016-10-03 02:38:03 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:08:19 -05:00
|
|
|
func subjectTemplateFunc(obj map[string]interface{}, value string) string {
|
|
|
|
obj["value"] = value
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
func (ns *NotificationService) sendEmailCommandHandlerSync(ctx context.Context, cmd *models.SendEmailCommandSync) error {
|
|
|
|
message, err := ns.buildEmailMessage(&models.SendEmailCommand{
|
2020-06-01 10:11:25 -05:00
|
|
|
Data: cmd.Data,
|
|
|
|
Info: cmd.Info,
|
|
|
|
Template: cmd.Template,
|
|
|
|
To: cmd.To,
|
|
|
|
SingleEmail: cmd.SingleEmail,
|
|
|
|
EmbeddedFiles: cmd.EmbeddedFiles,
|
|
|
|
Subject: cmd.Subject,
|
2020-06-22 11:56:49 -05:00
|
|
|
ReplyTo: cmd.ReplyTo,
|
2016-10-03 02:38:03 -05:00
|
|
|
})
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2015-09-08 03:49:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-18 09:55:11 -05:00
|
|
|
_, err = ns.Send(message)
|
2016-10-03 02:38:03 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2021-12-01 10:56:08 -06:00
|
|
|
func (ns *NotificationService) sendEmailCommandHandler(ctx context.Context, cmd *models.SendEmailCommand) error {
|
2018-04-30 09:21:04 -05:00
|
|
|
message, err := ns.buildEmailMessage(cmd)
|
2015-06-13 23:07:36 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
ns.mailQueue <- message
|
2015-06-05 01:15:38 -05:00
|
|
|
return nil
|
2015-06-04 10:23:46 -05:00
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2021-12-01 10:56:08 -06:00
|
|
|
func (ns *NotificationService) sendResetPasswordEmail(ctx context.Context, cmd *models.SendResetPasswordEmailCommand) error {
|
2021-08-25 08:11:22 -05:00
|
|
|
code, err := createUserEmailCode(ns.Cfg, cmd.User, nil)
|
2019-10-22 07:08:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-01 10:56:08 -06:00
|
|
|
return ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{
|
2015-06-08 09:51:25 -05:00
|
|
|
To: []string{cmd.User.Email},
|
|
|
|
Template: tmplResetPassword,
|
|
|
|
Data: map[string]interface{}{
|
2019-10-22 07:08:18 -05:00
|
|
|
"Code": code,
|
2015-06-08 09:51:25 -05:00
|
|
|
"Name": cmd.User.NameOrFallback(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-08 08:53:51 -06:00
|
|
|
func (ns *NotificationService) validateResetPasswordCode(ctx context.Context, query *models.ValidateResetPasswordCodeQuery) error {
|
2015-06-08 06:39:02 -05:00
|
|
|
login := getLoginForEmailCode(query.Code)
|
|
|
|
if login == "" {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrInvalidEmailCode
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
2015-06-05 04:08:19 -05:00
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
userQuery := models.GetUserByLoginQuery{LoginOrEmail: login}
|
2021-12-28 10:36:22 -06:00
|
|
|
if err := bus.Dispatch(ctx, &userQuery); err != nil {
|
2015-06-08 06:39:02 -05:00
|
|
|
return err
|
|
|
|
}
|
2015-06-08 03:57:01 -05:00
|
|
|
|
2021-08-25 08:11:22 -05:00
|
|
|
validEmailCode, err := validateUserEmailCode(ns.Cfg, userQuery.Result, query.Code)
|
2019-10-22 07:08:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !validEmailCode {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrInvalidEmailCode
|
2015-06-08 06:39:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = userQuery.Result
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-08 10:56:56 -05:00
|
|
|
|
2021-11-29 07:23:24 -06:00
|
|
|
func (ns *NotificationService) signUpStartedHandler(ctx context.Context, evt *events.SignUpStarted) error {
|
2015-08-31 04:35:07 -05:00
|
|
|
if !setting.VerifyEmailEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-27 06:01:32 -05:00
|
|
|
ns.log.Info("User signup started", "email", evt.Email)
|
2015-06-08 10:56:56 -05:00
|
|
|
|
2015-08-28 06:45:16 -05:00
|
|
|
if evt.Email == "" {
|
2015-06-08 10:56:56 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:56:08 -06:00
|
|
|
err := ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{
|
2015-06-08 10:56:56 -05:00
|
|
|
To: []string{evt.Email},
|
2015-08-28 06:45:16 -05:00
|
|
|
Template: tmplSignUpStarted,
|
2015-06-08 10:56:56 -05:00
|
|
|
Data: map[string]interface{}{
|
2015-08-28 06:45:16 -05:00
|
|
|
"Email": evt.Email,
|
|
|
|
"Code": evt.Code,
|
|
|
|
"SignUpUrl": setting.ToAbsUrl(fmt.Sprintf("signup/?email=%s&code=%s", url.QueryEscape(evt.Email), url.QueryEscape(evt.Code))),
|
2015-06-08 10:56:56 -05:00
|
|
|
},
|
|
|
|
})
|
2018-04-27 06:01:32 -05:00
|
|
|
|
2017-06-30 13:21:05 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
emailSentCmd := models.UpdateTempUserWithEmailSentCommand{Code: evt.Code}
|
2021-12-28 10:36:22 -06:00
|
|
|
return bus.Dispatch(ctx, &emailSentCmd)
|
2015-06-08 10:56:56 -05:00
|
|
|
}
|
2015-08-31 04:42:12 -05:00
|
|
|
|
2021-11-29 07:23:24 -06:00
|
|
|
func (ns *NotificationService) signUpCompletedHandler(ctx context.Context, evt *events.SignUpCompleted) error {
|
2018-04-30 09:21:04 -05:00
|
|
|
if evt.Email == "" || !ns.Cfg.Smtp.SendWelcomeEmailOnSignUp {
|
2015-08-31 04:42:12 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-12-01 10:56:08 -06:00
|
|
|
return ns.sendEmailCommandHandler(ctx, &models.SendEmailCommand{
|
2015-08-31 04:42:12 -05:00
|
|
|
To: []string{evt.Email},
|
2015-09-01 05:35:06 -05:00
|
|
|
Template: tmplWelcomeOnSignUp,
|
2015-08-31 04:42:12 -05:00
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Name": evt.Name,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|