Skip to content

Commit

Permalink
Fix redacting list and objects (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
liranbg authored Jan 17, 2022
1 parent 2958fb9 commit a9281bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
28 changes: 27 additions & 1 deletion buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,26 @@ type BufferLoggerTestSuite struct {
}

func (suite *BufferLoggerTestSuite) TestRedactor() {
someStruct := struct {
Headers map[string][]string `json:"headers"`
TopSecret interface{} `json:"topSecret"`
}{
Headers: map[string][]string{
"cookie": {
"session=some-secret-session",
},
},
TopSecret: struct {
Address string `json:"address"`
}{
Address: "some-secret-address",
},
}
redactor := NewRedactor(&bytes.Buffer{})
redactor.AddValueRedactions([]string{"password"})
redactor.AddRedactions([]string{"replaceme"})
redactor.AddValueRedactions([]string{"cookie"})
redactor.AddValueRedactions([]string{"topSecret"})
bufferLogger, err := NewBufferLoggerWithRedactor("test", "json", InfoLevel, redactor)
suite.Require().NoError(err, "Failed creating buffer logger")

Expand All @@ -41,7 +58,10 @@ func (suite *BufferLoggerTestSuite) TestRedactor() {
bufferLogger.Logger.prepareVarsCallback = bufferLogger.Logger.prepareVarsStructured

// log
bufferLogger.Logger.InfoWith("Check", "password", "123456", "replaceme", "55")
bufferLogger.Logger.InfoWith("Check",
"password", "123456",
"replaceme", "55",
"complexStruct", someStruct)

// get log entries
logEntries, err := bufferLogger.GetLogEntries()
Expand All @@ -53,6 +73,12 @@ func (suite *BufferLoggerTestSuite) TestRedactor() {
suite.Require().Equal(map[string]interface{}{
"*****": "55",
"password": "[redacted]",
"complexStruct": map[string]interface{}{
"headers": map[string]interface{}{
"cookie": "[redacted]",
},
"topSecret": "[redacted]",
},
}, logEntries[0][bufferLogger.Logger.customEncoderConfig.JSON.VarGroupName])
}

Expand Down
10 changes: 6 additions & 4 deletions redactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,14 @@ func (r *Redactor) prepareReplacements() {
// w/wo single/double quotes
// golang regex doesn't support lookarounds, so we will check things manually
matchKeyWithSeparatorTemplate := `\\*[\'"]?(?i)%s\\*[\'"]?\s*[=:]\s*`
matchValue := `\'[^\']*?\'|\"[^\"]*\"|\S*`
matchValue := `\'[^\']*?\'|\"[^\"]*\"|\[[^\]]*?\]|\{[^\}]*?\}|\S*`

for _, redactionField := range r.valueRedactions {
// reset to avoid duplicates
r.valueRedactionsRegexps = make([]regexp.Regexp, len(r.valueRedactions))
for idx, redactionField := range r.valueRedactions {
matchKeyWithSeparator := fmt.Sprintf(matchKeyWithSeparatorTemplate, redactionField)
r.valueRedactionsRegexps = append(r.valueRedactionsRegexps,
*regexp.MustCompile(fmt.Sprintf(`(%s)(%s)`, matchKeyWithSeparator, matchValue)),
r.valueRedactionsRegexps[idx] = *regexp.MustCompile(
fmt.Sprintf(`(%s)(%s)`, matchKeyWithSeparator, matchValue),
)
}

Expand Down

0 comments on commit a9281bb

Please sign in to comment.