Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions cmd/notification-dummy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

type PluginConfig struct {
Name string `yaml:"name"`
LogLevel *string `yaml:"log_level"`
OutputFile *string `yaml:"output_file"`
Name string `yaml:"name"`
LogLevel string `yaml:"log_level"`
OutputFile string `yaml:"output_file"`
}

type DummyPlugin struct {
Expand All @@ -32,26 +32,30 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
})

func (s *DummyPlugin) Notify(_ context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
}
name := notification.GetName()
cfg, ok := s.PluginConfigByName[name]

cfg := s.PluginConfigByName[notification.GetName()]
if !ok {
return nil, fmt.Errorf("invalid plugin config name %s", name)
}

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Info(fmt.Sprintf("received signal for %s config", notification.GetName()))
logger.Debug(notification.GetText())
logger.Info(fmt.Sprintf("received signal for %s config", name))

text := notification.GetText()

logger.Debug(text)

if cfg.OutputFile != nil && *cfg.OutputFile != "" {
f, err := os.OpenFile(*cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if cfg.OutputFile != "" {
f, err := os.OpenFile(cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
logger.Error(fmt.Sprintf("Cannot open notification file: %s", err))
}

if _, err := f.WriteString(notification.GetText() + "\n"); err != nil {
if _, err := f.WriteString(text + "\n"); err != nil {
f.Close()
logger.Error(fmt.Sprintf("Cannot write notification to file: %s", err))
}
Expand All @@ -62,7 +66,7 @@ func (s *DummyPlugin) Notify(_ context.Context, notification *protobufs.Notifica
}
}

fmt.Fprintln(os.Stdout, notification.GetText())
fmt.Fprintln(os.Stdout, text)

return &protobufs.Empty{}, nil
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/notification-email/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var EncryptionStringToType map[string]mail.Encryption = map[string]mail.Encrypti
}

type PluginConfig struct {
Name string `yaml:"name"`
LogLevel *string `yaml:"log_level"`
Name string `yaml:"name"`
LogLevel string `yaml:"log_level"`

SMTPHost string `yaml:"smtp_host"`
SMTPPort int `yaml:"smtp_port"`
Expand Down Expand Up @@ -94,16 +94,17 @@ func (n *EmailPlugin) Configure(_ context.Context, config *protobufs.Config) (*p
}

func (n *EmailPlugin) Notify(_ context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := n.ConfigByName[notification.GetName()]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
}
name := notification.GetName()
cfg, ok := n.ConfigByName[name]

cfg := n.ConfigByName[notification.GetName()]
if !ok {
return nil, fmt.Errorf("invalid plugin config name %s", name)
}

logger := baseLogger.Named(cfg.Name)

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Debug("got notification")
Expand Down
23 changes: 13 additions & 10 deletions cmd/notification-http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type PluginConfig struct {
Headers map[string]string `yaml:"headers"`
SkipTLSVerification bool `yaml:"skip_tls_verification"`
Method string `yaml:"method"`
LogLevel *string `yaml:"log_level"`
LogLevel string `yaml:"log_level"`
Client *http.Client `yaml:"-"`
CertPath string `yaml:"cert_path"`
KeyPath string `yaml:"key_path"`
Expand Down Expand Up @@ -117,19 +117,22 @@ func getTLSClient(c *PluginConfig) error {
}

func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
}
name := notification.GetName()
cfg, ok := s.PluginConfigByName[name]

cfg := s.PluginConfigByName[notification.GetName()]
if !ok {
return nil, fmt.Errorf("invalid plugin config name %s", name)
}

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Info(fmt.Sprintf("received signal for %s config", notification.GetName()))
logger.Info(fmt.Sprintf("received signal for %s config", name))

text := notification.GetText()

request, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL, bytes.NewReader([]byte(notification.GetText())))
request, err := http.NewRequestWithContext(ctx, cfg.Method, cfg.URL, bytes.NewReader([]byte(text)))
if err != nil {
return nil, err
}
Expand All @@ -139,7 +142,7 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
request.Header.Add(headerName, headerValue)
}

logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.GetText()))
logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, text))

resp, err := cfg.Client.Do(request.WithContext(ctx))
if err != nil {
Expand Down
43 changes: 24 additions & 19 deletions cmd/notification-sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
)

type PluginConfig struct {
Name string `yaml:"name"`
CustomerID string `yaml:"customer_id"`
SharedKey string `yaml:"shared_key"`
LogType string `yaml:"log_type"`
LogLevel *string `yaml:"log_level"`
Name string `yaml:"name"`
CustomerID string `yaml:"customer_id"`
SharedKey string `yaml:"shared_key"`
LogType string `yaml:"log_type"`
LogLevel string `yaml:"log_level"`
}

type SentinelPlugin struct {
Expand All @@ -39,43 +39,48 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
JSONFormat: true,
})

