Trim contents of slack api urls from files (#2929)

* 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>
This commit is contained in:
Sarah Brofeldt 2022-09-09 16:37:31 +02:00 committed by GitHub
parent a862b7f4b4
commit 565d73da5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -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)

View File

@ -15,6 +15,7 @@ package slack
import (
"fmt"
"io/ioutil"
"os"
"testing"
@ -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())
}