From 143e02f251ac6f8b4e1dbc11b12d05bfb789b215 Mon Sep 17 00:00:00 2001 From: zoov-xavier Date: Mon, 6 Jan 2025 14:54:35 +0100 Subject: [PATCH] opsgenie : trim space for api key file Signed-off-by: zoov-xavier --- notify/opsgenie/api_key_file | 1 + notify/opsgenie/opsgenie.go | 1 + notify/opsgenie/opsgenie_test.go | 46 +++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 notify/opsgenie/api_key_file diff --git a/notify/opsgenie/api_key_file b/notify/opsgenie/api_key_file new file mode 100644 index 0000000000..3ccc5c1c6a --- /dev/null +++ b/notify/opsgenie/api_key_file @@ -0,0 +1 @@ +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..9093318d47 100644 --- a/notify/opsgenie/opsgenie_test.go +++ b/notify/opsgenie/opsgenie_test.go @@ -34,6 +34,10 @@ import ( "github.com/prometheus/alertmanager/types" ) +var ( + apiKey = "my_secret_api_key" +) + func TestOpsGenieRetry(t *testing.T) { notifier, err := New( &config.OpsGenieConfig{ @@ -134,7 +138,7 @@ func TestOpsGenie(t *testing.T) { Priority: `{{ .CommonLabels.Priority }}`, Entity: `{{ .CommonLabels.Entity }}`, Actions: `{{ .CommonLabels.Actions }}`, - APIKey: `{{ .ExternalURL }}`, + APIKey: config.Secret(apiKey), APIURL: &config.URL{URL: u}, HTTPConfig: &commoncfg.HTTPClientConfig{}, }, @@ -170,7 +174,7 @@ func TestOpsGenie(t *testing.T) { Priority: `{{ .CommonLabels.Priority }}`, Entity: `{{ .CommonLabels.Entity }}`, Actions: `{{ .CommonLabels.Actions }}`, - APIKey: `{{ .ExternalURL }}`, + APIKey: config.Secret(apiKey), APIURL: &config.URL{URL: u}, HTTPConfig: &commoncfg.HTTPClientConfig{}, }, @@ -200,13 +204,47 @@ func TestOpsGenie(t *testing.T) { Tags: `{{ .CommonLabels.Tags }}`, Note: `{{ .CommonLabels.Note }}`, Priority: `{{ .CommonLabels.Priority }}`, - APIKey: `{{ .ExternalURL }}`, + APIKey: config.Secret(apiKey), APIURL: &config.URL{URL: u}, HTTPConfig: &commoncfg.HTTPClientConfig{}, }, expectedEmptyAlertBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"","details":{"Description":"adjusted "},"source":""} `, expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Actions":"doThis,doThat","Description":"adjusted description","Entity":"test-domain","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderName3":"TeamA,TeamB","ResponderType1":"team","ResponderType2":"escalation","ResponderType3":"teams","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"TeamB","type":"team"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1"} +`, + }, + { + title: "config with api key file", + cfg: &config.OpsGenieConfig{ + NotifierConfig: config.NotifierConfig{ + VSendResolved: true, + }, + Message: `{{ .CommonLabels.Message }}`, + Description: `{{ .CommonLabels.Description }}`, + Source: `{{ .CommonLabels.Source }}`, + Responders: []config.OpsGenieConfigResponder{ + { + Name: `{{ .CommonLabels.ResponderName1 }}`, + Type: `{{ .CommonLabels.ResponderType1 }}`, + }, + { + Name: `{{ .CommonLabels.ResponderName2 }}`, + Type: `{{ .CommonLabels.ResponderType2 }}`, + }, + }, + Tags: `{{ .CommonLabels.Tags }}`, + Note: `{{ .CommonLabels.Note }}`, + Priority: `{{ .CommonLabels.Priority }}`, + Entity: `{{ .CommonLabels.Entity }}`, + Actions: `{{ .CommonLabels.Actions }}`, + APIKeyFile: `./api_key_file`, + + APIURL: &config.URL{URL: u}, + HTTPConfig: &commoncfg.HTTPClientConfig{}, + }, + expectedEmptyAlertBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"","details":{},"source":""} +`, + expectedBody: `{"alias":"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b","message":"message","description":"description","details":{"Actions":"doThis,doThat","Description":"description","Entity":"test-domain","Message":"message","Note":"this is a note","Priority":"P1","ResponderName1":"TeamA","ResponderName2":"EscalationA","ResponderName3":"TeamA,TeamB","ResponderType1":"team","ResponderType2":"escalation","ResponderType3":"teams","Source":"http://prometheus","Tags":"tag1,tag2"},"source":"http://prometheus","responders":[{"name":"TeamA","type":"team"},{"name":"EscalationA","type":"escalation"}],"tags":["tag1","tag2"],"note":"this is a note","priority":"P1","entity":"test-domain","actions":["doThis","doThat"]} `, }, } { @@ -232,7 +270,7 @@ func TestOpsGenie(t *testing.T) { require.Len(t, req, 1) require.True(t, retry) require.Equal(t, expectedURL, req[0].URL) - require.Equal(t, "GenieKey http://am", req[0].Header.Get("Authorization")) + require.Equal(t, "GenieKey my_secret_api_key", req[0].Header.Get("Authorization")) require.Equal(t, tc.expectedEmptyAlertBody, readBody(t, req[0])) // Fully defined alert.