Skip to content

Commit ab35aab

Browse files
author
Zhen
committed
Added top class SecurityException for all errors that related to security
Changed from UnauthorizedException to AuthenticationException for wrong username/password Use SecurityException for failed to tls handshake
1 parent b4e4a14 commit ab35aab

File tree

11 files changed

+92
-68
lines changed

11 files changed

+92
-68
lines changed

driver/src/main/java/org/neo4j/driver/internal/cluster/Rediscovery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import org.neo4j.driver.internal.util.Clock;
2525
import org.neo4j.driver.v1.Logger;
2626
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
27-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
27+
import org.neo4j.driver.v1.exceptions.SecurityException;
2828

2929
import static java.lang.String.format;
3030

@@ -76,7 +76,7 @@ public ClusterComposition lookupRoutingTable( ConnectionPool connections, Routin
7676
cluster = provider.getClusterComposition( connection );
7777
logger.info( "Got cluster composition %s", cluster );
7878
}
79-
catch( UnauthorizedException e )
79+
catch( SecurityException e )
8080
{
8181
throw e; // terminate the discovery immediately
8282
}

driver/src/main/java/org/neo4j/driver/internal/net/SocketResponseHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Queue;
2323
import java.util.concurrent.ConcurrentLinkedQueue;
2424

25+
import org.neo4j.driver.v1.exceptions.AuthenticationException;
2526
import org.neo4j.driver.internal.messaging.MessageHandler;
2627
import org.neo4j.driver.internal.spi.Collector;
2728
import org.neo4j.driver.internal.summary.InternalNotification;
@@ -33,7 +34,6 @@
3334
import org.neo4j.driver.v1.exceptions.DatabaseException;
3435
import org.neo4j.driver.v1.exceptions.Neo4jException;
3536
import org.neo4j.driver.v1.exceptions.TransientException;
36-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
3737
import org.neo4j.driver.v1.summary.Notification;
3838
import org.neo4j.driver.v1.summary.StatementType;
3939
import org.neo4j.driver.v1.util.Function;
@@ -68,7 +68,7 @@ public void handleFailureMessage( String code, String message )
6868
case "ClientError":
6969
if( code.equalsIgnoreCase( "Neo.ClientError.Security.Unauthorized" ) )
7070
{
71-
error = new UnauthorizedException( code, message );
71+
error = new AuthenticationException( code, message );
7272
}
7373
else
7474
{

driver/src/main/java/org/neo4j/driver/internal/security/TLSSocketChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.neo4j.driver.internal.util.BytePrinter;
3333
import org.neo4j.driver.v1.exceptions.ClientException;
3434
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
35-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
35+
import org.neo4j.driver.v1.exceptions.SecurityException;
3636

3737
import static java.lang.String.format;
3838
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.FINISHED;
@@ -82,7 +82,7 @@ public static TLSSocketChannel create( ByteChannel channel, Logger logger, SSLEn
8282
}
8383
catch ( SSLHandshakeException e )
8484
{
85-
throw new UnauthorizedException( "Failed to establish secured connection with the server: " + e.getMessage(), e );
85+
throw new SecurityException( "Failed to establish secured connection with the server: " + e.getMessage(), e );
8686
}
8787
return tlsChannel;
8888
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.v1.exceptions;
20+
21+
/**
22+
* Failed to authenticate the driver to the server due to bad credentials provided.
23+
* When this error happens, the error could be recovered by closing the current driver and restart a new driver with
24+
* the correct credentials.
25+
*
26+
* @since 1.1
27+
*/
28+
public class AuthenticationException extends SecurityException
29+
{
30+
public AuthenticationException( String code, String message )
31+
{
32+
super( code, message );
33+
}
34+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.neo4j.driver.v1.exceptions;
21+
22+
/**
23+
* Failed to communicate with the server due to security errors.
24+
* When this type of error happens, the security cause of the error should be fixed to ensure the safety of your data.
25+
* Restart of server/driver/cluster might be required to recover from this error.
26+
* @since 1.1
27+
*/
28+
public class SecurityException extends Neo4jException
29+
{
30+
public SecurityException( String code, String message )
31+
{
32+
super( code, message );
33+
}
34+
35+
public SecurityException( String message, Throwable t )
36+
{
37+
super( message, t );
38+
}
39+
}

driver/src/main/java/org/neo4j/driver/v1/exceptions/UnauthorizedException.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

driver/src/test/java/org/neo4j/driver/internal/security/TLSSocketChannelTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import javax.net.ssl.SSLSession;
2828

2929
import org.neo4j.driver.v1.exceptions.ServiceUnavailableException;
30-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
30+
import org.neo4j.driver.v1.exceptions.SecurityException;
3131

3232
import static junit.framework.TestCase.fail;
3333
import static org.hamcrest.MatcherAssert.assertThat;
@@ -128,7 +128,7 @@ public void shouldThrowUnauthorizedIfFailedToHandshake() throws Throwable
128128
}
129129
catch( Exception e )
130130
{
131-
assertThat( e, instanceOf( UnauthorizedException.class ) );
131+
assertThat( e, instanceOf( SecurityException.class ) );
132132
assertThat( e.getMessage(), startsWith( "Failed to establish secured connection with the server: Failed handshake!" ) );
133133
}
134134
verify( mockedChannel, never() ).close();

driver/src/test/java/org/neo4j/driver/v1/integration/CredentialsIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.neo4j.driver.v1.GraphDatabase;
3131
import org.neo4j.driver.v1.Session;
3232
import org.neo4j.driver.v1.Value;
33-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
33+
import org.neo4j.driver.v1.exceptions.SecurityException;
3434
import org.neo4j.driver.v1.util.Neo4jSettings;
3535
import org.neo4j.driver.v1.util.TestNeo4j;
3636

@@ -79,7 +79,7 @@ public void shouldGetHelpfulErrorOnInvalidCredentials() throws Throwable
7979
}
8080
catch( Throwable e )
8181
{
82-
assertThat( e, instanceOf( UnauthorizedException.class ) );
82+
assertThat( e, instanceOf( SecurityException.class ) );
8383
assertThat( e.getMessage(), containsString( "The client is unauthorized due to authentication failure." ) );
8484
}
8585
}

