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 <twin@linux.com>
This commit is contained in:
Oskar Carl 2023-07-03 05:10:16 +02:00 committed by GitHub
parent ecda4a9987
commit 5eebe6d9cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 1 deletions

View File

@ -733,6 +733,7 @@ endpoints:
| `alerting.ntfy` | Configuration for alerts of type `ntfy` | `{}` | | `alerting.ntfy` | Configuration for alerts of type `ntfy` | `{}` |
| `alerting.ntfy.topic` | Topic at which the alert will be sent | Required `""` | | `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.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.priority` | The priority of the alert | `3` |
| `alerting.ntfy.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A | | `alerting.ntfy.default-alert` | Default alert configuration. <br />See [Setting a default alert](#setting-a-default-alert) | N/A |
@ -745,6 +746,7 @@ alerting:
ntfy: ntfy:
topic: "gatus-test-topic" topic: "gatus-test-topic"
priority: 2 priority: 2
token: faketoken
default-alert: default-alert:
failure-threshold: 3 failure-threshold: 3
send-on-resolved: true send-on-resolved: true

View File

@ -7,6 +7,7 @@ import (
"io" "io"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"github.com/TwiN/gatus/v5/alerting/alert" "github.com/TwiN/gatus/v5/alerting/alert"
"github.com/TwiN/gatus/v5/client" "github.com/TwiN/gatus/v5/client"
@ -23,6 +24,7 @@ type AlertProvider struct {
Topic string `yaml:"topic"` Topic string `yaml:"topic"`
URL string `yaml:"url,omitempty"` // Defaults to DefaultURL URL string `yaml:"url,omitempty"` // Defaults to DefaultURL
Priority int `yaml:"priority,omitempty"` // Defaults to DefaultPriority 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 is the default alert configuration to use for endpoints with an alert of the appropriate type
DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"` DefaultAlert *alert.Alert `yaml:"default-alert,omitempty"`
@ -36,7 +38,11 @@ func (provider *AlertProvider) IsValid() bool {
if provider.Priority == 0 { if provider.Priority == 0 {
provider.Priority = DefaultPriority 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 // Send an alert using the provider
@ -47,6 +53,9 @@ func (provider *AlertProvider) Send(endpoint *core.Endpoint, alert *alert.Alert,
return err return err
} }
request.Header.Set("Content-Type", "application/json") 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) response, err := client.GetHTTPClient(nil).Do(request)
if err != nil { if err != nil {
return err return err

View File

@ -24,6 +24,16 @@ func TestAlertDefaultProvider_IsValid(t *testing.T) {
provider: AlertProvider{Topic: "example", Priority: 1}, provider: AlertProvider{Topic: "example", Priority: 1},
expected: true, 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", name: "invalid-topic",
provider: AlertProvider{URL: "https://ntfy.sh", Topic: "", Priority: 1}, provider: AlertProvider{URL: "https://ntfy.sh", Topic: "", Priority: 1},