@@ -37,13 +37,15 @@ public void testObfuscateLicenseKey() {
3737 serviceManager .setConfigService (configService );
3838
3939 // When
40+ long startTime = System .nanoTime ();
4041 String actualRequestUrl = LicenseKeyUtil .obfuscateLicenseKey (originalRequestUrl );
42+ System .out .println ("Time (ns): " + (System .nanoTime () - startTime ));
4143 String actualJsonPayload = LicenseKeyUtil .obfuscateLicenseKey (originalJsonPayload );
4244
4345 // Then
44- String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=obfuscated &marshal_format=json&protocol_version=17" ;
46+ String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghij************************** &marshal_format=json&protocol_version=17" ;
4547
46- String expectedJsonPayload = "[{\" license_key\" :\" obfuscated \" }]" ;
48+ String expectedJsonPayload = "[{\" license_key\" :\" abcdefghij************************** \" }]" ;
4749
4850 Assert .assertEquals (expectedRequestUrl , actualRequestUrl );
4951 Assert .assertEquals (expectedJsonPayload , actualJsonPayload );
@@ -68,33 +70,29 @@ public void testObfuscateLicenseKeyWithMultipleLicenseKeyEntries() {
6870
6971 // Then
7072 String expectedJsonPayload = "[" +
71- "{\" license_key\" :\" obfuscated \" }, {\" license_key\" :\" obfuscated \" }, {\" license_key\" :\" obfuscated \" }, {\" license_key\" :\" obfuscated \" }]" ;
73+ "{\" license_key\" :\" abcdefghij************************** \" }, {\" license_key\" :\" abcdefghij************************** \" }, {\" license_key\" :\" abcdefghij************************** \" }, {\" license_key\" :\" abcdefghij************************** \" }]" ;
7274
7375 Assert .assertEquals (expectedJsonPayload , actualJsonPayload );
7476 }
7577
7678 public void testObfuscateLicenseKeyWithNullLicenseKey () {
79+ // The configured license key is not used for obfuscation; the value found in the string drives it.
80+ // Even with a null configured key, the pattern-based obfuscation still applies.
7781 // Given
7882 String originalRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghijklmonpqrstuvwxyz1234567890&marshal_format=json&protocol_version=17" ;
7983
8084 String originalJsonPayload = "[{\" license_key\" :\" abcdefghijklmonpqrstuvwxyz1234567890\" }]" ;
8185
82- MockServiceManager serviceManager = new MockServiceManager ();
83- ServiceFactory .setServiceManager (serviceManager );
84- Map <String , Object > configMap = new HashMap <>();
85- configMap .put ("license_key" , null );
86-
87- AgentConfig config = AgentConfigImpl .createAgentConfig (configMap );
88- ConfigService configService = ConfigServiceFactory .createConfigService (config , configMap );
89- serviceManager .setConfigService (configService );
90-
9186 // When
9287 String actualRequestUrl = LicenseKeyUtil .obfuscateLicenseKey (originalRequestUrl );
9388 String actualJsonPayload = LicenseKeyUtil .obfuscateLicenseKey (originalJsonPayload );
9489
9590 // Then
96- Assert .assertEquals (originalRequestUrl , actualRequestUrl );
97- Assert .assertEquals (originalJsonPayload , actualJsonPayload );
91+ String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghij**************************&marshal_format=json&protocol_version=17" ;
92+ String expectedJsonPayload = "[{\" license_key\" :\" abcdefghij**************************\" }]" ;
93+
94+ Assert .assertEquals (expectedRequestUrl , actualRequestUrl );
95+ Assert .assertEquals (expectedJsonPayload , actualJsonPayload );
9896 }
9997
10098 public void testObfuscateLicenseKeyWithNullOrEmptyString () {
@@ -116,4 +114,53 @@ public void testObfuscateLicenseKeyWithNullOrEmptyString() {
116114 Assert .assertEquals ("" , actualEmptyString );
117115 Assert .assertNull (actualNullString );
118116 }
117+
118+ public void testObfuscatedLicenseKeyWithSmallLicenseKey () {
119+ // License keys <= 10 chars are fully obfuscated with "*"
120+ // Given
121+ String originalRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcde&marshal_format=json&protocol_version=17" ;
122+
123+ String originalJsonPayload = "[{\" license_key\" :\" abcde\" }]" ;
124+
125+ MockServiceManager serviceManager = new MockServiceManager ();
126+ ServiceFactory .setServiceManager (serviceManager );
127+ Map <String , Object > configMap = new HashMap <>();
128+ configMap .put ("license_key" , "abcde" );
129+
130+ AgentConfig config = AgentConfigImpl .createAgentConfig (configMap );
131+ ConfigService configService = ConfigServiceFactory .createConfigService (config , configMap );
132+ serviceManager .setConfigService (configService );
133+
134+ // When
135+ String actualRequestUrl = LicenseKeyUtil .obfuscateLicenseKey (originalRequestUrl );
136+ String actualJsonPayload = LicenseKeyUtil .obfuscateLicenseKey (originalJsonPayload );
137+
138+ // Then
139+ String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=*****&marshal_format=json&protocol_version=17" ;
140+ String expectedJsonPayload = "[{\" license_key\" :\" *****\" }]" ;
141+
142+ Assert .assertEquals (expectedRequestUrl , actualRequestUrl );
143+ Assert .assertEquals (expectedJsonPayload , actualJsonPayload );
144+ }
145+
146+ public void testObfuscatedLicenseKeyIfOriginalLicenseKeyIsChanged () {
147+ // If the key is changed in config mid-run, the agent still uses the original key in its requests.
148+ // Obfuscation is driven by the value found in the string, not the configured key,
149+ // so the original key is always obfuscated regardless of what the config currently holds.
150+ String originalRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghijklmonpqrstuvwxyz1234567890&marshal_format=json&protocol_version=17" ;
151+
152+ String originalJsonPayload = "[{\" license_key\" :\" abcdefghijklmonpqrstuvwxyz1234567890\" }]" ;
153+
154+ // When
155+ String actualRequestUrl = LicenseKeyUtil .obfuscateLicenseKey (originalRequestUrl );
156+ String actualJsonPayload = LicenseKeyUtil .obfuscateLicenseKey (originalJsonPayload );
157+
158+ // Then
159+ String expectedRequestUrl = "https://staging-collector.newrelic.com:443/agent_listener/invoke_raw_method?method=connect&license_key=abcdefghij**************************&marshal_format=json&protocol_version=17" ;
160+
161+ String expectedJsonPayload = "[{\" license_key\" :\" abcdefghij**************************\" }]" ;
162+
163+ Assert .assertEquals (expectedRequestUrl , actualRequestUrl );
164+ Assert .assertEquals (expectedJsonPayload , actualJsonPayload );
165+ }
119166}
0 commit comments