diff --git a/notify/opsgenie/api_key_file b/notify/opsgenie/api_key_file new file mode 100644 index 0000000000..90d2bb3b06 --- /dev/null +++ b/notify/opsgenie/api_key_file @@ -0,0 +1,2 @@ +my_secret_api_key + diff --git a/notify/opsgenie/opsgenie.go b/notify/opsgenie/opsgenie.go index 7554984639..54c0e24554 100644 --- a/notify/opsgenie/opsgenie.go +++ b/notify/opsgenie/opsgenie.go @@ -282,6 +282,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h return nil, false, fmt.Errorf("read key_file error: %w", err) } apiKey = tmpl(string(content)) + apiKey = strings.TrimSpace(string(apiKey)) } if err != nil { diff --git a/notify/opsgenie/opsgenie_test.go b/notify/opsgenie/opsgenie_test.go index 9db635d1d9..b9d61c0e4e 100644 --- a/notify/opsgenie/opsgenie_test.go +++ b/notify/opsgenie/opsgenie_test.go @@ -321,6 +321,24 @@ func TestOpsGenieWithUpdate(t *testing.T) { `, body2) } +func TestOpsGenieApiKeyFile(t *testing.T) { + u, err := url.Parse("https://test-opsgenie-url") + require.NoError(t, err) + tmpl := test.CreateTmpl(t) + ctx := context.Background() + ctx = notify.WithGroupKey(ctx, "1") + opsGenieConfigWithUpdate := config.OpsGenieConfig{ + APIKeyFile: `./api_key_file`, + APIURL: &config.URL{URL: u}, + HTTPConfig: &commoncfg.HTTPClientConfig{}, + } + notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger()) + + require.NoError(t, err) + requests, _, err := notifierWithUpdate.createRequests(ctx) + require.Equal(t, "GenieKey my_secret_api_key", requests[0].Header.Get("Authorization")) +} + func readBody(t *testing.T, r *http.Request) string { t.Helper() body, err := io.ReadAll(r.Body)