-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into captaindata-v3-detector
- Loading branch information
Showing
8 changed files
with
247 additions
and
60 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 was deleted.
Oops, something went wrong.
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,50 @@ | ||
package log | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type dynamicRedactor struct { | ||
denySet map[string]struct{} | ||
denySlice []string | ||
denyMu sync.Mutex | ||
|
||
replacer atomic.Pointer[strings.Replacer] | ||
} | ||
|
||
var globalRedactor *dynamicRedactor | ||
|
||
func init() { | ||
globalRedactor = &dynamicRedactor{denySet: make(map[string]struct{})} | ||
globalRedactor.replacer.CompareAndSwap(nil, strings.NewReplacer()) | ||
} | ||
|
||
// RedactGlobally configures the global log redactor to redact the provided value during log emission. The value will be | ||
// redacted in log messages and values that are strings, but not in log keys or values of other types. | ||
func RedactGlobally(sensitiveValue string) { | ||
globalRedactor.configureForRedaction(sensitiveValue) | ||
} | ||
|
||
func (r *dynamicRedactor) configureForRedaction(sensitiveValue string) { | ||
if sensitiveValue == "" { | ||
return | ||
} | ||
|
||
r.denyMu.Lock() | ||
defer r.denyMu.Unlock() | ||
|
||
if _, ok := r.denySet[sensitiveValue]; ok { | ||
return | ||
} | ||
|
||
r.denySet[sensitiveValue] = struct{}{} | ||
r.denySlice = append(r.denySlice, sensitiveValue, "*****") | ||
|
||
r.replacer.Store(strings.NewReplacer(r.denySlice...)) | ||
} | ||
|
||
func (r *dynamicRedactor) redact(s string) string { | ||
return r.replacer.Load().Replace(s) | ||
} |
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
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,42 @@ | ||
package log | ||
|
||
import ( | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// redactionCore wraps a zapcore.Core to perform redaction of log messages in | ||
// the message and field values. | ||
type redactionCore struct { | ||
zapcore.Core | ||
redactor *dynamicRedactor | ||
} | ||
|
||
// NewRedactionCore creates a zapcore.Core that performs redaction of logs in | ||
// the message and field values. | ||
func NewRedactionCore(core zapcore.Core, redactor *dynamicRedactor) zapcore.Core { | ||
return &redactionCore{core, redactor} | ||
} | ||
|
||
// Check overrides the embedded zapcore.Core Check() method to add the | ||
// redactionCore to the zapcore.CheckedEntry. | ||
func (c *redactionCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry { | ||
if c.Enabled(ent.Level) { | ||
return ce.AddCore(ent, c) | ||
} | ||
return ce | ||
} | ||
|
||
func (c *redactionCore) With(fields []zapcore.Field) zapcore.Core { | ||
return NewRedactionCore(c.Core.With(fields), c.redactor) | ||
} | ||
|
||
// Write overrides the embedded zapcore.Core Write() method to redact the message and fields before passing them to be | ||
// written. Only message and string values are redacted; keys and non-string values (e.g. those inside of arrays and | ||
// structured objects) are not redacted. | ||
func (c *redactionCore) Write(ent zapcore.Entry, fields []zapcore.Field) error { | ||
ent.Message = c.redactor.redact(ent.Message) | ||
for i := range fields { | ||
fields[i].String = c.redactor.redact(fields[i].String) | ||
} | ||
return c.Core.Write(ent, fields) | ||
} |
Oops, something went wrong.