notifications: add http service

This commit is contained in:
Andrew Woodlee 2025-03-13 23:34:37 -05:00
parent 52dbc353e5
commit b542711078
3 changed files with 12 additions and 13 deletions

View File

@ -125,6 +125,10 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
log.Info().Str("config file", opts.ConfigFilePath).Send() log.Info().Str("config file", opts.ConfigFilePath).Send()
if err := opts.setupVault(); err != nil {
log.Err(err).Send()
}
unmarshalConfig(backyKoanf, "commands", &opts.Cmds, opts.Logger) unmarshalConfig(backyKoanf, "commands", &opts.Cmds, opts.Logger)
getCommandEnvironments(opts) getCommandEnvironments(opts)
@ -153,10 +157,6 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
opts.SetupNotify() opts.SetupNotify()
if err := opts.setupVault(); err != nil {
log.Err(err).Send()
}
return opts return opts
} }

View File

@ -5,7 +5,6 @@ package backy
import ( import (
"fmt" "fmt"
stdHttp "net/http"
"strings" "strings"
"git.andrewnw.xyz/CyberShell/backy/pkg/logging" "git.andrewnw.xyz/CyberShell/backy/pkg/logging"
@ -33,7 +32,8 @@ type MailConfig struct {
} }
type HttpConfig struct { type HttpConfig struct {
Url string `yaml:"url"` URL string `yaml:"url"`
Method string `yaml:"method"`
Headers map[string][]string `yaml:"headers"` Headers map[string][]string `yaml:"headers"`
} }
@ -66,6 +66,7 @@ func (opts *ConfigOpts) SetupNotify() {
continue continue
} }
conf.Password = getExternalConfigDirectiveValue(conf.Password, opts) conf.Password = getExternalConfigDirectiveValue(conf.Password, opts)
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding mail notification service")
mailConf := setupMail(conf) mailConf := setupMail(conf)
services = append(services, mailConf) services = append(services, mailConf)
case "matrix": case "matrix":
@ -75,6 +76,7 @@ func (opts *ConfigOpts) SetupNotify() {
continue continue
} }
conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts) conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts)
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding matrix notification service")
mtrxConf, mtrxErr := setupMatrix(conf) mtrxConf, mtrxErr := setupMatrix(conf)
if mtrxErr != nil { if mtrxErr != nil {
opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr)) opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr))
@ -87,9 +89,9 @@ func (opts *ConfigOpts) SetupNotify() {
opts.Logger.Info().Err(fmt.Errorf("error: ID %s not found in http object", confId)).Str("list", confName).Send() opts.Logger.Info().Err(fmt.Errorf("error: ID %s not found in http object", confId)).Str("list", confName).Send()
continue continue
} }
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding http notification service")
httpConf := setupHttp(conf) httpConf := setupHttp(conf)
services = append(services, httpConf) services = append(services, httpConf)
default: default:
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send() opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
} }
@ -119,14 +121,13 @@ func setupMail(config MailConfig) *mail.Mail {
func setupHttp(httpConf HttpConfig) *http.Service { func setupHttp(httpConf HttpConfig) *http.Service {
httpService := http.New() httpService := http.New()
// httpService.AddReceiversURLs(httpConf.Url)
httpService.AddReceivers(&http.Webhook{ httpService.AddReceivers(&http.Webhook{
URL: httpConf.Url, URL: httpConf.URL,
Header: httpConf.Headers, Header: httpConf.Headers,
ContentType: "text/plain", ContentType: "text/plain",
Method: stdHttp.MethodPost, Method: httpConf.Method,
BuildPayload: func(subject, message string) (payload any) { BuildPayload: func(subject, message string) (payload any) {
return "[text/plain]: " + subject + " - " + message return subject + "\n\n" + message
}, },
}) })

View File

@ -393,7 +393,6 @@ func getExternalConfigDirectiveValue(key string, opts *ConfigOpts) string {
key = strings.TrimSuffix(key, externDirectiveEnd) key = strings.TrimSuffix(key, externDirectiveEnd)
key = GetVaultKey(key, opts, opts.Logger) key = GetVaultKey(key, opts, opts.Logger)
} }
println(key)
return key return key
} }
@ -418,7 +417,6 @@ func getVaultSecret(vaultClient *vault.Client, key *VaultKey) (string, error) {
} }
value, ok := secret.Data[key.Key].(string) value, ok := secret.Data[key.Key].(string)
println(value)
if !ok { if !ok {
return "", fmt.Errorf("value type assertion failed for vault key %s: %T %#v", key.Name, secret.Data[key.Name], secret.Data[key.Name]) return "", fmt.Errorf("value type assertion failed for vault key %s: %T %#v", key.Name, secret.Data[key.Name], secret.Data[key.Name])
} }