Skip to content

Commit

Permalink
Trim contents of slack api urls from files
Browse files Browse the repository at this point in the history
Signed-off-by: Sarah Brofeldt <sarah@qtr.dk>
  • Loading branch information
srhb committed Jul 29, 2022
1 parent f958b8b commit 34d3162
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion notify/slack/slack.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions notify/slack/slack_test.go
Expand Up @@ -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())
}

0 comments on commit 34d3162

Please sign in to comment.