722c414fef
* Encryption: Add support to encrypt/decrypt sjd * Add datasources.Service as a proxy to datasources db operations * Encrypt ds.SecureJsonData before calling SQLStore * Move ds cache code into ds service * Fix tlsmanager tests * Fix pluginproxy tests * Remove some securejsondata.GetEncryptedJsonData usages * Add pluginsettings.Service as a proxy for plugin settings db operations * Add AlertNotificationService as a proxy for alert notification db operations * Remove some securejsondata.GetEncryptedJsonData usages * Remove more securejsondata.GetEncryptedJsonData usages * Fix lint errors * Minor fixes * Remove encryption global functions usages from ngalert * Fix lint errors * Minor fixes * Minor fixes * Remove securejsondata.DecryptedValue usage * Refactor the refactor * Remove securejsondata.DecryptedValue usage * Move securejsondata to migrations package * Move securejsondata to migrations package * Minor fix * Fix integration test * Fix integration tests * Undo undesired changes * Fix tests * Add context.Context into encryption methods * Fix tests * Fix tests * Fix tests * Trigger CI * Fix test * Add names to params of encryption service interface * Remove bus from CacheServiceImpl * Add logging * Add keys to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Add missing key to logger Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Undo changes in markdown files * Fix formatting * Add context to secrets service * Rename decryptSecureJsonData to decryptSecureJsonDataFn * Name args in GetDecryptedValueFn * Add template back to NewAlertmanagerNotifier * Copy GetDecryptedValueFn to ngalert * Add logging to pluginsettings * Fix pluginsettings test Co-authored-by: Tania B <yalyna.ts@gmail.com> Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
103 lines
3.9 KiB
Go
103 lines
3.9 KiB
Go
package alerting
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/encryption"
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
type AlertNotificationService struct {
|
|
Bus bus.Bus
|
|
SQLStore *sqlstore.SQLStore
|
|
EncryptionService encryption.Service
|
|
}
|
|
|
|
func ProvideService(bus bus.Bus, store *sqlstore.SQLStore, encryptionService encryption.Service,
|
|
) *AlertNotificationService {
|
|
s := &AlertNotificationService{
|
|
Bus: bus,
|
|
SQLStore: store,
|
|
EncryptionService: encryptionService,
|
|
}
|
|
|
|
s.Bus.AddHandler(s.GetAlertNotifications)
|
|
s.Bus.AddHandlerCtx(s.CreateAlertNotificationCommand)
|
|
s.Bus.AddHandlerCtx(s.UpdateAlertNotification)
|
|
s.Bus.AddHandler(s.DeleteAlertNotification)
|
|
s.Bus.AddHandler(s.GetAllAlertNotifications)
|
|
s.Bus.AddHandlerCtx(s.GetOrCreateAlertNotificationState)
|
|
s.Bus.AddHandlerCtx(s.SetAlertNotificationStateToCompleteCommand)
|
|
s.Bus.AddHandlerCtx(s.SetAlertNotificationStateToPendingCommand)
|
|
s.Bus.AddHandler(s.GetAlertNotificationsWithUid)
|
|
s.Bus.AddHandler(s.UpdateAlertNotificationWithUid)
|
|
s.Bus.AddHandler(s.DeleteAlertNotificationWithUid)
|
|
s.Bus.AddHandler(s.GetAlertNotificationsWithUidToSend)
|
|
s.Bus.AddHandlerCtx(s.HandleNotificationTestCommand)
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *AlertNotificationService) GetAlertNotifications(query *models.GetAlertNotificationsQuery) error {
|
|
return s.SQLStore.GetAlertNotifications(query)
|
|
}
|
|
|
|
func (s *AlertNotificationService) CreateAlertNotificationCommand(ctx context.Context, cmd *models.CreateAlertNotificationCommand) error {
|
|
var err error
|
|
cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.SQLStore.CreateAlertNotificationCommand(cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) UpdateAlertNotification(ctx context.Context, cmd *models.UpdateAlertNotificationCommand) error {
|
|
var err error
|
|
cmd.EncryptedSecureSettings, err = s.EncryptionService.EncryptJsonData(ctx, cmd.SecureSettings, setting.SecretKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.SQLStore.UpdateAlertNotification(cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) DeleteAlertNotification(cmd *models.DeleteAlertNotificationCommand) error {
|
|
return s.SQLStore.DeleteAlertNotification(cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) GetAllAlertNotifications(query *models.GetAllAlertNotificationsQuery) error {
|
|
return s.SQLStore.GetAllAlertNotifications(query)
|
|
}
|
|
|
|
func (s *AlertNotificationService) GetOrCreateAlertNotificationState(ctx context.Context, cmd *models.GetOrCreateNotificationStateQuery) error {
|
|
return s.SQLStore.GetOrCreateAlertNotificationState(ctx, cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) SetAlertNotificationStateToCompleteCommand(ctx context.Context, cmd *models.SetAlertNotificationStateToCompleteCommand) error {
|
|
return s.SQLStore.SetAlertNotificationStateToCompleteCommand(ctx, cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *models.SetAlertNotificationStateToPendingCommand) error {
|
|
return s.SQLStore.SetAlertNotificationStateToPendingCommand(ctx, cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) GetAlertNotificationsWithUid(query *models.GetAlertNotificationsWithUidQuery) error {
|
|
return s.SQLStore.GetAlertNotificationsWithUid(query)
|
|
}
|
|
|
|
func (s *AlertNotificationService) UpdateAlertNotificationWithUid(cmd *models.UpdateAlertNotificationWithUidCommand) error {
|
|
return s.SQLStore.UpdateAlertNotificationWithUid(cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) DeleteAlertNotificationWithUid(cmd *models.DeleteAlertNotificationWithUidCommand) error {
|
|
return s.SQLStore.DeleteAlertNotificationWithUid(cmd)
|
|
}
|
|
|
|
func (s *AlertNotificationService) GetAlertNotificationsWithUidToSend(query *models.GetAlertNotificationsWithUidToSendQuery) error {
|
|
return s.SQLStore.GetAlertNotificationsWithUidToSend(query)
|
|
}
|