|
43 | 43 | import org.neo4j.driver.v1.util.CertificateToolTest;
|
44 | 44 | import org.neo4j.driver.v1.util.Neo4jInstaller;
|
45 | 45 | import org.neo4j.driver.v1.util.Neo4jRunner;
|
| 46 | +import org.neo4j.driver.v1.util.Neo4jSettings; |
46 | 47 | import org.neo4j.driver.v1.util.TestNeo4j;
|
47 | 48 |
|
| 49 | +import static java.io.File.createTempFile; |
48 | 50 | import static org.junit.Assert.assertEquals;
|
49 | 51 | import static org.junit.Assert.assertFalse;
|
50 | 52 | import static org.junit.Assert.assertTrue;
|
@@ -92,6 +94,65 @@ private void performTLSHandshakeUsingKnownCerts( File knownCerts ) throws Throwa
|
92 | 94 | verify( logger, atLeastOnce() ).debug( "TLS connection closed" );
|
93 | 95 | }
|
94 | 96 |
|
| 97 | + private File tempFile(String prefix, String suffix) throws Throwable |
| 98 | + { |
| 99 | + File file = createTempFile( prefix, suffix ); |
| 100 | + file.deleteOnExit(); |
| 101 | + return file; |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void shouldPerformTLSHandshakeWithTrustedCert() throws Throwable |
| 106 | + { |
| 107 | + try |
| 108 | + { |
| 109 | + // Given |
| 110 | + // Create root certificate |
| 111 | + File rootCert = tempFile( "temp_root_cert", ".cert" ); |
| 112 | + File rootKey = tempFile( "temp_root_key", ".key" ); |
| 113 | + |
| 114 | + CertificateToolTest.SelfSignedCertificateGenerator |
| 115 | + certGenerator = new CertificateToolTest.SelfSignedCertificateGenerator(); |
| 116 | + certGenerator.saveSelfSignedCertificate( rootCert ); |
| 117 | + certGenerator.savePrivateKey( rootKey ); |
| 118 | + |
| 119 | + // Generate certificate signing request and get a certificate signed by the root private key |
| 120 | + File cert = tempFile( "temp_cert", ".cert" ); |
| 121 | + File key = tempFile( "temp_key", ".key" ); |
| 122 | + CertificateToolTest.CertificateSigningRequestGenerator |
| 123 | + csrGenerator = new CertificateToolTest.CertificateSigningRequestGenerator(); |
| 124 | + X509Certificate signedCert = certGenerator.sign( |
| 125 | + csrGenerator.certificateSigningRequest(), csrGenerator.publicKey() ); |
| 126 | + csrGenerator.savePrivateKey( key ); |
| 127 | + CertificateTool.saveX509Cert( signedCert, cert ); |
| 128 | + |
| 129 | + // Give the server certs to database |
| 130 | + neo4j.restartServerOnEmptyDatabase( |
| 131 | + Neo4jSettings.DEFAULT |
| 132 | + .usingEncryptionKeyAndCert( key, cert ) ); |
| 133 | + |
| 134 | + Logger logger = mock( Logger.class ); |
| 135 | + SocketChannel channel = SocketChannel.open(); |
| 136 | + channel.connect( new InetSocketAddress( "localhost", 7687 ) ); |
| 137 | + |
| 138 | + // When |
| 139 | + TLSSocketChannel sslChannel = |
| 140 | + new TLSSocketChannel( "localhost", 7687, channel, logger, |
| 141 | + Config.TrustStrategy.trustSignedBy( rootCert ) ); |
| 142 | + sslChannel.close(); |
| 143 | + |
| 144 | + // Then |
| 145 | + verify( logger, atLeastOnce() ).debug( "TLS connection enabled" ); |
| 146 | + verify( logger, atLeastOnce() ).debug( "TLS connection established" ); |
| 147 | + verify( logger, atLeastOnce() ).debug( "TLS connection closed" ); |
| 148 | + } |
| 149 | + finally |
| 150 | + { |
| 151 | + // always restore the db default settings |
| 152 | + neo4j.restartServerOnEmptyDatabase( Neo4jSettings.DEFAULT ); |
| 153 | + } |
| 154 | + } |
| 155 | + |
95 | 156 | @Test
|
96 | 157 | public void shouldFailTLSHandshakeDueToWrongCertInKnownCertsFile() throws Throwable
|
97 | 158 | {
|
@@ -178,7 +239,7 @@ public void shouldFailTLSHandshakeDueToServerCertNotSignedByKnownCA() throws Thr
|
178 | 239 | }
|
179 | 240 |
|
180 | 241 | @Test
|
181 |
| - public void shouldPerformTLSHandshakeWithTrustedServerCert() throws Throwable |
| 242 | + public void shouldPerformTLSHandshakeWithTheSameTrustedServerCert() throws Throwable |
182 | 243 | {
|
183 | 244 |
|
184 | 245 | Logger logger = mock( Logger.class );
|
|
0 commit comments