@@ -18,18 +18,19 @@ import (
18
18
"go.uber.org/zap/zaptest"
19
19
)
20
20
21
- type testConfig struct {
22
- allowed map [string ]pcommon.Value
23
- ignored map [string ]pcommon.Value
24
- redacted map [string ]pcommon.Value
25
- masked map [string ]pcommon.Value
26
- config * Config
21
+ type TestConfig struct {
22
+ allowed map [string ]pcommon.Value
23
+ ignored map [string ]pcommon.Value
24
+ redacted map [string ]pcommon.Value
25
+ masked map [string ]pcommon.Value
26
+ blockedKeys map [string ]pcommon.Value
27
+ config * Config
27
28
}
28
29
29
30
// TestRedactUnknownAttributes validates that the processor deletes span
30
31
// attributes that are not the allowed keys list
31
32
func TestRedactUnknownAttributes (t * testing.T ) {
32
- testConfig := testConfig {
33
+ testConfig := TestConfig {
33
34
config : & Config {
34
35
AllowedKeys : []string {"group" , "id" , "name" },
35
36
},
@@ -81,7 +82,7 @@ func TestRedactUnknownAttributes(t *testing.T) {
81
82
// span attributes that are not the allowed keys list if Config.AllowAllKeys
82
83
// is set to true
83
84
func TestAllowAllKeys (t * testing.T ) {
84
- testConfig := testConfig {
85
+ testConfig := TestConfig {
85
86
config : & Config {
86
87
AllowedKeys : []string {"group" , "id" },
87
88
AllowAllKeys : true ,
@@ -125,7 +126,7 @@ func TestAllowAllKeys(t *testing.T) {
125
126
// TestAllowAllKeysMaskValues validates that the processor still redacts
126
127
// span attribute values if Config.AllowAllKeys is set to true
127
128
func TestAllowAllKeysMaskValues (t * testing.T ) {
128
- testConfig := testConfig {
129
+ testConfig := TestConfig {
129
130
config : & Config {
130
131
AllowedKeys : []string {"group" , "id" , "name" },
131
132
BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
@@ -176,12 +177,13 @@ func TestAllowAllKeysMaskValues(t *testing.T) {
176
177
// of any attributes it deleted to the new redaction.redacted.keys and
177
178
// redaction.redacted.count span attributes while set to full debug output
178
179
func TestRedactSummaryDebug (t * testing.T ) {
179
- testConfig := testConfig {
180
+ testConfig := TestConfig {
180
181
config : & Config {
181
- AllowedKeys : []string {"id" , "group" , "name" , "group.id" , "member (id)" },
182
- BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
183
- IgnoredKeys : []string {"safe_attribute" },
184
- Summary : "debug" ,
182
+ AllowedKeys : []string {"id" , "group" , "name" , "group.id" , "member (id)" , "token_some" , "api_key_some" },
183
+ BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
184
+ IgnoredKeys : []string {"safe_attribute" },
185
+ BlockedKeyPatterns : []string {".*token.*" , ".*api_key.*" },
186
+ Summary : "debug" ,
185
187
},
186
188
allowed : map [string ]pcommon.Value {
187
189
"id" : pcommon .NewValueInt (5 ),
@@ -197,6 +199,10 @@ func TestRedactSummaryDebug(t *testing.T) {
197
199
redacted : map [string ]pcommon.Value {
198
200
"credit_card" : pcommon .NewValueStr ("4111111111111111" ),
199
201
},
202
+ blockedKeys : map [string ]pcommon.Value {
203
+ "token_some" : pcommon .NewValueStr ("tokenize" ),
204
+ "api_key_some" : pcommon .NewValueStr ("apinize" ),
205
+ },
200
206
}
201
207
202
208
outTraces := runTest (t , testConfig )
@@ -236,15 +242,102 @@ func TestRedactSummaryDebug(t *testing.T) {
236
242
assert .True (t , ok )
237
243
assert .Equal (t , int64 (len (testConfig .ignored )), ignoredKeyCount .Int ())
238
244
239
- blockedKeys := []string {"name" }
245
+ blockedKeys := []string {"api_key_some" , " name" , "token_some " }
240
246
maskedValues , ok := attr .Get (maskedValues )
241
247
assert .True (t , ok )
242
248
assert .Equal (t , strings .Join (blockedKeys , "," ), maskedValues .Str ())
243
249
maskedValueCount , ok := attr .Get (maskedValueCount )
244
250
assert .True (t , ok )
245
- assert .Equal (t , int64 (1 ), maskedValueCount .Int ())
251
+ assert .Equal (t , int64 (3 ), maskedValueCount .Int ())
246
252
value , _ := attr .Get ("name" )
247
253
assert .Equal (t , "placeholder ****" , value .Str ())
254
+ value , _ = attr .Get ("api_key_some" )
255
+ assert .Equal (t , "****" , value .Str ())
256
+ value , _ = attr .Get ("token_some" )
257
+ assert .Equal (t , "****" , value .Str ())
258
+ }
259
+ }
260
+
261
+ func TestRedactSummaryDebugHashMD5 (t * testing.T ) {
262
+ testConfig := TestConfig {
263
+ config : & Config {
264
+ AllowedKeys : []string {"id" , "group" , "name" , "group.id" , "member (id)" , "token_some" , "api_key_some" },
265
+ BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
266
+ HashFunction : MD5 ,
267
+ IgnoredKeys : []string {"safe_attribute" },
268
+ BlockedKeyPatterns : []string {".*token.*" , ".*api_key.*" },
269
+ Summary : "debug" ,
270
+ },
271
+ allowed : map [string ]pcommon.Value {
272
+ "id" : pcommon .NewValueInt (5 ),
273
+ "group.id" : pcommon .NewValueStr ("some.valid.id" ),
274
+ "member (id)" : pcommon .NewValueStr ("some other valid id" ),
275
+ },
276
+ masked : map [string ]pcommon.Value {
277
+ "name" : pcommon .NewValueStr ("placeholder 4111111111111111" ),
278
+ },
279
+ ignored : map [string ]pcommon.Value {
280
+ "safe_attribute" : pcommon .NewValueStr ("harmless 4111111111111112" ),
281
+ },
282
+ redacted : map [string ]pcommon.Value {
283
+ "credit_card" : pcommon .NewValueStr ("4111111111111111" ),
284
+ },
285
+ blockedKeys : map [string ]pcommon.Value {
286
+ "token_some" : pcommon .NewValueStr ("tokenize" ),
287
+ "api_key_some" : pcommon .NewValueStr ("apinize" ),
288
+ },
289
+ }
290
+
291
+ outTraces := runTest (t , testConfig )
292
+ outLogs := runLogsTest (t , testConfig )
293
+ outMetricsGauge := runMetricsTest (t , testConfig , pmetric .MetricTypeGauge )
294
+ outMetricsSum := runMetricsTest (t , testConfig , pmetric .MetricTypeSum )
295
+ outMetricsHistogram := runMetricsTest (t , testConfig , pmetric .MetricTypeHistogram )
296
+ outMetricsExponentialHistogram := runMetricsTest (t , testConfig , pmetric .MetricTypeExponentialHistogram )
297
+ outMetricsSummary := runMetricsTest (t , testConfig , pmetric .MetricTypeSummary )
298
+
299
+ attrs := []pcommon.Map {
300
+ outTraces .ResourceSpans ().At (0 ).ScopeSpans ().At (0 ).Spans ().At (0 ).Attributes (),
301
+ outLogs .ResourceLogs ().At (0 ).ScopeLogs ().At (0 ).LogRecords ().At (0 ).Attributes (),
302
+ outMetricsGauge .ResourceMetrics ().At (0 ).ScopeMetrics ().At (0 ).Metrics ().At (0 ).Gauge ().DataPoints ().At (0 ).Attributes (),
303
+ outMetricsSum .ResourceMetrics ().At (0 ).ScopeMetrics ().At (0 ).Metrics ().At (0 ).Sum ().DataPoints ().At (0 ).Attributes (),
304
+ outMetricsHistogram .ResourceMetrics ().At (0 ).ScopeMetrics ().At (0 ).Metrics ().At (0 ).Histogram ().DataPoints ().At (0 ).Attributes (),
305
+ outMetricsExponentialHistogram .ResourceMetrics ().At (0 ).ScopeMetrics ().At (0 ).Metrics ().At (0 ).ExponentialHistogram ().DataPoints ().At (0 ).Attributes (),
306
+ outMetricsSummary .ResourceMetrics ().At (0 ).ScopeMetrics ().At (0 ).Metrics ().At (0 ).Summary ().DataPoints ().At (0 ).Attributes (),
307
+ }
308
+
309
+ for _ , attr := range attrs {
310
+ deleted := make ([]string , 0 , len (testConfig .redacted ))
311
+ for k := range testConfig .redacted {
312
+ _ , ok := attr .Get (k )
313
+ assert .False (t , ok )
314
+ deleted = append (deleted , k )
315
+ }
316
+ maskedKeys , ok := attr .Get (redactedKeys )
317
+ assert .True (t , ok )
318
+ sort .Strings (deleted )
319
+ assert .Equal (t , strings .Join (deleted , "," ), maskedKeys .Str ())
320
+ maskedKeyCount , ok := attr .Get (redactedKeyCount )
321
+ assert .True (t , ok )
322
+ assert .Equal (t , int64 (len (deleted )), maskedKeyCount .Int ())
323
+
324
+ ignoredKeyCount , ok := attr .Get (ignoredKeyCount )
325
+ assert .True (t , ok )
326
+ assert .Equal (t , int64 (len (testConfig .ignored )), ignoredKeyCount .Int ())
327
+
328
+ blockedKeys := []string {"api_key_some" , "name" , "token_some" }
329
+ maskedValues , ok := attr .Get (maskedValues )
330
+ assert .True (t , ok )
331
+ assert .Equal (t , strings .Join (blockedKeys , "," ), maskedValues .Str ())
332
+ maskedValueCount , ok := attr .Get (maskedValueCount )
333
+ assert .True (t , ok )
334
+ assert .Equal (t , int64 (3 ), maskedValueCount .Int ())
335
+ value , _ := attr .Get ("name" )
336
+ assert .Equal (t , "placeholder 5910f4ea0062a0e29afd3dccc741e3ce" , value .Str ())
337
+ value , _ = attr .Get ("api_key_some" )
338
+ assert .Equal (t , "93a699237950bde9eb9d25c7ead025f3" , value .Str ())
339
+ value , _ = attr .Get ("token_some" )
340
+ assert .Equal (t , "77e9ef3680c5518785ef0121d3884c3d" , value .Str ())
248
341
}
249
342
}
250
343
@@ -253,7 +346,7 @@ func TestRedactSummaryDebug(t *testing.T) {
253
346
// attribute (but not to redaction.redacted.keys) when set to the info level
254
347
// of output
255
348
func TestRedactSummaryInfo (t * testing.T ) {
256
- testConfig := testConfig {
349
+ testConfig := TestConfig {
257
350
config : & Config {
258
351
AllowedKeys : []string {"id" , "name" , "group" },
259
352
BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
@@ -324,7 +417,7 @@ func TestRedactSummaryInfo(t *testing.T) {
324
417
// TestRedactSummarySilent validates that the processor does not create the
325
418
// summary attributes when set to silent
326
419
func TestRedactSummarySilent (t * testing.T ) {
327
- testConfig := testConfig {
420
+ testConfig := TestConfig {
328
421
config : & Config {
329
422
AllowedKeys : []string {"id" , "name" , "group" },
330
423
BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" },
@@ -380,7 +473,7 @@ func TestRedactSummarySilent(t *testing.T) {
380
473
// TestRedactSummaryDefault validates that the processor does not create the
381
474
// summary attributes by default
382
475
func TestRedactSummaryDefault (t * testing.T ) {
383
- testConfig := testConfig {
476
+ testConfig := TestConfig {
384
477
config : & Config {AllowedKeys : []string {"id" , "name" , "group" }},
385
478
allowed : map [string ]pcommon.Value {
386
479
"id" : pcommon .NewValueInt (5 ),
@@ -428,7 +521,7 @@ func TestRedactSummaryDefault(t *testing.T) {
428
521
// TestMultipleBlockValues validates that the processor can block multiple
429
522
// patterns
430
523
func TestMultipleBlockValues (t * testing.T ) {
431
- testConfig := testConfig {
524
+ testConfig := TestConfig {
432
525
config : & Config {
433
526
AllowedKeys : []string {"id" , "name" , "mystery" },
434
527
BlockedValues : []string {"4[0-9]{12}(?:[0-9]{3})?" , "(5[1-5][0-9]{3})" },
@@ -537,7 +630,7 @@ func TestProcessAttrsAppliedTwice(t *testing.T) {
537
630
// runTest transforms the test input data and passes it through the processor
538
631
func runTest (
539
632
t * testing.T ,
540
- cfg testConfig ,
633
+ cfg TestConfig ,
541
634
) ptrace.Traces {
542
635
inBatch := ptrace .NewTraces ()
543
636
rs := inBatch .ResourceSpans ().AppendEmpty ()
@@ -549,13 +642,16 @@ func runTest(
549
642
span .SetName ("first-batch-first-span" )
550
643
span .SetTraceID ([16 ]byte {1 , 2 , 3 , 4 })
551
644
552
- length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored )
645
+ length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored ) + len ( cfg . blockedKeys )
553
646
for k , v := range cfg .allowed {
554
647
v .CopyTo (span .Attributes ().PutEmpty (k ))
555
648
}
556
649
for k , v := range cfg .masked {
557
650
v .CopyTo (span .Attributes ().PutEmpty (k ))
558
651
}
652
+ for k , v := range cfg .blockedKeys {
653
+ v .CopyTo (span .Attributes ().PutEmpty (k ))
654
+ }
559
655
for k , v := range cfg .redacted {
560
656
v .CopyTo (span .Attributes ().PutEmpty (k ))
561
657
}
@@ -581,7 +677,7 @@ func runTest(
581
677
// runLogsTest transforms the test input log data and passes it through the processor
582
678
func runLogsTest (
583
679
t * testing.T ,
584
- cfg testConfig ,
680
+ cfg TestConfig ,
585
681
) plog.Logs {
586
682
inBatch := plog .NewLogs ()
587
683
rl := inBatch .ResourceLogs ().AppendEmpty ()
@@ -594,13 +690,16 @@ func runLogsTest(
594
690
logEntry .Body ().SetStr ("first-batch-first-logEntry" )
595
691
logEntry .SetTraceID ([16 ]byte {1 , 2 , 3 , 4 })
596
692
597
- length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored )
693
+ length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored ) + len ( cfg . blockedKeys )
598
694
for k , v := range cfg .allowed {
599
695
v .CopyTo (logEntry .Attributes ().PutEmpty (k ))
600
696
}
601
697
for k , v := range cfg .masked {
602
698
v .CopyTo (logEntry .Attributes ().PutEmpty (k ))
603
699
}
700
+ for k , v := range cfg .blockedKeys {
701
+ v .CopyTo (logEntry .Attributes ().PutEmpty (k ))
702
+ }
604
703
for k , v := range cfg .redacted {
605
704
v .CopyTo (logEntry .Attributes ().PutEmpty (k ))
606
705
}
@@ -626,7 +725,7 @@ func runLogsTest(
626
725
// runMetricsTest transforms the test input metric data and passes it through the processor
627
726
func runMetricsTest (
628
727
t * testing.T ,
629
- cfg testConfig ,
728
+ cfg TestConfig ,
630
729
metricType pmetric.MetricType ,
631
730
) pmetric.Metrics {
632
731
inBatch := pmetric .NewMetrics ()
@@ -638,7 +737,7 @@ func runMetricsTest(
638
737
metric := ils .Metrics ().AppendEmpty ()
639
738
metric .SetDescription ("first-batch-first-metric" )
640
739
641
- length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored )
740
+ length := len (cfg .allowed ) + len (cfg .masked ) + len (cfg .redacted ) + len (cfg .ignored ) + len ( cfg . blockedKeys )
642
741
643
742
var dataPointAttrs pcommon.Map
644
743
switch metricType {
@@ -662,6 +761,10 @@ func runMetricsTest(
662
761
v .CopyTo (dataPointAttrs .PutEmpty (k ))
663
762
v .CopyTo (rl .Resource ().Attributes ().PutEmpty (k ))
664
763
}
764
+ for k , v := range cfg .blockedKeys {
765
+ v .CopyTo (dataPointAttrs .PutEmpty (k ))
766
+ v .CopyTo (rl .Resource ().Attributes ().PutEmpty (k ))
767
+ }
665
768
for k , v := range cfg .redacted {
666
769
v .CopyTo (dataPointAttrs .PutEmpty (k ))
667
770
v .CopyTo (rl .Resource ().Attributes ().PutEmpty (k ))
0 commit comments