-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…y-in-logs Prevent license_key value from being written to the agent logs when u…
- Loading branch information
Showing
4 changed files
with
178 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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
38 changes: 38 additions & 0 deletions
38
newrelic-agent/src/main/java/com/newrelic/agent/util/LicenseKeyUtil.java
This file contains 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,38 @@ | ||
/* | ||
* | ||
* * Copyright 2023 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.newrelic.agent.util; | ||
|
||
import com.newrelic.agent.Agent; | ||
import com.newrelic.agent.service.ServiceFactory; | ||
|
||
public class LicenseKeyUtil { | ||
private static final String OBFUSCATED_LICENSE_KEY = "obfuscated"; | ||
|
||
/** | ||
* Removes the license_key value from a given string. | ||
* <p> | ||
* This is primarily used to prevent the license_key from being | ||
* written to the agent logs when using debug and/or audit_mode logging. | ||
* | ||
* @param originalString String to be evaluated and obfuscated if it contains the license_key | ||
* @return A modified String with the license_key value replaced, if it exists. Otherwise, the originalString is returned. | ||
*/ | ||
public static String obfuscateLicenseKey(String originalString) { | ||
if (originalString == null || originalString.isEmpty()) { | ||
Agent.LOG.finest("Unable to obfuscate the license_key in a null or empty string."); | ||
return originalString; | ||
} | ||
String licenseKey = ServiceFactory.getConfigService().getDefaultAgentConfig().getLicenseKey(); | ||
if (licenseKey == null) { | ||
Agent.LOG.finest("Unable to obfuscate a null license_key."); | ||
return originalString; | ||
} else { | ||
return originalString.replace(licenseKey, OBFUSCATED_LICENSE_KEY); | ||
} | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
newrelic-agent/src/test/java/com/newrelic/agent/util/LicenseKeyUtilTest.java
This file contains 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,119 @@ | ||
/* | ||
* | ||
* * Copyright 2023 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package com.newrelic.agent.util; | ||
|
||
import com.newrelic.agent.MockServiceManager; | ||
import com.newrelic.agent.config.AgentConfig; | ||
import com.newrelic.agent.config.AgentConfigImpl; | ||
import com.newrelic.agent.config.ConfigService; | ||
import com.newrelic.agent.config.ConfigServiceFactory; | ||
import com.newrelic.agent.service.ServiceFactory; | ||
import junit.framework.TestCase; | ||
import org.junit.Assert; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class LicenseKeyUtilTest extends TestCase { | ||
|
||
public void testObfuscateLicenseKey() { | ||
// Given | ||
String originalRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghijklmonpqrstuvwxyz1234567890&marshal_format=json&protocol_version=17"; | ||
|
||
String originalJsonPayload = "[{\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}]"; | ||
|
||
MockServiceManager serviceManager = new MockServiceManager(); | ||
ServiceFactory.setServiceManager(serviceManager); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
configMap.put("license_key", "abcdefghijklmonpqrstuvwxyz1234567890"); | ||
|
||
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap); | ||
ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap); | ||
serviceManager.setConfigService(configService); | ||
|
||
// When | ||
String actualRequestUrl = LicenseKeyUtil.obfuscateLicenseKey(originalRequestUrl); | ||
String actualJsonPayload = LicenseKeyUtil.obfuscateLicenseKey(originalJsonPayload); | ||
|
||
// Then | ||
String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=obfuscated&marshal_format=json&protocol_version=17"; | ||
|
||
String expectedJsonPayload = "[{\"license_key\":\"obfuscated\"}]"; | ||
|
||
Assert.assertEquals(expectedRequestUrl, actualRequestUrl); | ||
Assert.assertEquals(expectedJsonPayload, actualJsonPayload); | ||
} | ||
|
||
public void testObfuscateLicenseKeyWithMultipleLicenseKeyEntries() { | ||
// Given | ||
String originalJsonPayload = "[" + | ||
"{\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}, {\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}, {\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}, {\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}]"; | ||
|
||
MockServiceManager serviceManager = new MockServiceManager(); | ||
ServiceFactory.setServiceManager(serviceManager); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
configMap.put("license_key", "abcdefghijklmonpqrstuvwxyz1234567890"); | ||
|
||
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap); | ||
ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap); | ||
serviceManager.setConfigService(configService); | ||
|
||
// When | ||
String actualJsonPayload = LicenseKeyUtil.obfuscateLicenseKey(originalJsonPayload); | ||
|
||
// Then | ||
String expectedJsonPayload = "[" + | ||
"{\"license_key\":\"obfuscated\"}, {\"license_key\":\"obfuscated\"}, {\"license_key\":\"obfuscated\"}, {\"license_key\":\"obfuscated\"}]"; | ||
|
||
Assert.assertEquals(expectedJsonPayload, actualJsonPayload); | ||
} | ||
|
||
public void testObfuscateLicenseKeyWithNullLicenseKey() { | ||
// Given | ||
String originalRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghijklmonpqrstuvwxyz1234567890&marshal_format=json&protocol_version=17"; | ||
|
||
String originalJsonPayload = "[{\"license_key\":\"abcdefghijklmonpqrstuvwxyz1234567890\"}]"; | ||
|
||
MockServiceManager serviceManager = new MockServiceManager(); | ||
ServiceFactory.setServiceManager(serviceManager); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
configMap.put("license_key", null); | ||
|
||
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap); | ||
ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap); | ||
serviceManager.setConfigService(configService); | ||
|
||
// When | ||
String actualRequestUrl = LicenseKeyUtil.obfuscateLicenseKey(originalRequestUrl); | ||
String actualJsonPayload = LicenseKeyUtil.obfuscateLicenseKey(originalJsonPayload); | ||
|
||
// Then | ||
Assert.assertEquals(originalRequestUrl, actualRequestUrl); | ||
Assert.assertEquals(originalJsonPayload, actualJsonPayload); | ||
} | ||
|
||
public void testObfuscateLicenseKeyWithNullOrEmptyString() { | ||
// Given | ||
MockServiceManager serviceManager = new MockServiceManager(); | ||
ServiceFactory.setServiceManager(serviceManager); | ||
Map<String, Object> configMap = new HashMap<>(); | ||
configMap.put("license_key", "abcdefghijklmonpqrstuvwxyz1234567890"); | ||
|
||
AgentConfig config = AgentConfigImpl.createAgentConfig(configMap); | ||
ConfigService configService = ConfigServiceFactory.createConfigService(config, configMap); | ||
serviceManager.setConfigService(configService); | ||
|
||
// When | ||
String actualEmptyString = LicenseKeyUtil.obfuscateLicenseKey(""); | ||
String actualNullString = LicenseKeyUtil.obfuscateLicenseKey(null); | ||
|
||
// Then | ||
Assert.assertEquals("", actualEmptyString); | ||
Assert.assertNull(actualNullString); | ||
} | ||
} |