Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Bearer Token authentication to Provider alertmanager #1021

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions docs/spec/v1beta3/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -919,10 +919,13 @@ and [TLS certificates](#tls-certificates).

###### Prometheus Alertmanager example

To configure a Provider for Prometheus Alertmanager, create a Secret with [the
`address`](#address-example) set to the Prometheus Alertmanager [HTTP API
To configure a Provider for Prometheus Alertmanager, authentication can be done using either Basic Authentication or a Bearer Token.
Both methods are supported, but using authentication is optional based on your setup.

Basic Authentication:
Create a Secret with [the `address`](#address-example) set to the Prometheus Alertmanager [HTTP API
URL](https://prometheus.io/docs/alerting/latest/https/#http-traffic)
including Basic Auth credentials, and a `alertmanager` Provider with a [Secret
including Basic Auth credentials, and an `alertmanager` Provider with a [Secret
reference](#secret-reference).

```yaml
Expand All @@ -943,7 +946,33 @@ metadata:
name: alertmanager-address
namespace: default
stringData:
address: https://username:password@<alertmanager-url>/api/v2/alerts/"
address: https://<username>:<password>@<alertmanager-hostport>/api/v2/alerts/
```
Bearer Token Authentication:
Create a Secret with [the `token`](#token-example), and an `alertmanager` Provider with a [Secret
reference](#secret-reference) and the Prometheus Alertmanager [HTTP API
URL](https://prometheus.io/docs/alerting/latest/https/#http-traffic) set directly in the `.spec.address` field.

```yaml
---
apiVersion: notification.toolkit.fluxcd.io/v1beta3
kind: Provider
metadata:
name: alertmanager
namespace: default
spec:
type: alertmanager
address: https://<alertmanager-hostport>/api/v2/alerts/
secretRef:
name: alertmanager-token
---
apiVersion: v1
kind: Secret
metadata:
name: alertmanager-token
namespace: default
stringData:
token: <token>
```

##### Webex
Expand Down
14 changes: 11 additions & 3 deletions internal/notifier/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"
"time"

"github.com/hashicorp/go-retryablehttp"
"golang.org/x/text/cases"
"golang.org/x/text/language"

Expand All @@ -34,6 +35,7 @@ type Alertmanager struct {
URL string
ProxyURL string
CertPool *x509.CertPool
Token string
}

type AlertManagerAlert struct {
Expand Down Expand Up @@ -72,7 +74,7 @@ func (a *AlertManagerTime) UnmarshalJSON(jsonRepr []byte) error {
return nil
}

func NewAlertmanager(hookURL string, proxyURL string, certPool *x509.CertPool) (*Alertmanager, error) {
func NewAlertmanager(hookURL string, proxyURL string, certPool *x509.CertPool, token string) (*Alertmanager, error) {
_, err := url.ParseRequestURI(hookURL)
if err != nil {
return nil, fmt.Errorf("invalid Alertmanager URL %s: '%w'", hookURL, err)
Expand All @@ -82,6 +84,7 @@ func NewAlertmanager(hookURL string, proxyURL string, certPool *x509.CertPool) (
URL: hookURL,
ProxyURL: proxyURL,
CertPool: certPool,
Token: token,
}, nil
}

Expand Down Expand Up @@ -134,8 +137,13 @@ func (s *Alertmanager) Post(ctx context.Context, event eventv1.Event) error {
},
}

err := postMessage(ctx, s.URL, s.ProxyURL, s.CertPool, payload)

var opts []requestOptFunc
if s.Token != "" {
opts = append(opts, func(request *retryablehttp.Request) {
request.Header.Add("Authorization", "Bearer "+s.Token)
})
}
err := postMessage(ctx, s.URL, s.ProxyURL, s.CertPool, payload, opts...)
if err != nil {
return fmt.Errorf("postMessage failed: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/alertmanager_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Fuzz_AlertManager(f *testing.F) {
var cert x509.CertPool
_ = fuzz.NewConsumer(seed).GenerateStruct(&cert)

alertmanager, err := NewAlertmanager(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", &cert)
alertmanager, err := NewAlertmanager(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", &cert, "")
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestAlertmanager_Post(t *testing.T) {
}))
defer ts.Close()

alertmanager, err := NewAlertmanager(ts.URL, "", nil)
alertmanager, err := NewAlertmanager(ts.URL, "", nil, "")
require.NoError(t, err)

err = alertmanager.Post(context.TODO(), testEvent())
Expand Down
2 changes: 1 addition & 1 deletion internal/notifier/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func opsgenieNotifierFunc(opts notifierOptions) (Interface, error) {
}

func alertmanagerNotifierFunc(opts notifierOptions) (Interface, error) {
return NewAlertmanager(opts.URL, opts.ProxyURL, opts.CertPool)
return NewAlertmanager(opts.URL, opts.ProxyURL, opts.CertPool, opts.Token)
}

func grafanaNotifierFunc(opts notifierOptions) (Interface, error) {
Expand Down
Loading