@@ -26,6 +26,8 @@ type redaction struct {
26
26
ignoreList map [string ]string
27
27
// Attribute values blocked in a span
28
28
blockRegexList map [string ]* regexp.Regexp
29
+ // Attribute values allowed in a span
30
+ allowRegexList map [string ]* regexp.Regexp
29
31
// Redaction processor configuration
30
32
config * Config
31
33
// Logger
@@ -36,16 +38,23 @@ type redaction struct {
36
38
func newRedaction (ctx context.Context , config * Config , logger * zap.Logger ) (* redaction , error ) {
37
39
allowList := makeAllowList (config )
38
40
ignoreList := makeIgnoreList (config )
39
- blockRegexList , err := makeBlockRegexList (ctx , config )
41
+ blockRegexList , err := makeRegexList (ctx , config . BlockedValues )
40
42
if err != nil {
41
43
// TODO: Placeholder for an error metric in the next PR
42
44
return nil , fmt .Errorf ("failed to process block list: %w" , err )
43
45
}
44
46
47
+ allowRegexList , err := makeRegexList (ctx , config .AllowedValues )
48
+ if err != nil {
49
+ // TODO: Placeholder for an error metric in the next PR
50
+ return nil , fmt .Errorf ("failed to process allow list: %w" , err )
51
+ }
52
+
45
53
return & redaction {
46
54
allowList : allowList ,
47
55
ignoreList : ignoreList ,
48
56
blockRegexList : blockRegexList ,
57
+ allowRegexList : allowRegexList ,
49
58
config : config ,
50
59
logger : logger ,
51
60
}, nil
@@ -159,6 +168,7 @@ func (s *redaction) processAttrs(_ context.Context, attributes pcommon.Map) {
159
168
// TODO: Use the context for recording metrics
160
169
var toDelete []string
161
170
var toBlock []string
171
+ var allowed []string
162
172
var ignoring []string
163
173
164
174
// Identify attributes to redact and mask in the following sequence
@@ -186,8 +196,17 @@ func (s *redaction) processAttrs(_ context.Context, attributes pcommon.Map) {
186
196
}
187
197
}
188
198
189
- // Mask any blocked values for the other attributes
190
199
strVal := value .Str ()
200
+
201
+ // Allow any values matching the allowed list regex
202
+ for _ , compiledRE := range s .allowRegexList {
203
+ if match := compiledRE .MatchString (strVal ); match {
204
+ allowed = append (allowed , k )
205
+ return true
206
+ }
207
+ }
208
+
209
+ // Mask any blocked values for the other attributes
191
210
var matched bool
192
211
for _ , compiledRE := range s .blockRegexList {
193
212
match := compiledRE .MatchString (strVal )
@@ -212,6 +231,7 @@ func (s *redaction) processAttrs(_ context.Context, attributes pcommon.Map) {
212
231
// Add diagnostic information to the span
213
232
s .addMetaAttrs (toDelete , attributes , redactedKeys , redactedKeyCount )
214
233
s .addMetaAttrs (toBlock , attributes , maskedValues , maskedValueCount )
234
+ s .addMetaAttrs (allowed , attributes , allowedValues , allowedValueCount )
215
235
s .addMetaAttrs (ignoring , attributes , "" , ignoredKeyCount )
216
236
}
217
237
@@ -239,13 +259,15 @@ func (s *redaction) addMetaAttrs(redactedAttrs []string, attributes pcommon.Map,
239
259
}
240
260
241
261
const (
242
- debug = "debug"
243
- info = "info"
244
- redactedKeys = "redaction.redacted.keys"
245
- redactedKeyCount = "redaction.redacted.count"
246
- maskedValues = "redaction.masked.keys"
247
- maskedValueCount = "redaction.masked.count"
248
- ignoredKeyCount = "redaction.ignored.count"
262
+ debug = "debug"
263
+ info = "info"
264
+ redactedKeys = "redaction.redacted.keys"
265
+ redactedKeyCount = "redaction.redacted.count"
266
+ maskedValues = "redaction.masked.keys"
267
+ maskedValueCount = "redaction.masked.count"
268
+ allowedValues = "redaction.allowed.keys"
269
+ allowedValueCount = "redaction.allowed.count"
270
+ ignoredKeyCount = "redaction.ignored.count"
249
271
)
250
272
251
273
// makeAllowList sets up a lookup table of allowed span attribute keys
@@ -282,16 +304,16 @@ func makeIgnoreList(c *Config) map[string]string {
282
304
return ignoreList
283
305
}
284
306
285
- // makeBlockRegexList precompiles all the blocked regex patterns
286
- func makeBlockRegexList (_ context.Context , config * Config ) (map [string ]* regexp.Regexp , error ) {
287
- blockRegexList := make (map [string ]* regexp.Regexp , len (config . BlockedValues ))
288
- for _ , pattern := range config . BlockedValues {
307
+ // makeRegexList precompiles all the regex patterns in the defined list
308
+ func makeRegexList (_ context.Context , valuesList [] string ) (map [string ]* regexp.Regexp , error ) {
309
+ regexList := make (map [string ]* regexp.Regexp , len (valuesList ))
310
+ for _ , pattern := range valuesList {
289
311
re , err := regexp .Compile (pattern )
290
312
if err != nil {
291
313
// TODO: Placeholder for an error metric in the next PR
292
- return nil , fmt .Errorf ("error compiling regex in block list: %w" , err )
314
+ return nil , fmt .Errorf ("error compiling regex in list: %w" , err )
293
315
}
294
- blockRegexList [pattern ] = re
316
+ regexList [pattern ] = re
295
317
}
296
- return blockRegexList , nil
318
+ return regexList , nil
297
319
}
0 commit comments