Skip to content

Commit 2392848

Browse files
committed
[scrubber] Expose CreateCustomScrubber
1 parent 0e2a44c commit 2392848

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

components/scrubber/scrubber.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"fmt"
1010
"reflect"
11+
"regexp"
1112
"strings"
1213
"unsafe"
1314

@@ -95,22 +96,47 @@ type Scrubber interface {
9596
DeepCopyStruct(val any) any
9697
}
9798

99+
type ScrubberImplConfig struct {
100+
HashedFieldNames []string
101+
HashedURLPathsFieldNames []string
102+
RedactedFieldNames []string
103+
HashedValues map[string]*regexp.Regexp
104+
RedactedValues map[string]*regexp.Regexp
105+
}
106+
107+
// CreateCustomScrubber creates a new scrubber with the given configuration
108+
// !!! Only use this if you know what you're doing. For all logging purposes, use the "Default" impl !!!
109+
func CreateCustomScrubber(cfg *ScrubberImplConfig) Scrubber {
110+
return createScrubberImpl(cfg)
111+
}
112+
98113
// Default is the default scrubber consumers of this package should use
99114
var Default Scrubber = newScrubberImpl()
100115

101116
func newScrubberImpl() *scrubberImpl {
117+
defaultCfg := ScrubberImplConfig{
118+
HashedFieldNames: HashedFieldNames,
119+
HashedURLPathsFieldNames: HashedURLPathsFieldNames,
120+
RedactedFieldNames: RedactedFieldNames,
121+
HashedValues: HashedValues,
122+
RedactedValues: RedactedValues,
123+
}
124+
return createScrubberImpl(&defaultCfg)
125+
}
126+
127+
func createScrubberImpl(cfg *ScrubberImplConfig) *scrubberImpl {
102128
var (
103129
lowerSanitiseHash []string
104130
lowerSanitiseHashURLPaths []string
105131
lowerSanitiseRedact []string
106132
)
107-
for _, v := range HashedFieldNames {
133+
for _, v := range cfg.HashedFieldNames {
108134
lowerSanitiseHash = append(lowerSanitiseHash, strings.ToLower(v))
109135
}
110-
for _, v := range HashedURLPathsFieldNames {
136+
for _, v := range cfg.HashedURLPathsFieldNames {
111137
lowerSanitiseHashURLPaths = append(lowerSanitiseHashURLPaths, strings.ToLower(v))
112138
}
113-
for _, v := range RedactedFieldNames {
139+
for _, v := range cfg.RedactedFieldNames {
114140
lowerSanitiseRedact = append(lowerSanitiseRedact, strings.ToLower(v))
115141
}
116142

@@ -123,6 +149,8 @@ func newScrubberImpl() *scrubberImpl {
123149
LowerSanitiseHash: lowerSanitiseHash,
124150
LowerSanitiseHashURLPaths: lowerSanitiseHashURLPaths,
125151
LowerSanitiseRedact: lowerSanitiseRedact,
152+
HashedValues: cfg.HashedValues,
153+
RedactedValues: cfg.RedactedValues,
126154
KeySanitiserCache: cache,
127155
}
128156
res.Walker = &structScrubber{Parent: res}
@@ -135,6 +163,8 @@ type scrubberImpl struct {
135163
LowerSanitiseHash []string
136164
LowerSanitiseHashURLPaths []string
137165
LowerSanitiseRedact []string
166+
HashedValues map[string]*regexp.Regexp
167+
RedactedValues map[string]*regexp.Regexp
138168
KeySanitiserCache *lru.Cache
139169
}
140170

@@ -413,12 +443,12 @@ func (s *scrubberImpl) scrubJsonSlice(val []interface{}) error {
413443

414444
// Value implements Scrubber
415445
func (s *scrubberImpl) Value(value string) string {
416-
for key, expr := range HashedValues {
446+
for key, expr := range s.HashedValues {
417447
value = expr.ReplaceAllStringFunc(value, func(s string) string {
418448
return SanitiseHash(s, SanitiseWithKeyName(key))
419449
})
420450
}
421-
for key, expr := range RedactedValues {
451+
for key, expr := range s.RedactedValues {
422452
value = expr.ReplaceAllStringFunc(value, func(s string) string {
423453
return SanitiseRedact(s, SanitiseWithKeyName(key))
424454
})

0 commit comments

Comments
 (0)