-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: made sure any tokens output in the logs are now censored (#143)
- Loading branch information
Showing
2 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
// CensorFormatter makes sure sensitive data is not logged. | ||
// It works as a middleware and sensors the data before sending it to an underlying formatter | ||
type CensorFormatter struct { | ||
CensorItems []CensorItem | ||
UnderlyingFormatter log.Formatter | ||
} | ||
|
||
// CensorItem is something that should be censored, Sensitive will be replaced with Replacement | ||
type CensorItem struct { | ||
Sensitive string | ||
Replacement string | ||
} | ||
|
||
// Format censors some data and sends the entry to the underlying formatter | ||
func (f *CensorFormatter) Format(entry *log.Entry) ([]byte, error) { | ||
for _, s := range f.CensorItems { | ||
entry.Message = strings.ReplaceAll(entry.Message, s.Sensitive, s.Replacement) | ||
|
||
for key := range entry.Data { | ||
if str, ok := entry.Data[key].(string); ok { | ||
entry.Data[key] = strings.ReplaceAll(str, s.Sensitive, s.Replacement) | ||
} | ||
if bb, ok := entry.Data[key].([]byte); ok { | ||
entry.Data[key] = bytes.ReplaceAll(bb, []byte(s.Sensitive), []byte(s.Replacement)) | ||
} | ||
} | ||
} | ||
return f.UnderlyingFormatter.Format(entry) | ||
} |