Alerting: Configure alert manager data source as an external AM (#52081)

Co-authored-by: Jean-Philippe Quéméner <JohnnyQQQQ@users.noreply.github.com>
Co-authored-by: gotjosh <josue.abreu@gmail.com>
Co-authored-by: brendamuir <100768211+brendamuir@users.noreply.github.com>
This commit is contained in:
Konrad Lalik
2022-08-01 10:20:43 +02:00
committed by GitHub
parent 1fc9f6f1c6
commit 54f2c056f5
16 changed files with 808 additions and 227 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/benbjohnson/clock"
"github.com/grafana/grafana/pkg/api/datasource"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
@@ -126,6 +127,8 @@ func (d *AlertsRouter) SyncAndApplyConfigFromDatabase() error {
continue
}
d.logger.Debug("alertmanagers found in the configuration", "alertmanagers", cfg.Alertmanagers)
// We have a running sender, check if we need to apply a new config.
if ok {
if d.externalAlertmanagersCfgHash[cfg.OrgID] == cfg.AsSHA256() {
@@ -218,16 +221,17 @@ func (d *AlertsRouter) alertmanagersFromDatasources(orgID int64) ([]string, erro
}
func (d *AlertsRouter) buildExternalURL(ds *datasources.DataSource) (string, error) {
amURL := ds.Url
// if basic auth is enabled we need to build the url with basic auth baked in
if !ds.BasicAuth {
return amURL, nil
}
parsed, err := url.Parse(ds.Url)
// We re-use the same parsing logic as the datasource to make sure it matches whatever output the user received
// when doing the healthcheck.
parsed, err := datasource.ValidateURL(datasources.DS_ALERTMANAGER, ds.Url)
if err != nil {
return "", fmt.Errorf("failed to parse alertmanager datasource url: %w", err)
}
// if basic auth is enabled we need to build the url with basic auth baked in
if !ds.BasicAuth {
return parsed.String(), nil
}
password := d.secretService.GetDecryptedValue(context.Background(), ds.SecureJsonData, "basicAuthPassword", "")
if password == "" {
return "", fmt.Errorf("basic auth enabled but no password set")