Skip to content

Commit b3a192a

Browse files
committed
more tests
Signed-off-by: odubajDT <[email protected]>
1 parent d1fa8be commit b3a192a

File tree

1 file changed

+129
-26
lines changed

1 file changed

+129
-26
lines changed

processor/redactionprocessor/processor_test.go

Lines changed: 129 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,19 @@ import (
1818
"go.uber.org/zap/zaptest"
1919
)
2020

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
2728
}
2829

2930
// TestRedactUnknownAttributes validates that the processor deletes span
3031
// attributes that are not the allowed keys list
3132
func TestRedactUnknownAttributes(t *testing.T) {
32-
testConfig := testConfig{
33+
testConfig := TestConfig{
3334
config: &Config{
3435
AllowedKeys: []string{"group", "id", "name"},
3536
},
@@ -81,7 +82,7 @@ func TestRedactUnknownAttributes(t *testing.T) {
8182
// span attributes that are not the allowed keys list if Config.AllowAllKeys
8283
// is set to true
8384
func TestAllowAllKeys(t *testing.T) {
84-
testConfig := testConfig{
85+
testConfig := TestConfig{
8586
config: &Config{
8687
AllowedKeys: []string{"group", "id"},
8788
AllowAllKeys: true,
@@ -125,7 +126,7 @@ func TestAllowAllKeys(t *testing.T) {
125126
// TestAllowAllKeysMaskValues validates that the processor still redacts
126127
// span attribute values if Config.AllowAllKeys is set to true
127128
func TestAllowAllKeysMaskValues(t *testing.T) {
128-
testConfig := testConfig{
129+
testConfig := TestConfig{
129130
config: &Config{
130131
AllowedKeys: []string{"group", "id", "name"},
131132
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?"},
@@ -176,12 +177,13 @@ func TestAllowAllKeysMaskValues(t *testing.T) {
176177
// of any attributes it deleted to the new redaction.redacted.keys and
177178
// redaction.redacted.count span attributes while set to full debug output
178179
func TestRedactSummaryDebug(t *testing.T) {
179-
testConfig := testConfig{
180+
testConfig := TestConfig{
180181
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",
185187
},
186188
allowed: map[string]pcommon.Value{
187189
"id": pcommon.NewValueInt(5),
@@ -197,6 +199,10 @@ func TestRedactSummaryDebug(t *testing.T) {
197199
redacted: map[string]pcommon.Value{
198200
"credit_card": pcommon.NewValueStr("4111111111111111"),
199201
},
202+
blockedKeys: map[string]pcommon.Value{
203+
"token_some": pcommon.NewValueStr("tokenize"),
204+
"api_key_some": pcommon.NewValueStr("apinize"),
205+
},
200206
}
201207

202208
outTraces := runTest(t, testConfig)
@@ -236,15 +242,102 @@ func TestRedactSummaryDebug(t *testing.T) {
236242
assert.True(t, ok)
237243
assert.Equal(t, int64(len(testConfig.ignored)), ignoredKeyCount.Int())
238244

239-
blockedKeys := []string{"name"}
245+
blockedKeys := []string{"api_key_some", "name", "token_some"}
240246
maskedValues, ok := attr.Get(maskedValues)
241247
assert.True(t, ok)
242248
assert.Equal(t, strings.Join(blockedKeys, ","), maskedValues.Str())
243249
maskedValueCount, ok := attr.Get(maskedValueCount)
244250
assert.True(t, ok)
245-
assert.Equal(t, int64(1), maskedValueCount.Int())
251+
assert.Equal(t, int64(3), maskedValueCount.Int())
246252
value, _ := attr.Get("name")
247253
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())
248341
}
249342
}
250343

@@ -253,7 +346,7 @@ func TestRedactSummaryDebug(t *testing.T) {
253346
// attribute (but not to redaction.redacted.keys) when set to the info level
254347
// of output
255348
func TestRedactSummaryInfo(t *testing.T) {
256-
testConfig := testConfig{
349+
testConfig := TestConfig{
257350
config: &Config{
258351
AllowedKeys: []string{"id", "name", "group"},
259352
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?"},
@@ -324,7 +417,7 @@ func TestRedactSummaryInfo(t *testing.T) {
324417
// TestRedactSummarySilent validates that the processor does not create the
325418
// summary attributes when set to silent
326419
func TestRedactSummarySilent(t *testing.T) {
327-
testConfig := testConfig{
420+
testConfig := TestConfig{
328421
config: &Config{
329422
AllowedKeys: []string{"id", "name", "group"},
330423
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?"},
@@ -380,7 +473,7 @@ func TestRedactSummarySilent(t *testing.T) {
380473
// TestRedactSummaryDefault validates that the processor does not create the
381474
// summary attributes by default
382475
func TestRedactSummaryDefault(t *testing.T) {
383-
testConfig := testConfig{
476+
testConfig := TestConfig{
384477
config: &Config{AllowedKeys: []string{"id", "name", "group"}},
385478
allowed: map[string]pcommon.Value{
386479
"id": pcommon.NewValueInt(5),
@@ -428,7 +521,7 @@ func TestRedactSummaryDefault(t *testing.T) {
428521
// TestMultipleBlockValues validates that the processor can block multiple
429522
// patterns
430523
func TestMultipleBlockValues(t *testing.T) {
431-
testConfig := testConfig{
524+
testConfig := TestConfig{
432525
config: &Config{
433526
AllowedKeys: []string{"id", "name", "mystery"},
434527
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?", "(5[1-5][0-9]{3})"},
@@ -537,7 +630,7 @@ func TestProcessAttrsAppliedTwice(t *testing.T) {
537630
// runTest transforms the test input data and passes it through the processor
538631
func runTest(
539632
t *testing.T,
540-
cfg testConfig,
633+
cfg TestConfig,
541634
) ptrace.Traces {
542635
inBatch := ptrace.NewTraces()
543636
rs := inBatch.ResourceSpans().AppendEmpty()
@@ -549,13 +642,16 @@ func runTest(
549642
span.SetName("first-batch-first-span")
550643
span.SetTraceID([16]byte{1, 2, 3, 4})
551644

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)
553646
for k, v := range cfg.allowed {
554647
v.CopyTo(span.Attributes().PutEmpty(k))
555648
}
556649
for k, v := range cfg.masked {
557650
v.CopyTo(span.Attributes().PutEmpty(k))
558651
}
652+
for k, v := range cfg.blockedKeys {
653+
v.CopyTo(span.Attributes().PutEmpty(k))
654+
}
559655
for k, v := range cfg.redacted {
560656
v.CopyTo(span.Attributes().PutEmpty(k))
561657
}
@@ -581,7 +677,7 @@ func runTest(
581677
// runLogsTest transforms the test input log data and passes it through the processor
582678
func runLogsTest(
583679
t *testing.T,
584-
cfg testConfig,
680+
cfg TestConfig,
585681
) plog.Logs {
586682
inBatch := plog.NewLogs()
587683
rl := inBatch.ResourceLogs().AppendEmpty()
@@ -594,13 +690,16 @@ func runLogsTest(
594690
logEntry.Body().SetStr("first-batch-first-logEntry")
595691
logEntry.SetTraceID([16]byte{1, 2, 3, 4})
596692

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)
598694
for k, v := range cfg.allowed {
599695
v.CopyTo(logEntry.Attributes().PutEmpty(k))
600696
}
601697
for k, v := range cfg.masked {
602698
v.CopyTo(logEntry.Attributes().PutEmpty(k))
603699
}
700+
for k, v := range cfg.blockedKeys {
701+
v.CopyTo(logEntry.Attributes().PutEmpty(k))
702+
}
604703
for k, v := range cfg.redacted {
605704
v.CopyTo(logEntry.Attributes().PutEmpty(k))
606705
}
@@ -626,7 +725,7 @@ func runLogsTest(
626725
// runMetricsTest transforms the test input metric data and passes it through the processor
627726
func runMetricsTest(
628727
t *testing.T,
629-
cfg testConfig,
728+
cfg TestConfig,
630729
metricType pmetric.MetricType,
631730
) pmetric.Metrics {
632731
inBatch := pmetric.NewMetrics()
@@ -638,7 +737,7 @@ func runMetricsTest(
638737
metric := ils.Metrics().AppendEmpty()
639738
metric.SetDescription("first-batch-first-metric")
640739

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)
642741

643742
var dataPointAttrs pcommon.Map
644743
switch metricType {
@@ -662,6 +761,10 @@ func runMetricsTest(
662761
v.CopyTo(dataPointAttrs.PutEmpty(k))
663762
v.CopyTo(rl.Resource().Attributes().PutEmpty(k))
664763
}
764+
for k, v := range cfg.blockedKeys {
765+
v.CopyTo(dataPointAttrs.PutEmpty(k))
766+
v.CopyTo(rl.Resource().Attributes().PutEmpty(k))
767+
}
665768
for k, v := range cfg.redacted {
666769
v.CopyTo(dataPointAttrs.PutEmpty(k))
667770
v.CopyTo(rl.Resource().Attributes().PutEmpty(k))

0 commit comments

Comments
 (0)