Skip to content

Commit

Permalink
Trim contents of slack api urls from files (#2929)
Browse files Browse the repository at this point in the history
* Trim contents of slack api urls from files

Signed-off-by: Sarah Brofeldt <sarah@qtr.dk>

* notify/slack: fix test imports

Signed-off-by: Simon Pasquier <spasquie@redhat.com>

Signed-off-by: Sarah Brofeldt <sarah@qtr.dk>
Signed-off-by: Simon Pasquier <spasquie@redhat.com>
Co-authored-by: Simon Pasquier <spasquie@redhat.com>
  • Loading branch information
srhb and simonpasquier committed Sep 9, 2022
1 parent a862b7f commit 565d73d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion notify/slack/slack.go
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"net/http"
"os"
"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
23 changes: 23 additions & 0 deletions notify/slack/slack_test.go
Expand Up @@ -15,6 +15,7 @@ package slack

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -80,3 +81,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 565d73d

Please sign in to comment.