From b54271107876c4791bc6deaf93d8b16e5a9f1861 Mon Sep 17 00:00:00 2001 From: Andrew Woodlee Date: Thu, 13 Mar 2025 23:34:37 -0500 Subject: [PATCH] notifications: add http service --- pkg/backy/config.go | 8 ++++---- pkg/backy/notification.go | 15 ++++++++------- pkg/backy/utils.go | 2 -- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/pkg/backy/config.go b/pkg/backy/config.go index 90a1fec..efa2d3a 100644 --- a/pkg/backy/config.go +++ b/pkg/backy/config.go @@ -125,6 +125,10 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts { 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) getCommandEnvironments(opts) @@ -153,10 +157,6 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts { opts.SetupNotify() - if err := opts.setupVault(); err != nil { - log.Err(err).Send() - } - return opts } diff --git a/pkg/backy/notification.go b/pkg/backy/notification.go index 1bd5ec9..8ddae46 100644 --- a/pkg/backy/notification.go +++ b/pkg/backy/notification.go @@ -5,7 +5,6 @@ package backy import ( "fmt" - stdHttp "net/http" "strings" "git.andrewnw.xyz/CyberShell/backy/pkg/logging" @@ -33,7 +32,8 @@ type MailConfig struct { } type HttpConfig struct { - Url string `yaml:"url"` + URL string `yaml:"url"` + Method string `yaml:"method"` Headers map[string][]string `yaml:"headers"` } @@ -66,6 +66,7 @@ func (opts *ConfigOpts) SetupNotify() { continue } conf.Password = getExternalConfigDirectiveValue(conf.Password, opts) + opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding mail notification service") mailConf := setupMail(conf) services = append(services, mailConf) case "matrix": @@ -75,6 +76,7 @@ func (opts *ConfigOpts) SetupNotify() { continue } conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts) + opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding matrix notification service") mtrxConf, mtrxErr := setupMatrix(conf) if mtrxErr != nil { 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() continue } + opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding http notification service") httpConf := setupHttp(conf) services = append(services, httpConf) - default: 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 { httpService := http.New() - // httpService.AddReceiversURLs(httpConf.Url) httpService.AddReceivers(&http.Webhook{ - URL: httpConf.Url, + URL: httpConf.URL, Header: httpConf.Headers, ContentType: "text/plain", - Method: stdHttp.MethodPost, + Method: httpConf.Method, BuildPayload: func(subject, message string) (payload any) { - return "[text/plain]: " + subject + " - " + message + return subject + "\n\n" + message }, }) diff --git a/pkg/backy/utils.go b/pkg/backy/utils.go index b6489b4..6189c44 100644 --- a/pkg/backy/utils.go +++ b/pkg/backy/utils.go @@ -393,7 +393,6 @@ func getExternalConfigDirectiveValue(key string, opts *ConfigOpts) string { key = strings.TrimSuffix(key, externDirectiveEnd) key = GetVaultKey(key, opts, opts.Logger) } - println(key) return key } @@ -418,7 +417,6 @@ func getVaultSecret(vaultClient *vault.Client, key *VaultKey) (string, error) { } value, ok := secret.Data[key.Key].(string) - println(value) 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]) }