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
99114var Default Scrubber = newScrubberImpl ()
100115
101116func 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
@@ -191,18 +221,19 @@ func (s *scrubberImpl) getSanitisatiser(key string) Sanitisatiser {
191221 return SanitiseRedact
192222 }
193223 }
194- for _ , f := range s .LowerSanitiseHash {
195- if strings .Contains (lower , f ) {
196- s .KeySanitiserCache .Add (lower , sanitiseHash )
197- return SanitiseHash
198- }
199- }
224+ // Give sanitiseHashURLPathSegments precedence over sanitiseHash
200225 for _ , f := range s .LowerSanitiseHashURLPaths {
201226 if strings .Contains (lower , f ) {
202227 s .KeySanitiserCache .Add (lower , sanitiseHashURLPathSegments )
203228 return SanitiseHashURLPathSegments
204229 }
205230 }
231+ for _ , f := range s .LowerSanitiseHash {
232+ if strings .Contains (lower , f ) {
233+ s .KeySanitiserCache .Add (lower , sanitiseHash )
234+ return SanitiseHash
235+ }
236+ }
206237
207238 s .KeySanitiserCache .Add (lower , sanitiseIgnore )
208239 return nil
@@ -413,12 +444,12 @@ func (s *scrubberImpl) scrubJsonSlice(val []interface{}) error {
413444
414445// Value implements Scrubber
415446func (s * scrubberImpl ) Value (value string ) string {
416- for key , expr := range HashedValues {
447+ for key , expr := range s . HashedValues {
417448 value = expr .ReplaceAllStringFunc (value , func (s string ) string {
418449 return SanitiseHash (s , SanitiseWithKeyName (key ))
419450 })
420451 }
421- for key , expr := range RedactedValues {
452+ for key , expr := range s . RedactedValues {
422453 value = expr .ReplaceAllStringFunc (value , func (s string ) string {
423454 return SanitiseRedact (s , SanitiseWithKeyName (key ))
424455 })
0 commit comments