From 2ab623012df11018ade34172beacca408ee05d52 Mon Sep 17 00:00:00 2001 From: Sarah Brofeldt Date: Mon, 23 May 2022 10:21:10 +0200 Subject: [PATCH 1/2] Trim contents of slack api urls from files Signed-off-by: Sarah Brofeldt --- notify/slack/slack.go | 3 ++- notify/slack/slack_test.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/notify/slack/slack.go b/notify/slack/slack.go index b8bdb1fba0..8f3445e093 100644 --- a/notify/slack/slack.go +++ b/notify/slack/slack.go @@ -20,6 +20,7 @@ import ( "fmt" "net/http" "os" + "strings" "github.com/go-kit/log" "github.com/go-kit/log/level" @@ -194,7 +195,7 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error) if err != nil { return false, err } - u = string(content) + u = strings.TrimSpace(string(content)) } resp, err := notify.PostJSON(ctx, n.client, u, &buf) diff --git a/notify/slack/slack_test.go b/notify/slack/slack_test.go index 471f87198c..0b48326456 100644 --- a/notify/slack/slack_test.go +++ b/notify/slack/slack_test.go @@ -80,3 +80,25 @@ func TestGettingSlackURLFromFile(t *testing.T) { test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String()) } + +func TestTrimmingSlackURLFromFile(t *testing.T) { + ctx, u, fn := test.GetContextWithCancelingURL() + defer fn() + + f, err := ioutil.TempFile("", "slack_test_newline") + require.NoError(t, err, "creating temp file failed") + _, err = f.WriteString(u.String() + "\n\n") + require.NoError(t, err, "writing to temp file failed") + + notifier, err := New( + &config.SlackConfig{ + APIURLFile: f.Name(), + HTTPConfig: &commoncfg.HTTPClientConfig{}, + }, + test.CreateTmpl(t), + log.NewNopLogger(), + ) + require.NoError(t, err) + + test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String()) +} From 0b28f0a1f8fe24b32959f73bc724044a9f4e0b96 Mon Sep 17 00:00:00 2001 From: Simon Pasquier Date: Fri, 9 Sep 2022 16:29:43 +0200 Subject: [PATCH 2/2] notify/slack: fix test imports Signed-off-by: Simon Pasquier --- notify/slack/slack_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/notify/slack/slack_test.go b/notify/slack/slack_test.go index 0b48326456..c61ffdb26b 100644 --- a/notify/slack/slack_test.go +++ b/notify/slack/slack_test.go @@ -15,6 +15,7 @@ package slack import ( "fmt" + "io/ioutil" "os" "testing"