Skip to content

Commit 8851245

Browse files
authored
Validate configured -alertmanager.web.external-url and fail if ends with / (#4081)
* Validate configured -alertmanager.web.external-url and fail if ends with / Signed-off-by: Marco Pracucci <[email protected]> * Fixed linter error Signed-off-by: Marco Pracucci <[email protected]>
1 parent d50ce22 commit 8851245

File tree

8 files changed

+57
-23
lines changed

8 files changed

+57
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [ENHANCEMENT] Distributor: added per-distributor limits: max number of inflight requests (`-distributor.instance-limits.max-inflight-push-requests`) and max ingestion rate in samples/sec (`-distributor.instance-limits.max-ingestion-rate`). If not set, these two are unlimited. Also added metrics to expose current values (`cortex_distributor_inflight_push_requests`, `cortex_distributor_ingestion_rate_samples_per_second`) as well as limits (`cortex_distributor_instance_limits` with various `limit` label values). #4071
4242
* [ENHANCEMENT] Ruler: Added `-ruler.enabled-tenants` and `-ruler.disabled-tenants` to explicitly enable or disable rules processing for specific tenants. #4074
4343
* [ENHANCEMENT] Block Storage Ingester: `/flush` now accepts two new parameters: `tenant` to specify tenant to flush and `wait=true` to make call synchronous. Multiple tenants can be specified by repeating `tenant` parameter. If no `tenant` is specified, all tenants are flushed, as before. #4073
44+
* [ENHANCEMENT] Alertmanager: validate configured `-alertmanager.web.external-url` and fail if ends with `/`. #4081
4445
* [ENHANCEMENT] Allow configuration of Cassandra's host selection policy. #4069
4546
* [BUGFIX] Ruler-API: fix bug where `/api/v1/rules/<namespace>/<group_name>` endpoint return `400` instead of `404`. #4013
4647
* [BUGFIX] Distributor: reverted changes done to rate limiting in #3825. #3948

development/tsdb-blocks-storage-s3-gossip/config/cortex.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ ruler:
8989
kvstore:
9090
store: memberlist
9191

92+
alertmanager_url: http://alertmanager:8010/alertmanager
93+
enable_alertmanager_v2: false
94+
9295
ruler_storage:
9396
backend: s3
9497
s3:

development/tsdb-blocks-storage-s3-gossip/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ services:
220220
context: .
221221
dockerfile: dev.dockerfile
222222
image: cortex
223-
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18010 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8010 -server.grpc-listen-port=9010 -alertmanager.web.external-url=localhost:8010"]
223+
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18010 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8010 -server.grpc-listen-port=9010 -alertmanager.web.external-url=http://localhost:8010/alertmanager"]
224224
depends_on:
225225
- consul
226226
- minio

development/tsdb-blocks-storage-s3/config/cortex.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ ruler:
7878
consul:
7979
host: consul:8500
8080

81+
alertmanager_url: http://alertmanager-1:8031/alertmanager,http://alertmanager-2:8032/alertmanager,http://alertmanager-3:8033/alertmanager
82+
enable_alertmanager_v2: false
83+
8184
ruler_storage:
8285
backend: s3
8386
s3:

development/tsdb-blocks-storage-s3/config/rules.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ groups:
99
rules:
1010
- alert: TooManyServices
1111
expr: count(up) > 1
12-
for: 1m
1312
labels:
1413
severity: page
1514
annotations:

development/tsdb-blocks-storage-s3/docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ services:
220220
context: .
221221
dockerfile: dev.dockerfile
222222
image: cortex
223-
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18031 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8031 -server.grpc-listen-port=9031 -alertmanager.web.external-url=localhost:8031"]
223+
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18031 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8031 -server.grpc-listen-port=9031 -alertmanager.web.external-url=http://localhost:8031/alertmanager"]
224224
depends_on:
225225
- consul
226226
- minio
@@ -235,7 +235,7 @@ services:
235235
context: .
236236
dockerfile: dev.dockerfile
237237
image: cortex
238-
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18032 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8032 -server.grpc-listen-port=9032 -alertmanager.web.external-url=localhost:8032"]
238+
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18032 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8032 -server.grpc-listen-port=9032 -alertmanager.web.external-url=http://localhost:8032/alertmanager"]
239239
depends_on:
240240
- consul
241241
- minio
@@ -250,7 +250,7 @@ services:
250250
context: .
251251
dockerfile: dev.dockerfile
252252
image: cortex
253-
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18033 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8033 -server.grpc-listen-port=9033 -alertmanager.web.external-url=localhost:8033"]
253+
command: ["sh", "-c", "sleep 3 && exec ./dlv exec ./cortex --listen=:18033 --headless=true --api-version=2 --accept-multiclient --continue -- -config.file=./config/cortex.yaml -target=alertmanager -server.http-listen-port=8033 -server.grpc-listen-port=9033 -alertmanager.web.external-url=http://localhost:8033/alertmanager"]
254254
depends_on:
255255
- consul
256256
- minio

pkg/alertmanager/multitenant.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ const (
8585

8686
var (
8787
statusTemplate *template.Template
88+
89+
errInvalidExternalURL = errors.New("the configured external URL is invalid: should not end with /")
8890
)
8991

9092
func init() {
@@ -176,6 +178,10 @@ func (cfg *ClusterConfig) RegisterFlags(f *flag.FlagSet) {
176178

177179
// Validate config and returns error on failure
178180
func (cfg *MultitenantAlertmanagerConfig) Validate() error {
181+
if cfg.ExternalURL.URL != nil && strings.HasSuffix(cfg.ExternalURL.Path, "/") {
182+
return errInvalidExternalURL
183+
}
184+
179185
if err := cfg.Store.Validate(); err != nil {
180186
return errors.Wrap(err, "invalid storage config")
181187
}

pkg/alertmanager/multitenant_test.go

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,47 @@ func mockAlertmanagerConfig(t *testing.T) *MultitenantAlertmanagerConfig {
8686
}
8787

8888
func TestMultitenantAlertmanagerConfig_Validate(t *testing.T) {
89-
// Default values only.
90-
{
91-
cfg := &MultitenantAlertmanagerConfig{}
92-
flagext.DefaultValues(cfg)
93-
assert.NoError(t, cfg.Validate())
94-
}
95-
// Invalid persist interval (zero).
96-
{
97-
cfg := &MultitenantAlertmanagerConfig{}
98-
flagext.DefaultValues(cfg)
99-
cfg.Persister.Interval = 0
100-
assert.Equal(t, errInvalidPersistInterval, cfg.Validate())
89+
tests := map[string]struct {
90+
setup func(t *testing.T, cfg *MultitenantAlertmanagerConfig)
91+
expected error
92+
}{
93+
"should pass with default config": {
94+
setup: func(t *testing.T, cfg *MultitenantAlertmanagerConfig) {},
95+
expected: nil,
96+
},
97+
"should fail if persistent interval is 0": {
98+
setup: func(t *testing.T, cfg *MultitenantAlertmanagerConfig) {
99+
cfg.Persister.Interval = 0
100+
},
101+
expected: errInvalidPersistInterval,
102+
},
103+
"should fail if persistent interval is negative": {
104+
setup: func(t *testing.T, cfg *MultitenantAlertmanagerConfig) {
105+
cfg.Persister.Interval = -1
106+
},
107+
expected: errInvalidPersistInterval,
108+
},
109+
"should fail if external URL ends with /": {
110+
setup: func(t *testing.T, cfg *MultitenantAlertmanagerConfig) {
111+
require.NoError(t, cfg.ExternalURL.Set("http://localhost/prefix/"))
112+
},
113+
expected: errInvalidExternalURL,
114+
},
115+
"should succeed if external URL does not end with /": {
116+
setup: func(t *testing.T, cfg *MultitenantAlertmanagerConfig) {
117+
require.NoError(t, cfg.ExternalURL.Set("http://localhost/prefix"))
118+
},
119+
expected: nil,
120+
},
101121
}
102-
// Invalid persist interval (negative).
103-
{
104-
cfg := &MultitenantAlertmanagerConfig{}
105-
flagext.DefaultValues(cfg)
106-
cfg.Persister.Interval = -1
107-
assert.Equal(t, errInvalidPersistInterval, cfg.Validate())
122+
123+
for testName, testData := range tests {
124+
t.Run(testName, func(t *testing.T) {
125+
cfg := &MultitenantAlertmanagerConfig{}
126+
flagext.DefaultValues(cfg)
127+
testData.setup(t, cfg)
128+
assert.Equal(t, testData.expected, cfg.Validate())
129+
})
108130
}
109131
}
110132

0 commit comments

Comments
 (0)