Adds 93 services, bringing the total to 109 plus webhook and email: chat and messengers (DingTalk, Feishu, LINE, Nextcloud Talk, Threema, WhatsApp gateways, Nostr, Home Assistant, …), push (Bark, Gorush, ServerChan, …), SMS and calls (Twilio, Plivo, 46elks, Alibaba Cloud, Teltonika routers, …), email APIs (SendGrid, Brevo, Resend, Microsoft Graph, …) and incident management (Grafana OnCall, Jira Service Management, Splunk On-Call, Squadcast, SIGNL4, …). Customers bring their own credentials; secrets are sealed like the others. Services pair incidents by their ID where they can resolve alerts. Signed requests (DingTalk, Nextcloud Talk, Alibaba Cloud) and replies that report failures with status 200 are handled. Nostr messages are sealed and gift-wrapped (NIP-17/59) with go-nostr, but sent over our own WebSocket connections so relays go through the network guard. Messages now carry the monitor's name, ID and target. The channel form groups services, the i18n test derives provider keys from the registry, and channel types may contain digits. Refs #92 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
159 lines
4.6 KiB
Go
159 lines
4.6 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/url"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
|
|
)
|
|
|
|
// providerConfig is stored for channels of notify.Providers. Secret
|
|
// settings are sealed, bound to the channel type and field, and never
|
|
// shown again.
|
|
type providerConfig struct {
|
|
Settings map[string]string `json:"settings"`
|
|
Sealed map[string]string `json:"sealed"`
|
|
Locale string `json:"locale"`
|
|
}
|
|
|
|
func secretAAD(typ, field string) []byte { return []byte("channel:" + typ + ":" + field) }
|
|
|
|
// providerSettings validates the settings of a provider channel. Secret
|
|
// fields left empty keep their previous value, so a form can be saved
|
|
// without asking for tokens again.
|
|
func (s *Server) providerSettings(p notify.Provider, req *channelRequest, previous []byte) ([]byte, string) {
|
|
var old providerConfig
|
|
if previous != nil {
|
|
_ = json.Unmarshal(previous, &old)
|
|
}
|
|
cfg := providerConfig{Settings: map[string]string{}, Sealed: map[string]string{}, Locale: req.Locale}
|
|
for _, f := range p.Fields {
|
|
v := strings.TrimSpace(req.Settings[f.Name])
|
|
if f.Secret {
|
|
if v == "" {
|
|
if sealed, ok := old.Sealed[f.Name]; ok {
|
|
cfg.Sealed[f.Name] = sealed
|
|
continue
|
|
}
|
|
if f.Required {
|
|
return nil, "invalid_channel_settings"
|
|
}
|
|
continue
|
|
}
|
|
} else if v == "" {
|
|
v = f.Default
|
|
}
|
|
if v == "" {
|
|
if f.Required {
|
|
return nil, "invalid_channel_settings"
|
|
}
|
|
continue
|
|
}
|
|
if len(v) > 2000 || !s.validSetting(f, v) {
|
|
return nil, "invalid_channel_settings"
|
|
}
|
|
if f.Secret {
|
|
cfg.Sealed[f.Name] = base64.StdEncoding.EncodeToString(s.keys.Seal([]byte(v), secretAAD(p.Type, f.Name)))
|
|
} else {
|
|
cfg.Settings[f.Name] = v
|
|
}
|
|
}
|
|
if cfg.Locale == "" || !s.catalog.IsSupported(cfg.Locale) {
|
|
cfg.Locale = "en"
|
|
}
|
|
raw, _ := json.Marshal(cfg)
|
|
return raw, ""
|
|
}
|
|
|
|
func (s *Server) validSetting(f notify.Field, v string) bool {
|
|
switch f.Kind {
|
|
case "url":
|
|
u, err := url.Parse(v)
|
|
// Plain http only where services may live on private networks.
|
|
return err == nil && u.Host != "" && u.User == nil && (u.Scheme == "https" || (u.Scheme == "http" && !s.cloud()))
|
|
case "number":
|
|
_, err := strconv.Atoi(v)
|
|
return err == nil
|
|
case "select":
|
|
return slices.Contains(f.Options, v)
|
|
}
|
|
return !strings.ContainsAny(v, "\r\n")
|
|
}
|
|
|
|
// providerView shows the settings of a provider channel; secrets appear
|
|
// only as the names of fields that are set.
|
|
func providerView(raw []byte) (map[string]string, []string, string) {
|
|
var cfg providerConfig
|
|
_ = json.Unmarshal(raw, &cfg)
|
|
set := make([]string, 0, len(cfg.Sealed))
|
|
for k := range cfg.Sealed {
|
|
set = append(set, k)
|
|
}
|
|
slices.Sort(set)
|
|
if cfg.Settings == nil {
|
|
cfg.Settings = map[string]string{}
|
|
}
|
|
return cfg.Settings, set, cfg.Locale
|
|
}
|
|
|
|
// openProviderSettings returns all settings with secrets unsealed.
|
|
func (s *Server) openProviderSettings(typ string, raw []byte) (map[string]string, string, error) {
|
|
var cfg providerConfig
|
|
if err := json.Unmarshal(raw, &cfg); err != nil {
|
|
return nil, "", err
|
|
}
|
|
out := map[string]string{}
|
|
for k, v := range cfg.Settings {
|
|
out[k] = v
|
|
}
|
|
for k, v := range cfg.Sealed {
|
|
sealed, err := base64.StdEncoding.DecodeString(v)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
plain, err := s.keys.Open(sealed, secretAAD(typ, k))
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
out[k] = string(plain)
|
|
}
|
|
return out, cfg.Locale, nil
|
|
}
|
|
|
|
// messageFor renders an event for chat and push services in locale.
|
|
func (s *Server) messageFor(ev notify.Event, locale string) notify.Message {
|
|
l := s.catalog.Match(locale)
|
|
data := map[string]any{"Monitor": ev.Monitor.Name}
|
|
m := notify.Message{Event: ev.Kind, Key: ev.IncidentID, Monitor: ev.Monitor.Name, MonitorID: ev.Monitor.ID, Target: ev.Monitor.Target}
|
|
var lines []string
|
|
switch ev.Kind {
|
|
case notify.Opened:
|
|
m.Title = l.T("notify.title_opened", data)
|
|
for region, msg := range ev.Cause {
|
|
lines = append(lines, region+": "+msg)
|
|
}
|
|
slices.Sort(lines)
|
|
case notify.Resolved:
|
|
m.Title = l.T("notify.title_resolved", data)
|
|
if ev.ResolvedAt != nil {
|
|
lines = append(lines, l.T("notify.duration", map[string]any{"Duration": ev.ResolvedAt.Sub(ev.StartedAt).Round(time.Second).String()}))
|
|
}
|
|
default:
|
|
m.Title = l.T("notify.title_test", data)
|
|
lines = append(lines, l.T("notify.test_body", nil))
|
|
}
|
|
if ev.Monitor.Target != "" && ev.Monitor.Type != "heartbeat" {
|
|
lines = append([]string{ev.Monitor.Type + " · " + ev.Monitor.Target}, lines...)
|
|
}
|
|
m.Body = strings.Join(lines, "\n")
|
|
if ev.Monitor.ID != [16]byte{} {
|
|
m.URL = strings.TrimSuffix(s.cfg.PublicURL, "/") + "/monitors/" + ev.Monitor.ID.String()
|
|
}
|
|
return m
|
|
}
|