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 masktoken pkg for redacting token #388

Merged
merged 2 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/containrrr/shoutrrr v0.6.0
github.com/fluxcd/notification-controller/api v0.24.0
github.com/fluxcd/pkg/apis/meta v0.14.2
github.com/fluxcd/pkg/masktoken v0.0.1
github.com/fluxcd/pkg/runtime v0.16.2
github.com/fluxcd/pkg/ssa v0.16.1
github.com/getsentry/sentry-go v0.13.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ github.com/fluxcd/pkg/apis/acl v0.0.3 h1:Lw0ZHdpnO4G7Zy9KjrzwwBmDZQuy4qEjaU/RvA6
github.com/fluxcd/pkg/apis/acl v0.0.3/go.mod h1:XPts6lRJ9C9fIF9xVWofmQwftvhY25n1ps7W9xw0XLU=
github.com/fluxcd/pkg/apis/meta v0.14.2 h1:/Hf7I/Vz01vv3m7Qx7DtQvrzAL1oVt0MJcLb/I1Y1HE=
github.com/fluxcd/pkg/apis/meta v0.14.2/go.mod h1:ijZ61VG/8T3U17gj0aFL3fdtZL+mulD6V8VrLLUCAgM=
github.com/fluxcd/pkg/masktoken v0.0.1 h1:egWR/ibTzf4L3PxE8TauKO1srD1Ye/aalgQRQuKKRdU=
github.com/fluxcd/pkg/masktoken v0.0.1/go.mod h1:sQmMtX4s5RwdGlByJazzNasWFFgBdmtNcgeZcGBI72Y=
github.com/fluxcd/pkg/runtime v0.16.2 h1:CexfMmJK+r12sHTvKWyAax0pcPomjd6VnaHXcxjUrRY=
github.com/fluxcd/pkg/runtime v0.16.2/go.mod h1:OHSKsrO+T+Ym8WZRS2oidrnauWRARuE2nfm8ewevm7M=
github.com/fluxcd/pkg/ssa v0.16.1 h1:hWXMtDhiAPRPHpHiQ5NzVjqIDhOfyzWmc2zA49Wxw7E=
Expand Down
27 changes: 8 additions & 19 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import (
"time"

"github.com/fluxcd/pkg/runtime/conditions"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/yaml"

"github.com/fluxcd/pkg/masktoken"
"github.com/fluxcd/pkg/runtime/events"

"github.com/fluxcd/notification-controller/api/v1beta1"
Expand Down Expand Up @@ -265,8 +265,13 @@ 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 {
err = redactTokenFromError(err, token, s.logger)

maskedErrStr, maskErr := masktoken.MaskTokenFromString(err.Error(), token)
if maskErr != nil {
err = maskErr
} else {
err = errors.New(maskedErrStr)
}
err := errors.New(maskedErrStr)
Copy link
Member

@pjbgf pjbgf Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this overrides the err created in the if/else above.

Suggested change
err := errors.New(maskedErrStr)

s.logger.Error(err, "failed to send notification",
"reconciler kind", event.InvolvedObject.Kind,
"name", event.InvolvedObject.Name,
Expand Down Expand Up @@ -319,22 +324,6 @@ func (s *EventServer) eventMatchesAlert(ctx context.Context, event *events.Event
return false
}

func redactTokenFromError(err error, token string, log logr.Logger) error {
if token == "" {
return err
}

re, compileErr := regexp.Compile(fmt.Sprintf("%s*", regexp.QuoteMeta(token)))
if compileErr != nil {
newErrStr := fmt.Sprintf("error redacting token from error message: %s", compileErr)
return errors.New(newErrStr)
}

redacted := re.ReplaceAllString(err.Error(), "*****")

return errors.New(redacted)
}

// TODO: move the metadata filtering function to fluxcd/pkg/runtime/events
// cleanupMetadata removes metadata entries which are not used for alerting
func cleanupMetadata(event *events.Event) {
Expand Down
87 changes: 0 additions & 87 deletions internal/server/event_handlers_test.go

This file was deleted.