Skip to content

Commit 762665f

Browse files
fix: Check for equality of value instead of equality of instance.
isCustomized returned the wrong value for all instances that have been serialized and read back as they obviously does not point to the default instance. The solution is to check for equality of the values, not for equality of the instances. This is a minor change in behaviour: A setting that is created with the builder matching the exact same defaults is now considered to be not customized. This solution has been preferred over a custom readResolve returning the DEFAULT when things match as it would otherwise be able to create an instance that identifies itself as customized but changed to not customized when read back, basically the oppositte of what is fixed here: An instances created as not customized must never be read back as customized.
1 parent c3e60fb commit 762665f

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

driver/src/main/java/org/neo4j/driver/internal/SecuritySettings.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.io.Serializable;
2323
import java.security.GeneralSecurityException;
24+
import java.util.Objects;
2425

2526
import org.neo4j.driver.Config;
2627
import org.neo4j.driver.exceptions.ClientException;
@@ -59,7 +60,20 @@ public Config.TrustStrategy trustStrategy()
5960

6061
private boolean isCustomized()
6162
{
62-
return this != DEFAULT;
63+
return !(DEFAULT.encrypted() == this.encrypted() && DEFAULT.hasEqualTrustStrategy( this ));
64+
}
65+
66+
private boolean hasEqualTrustStrategy( SecuritySettings other )
67+
{
68+
Config.TrustStrategy t1 = this.trustStrategy;
69+
Config.TrustStrategy t2 = other.trustStrategy;
70+
if ( t1 == t2 )
71+
{
72+
return true;
73+
}
74+
75+
return t1.isHostnameVerificationEnabled() == t2.isHostnameVerificationEnabled() && t1.strategy() == t2.strategy() &&
76+
Objects.equals( t1.certFile(), t2.certFile() ) && t1.revocationStrategy() == t2.revocationStrategy();
6377
}
6478

6579
public SecurityPlan createSecurityPlan( String uriScheme )

driver/src/test/java/org/neo4j/driver/internal/SecuritySettingsTest.java

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21+
import org.junit.jupiter.api.Nested;
22+
import org.junit.jupiter.api.Test;
2123
import org.junit.jupiter.params.ParameterizedTest;
2224
import org.junit.jupiter.params.provider.MethodSource;
25+
import org.junit.platform.commons.support.ReflectionSupport;
2326

27+
import java.io.File;
28+
import java.io.IOException;
29+
import java.lang.reflect.InvocationTargetException;
30+
import java.lang.reflect.Method;
2431
import java.util.stream.Stream;
2532

2633
import org.neo4j.driver.Config;
2734
import org.neo4j.driver.exceptions.ClientException;
2835
import org.neo4j.driver.internal.security.SecurityPlan;
36+
import org.neo4j.driver.util.TestUtil;
2937

3038
import static org.junit.jupiter.api.Assertions.assertEquals;
3139
import static org.junit.jupiter.api.Assertions.assertFalse;
3240
import static org.junit.jupiter.api.Assertions.assertThrows;
3341
import static org.junit.jupiter.api.Assertions.assertTrue;
34-
import static org.neo4j.driver.internal.RevocationStrategy.STRICT;
3542
import static org.neo4j.driver.internal.RevocationStrategy.NO_CHECKS;
43+
import static org.neo4j.driver.internal.RevocationStrategy.STRICT;
3644
import static org.neo4j.driver.internal.RevocationStrategy.VERIFY_IF_PRESENT;
3745

3846
class SecuritySettingsTest
@@ -218,4 +226,102 @@ void testRevocationCheckingDisabledByDefault( String scheme )
218226
assertEquals( NO_CHECKS, securityPlan.revocationStrategy() );
219227
}
220228

