Skip to content

Commit

Permalink
opsgenie : trim space for api key file
Browse files Browse the repository at this point in the history
Signed-off-by: zoov-xavier <[email protected]>
  • Loading branch information
zoov-xavier committed Jan 7, 2025
1 parent 3b61ae8 commit cd1de9c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions notify/opsgenie/api_key_file
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
my_secret_api_key

1 change: 1 addition & 0 deletions notify/opsgenie/opsgenie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions notify/opsgenie/opsgenie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cd1de9c

Please sign in to comment.