Skip to content

Commit 3e69e74

Browse files
committed
Introduce proper basic auth support for Alertmanager Provider
Signed-off-by: Matheus Pimenta <[email protected]>
1 parent 5d49b42 commit 3e69e74

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

docs/spec/v1beta3/providers.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,9 @@ To configure a Provider for Prometheus Alertmanager, authentication can be done
924924
Both methods are supported, but using authentication is optional based on your setup.
925925

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

932931
```yaml
933932
---
@@ -938,17 +937,20 @@ metadata:
938937
namespace: default
939938
spec:
940939
type: alertmanager
940+
address: https://<alertmanager-hostport>/api/v2/alerts/
941941
secretRef:
942-
name: alertmanager-address
942+
name: alertmanager-basic-auth
943943
---
944944
apiVersion: v1
945945
kind: Secret
946946
metadata:
947-
name: alertmanager-address
947+
name: alertmanager-basic-auth
948948
namespace: default
949949
stringData:
950-
address: https://<username>:<password>@<alertmanager-hostport>/api/v2/alerts/
950+
username: <username>
951+
password: <password>
951952
```
953+
952954
Bearer Token Authentication:
953955
Create a Secret with [the `token`](#token-example), and an `alertmanager` Provider with a [Secret
954956
reference](#secret-reference) and the Prometheus Alertmanager [HTTP API
@@ -973,7 +975,7 @@ metadata:
973975
name: alertmanager-token
974976
namespace: default
975977
stringData:
976-
token: <token>
978+
token: <token>
977979
```
978980

979981
##### Webex
@@ -1100,6 +1102,7 @@ The Kubernetes secret can have any of the following keys:
11001102
- `proxy` - overrides `.spec.proxy` (deprecated, use `.spec.proxySecretRef` instead. **Support for this key will be removed in v1**)
11011103
- `token` - used for authentication
11021104
- `username` - overrides `.spec.username`
1105+
- `password` - used for authentication, often in combination with `username` (or `.spec.username`)
11031106
- `headers` - HTTP headers values included in the POST request
11041107

11051108
#### Address example

internal/notifier/alertmanager.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type Alertmanager struct {
3636
ProxyURL string
3737
TLSConfig *tls.Config
3838
Token string
39+
Username string
40+
Password string
3941
}
4042

4143
type AlertManagerAlert struct {
@@ -74,7 +76,7 @@ func (a *AlertManagerTime) UnmarshalJSON(jsonRepr []byte) error {
7476
return nil
7577
}
7678

77-
func NewAlertmanager(hookURL string, proxyURL string, tlsConfig *tls.Config, token string) (*Alertmanager, error) {
79+
func NewAlertmanager(hookURL string, proxyURL string, tlsConfig *tls.Config, token, user, pass string) (*Alertmanager, error) {
7880
_, err := url.ParseRequestURI(hookURL)
7981
if err != nil {
8082
return nil, fmt.Errorf("invalid Alertmanager URL %s: '%w'", hookURL, err)
@@ -84,6 +86,8 @@ func NewAlertmanager(hookURL string, proxyURL string, tlsConfig *tls.Config, tok
8486
URL: hookURL,
8587
ProxyURL: proxyURL,
8688
Token: token,
89+
Username: user,
90+
Password: pass,
8791
TLSConfig: tlsConfig,
8892
}, nil
8993
}
@@ -149,6 +153,11 @@ func (s *Alertmanager) Post(ctx context.Context, event eventv1.Event) error {
149153
request.Header.Add("Authorization", "Bearer "+s.Token)
150154
}))
151155
}
156+
if s.Username != "" && s.Password != "" {
157+
opts = append(opts, withRequestModifier(func(request *retryablehttp.Request) {
158+
request.SetBasicAuth(s.Username, s.Password)
159+
}))
160+
}
152161

153162
if err := postMessage(ctx, s.URL, payload, opts...); err != nil {
154163
return fmt.Errorf("postMessage failed: %w", err)

internal/notifier/alertmanager_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func Fuzz_AlertManager(f *testing.F) {
4646
var tlsConfig tls.Config
4747
_ = fuzz.NewConsumer(seed).GenerateStruct(&tlsConfig)
4848

49-
alertmanager, err := NewAlertmanager(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", &tlsConfig, "")
49+
alertmanager, err := NewAlertmanager(fmt.Sprintf("%s/%s", ts.URL, urlSuffix), "", &tlsConfig, "", "", "")
5050
if err != nil {
5151
return
5252
}

internal/notifier/alertmanager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestAlertmanager_Post(t *testing.T) {
3838
}))
3939
defer ts.Close()
4040

41-
alertmanager, err := NewAlertmanager(ts.URL, "", nil, "")
41+
alertmanager, err := NewAlertmanager(ts.URL, "", nil, "", "", "")
4242
require.NoError(t, err)
4343

4444
err = alertmanager.Post(context.TODO(), testEvent())

internal/notifier/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func opsgenieNotifierFunc(opts notifierOptions) (Interface, error) {
295295
}
296296

297297
func alertmanagerNotifierFunc(opts notifierOptions) (Interface, error) {
298-
return NewAlertmanager(opts.URL, opts.ProxyURL, opts.TLSConfig, opts.Token)
298+
return NewAlertmanager(opts.URL, opts.ProxyURL, opts.TLSConfig, opts.Token, opts.Username, opts.Password)
299299
}
300300

301301
func grafanaNotifierFunc(opts notifierOptions) (Interface, error) {

0 commit comments

Comments
 (0)