-
Notifications
You must be signed in to change notification settings - Fork 33
Adding Redacted Keys #217
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
Closed
Closed
Adding Redacted Keys #217
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3d06a86
Adding Redacted Keys
clr182 bce2bc4
Fixed check tests
clr182 6ec55a3
Fixed conflicting filters and redacted keys tests
clr182 9ac6180
update AutoRedactedKeysScenario.java
clr182 fc657ac
altered default filters string array in the configuration
clr182 6f774b4
update CHANGELOG
clr182 42ec37b
Changes to RedactedKeysMap
clr182 0fd5542
remove duplicate filter config
clr182 088aa21
fix fixed reference errors
clr182 1c840e1
config option broken
clr182 16760f5
Added tests to check regex matching
clr182 415f03e
scenario syntax fix
clr182 9b2e42d
Changes to add back public filters
clr182 4dab99e
updated variable names
clr182 9e9e84b
Update bugsnag/src/test/resources/logback.xml
clr182 c17fb7c
Merge branch 'next' into PLAT-4543-AddRedactedKeys
clr182 2fc8d1c
Converting to Patterns
clr182 a536540
update javadoc reference
clr182 a3e5fd0
setRedactedKey to string parameter rather than pattern for logback
clr182 cae37b6
adding in scenarios
clr182 0fc2727
Adding Pattern import to scenario
clr182 089e3a8
filters are appended to setRedactedKeys
clr182 d1dbada
Update bugsnag/src/main/java/com/bugsnag/Bugsnag.java
clr182 51203d1
Added concat list
clr182 b663e58
fix(redacted keys): changed RedactedKeysMap to retain logic for `filt…
lemnik 7b2b506
Merge branch 'next' into PLAT-4543-AddRedactedKeys
lemnik 17d7fda
chore(changelog): fixed the changelog after next sync
lemnik 1b7b28c
chore(checkstyle): fixed checkstyle errors
lemnik 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
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
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 |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package com.bugsnag; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Decorates a map by replacing values of redacted keys. | ||
*/ | ||
class RedactedKeysMap implements Map<String, Object> { | ||
|
||
private static final String REDACTED_PLACEHOLDER = "[REDACTED]"; | ||
|
||
private final Map<String, Object> redactedKeyCopy; | ||
private final Pattern[] redactedKeys; | ||
private final String[] filters; | ||
|
||
/** | ||
* Constructs a new RedactedKeysMap by copying the provided map and applying | ||
* redaction rules to the specified keys. | ||
* | ||
* @param map the original map to be wrapped and redacted | ||
* @param configuration the configuration | ||
*/ | ||
RedactedKeysMap(Map<String, Object> map, Configuration configuration) { | ||
this(map, configuration.redactedKeys, configuration.filters); | ||
} | ||
|
||
RedactedKeysMap(Map<String, Object> map, Pattern[] redactedKeys, String[] filters) { | ||
this.filters = filters; | ||
this.redactedKeys = redactedKeys; | ||
this.redactedKeyCopy = createCopy(map); | ||
} | ||
|
||
/** | ||
* Creates a copy of the given map, applying redaction rules to the specified keys. | ||
* | ||
* @param map the original map to be copied and redacted | ||
* @return a new map with the same entries as the original, but with redacted values where applicable | ||
*/ | ||
private Map<String, Object> createCopy(Map<? extends String, ?> map) { | ||
Map<String, Object> copy = new HashMap<>(); | ||
for (Entry<? extends String, ?> entry : map.entrySet()) { | ||
if (entry.getValue() == null) { | ||
copy.put(entry.getKey(), null); | ||
} else { | ||
Object transformedValue = transformEntry(entry.getKey(), entry.getValue()); | ||
copy.put(entry.getKey(), transformedValue); | ||
} | ||
} | ||
return copy; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return redactedKeyCopy.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return redactedKeyCopy.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
return redactedKeyCopy.containsKey(key); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
return redactedKeyCopy.containsValue(value); | ||
} | ||
|
||
@Override | ||
public Object get(Object key) { | ||
return redactedKeyCopy.get(key); | ||
} | ||
|
||
@Override | ||
public Object put(String key, Object value) { | ||
if (value == null) { | ||
return redactedKeyCopy.put(key, null); | ||
} | ||
Object transformedValue = transformEntry(key, value); | ||
return redactedKeyCopy.put(key, transformedValue); | ||
} | ||
|
||
@Override | ||
public Object remove(Object key) { | ||
return redactedKeyCopy.remove(key); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends String, ?> mapValues) { | ||
Map<String, Object> copy = createCopy(mapValues); | ||
redactedKeyCopy.putAll(copy); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
redactedKeyCopy.clear(); | ||
} | ||
|
||
@Override | ||
public Set<String> keySet() { | ||
return redactedKeyCopy.keySet(); | ||
} | ||
|
||
@Override | ||
public Collection<Object> values() { | ||
return redactedKeyCopy.values(); | ||
} | ||
|
||
@Override | ||
public Set<Entry<String, Object>> entrySet() { | ||
return redactedKeyCopy.entrySet(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private Object transformEntry(Object key, Object value) { | ||
if (value instanceof Map) { | ||
return new RedactedKeysMap((Map<String, Object>) value, redactedKeys, filters); | ||
} | ||
return shouldRedactKey((String) key) ? REDACTED_PLACEHOLDER : value; | ||
} | ||
|
||
private boolean shouldRedactKey(String key) { | ||
if (key == null) { | ||
return false; | ||
} | ||
|
||
Pattern[] redactedKeys = this.redactedKeys; | ||
if (redactedKeys != null) { | ||
for (Pattern pattern : redactedKeys) { | ||
if (pattern.matcher(key).matches()) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
String[] filters = this.filters; | ||
if (filters != null) { | ||
for (String filter : filters) { | ||
if (filter != null && key.contains(filter)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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
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.