driver/src/test/java/org/neo4j/driver/v1/integration/TLSSocketChannelIT.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import org.neo4j.driver.v1.Logging;
4343
import org.neo4j.driver.v1.Session;
4444
import org.neo4j.driver.v1.StatementResult;
45-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
45+
import org.neo4j.driver.v1.exceptions.SecurityException;
4646
import org.neo4j.driver.v1.util.CertificateToolTest;
4747
import org.neo4j.driver.v1.util.Neo4jRunner;
4848
import org.neo4j.driver.v1.util.Neo4jSettings;
@@ -156,7 +156,7 @@ public void shouldNotPerformTLSHandshakeWithNonSystemCert() throws Throwable
156156
sslChannel = TLSSocketChannel.create(address, securityPlan, channel, logger);
157157
fail( "Should have thrown exception" );
158158
}
159-
catch ( UnauthorizedException e )
159+
catch ( SecurityException e )
160160
{
161161
assertThat( e.getMessage(), containsString( "General SSLEngine problem" ) );
162162
assertThat( getRootCause( e ).getMessage(),
@@ -198,7 +198,7 @@ public void shouldFailTLSHandshakeDueToWrongCertInKnownCertsFile() throws Throwa
198198
sslChannel = TLSSocketChannel.create( address, securityPlan, channel, DEV_NULL_LOGGER );
199199
fail( "Should have thrown exception" );
200200
}
201-
catch ( UnauthorizedException e )
201+
catch ( SecurityException e )
202202
{
203203
assertThat( e.getMessage(), containsString( "General SSLEngine problem" ) );
204204
assertThat( getRootCause( e ).getMessage(), containsString(
@@ -249,7 +249,7 @@ public void shouldFailTLSHandshakeDueToServerCertNotSignedByKnownCA() throws Thr
249249
sslChannel = TLSSocketChannel.create( neo4j.address(), securityPlan, channel, mock( Logger.class ) );
250250
fail( "Should have thrown exception" );
251251
}
252-
catch ( UnauthorizedException e )
252+
catch ( SecurityException e )
253253
{
254254
assertThat( e.getMessage(), containsString( "General SSLEngine problem" ) );
255255
assertThat( getRootCause( e ).getMessage(), containsString( "No trusted certificate found" ) );

driver/src/test/java/org/neo4j/driver/v1/stress/CausalClusteringStressIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import org.neo4j.driver.v1.StatementResult;
5252
import org.neo4j.driver.v1.Transaction;
5353
import org.neo4j.driver.v1.exceptions.ClientException;
54-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
54+
import org.neo4j.driver.v1.exceptions.SecurityException;
5555
import org.neo4j.driver.v1.types.Node;
5656
import org.neo4j.driver.v1.util.DaemonThreadFactory;
5757
import org.neo4j.driver.v1.util.cc.LocalOrRemoteClusterRule;
@@ -411,7 +411,7 @@ public void execute()
411411
}
412412
catch ( Exception e )
413413
{
414-
assertThat( e, instanceOf( UnauthorizedException.class ) );
414+
assertThat( e, instanceOf( SecurityException.class ) );
415415
assertThat( e.getMessage(), containsString( "authentication failure" ) );
416416

417417
ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass( Throwable.class );

driver/src/test/java/org/neo4j/driver/v1/tck/DriverSecurityComplianceSteps.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import org.neo4j.driver.v1.GraphDatabase;
3434
import org.neo4j.driver.v1.Session;
3535
import org.neo4j.driver.v1.StatementResult;
36-
import org.neo4j.driver.v1.exceptions.UnauthorizedException;
36+
import org.neo4j.driver.v1.exceptions.SecurityException;
3737
import org.neo4j.driver.v1.util.CertificateToolTest.CertificateSigningRequestGenerator;
3838
import org.neo4j.driver.v1.util.CertificateToolTest.SelfSignedCertificateGenerator;
3939
import org.neo4j.driver.v1.util.Neo4jRunner;
@@ -134,7 +134,7 @@ public void creatingSessionsShouldFail() throws Throwable
134134
public void iShouldGetAHelpfulErrorExplainingThatCertificateChanged( String str ) throws Throwable
135135
{
136136
assertThat( exception, notNullValue() );
137-
assertThat( exception, instanceOf( UnauthorizedException.class ) );
137+
assertThat( exception, instanceOf( SecurityException.class ) );
138138
Throwable rootCause = getRootCause( exception );
139139
assertThat( rootCause.toString(), containsString(
140140
"Unable to connect to neo4j at `localhost:7687`, because the certificate the server uses has changed. " +
@@ -244,7 +244,7 @@ public void aRunningNeo4jDatabaseUsingACertNotSignedByTheTrustedCertificates() t
244244
public void iShouldGetAHelpfulErrorExplainingThatCertificatedNotSigned() throws Throwable
245245
{
246246
assertThat( exception, notNullValue() );
247-
assertThat( exception, instanceOf( UnauthorizedException.class ) );
247+
assertThat( exception, instanceOf( SecurityException.class ) );
248248
Throwable rootCause = getRootCause( exception );
249249
assertThat( rootCause.toString(), containsString( "Signature does not match.") );
250250
}

0 commit comments

Comments
 (0)