mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 15:33:17 +01:00
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:
parent
ecda4a9987
commit
5eebe6d9cc
@ -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. <br />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
|
||||
|
@ -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
|
||||
|
@ -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},
|
||||
|
Loading…
Reference in New Issue
Block a user