Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opsgenie : trim space for api key file #4195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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