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

Use regex to find and replace token #271

Merged
merged 2 commits into from
Oct 21, 2021
Merged
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
17 changes: 12 additions & 5 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"io/ioutil"
"net/http"
"regexp"
"strings"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -224,10 +223,7 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)

go func(n notifier.Interface, e events.Event) {
if err := n.Post(e); err != nil {
if token != "" {
redacted := strings.ReplaceAll(err.Error(), token, "*****")
err = errors.New(redacted)
}
err = redactTokenFromError(err, token)

s.logger.Error(err, "failed to send notification",
"reconciler kind", event.InvolvedObject.Kind,
Expand All @@ -240,3 +236,14 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
w.WriteHeader(http.StatusAccepted)
}
}

func redactTokenFromError(err error, token string) error {
if token == "" {
return err
}

re := regexp.MustCompile(fmt.Sprintf("%s*", token))
redacted := re.ReplaceAllString(err.Error(), "*****")

return errors.New(redacted)
}
58 changes: 58 additions & 0 deletions internal/server/event_handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package server

import (
"errors"
"testing"
)

func TestRedactTokenFromError(t *testing.T) {
tests := []struct {
name string
token string
originalErrStr string
expectedErrStr string
}{
{
name: "no token",
token: "8h0387hdyehbwwa45",
originalErrStr: "Cannot post to github",
expectedErrStr: "Cannot post to github",
},
{
name: "empty token",
token: "",
originalErrStr: "Cannot post to github",
expectedErrStr: "Cannot post to github",
},
{
name: "exact token",
token: "8h0387hdyehbwwa45",
originalErrStr: "Cannot post to github with token 8h0387hdyehbwwa45",
expectedErrStr: "Cannot post to github with token *****",
},
{
name: "non-exact token",
token: "8h0387hdyehbwwa45",
originalErrStr: `Cannot post to github with token 8h0387hdyehbwwa45\\n`,
expectedErrStr: `Cannot post to github with token *****\\n`,
},
{
name: "extra text in front token",
token: "8h0387hdyehbwwa45",
originalErrStr: `Cannot post to github with token metoo8h0387hdyehbwwa45\\n`,
expectedErrStr: `Cannot post to github with token metoo*****\\n`,
},
}

for _, tt := range tests {
err := redactTokenFromError(errors.New(tt.originalErrStr), tt.token)
if err == nil {
t.Fatalf("error shouldn't be nil")
}

if err.Error() != tt.expectedErrStr {
t.Errorf("expected error string '%s' but got '%s'",
tt.expectedErrStr, err)
}
}
}