-
Notifications
You must be signed in to change notification settings - Fork 3k
[processor/redaction] Support hashing instead of masking values #38161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d68d01e
[processor/redaction] Support hashing instead of masking values via p…
odubajDT 10cdcfe
polishing
odubajDT 507171c
fix lint
odubajDT 6f2d0c7
Merge branch 'main' into redaction-hash
odubajDT 792c593
Update processor/redactionprocessor/README.md
odubajDT 9bf9061
Merge branch 'main' into redaction-hash
odubajDT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: processor/redaction | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Support hashing instead of masking values via 'hash_function' parameter" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35830] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,10 @@ processors: | |
# blocked span attributes. Values that match are not masked. | ||
allowed_values: | ||
- "[email protected]" | ||
# hash_function defines the function for hashing the values instead of | ||
# masking them with a fixed string. By default, no hash function is used | ||
# and masking with a fixed string is performed. | ||
hash_function: md5 | ||
# summary controls the verbosity level of the diagnostic attributes that | ||
# the processor adds to the spans/logs/datapoints when it redacts or masks other | ||
# attributes. In some contexts a list of redacted attributes leaks | ||
|
@@ -119,6 +123,11 @@ part of the value is masked with a fixed length of asterisks. | |
`blocked_key_patterns` applies to the values of the keys matching one of the patterns. | ||
The value is then masked according to the configuration. | ||
|
||
`hash_function` defines the function for hashing the values (or substrings of values) | ||
instead of masking them with a fixed string. By default, no hash function is used | ||
and masking with a fixed string is performed. The supported hash functions | ||
are `md5`, `sha1` and `sha3` (SHA-256). | ||
|
||
For example, if `notes` is on the list of allowed keys, then the `notes` | ||
attribute is retained. However, if there is a value such as a credit card | ||
number in the `notes` field that matched a regular expression on the list of | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
package redactionprocessor | ||
|
||
import ( | ||
"errors" | ||
"path/filepath" | ||
"testing" | ||
|
||
|
@@ -31,6 +32,7 @@ func TestLoadConfig(t *testing.T) { | |
IgnoredKeys: []string{"safe_attribute"}, | ||
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?", "(5[1-5][0-9]{14})"}, | ||
BlockedKeyPatterns: []string{".*token.*", ".*api_key.*"}, | ||
HashFunction: MD5, | ||
AllowedValues: []string{"[email protected]"}, | ||
Summary: debug, | ||
}, | ||
|
@@ -58,3 +60,38 @@ func TestLoadConfig(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestValidateConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
hash HashFunction | ||
expected error | ||
}{ | ||
{ | ||
name: "valid", | ||
hash: MD5, | ||
}, | ||
{ | ||
name: "empty", | ||
hash: None, | ||
}, | ||
{ | ||
name: "invalid", | ||
hash: "hash", | ||
expected: errors.New("unknown HashFunction hash, allowed functions are sha1, sha3 and md5"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var h HashFunction | ||
err := h.UnmarshalText([]byte(tt.hash)) | ||
if tt.expected != nil { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.hash, h) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,6 +271,92 @@ func TestRedactSummaryDebug(t *testing.T) { | |
} | ||
} | ||
|
||
func TestRedactSummaryDebugHashMD5(t *testing.T) { | ||
testConfig := TestConfig{ | ||
config: &Config{ | ||
AllowedKeys: []string{"id", "group", "name", "group.id", "member (id)", "token_some", "api_key_some", "email"}, | ||
BlockedValues: []string{"4[0-9]{12}(?:[0-9]{3})?"}, | ||
HashFunction: MD5, | ||
IgnoredKeys: []string{"safe_attribute"}, | ||
BlockedKeyPatterns: []string{".*token.*", ".*api_key.*"}, | ||
Summary: "debug", | ||
}, | ||
allowed: map[string]pcommon.Value{ | ||
"id": pcommon.NewValueInt(5), | ||
"group.id": pcommon.NewValueStr("some.valid.id"), | ||
"member (id)": pcommon.NewValueStr("some other valid id"), | ||
}, | ||
masked: map[string]pcommon.Value{ | ||
"name": pcommon.NewValueStr("placeholder 4111111111111111"), | ||
}, | ||
ignored: map[string]pcommon.Value{ | ||
"safe_attribute": pcommon.NewValueStr("harmless 4111111111111112"), | ||
}, | ||
redacted: map[string]pcommon.Value{ | ||
"credit_card": pcommon.NewValueStr("4111111111111111"), | ||
}, | ||
blockedKeys: map[string]pcommon.Value{ | ||
"token_some": pcommon.NewValueStr("tokenize"), | ||
"api_key_some": pcommon.NewValueStr("apinize"), | ||
}, | ||
allowedValues: map[string]pcommon.Value{ | ||
"email": pcommon.NewValueStr("[email protected]"), | ||
}, | ||
} | ||
|
||
outTraces := runTest(t, testConfig) | ||
outLogs := runLogsTest(t, testConfig) | ||
outMetricsGauge := runMetricsTest(t, testConfig, pmetric.MetricTypeGauge) | ||
outMetricsSum := runMetricsTest(t, testConfig, pmetric.MetricTypeSum) | ||
outMetricsHistogram := runMetricsTest(t, testConfig, pmetric.MetricTypeHistogram) | ||
outMetricsExponentialHistogram := runMetricsTest(t, testConfig, pmetric.MetricTypeExponentialHistogram) | ||
outMetricsSummary := runMetricsTest(t, testConfig, pmetric.MetricTypeSummary) | ||
|
||
attrs := []pcommon.Map{ | ||
outTraces.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Attributes(), | ||
outLogs.ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Attributes(), | ||
outMetricsGauge.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Gauge().DataPoints().At(0).Attributes(), | ||
outMetricsSum.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).Attributes(), | ||
outMetricsHistogram.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Histogram().DataPoints().At(0).Attributes(), | ||
outMetricsExponentialHistogram.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).ExponentialHistogram().DataPoints().At(0).Attributes(), | ||
outMetricsSummary.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Summary().DataPoints().At(0).Attributes(), | ||
} | ||
|
||
for _, attr := range attrs { | ||
deleted := make([]string, 0, len(testConfig.redacted)) | ||
for k := range testConfig.redacted { | ||
_, ok := attr.Get(k) | ||
assert.False(t, ok) | ||
deleted = append(deleted, k) | ||
} | ||
maskedKeys, ok := attr.Get(redactedKeys) | ||
assert.True(t, ok) | ||
sort.Strings(deleted) | ||
assert.Equal(t, strings.Join(deleted, ","), maskedKeys.Str()) | ||
maskedKeyCount, ok := attr.Get(redactedKeyCount) | ||
assert.True(t, ok) | ||
assert.Equal(t, int64(len(deleted)), maskedKeyCount.Int()) | ||
|
||
ignoredKeyCount, ok := attr.Get(ignoredKeyCount) | ||
assert.True(t, ok) | ||
assert.Equal(t, int64(len(testConfig.ignored)), ignoredKeyCount.Int()) | ||
|
||
blockedKeys := []string{"api_key_some", "name", "token_some"} | ||
maskedValues, ok := attr.Get(maskedValues) | ||
assert.True(t, ok) | ||
assert.Equal(t, strings.Join(blockedKeys, ","), maskedValues.Str()) | ||
maskedValueCount, ok := attr.Get(maskedValueCount) | ||
assert.True(t, ok) | ||
assert.Equal(t, int64(3), maskedValueCount.Int()) | ||
value, _ := attr.Get("name") | ||
assert.Equal(t, "placeholder 5910f4ea0062a0e29afd3dccc741e3ce", value.Str()) | ||
value, _ = attr.Get("api_key_some") | ||
assert.Equal(t, "93a699237950bde9eb9d25c7ead025f3", value.Str()) | ||
value, _ = attr.Get("token_some") | ||
assert.Equal(t, "77e9ef3680c5518785ef0121d3884c3d", value.Str()) | ||
} | ||
} | ||
|
||
// TestRedactSummaryInfo validates that the processor writes a verbose summary | ||
// of any attributes it deleted to the new redaction.redacted.count span | ||
// attribute (but not to redaction.redacted.keys) when set to the info level | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,10 @@ redaction: | |
# blocked span attributes. Values that match are not masked. | ||
allowed_values: | ||
- "[email protected]" | ||
# hash_function defines the function for hashing the values instead of | ||
# masking them with a fixed string. By default, no hash function is used | ||
# and masking with a fixed string is performed. | ||
hash_function: md5 | ||
# Summary controls the verbosity level of the diagnostic attributes that | ||
# the processor adds to the spans when it redacts or masks other | ||
# attributes. In some contexts a list of redacted attributes leaks | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.