8
8
"encoding/json"
9
9
"fmt"
10
10
"reflect"
11
+ "regexp"
11
12
"strings"
12
13
"unsafe"
13
14
@@ -95,22 +96,47 @@ type Scrubber interface {
95
96
DeepCopyStruct (val any ) any
96
97
}
97
98
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
+
98
113
// Default is the default scrubber consumers of this package should use
99
114
var Default Scrubber = newScrubberImpl ()
100
115
101
116
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 {
102
128
var (
103
129
lowerSanitiseHash []string
104
130
lowerSanitiseHashURLPaths []string
105
131
lowerSanitiseRedact []string
106
132
)
107
- for _ , v := range HashedFieldNames {
133
+ for _ , v := range cfg . HashedFieldNames {
108
134
lowerSanitiseHash = append (lowerSanitiseHash , strings .ToLower (v ))
109
135
}
110
- for _ , v := range HashedURLPathsFieldNames {
136
+ for _ , v := range cfg . HashedURLPathsFieldNames {
111
137
lowerSanitiseHashURLPaths = append (lowerSanitiseHashURLPaths , strings .ToLower (v ))
112
138
}
113
- for _ , v := range RedactedFieldNames {
139
+ for _ , v := range cfg . RedactedFieldNames {
114
140
lowerSanitiseRedact = append (lowerSanitiseRedact , strings .ToLower (v ))
115
141
}
116
142
@@ -123,6 +149,8 @@ func newScrubberImpl() *scrubberImpl {
123
149
LowerSanitiseHash : lowerSanitiseHash ,
124
150
LowerSanitiseHashURLPaths : lowerSanitiseHashURLPaths ,
125
151
LowerSanitiseRedact : lowerSanitiseRedact ,
152
+ HashedValues : cfg .HashedValues ,
153
+ RedactedValues : cfg .RedactedValues ,
126
154
KeySanitiserCache : cache ,
127
155
}
128
156
res .Walker = & structScrubber {Parent : res }
@@ -135,6 +163,8 @@ type scrubberImpl struct {
135
163
LowerSanitiseHash []string
136
164
LowerSanitiseHashURLPaths []string
137
165
LowerSanitiseRedact []string
166
+ HashedValues map [string ]* regexp.Regexp
167
+ RedactedValues map [string ]* regexp.Regexp
138
168
KeySanitiserCache * lru.Cache
139
169
}
140
170
@@ -191,18 +221,19 @@ func (s *scrubberImpl) getSanitisatiser(key string) Sanitisatiser {
191
221
return SanitiseRedact
192
222
}
193
223
}
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
200
225
for _ , f := range s .LowerSanitiseHashURLPaths {
201
226
if strings .Contains (lower , f ) {
202
227
s .KeySanitiserCache .Add (lower , sanitiseHashURLPathSegments )
203
228
return SanitiseHashURLPathSegments
204
229
}
205
230
}
231
+ for _ , f := range s .LowerSanitiseHash {
232
+ if strings .Contains (lower , f ) {
233
+ s .KeySanitiserCache .Add (lower , sanitiseHash )
234
+ return SanitiseHash
235
+ }
236
+ }
206
237
207
238
s .KeySanitiserCache .Add (lower , sanitiseIgnore )
208
239
return nil
@@ -413,12 +444,12 @@ func (s *scrubberImpl) scrubJsonSlice(val []interface{}) error {
413
444
414
445
// Value implements Scrubber
415
446
func (s * scrubberImpl ) Value (value string ) string {
416
- for key , expr := range HashedValues {
447
+ for key , expr := range s . HashedValues {
417
448
value = expr .ReplaceAllStringFunc (value , func (s string ) string {
418
449
return SanitiseHash (s , SanitiseWithKeyName (key ))
419
450
})
420
451
}
421
- for key , expr := range RedactedValues {
452
+ for key , expr := range s . RedactedValues {
422
453
value = expr .ReplaceAllStringFunc (value , func (s string ) string {
423
454
return SanitiseRedact (s , SanitiseWithKeyName (key ))
424
455
})
0 commit comments