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

Omit data from Sentry #559

Merged
merged 4 commits into from
May 17, 2022
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
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ builds:
- <<: &build_defaults
main: ./cmd/fastly
ldflags:
- -s -w -X "github.com/fastly/cli/pkg/revision.AppVersion={{ .Version }}"
- -s -w -X "github.com/fastly/cli/pkg/revision.AppVersion=v{{ .Version }}"
- -X "github.com/fastly/cli/pkg/revision.GitCommit={{ .ShortCommit }}"
- -X "github.com/fastly/cli/pkg/revision.GoVersion={{ .Env.GOVERSION }}"
- -X "github.com/fastly/cli/pkg/revision.Environment=release"
Expand Down
10 changes: 10 additions & 0 deletions cmd/fastly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func main() {
`error matching service name with available services`,
`open fastly.toml: no such file or directory`,
},
BeforeSend: func(event *sentry.Event, _ *sentry.EventHint) *sentry.Event {
for i, e := range event.Exception {
event.Exception[i].Value = fsterr.FilterToken(e.Value)
}
return event
},
BeforeBreadcrumb: func(breadcrumb *sentry.Breadcrumb, _ *sentry.BreadcrumbHint) *sentry.Breadcrumb {
breadcrumb.Message = fsterr.FilterToken(breadcrumb.Message)
return breadcrumb
},
})
if err != nil {
log.Fatal(err)
Expand Down
21 changes: 19 additions & 2 deletions pkg/errors/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -82,7 +83,7 @@ func (l LogEntries) Persist(logPath string, args []string) error {
//
// Disabling as the input is determined from our own package.
/* #nosec */
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
f, err := os.OpenFile(logPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return fmt.Errorf(errMsg, err)
}
Expand Down Expand Up @@ -141,6 +142,22 @@ ERROR:
return nil
}

var (
// TokenRegEx matches a Token as part of the error output (https://regex101.com/r/ulIw1m/1)
TokenRegEx = regexp.MustCompile(`Token ([\w-]+)`)
// TokenFlagRegEx matches the token flag (https://regex101.com/r/YNr78Q/1)
TokenFlagRegEx = regexp.MustCompile(`(-t|--token)(\s*=?\s*['"]?)([\w-]+)(['"]?)`)
)

// FilterToken replaces any matched patterns with "REDACTED".
//
// EXAMPLE: https://go.dev/play/p/cT4BwIh9Asa
func FilterToken(input string) (inputFiltered string) {
inputFiltered = TokenRegEx.ReplaceAllString(input, "Token REDACTED")
inputFiltered = TokenFlagRegEx.ReplaceAllString(inputFiltered, "${1}${2}REDACTED${4}")
return inputFiltered
}

// instrument reports errors to our error analysis platform.
func instrument(l LogEntries, cmd string) {
sentry.AddBreadcrumb(&sentry.Breadcrumb{
Expand All @@ -162,7 +179,7 @@ func instrument(l LogEntries, cmd string) {
// https://docs.sentry.io/product/issues/issue-details/breadcrumbs/
b := sentry.Breadcrumb{
Data: entry.Context,
Message: fmt.Sprintf("%s (file: %s, line: %d)", entry.Err, file, line),
Message: fmt.Sprintf("%s (file: %s, line: %d)", entry.Err.Error(), file, line),
Timestamp: entry.Time,
Type: "error",
}
Expand Down