1
0
Fork 0

Merge pull request #1 from jaseemabid/main

Enable basic auth only if username/password is set
This commit is contained in:
Pablo Ovelleiro Corral 2023-02-14 16:18:47 +01:00 committed by GitHub
commit 220226f468
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

16
main.go
View file

@ -3,13 +3,14 @@ package main
import (
"encoding/json"
"fmt"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/common/model"
"log"
"net/http"
"os"
"strings"
"github.com/prometheus/alertmanager/template"
"github.com/prometheus/common/model"
"golang.org/x/exp/maps"
)
@ -57,16 +58,23 @@ func WebhookHandler(w http.ResponseWriter, r *http.Request) {
// Tags
req.Header.Set("Tags", strings.Join(maps.Values(alert.Labels), ","))
req.SetBasicAuth(os.Getenv("NTFY_USER"), os.Getenv("NTFY_PASS"))
username, password := os.Getenv("NTFY_USER"), os.Getenv("NTFY_PASS")
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
log.Printf("Sending request: %v\n", req)
if _, err := http.DefaultClient.Do(req); err != nil {
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("Sending to %s failed: %s\n", req.RemoteAddr, err)
w.WriteHeader(http.StatusBadRequest)
return
}
if resp.StatusCode != 200 {
log.Printf("Failed to send notification: %v\n", resp)
}
}
w.WriteHeader(http.StatusOK)