func (s *SentinelPlugin) getAuthorizationHeader(now string, length int, pluginName string) (string, error) {
func (s *SentinelPlugin) getAuthorizationHeader(now string, length int, name string) (string, error) {
xHeaders := "X-Ms-Date:" + now

cfg := s.PluginConfigByName[name]

stringToHash := fmt.Sprintf("POST\n%d\napplication/json\n%s\n/api/logs", length, xHeaders)
decodedKey, _ := base64.StdEncoding.DecodeString(s.PluginConfigByName[pluginName].SharedKey)
decodedKey, _ := base64.StdEncoding.DecodeString(cfg.SharedKey)

h := hmac.New(sha256.New, decodedKey)
h.Write([]byte(stringToHash))

encodedHash := base64.StdEncoding.EncodeToString(h.Sum(nil))
authorization := "SharedKey " + s.PluginConfigByName[pluginName].CustomerID + ":" + encodedHash
authorization := "SharedKey " + cfg.CustomerID + ":" + encodedHash

logger.Trace("authorization header", "header", authorization)

return authorization, nil
}

func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
}
name := notification.GetName()
cfg, ok := s.PluginConfigByName[name]

cfg := s.PluginConfigByName[notification.GetName()]
if !ok {
return nil, fmt.Errorf("invalid plugin config name %s", name)
}

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Info("received notification for sentinel config", "name", notification.GetName())
logger.Info("received notification for sentinel config", "name", name)

text := notification.GetText()

url := fmt.Sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", s.PluginConfigByName[notification.GetName()].CustomerID)
body := strings.NewReader(notification.GetText())
url := fmt.Sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=2016-04-01", cfg.CustomerID)
body := strings.NewReader(text)

// Cannot use time.RFC1123 as azure wants GMT, not UTC
now := time.Now().UTC().Format("Mon, 02 Jan 2006 15:04:05 GMT")

authorization, err := s.getAuthorizationHeader(now, len(notification.GetText()), notification.GetName())
authorization, err := s.getAuthorizationHeader(now, len(text), name)
if err != nil {
return &protobufs.Empty{}, err
}
Expand All @@ -87,7 +92,7 @@ func (s *SentinelPlugin) Notify(ctx context.Context, notification *protobufs.Not
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Log-Type", s.PluginConfigByName[notification.GetName()].LogType)
req.Header.Set("Log-Type", cfg.LogType)
req.Header.Set("Authorization", authorization)
req.Header.Set("X-Ms-Date", now)

Expand Down
36 changes: 20 additions & 16 deletions cmd/notification-slack/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
)

type PluginConfig struct {
Name string `yaml:"name"`
Webhook string `yaml:"webhook"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
IconEmoji string `yaml:"icon_emoji"`
IconURL string `yaml:"icon_url"`
LogLevel *string `yaml:"log_level"`
Name string `yaml:"name"`
Webhook string `yaml:"webhook"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
IconEmoji string `yaml:"icon_emoji"`
IconURL string `yaml:"icon_url"`
LogLevel string `yaml:"log_level"`
}
type Notify struct {
protobufs.UnimplementedNotifierServer
Expand All @@ -36,21 +36,25 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
})

func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := n.ConfigByName[notification.GetName()]; !ok {
return nil, fmt.Errorf("invalid plugin config name %s", notification.GetName())
}
name := notification.GetName()

cfg, ok := n.ConfigByName[name]

cfg := n.ConfigByName[notification.GetName()]
if !ok {
return nil, fmt.Errorf("invalid plugin config name %s", name)
}

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Info(fmt.Sprintf("found notify signal for %s config", notification.GetName()))
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.GetText()))
text := notification.GetText()

logger.Info(fmt.Sprintf("found notify signal for %s config", name))
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, text))

err := slack.PostWebhookContext(ctx, cfg.Webhook, &slack.WebhookMessage{
Text: notification.GetText(),
Text: text,
Channel: cfg.Channel,
Username: cfg.Username,
IconEmoji: cfg.IconEmoji,
Expand Down
23 changes: 12 additions & 11 deletions cmd/notification-splunk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
})

type PluginConfig struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Token string `yaml:"token"`
LogLevel *string `yaml:"log_level"`
Name string `yaml:"name"`
URL string `yaml:"url"`
Token string `yaml:"token"`
LogLevel string `yaml:"log_level"`
}

type Splunk struct {
Expand All @@ -43,17 +43,18 @@ type Payload struct {
}

func (s *Splunk) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
if _, ok := s.PluginConfigByName[notification.GetName()]; !ok {
return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", notification.GetName())
}
name := notification.GetName()
cfg, ok := s.PluginConfigByName[name]

cfg := s.PluginConfigByName[notification.GetName()]
if !ok {
return &protobufs.Empty{}, fmt.Errorf("splunk invalid config name %s", name)
}

if cfg.LogLevel != nil && *cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
if cfg.LogLevel != "" {
logger.SetLevel(hclog.LevelFromString(cfg.LogLevel))
}

logger.Info(fmt.Sprintf("received notify signal for %s config", notification.GetName()))
logger.Info(fmt.Sprintf("received notify signal for %s config", name))

p := Payload{Event: notification.GetText()}

Expand Down