229+
@Nested
230+
class SerializationTests
231+
{
232+
Method isCustomized = ReflectionSupport.findMethod( SecuritySettings.class, "isCustomized" ).orElseThrow(
233+
() -> new RuntimeException( "This test requires isCustomized to be present." ) );
234+
235+
boolean isCustomized( SecuritySettings securitySettings )
236+
{
237+
isCustomized.setAccessible( true );
238+
try
239+
{
240+
return (boolean) isCustomized.invoke( securitySettings );
241+
}
242+
catch ( IllegalAccessException | InvocationTargetException e )
243+
{
244+
throw new RuntimeException( e );
245+
}
246+
}
247+
248+
@Test
249+
void defaultSettingsShouldNotBeCustomizedWhenReadBack() throws IOException, ClassNotFoundException
250+
{
251+
SecuritySettings securitySettings = new SecuritySettings.SecuritySettingsBuilder().build();
252+
253+
assertFalse( isCustomized( securitySettings ) );
254+
255+
SecuritySettings verify = TestUtil.serializeAndReadBack( securitySettings, SecuritySettings.class );
256+
257+
assertFalse( isCustomized( verify ) );
258+
}
259+
260+
@Test
261+
void defaultsShouldBeCheckCorrect() throws IOException, ClassNotFoundException
262+
{
263+
SecuritySettings securitySettings = new SecuritySettings.SecuritySettingsBuilder().withoutEncryption().withTrustStrategy(
264+
Config.TrustStrategy.trustSystemCertificates() ).build();
265+
266+
// The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
267+
assertFalse( isCustomized( securitySettings ) );
268+
269+
SecuritySettings verify = TestUtil.serializeAndReadBack( securitySettings, SecuritySettings.class );
270+
271+
assertFalse( isCustomized( verify ) );
272+
}
273+
274+
@Test
275+
void shouldReadBackChangedEncryption() throws IOException, ClassNotFoundException
276+
{
277+
SecuritySettings securitySettings =
278+
new SecuritySettings.SecuritySettingsBuilder().withEncryption().withTrustStrategy( Config.TrustStrategy.trustSystemCertificates() ).build();
279+
280+
assertTrue( isCustomized( securitySettings ) );
281+
assertTrue( securitySettings.encrypted() );
282+
283+
SecuritySettings verify = TestUtil.serializeAndReadBack( securitySettings, SecuritySettings.class );
284+
285+
assertTrue( isCustomized( verify ) );
286+
assertTrue( securitySettings.encrypted() );
287+
}
288+
289+
@Test
290+
void shouldReadBackChangedStrategey() throws IOException, ClassNotFoundException
291+
{
292+
SecuritySettings securitySettings =
293+
new SecuritySettings.SecuritySettingsBuilder().withoutEncryption().withTrustStrategy( Config.TrustStrategy.trustAllCertificates() ).build();
294+
295+
// The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
296+
assertTrue( isCustomized( securitySettings ) );
297+
assertFalse( securitySettings.encrypted() );
298+
assertEquals( Config.TrustStrategy.trustAllCertificates().strategy(), securitySettings.trustStrategy().strategy() );
299+
300+
SecuritySettings verify = TestUtil.serializeAndReadBack( securitySettings, SecuritySettings.class );
301+
302+
assertTrue( isCustomized( verify ) );
303+
assertFalse( securitySettings.encrypted() );
304+
assertEquals( Config.TrustStrategy.trustAllCertificates().strategy(), securitySettings.trustStrategy().strategy() );
305+
}
306+
307+
@Test
308+
void shouldReadBackChangedCertFile() throws IOException, ClassNotFoundException
309+
{
310+
SecuritySettings securitySettings = new SecuritySettings.SecuritySettingsBuilder().withoutEncryption().withTrustStrategy(
311+
Config.TrustStrategy.trustCustomCertificateSignedBy( new File( "some.cert" ) ) ).build();
312+
313+
// The settings are still equivalent to the defaults, even if the builder has been used. It is not customized.
314+
assertTrue( isCustomized( securitySettings ) );
315+
assertFalse( securitySettings.encrypted() );
316+
assertEquals( Config.TrustStrategy.trustCustomCertificateSignedBy( new File( "some.cert" ) ).strategy(),
317+
securitySettings.trustStrategy().strategy() );
318+
319+
SecuritySettings verify = TestUtil.serializeAndReadBack( securitySettings, SecuritySettings.class );
320+
321+
assertTrue( isCustomized( verify ) );
322+
assertFalse( securitySettings.encrypted() );
323+
assertEquals( Config.TrustStrategy.trustCustomCertificateSignedBy( new File( "some.cert" ) ).strategy(),
324+
securitySettings.trustStrategy().strategy() );
325+
}
326+
}
221327
}

0 commit comments

Comments
 (0)