Skip to content

Commit 9c38902

Browse files
authored
Merge pull request #2388 from newrelic/log-invalid-keys
Log an obfuscate invalid keys added as attributes to logs and events
2 parents 90071f2 + 8053d79 commit 9c38902

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

newrelic-agent/src/main/java/com/newrelic/agent/service/analytics/InsightsServiceImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.newrelic.agent.stats.TransactionStats;
3232
import com.newrelic.agent.tracing.DistributedTraceServiceImpl;
3333
import com.newrelic.agent.transport.HttpError;
34+
import com.newrelic.agent.util.Strings;
3435
import com.newrelic.api.agent.Insights;
3536

3637
import java.text.MessageFormat;
@@ -394,9 +395,9 @@ private static CustomInsightsEvent createValidatedEvent(String eventType, Map<St
394395

395396
// key or value is null, skip it with a log message and iterate to next entry in attributes.entrySet()
396397
if (key == null || value == null) {
397-
Agent.LOG.log(Level.WARNING, "Custom event [{0}] with invalid attributes key or value of null was reported for a transaction but ignored."
398+
Agent.LOG.log(Level.FINEST, "Custom event [{0}] with invalid attributes key or value of null was reported for a transaction but ignored."
398399
+ " Each key should be a String and each value should be a String, Number, or Boolean. Key: {1} / Value: {2}",
399-
eventType, (key == null ? "[null]" : key), (value == null ? "[null]" : value.toString()));
400+
eventType, (key == null ? "[null]" : Strings.obfuscate(key)));
400401
continue;
401402
}
402403

newrelic-agent/src/main/java/com/newrelic/agent/service/logging/LogSenderServiceImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.newrelic.agent.tracing.DistributedTraceServiceImpl;
4040
import com.newrelic.agent.transport.HttpError;
4141
import com.newrelic.agent.util.NoOpQueue;
42+
import com.newrelic.agent.util.Strings;
4243
import com.newrelic.api.agent.Logs;
4344

4445
import java.text.MessageFormat;
@@ -550,11 +551,13 @@ private static LogEvent createValidatedEvent(Map<LogAttributeKey, ?> attributes,
550551
for (Map.Entry<LogAttributeKey, ?> entry : attributes.entrySet()) {
551552
LogAttributeKey logAttrKey = entry.getKey();
552553
Object value = entry.getValue();
554+
String key = logAttrKey.getKey();
553555

554556
// key or value is null, skip it with a log message and iterate to next entry in attributes.entrySet()
555-
if (logAttrKey == null || logAttrKey.getKey() == null || value == null) {
556-
Agent.LOG.log(Level.WARNING, "Log event with invalid attributes key or value of null was reported for a transaction but ignored."
557-
+ " Each key should be a String and each value should be a String, Number, or Boolean.");
557+
if (key == null || value == null) {
558+
Agent.LOG.log(Level.FINEST, "Log event with invalid attributes key or value of null was reported for a transaction but ignored."
559+
+ " Each key should be a String and each value should be a String, Number, or Boolean."
560+
+ " Key: " + (key == null ? "[null]" : Strings.obfuscate(key)));
558561
continue;
559562
}
560563

newrelic-agent/src/main/java/com/newrelic/agent/util/Strings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,26 @@ public static String replaceDotHyphenWithUnderscore(String string) {
129129
return updated.toString();
130130
}
131131

132+
/**
133+
* Obfuscate the supplied String. If the String is null or less than 4 characters, return the
134+
* original String. Otherwise, return the first character + "***" + the last character.
135+
*
136+
* @param target The String to obfuscate
137+
* @return the obfuscated String or the original String if it's null or less than 4 characters
138+
*/
139+
public static String obfuscate(String target) {
140+
int MIN_LENGTH = 3;
141+
if (target != null) {
142+
if (target.length() <= MIN_LENGTH) {
143+
return target;
144+
}
145+
146+
return target.charAt(0) + "***" + target.charAt(target.length() - 1);
147+
}
148+
149+
return null;
150+
}
151+
132152
private static int findDotOrHyphen(String string, int start) {
133153
if (string == null) {
134154
return -1;

newrelic-agent/src/test/java/com/newrelic/agent/util/StringsTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,14 @@ public void replaceDotHyphenWithUnderscore() {
6868
Assert.assertEquals("___string_with_dots_AND_hyphens_at_ends__",
6969
Strings.replaceDotHyphenWithUnderscore("-.-string.with-dots-AND.hyphens.at_ends.-"));
7070
}
71+
72+
@Test
73+
public void obfuscate() {
74+
Assert.assertNull(Strings.obfuscate(null));
75+
Assert.assertEquals("f", Strings.obfuscate("f"));
76+
Assert.assertEquals("fo", Strings.obfuscate("fo"));
77+
Assert.assertEquals("foo", Strings.obfuscate("foo"));
78+
Assert.assertEquals("b***r", Strings.obfuscate("barbar"));
79+
Assert.assertEquals("l***g", Strings.obfuscate("long-long-string"));
80+
}
7181
}

0 commit comments

Comments
 (0)