Skip to content

Commit 5fecfde

Browse files
zoov-xaviergotjosh
andauthored
opsgenie : trim space for api key file (#4195)
* opsgenie : trim space for api key file Signed-off-by: zoov-xavier <[email protected]> * fix lint checking error Signed-off-by: gotjosh <[email protected]> --------- Signed-off-by: zoov-xavier <[email protected]> Signed-off-by: gotjosh <[email protected]> Co-authored-by: gotjosh <[email protected]>
1 parent 05e380a commit 5fecfde

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

notify/opsgenie/api_key_file

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
my_secret_api_key
2+

notify/opsgenie/opsgenie.go

+1
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ func (n *Notifier) createRequests(ctx context.Context, as ...*types.Alert) ([]*h
282282
return nil, false, fmt.Errorf("read key_file error: %w", err)
283283
}
284284
apiKey = tmpl(string(content))
285+
apiKey = strings.TrimSpace(string(apiKey))
285286
}
286287

287288
if err != nil {

notify/opsgenie/opsgenie_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,25 @@ func TestOpsGenieWithUpdate(t *testing.T) {
321321
`, body2)
322322
}
323323

324+
func TestOpsGenieApiKeyFile(t *testing.T) {
325+
u, err := url.Parse("https://test-opsgenie-url")
326+
require.NoError(t, err)
327+
tmpl := test.CreateTmpl(t)
328+
ctx := context.Background()
329+
ctx = notify.WithGroupKey(ctx, "1")
330+
opsGenieConfigWithUpdate := config.OpsGenieConfig{
331+
APIKeyFile: `./api_key_file`,
332+
APIURL: &config.URL{URL: u},
333+
HTTPConfig: &commoncfg.HTTPClientConfig{},
334+
}
335+
notifierWithUpdate, err := New(&opsGenieConfigWithUpdate, tmpl, promslog.NewNopLogger())
336+
337+
require.NoError(t, err)
338+
requests, _, err := notifierWithUpdate.createRequests(ctx)
339+
require.NoError(t, err)
340+
require.Equal(t, "GenieKey my_secret_api_key", requests[0].Header.Get("Authorization"))
341+
}
342+
324343
func readBody(t *testing.T, r *http.Request) string {
325344
t.Helper()
326345
body, err := io.ReadAll(r.Body)

0 commit comments

Comments
 (0)