Skip to content

Commit

Permalink
Rule: Error out earlier when building alertmanager config (#5405)
Browse files Browse the repository at this point in the history
* Error out earlier when building alertmanager config

Signed-off-by: Jéssica Lins <jessicaalins@gmail.com>

* Add test case for empty host

Signed-off-by: Jéssica Lins <jlins@redhat.com>
  • Loading branch information
Jéssica Lins authored Jun 7, 2022
1 parent a6f6ce0 commit 22aefd6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/alert/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,15 @@ func BuildAlertmanagerConfig(address string, timeout time.Duration) (Alertmanage
}

scheme := parsed.Scheme
if scheme == "" {
return AlertmanagerConfig{}, errors.New("alertmanagers.url contains empty scheme")
}

host := parsed.Host
if host == "" {
return AlertmanagerConfig{}, errors.New("alertmanagers.url contains empty host")
}

for _, qType := range []dns.QType{dns.A, dns.SRV, dns.SRVNoA} {
prefix := string(qType) + "+"
if strings.HasPrefix(strings.ToLower(scheme), prefix) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/alert/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func TestBuildAlertmanagerConfiguration(t *testing.T) {
address: "://user:pass@localhost:9093",
err: true,
},
{
address: "http://user:pass@",
err: true,
},
{
address: "dnssrv+_http._tcp.example.com",
err: true,
},
} {
t.Run(tc.address, func(t *testing.T) {
cfg, err := BuildAlertmanagerConfig(tc.address, time.Duration(0))
Expand Down

0 comments on commit 22aefd6

Please sign in to comment.