From 5eebe6d9cc1b849304a9f2813f7f4d9b60b958d0 Mon Sep 17 00:00:00 2001 From: Oskar Carl <7561975+OskarCarl@users.noreply.github.com> Date: Mon, 3 Jul 2023 05:10:16 +0200 Subject: [PATCH] feat(alerting): Add token authorization for ntfy (#512) * feat(alerting): Add token authorization for ntfy * feat(alerting): Fix suggestions for ntfy auth --------- Co-authored-by: TwiN --- README.md | 2 ++ alerting/provider/ntfy/ntfy.go | 11 ++++++++++- alerting/provider/ntfy/ntfy_test.go | 10 ++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e44bce37..7335d44d 100644 --- a/README.md +++ b/README.md @@ -733,6 +733,7 @@ endpoints: | `alerting.ntfy` | Configuration for alerts of type `ntfy` | `{}` | | `alerting.ntfy.topic` | Topic at which the alert will be sent | Required `""` | | `alerting.ntfy.url` | The URL of the target server | `https://ntfy.sh` | +| `alerting.ntfy.token` | [Access token](https://docs.ntfy.sh/publish/#access-tokens) for restricted topics | `""` | | `alerting.ntfy.priority` | The priority of the alert | `3` | | `alerting.ntfy.default-alert` | Default alert configuration.
See [Setting a default alert](#setting-a-default-alert) | N/A | @@ -745,6 +746,7 @@ alerting: ntfy: topic: "gatus-test-topic" priority: 2 + token: faketoken default-alert: failure-threshold: 3 send-on-resolved: true diff --git a/alerting/provider/ntfy/ntfy.go b/alerting/provider/ntfy/ntfy.go index 16caeffd..1c67f45f 100644 --- a/alerting/provider/ntfy/ntfy.go +++ b/alerting/provider/ntfy/ntfy.go @@ -7,6 +7,7 @@ import ( "io" "net/http" "strconv" + "strings" "github.com/TwiN/gatus/v5/alerting/alert" "github.com/TwiN/gatus/v5/client" @@ -23,6 +24,7 @@ type AlertProvider struct { Topic string `yaml:"topic"` URL string `yaml:"url,omitempty"` // Defaults to DefaultURL Priority int `yaml:"priority,omitempty"` // Defaults to DefaultPriority + Token string `yaml:"token,omitempty"` // Defaults to "" // DefaultAlert is the default alert configuration to use for endpoints with an alert of the appropriate type DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"` @@ -36,7 +38,11 @@ func (provider *AlertProvider) IsValid() bool { if provider.Priority == 0 { provider.Priority = DefaultPriority } - return len(provider.URL) > 0 && len(provider.Topic) > 0 && provider.Priority > 0 && provider.Priority < 6 + isTokenValid := true + if len(provider.Token) > 0 { + isTokenValid = strings.HasPrefix(provider.Token, "tk_") + } + return len(provider.URL) > 0 && len(provider.Topic) > 0 && provider.Priority > 0 && provider.Priority < 6 && isTokenValid } // Send an alert using the provider @@ -47,6 +53,9 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert, return err } request.Header.Set("Content-Type", "application/json") + if len(provider.Token) > 0 { + request.Header.Set("Authorization", "Bearer "+provider.Token) + } response, err := client.GetHTTPClient(nil).Do(request) if err != nil { return err diff --git a/alerting/provider/ntfy/ntfy_test.go b/alerting/provider/ntfy/ntfy_test.go index 4c402477..5aea9865 100644 --- a/alerting/provider/ntfy/ntfy_test.go +++ b/alerting/provider/ntfy/ntfy_test.go @@ -24,6 +24,16 @@ func TestAlertDefaultProvider_IsValid(t *testing.T) { provider: AlertProvider{Topic: "example", Priority: 1}, expected: true, }, + { + name: "valid-with-token", + provider: AlertProvider{Topic: "example", Priority: 1, Token: "tk_faketoken"}, + expected: true, + }, + { + name: "invalid-token", + provider: AlertProvider{Topic: "example", Priority: 1, Token: "xx_faketoken"}, + expected: false, + }, { name: "invalid-topic", provider: AlertProvider{URL: "https://ntfy.sh", Topic: "", Priority: 1},