Skip to content

Commit

Permalink
Removed identifier from auth failure output
Browse files Browse the repository at this point in the history
The unique identifier was meant as a mechanism of handling multiple instances
of neo4j. However it is not used as such and only confuses users.
  • Loading branch information
pontusmelke committed Jul 1, 2016
1 parent c7f639a commit 07b4332
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 111 deletions.

This file was deleted.

Expand Up @@ -27,19 +27,19 @@ public class AuthenticationException extends IOException implements Status.HasSt
{ {
private final Status status; private final Status status;


public AuthenticationException( Status status, String identifier ) public AuthenticationException( Status status )
{ {
this(status, identifier, status.code().description(), null); this( status, status.code().description(), null );
} }


public AuthenticationException( Status status, String identifier, String message ) public AuthenticationException( Status status, String message )
{ {
this(status, identifier, message, null); this( status, message, null );
} }


public AuthenticationException( Status status, String identifier, String message, Throwable e ) public AuthenticationException( Status status, String message, Throwable e )
{ {
super(message + " (ID:" + identifier + ")" , e); super( message, e );
this.status = status; this.status = status;
} }


Expand Down
Expand Up @@ -21,7 +21,6 @@


import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;


import org.neo4j.graphdb.security.AuthorizationViolationException; import org.neo4j.graphdb.security.AuthorizationViolationException;
import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
Expand All @@ -39,23 +38,20 @@ public class BasicAuthentication implements Authentication
private final BasicAuthManager authManager; private final BasicAuthManager authManager;
private final static String SCHEME = "basic"; private final static String SCHEME = "basic";
private final Log log; private final Log log;
private final Supplier<String> identifier;
private AuthSubject authSubject; private AuthSubject authSubject;



public BasicAuthentication( BasicAuthManager authManager, LogProvider logProvider)
public BasicAuthentication( BasicAuthManager authManager, LogProvider logProvider, Supplier<String> identifier )
{ {
this.authManager = authManager; this.authManager = authManager;
this.log = logProvider.getLog( getClass() ); this.log = logProvider.getLog( getClass() );
this.identifier = identifier;
} }


@Override @Override
public AuthenticationResult authenticate( Map<String,Object> authToken ) throws AuthenticationException public AuthenticationResult authenticate( Map<String,Object> authToken ) throws AuthenticationException
{ {
if ( !SCHEME.equals( authToken.get( SCHEME_KEY ) ) ) if ( !SCHEME.equals( authToken.get( SCHEME_KEY ) ) )
{ {
throw new AuthenticationException( Status.Security.Unauthorized, identifier.get(), throw new AuthenticationException( Status.Security.Unauthorized,
"Authentication token must contain: '" + SCHEME_KEY + " : " + SCHEME + "'" ); "Authentication token must contain: '" + SCHEME_KEY + " : " + SCHEME + "'" );
} }


Expand Down Expand Up @@ -83,10 +79,10 @@ private AuthenticationResult authenticate( String user, String password ) throws
credentialsExpired = true; credentialsExpired = true;
break; break;
case TOO_MANY_ATTEMPTS: case TOO_MANY_ATTEMPTS:
throw new AuthenticationException( Status.Security.AuthenticationRateLimit, identifier.get() ); throw new AuthenticationException( Status.Security.AuthenticationRateLimit);
default: default:
log.warn( "Failed authentication attempt for '%s'", user); log.warn( "Failed authentication attempt for '%s'", user);
throw new AuthenticationException( Status.Security.Unauthorized, identifier.get() ); throw new AuthenticationException( Status.Security.Unauthorized);
} }
return new BasicAuthenticationResult( authSubject, credentialsExpired ); return new BasicAuthenticationResult( authSubject, credentialsExpired );
} }
Expand All @@ -106,19 +102,19 @@ private AuthenticationResult update( String user, String password, String newPas
} }
catch ( AuthorizationViolationException e ) catch ( AuthorizationViolationException e )
{ {
throw new AuthenticationException( Status.Security.Forbidden, identifier.get(), e.getMessage(), e ); throw new AuthenticationException( Status.Security.Forbidden, e.getMessage(), e );
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new AuthenticationException( Status.Security.Unauthorized, identifier.get(), e.getMessage(), e ); throw new AuthenticationException( Status.Security.Unauthorized, e.getMessage(), e );
} }
catch ( IllegalCredentialsException e ) catch ( IllegalCredentialsException e )
{ {
throw new AuthenticationException(e.status(), identifier.get(), e.getMessage(), e ); throw new AuthenticationException(e.status(), e.getMessage(), e );
} }
break; break;
default: default:
throw new AuthenticationException( Status.Security.Unauthorized, identifier.get() ); throw new AuthenticationException( Status.Security.Unauthorized );
} }
return new BasicAuthenticationResult( authSubject, false ); return new BasicAuthenticationResult( authSubject, false );
} }
Expand All @@ -128,7 +124,7 @@ private String safeCast( String key, Map<String,Object> authToken ) throws Authe
Object value = authToken.get( key ); Object value = authToken.get( key );
if ( value == null || !(value instanceof String) ) if ( value == null || !(value instanceof String) )
{ {
throw new AuthenticationException( Status.Security.Unauthorized, identifier.get(), throw new AuthenticationException( Status.Security.Unauthorized,
"The value associated with the key `" + key + "` must be a String but was: " + "The value associated with the key `" + key + "` must be a String but was: " +
(value == null ? "null" : value.getClass().getSimpleName())); (value == null ? "null" : value.getClass().getSimpleName()));
} }
Expand Down
Expand Up @@ -19,22 +19,17 @@
*/ */
package org.neo4j.bolt.v1.runtime.internal; package org.neo4j.bolt.v1.runtime.internal;


import java.util.function.Supplier;

import org.neo4j.bolt.security.auth.AuthUtils;
import org.neo4j.bolt.security.auth.Authentication; import org.neo4j.bolt.security.auth.Authentication;
import org.neo4j.bolt.security.auth.BasicAuthentication; import org.neo4j.bolt.security.auth.BasicAuthentication;
import org.neo4j.bolt.v1.runtime.Session; import org.neo4j.bolt.v1.runtime.Session;
import org.neo4j.bolt.v1.runtime.Sessions; import org.neo4j.bolt.v1.runtime.Sessions;
import org.neo4j.graphdb.DependencyResolver; import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.graphdb.factory.GraphDatabaseSettings; import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.NeoStoreDataSource;
import org.neo4j.kernel.configuration.Config; import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge; import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade; import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
import org.neo4j.kernel.impl.logging.LogService; import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.impl.query.QueryExecutionEngine; import org.neo4j.kernel.impl.query.QueryExecutionEngine;
import org.neo4j.kernel.impl.store.StoreId;
import org.neo4j.kernel.lifecycle.LifeSupport; import org.neo4j.kernel.lifecycle.LifeSupport;
import org.neo4j.kernel.lifecycle.LifecycleAdapter; import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.server.security.auth.BasicAuthManager; import org.neo4j.server.security.auth.BasicAuthManager;
Expand Down Expand Up @@ -106,11 +101,7 @@ private Authentication authentication( DependencyResolver dependencyResolver )


if ( config.get( GraphDatabaseSettings.auth_enabled ) ) if ( config.get( GraphDatabaseSettings.auth_enabled ) )
{ {
Supplier<String> identifier = () -> { return new BasicAuthentication( dependencyResolver.resolveDependency( BasicAuthManager.class ), logging.getUserLogProvider());
StoreId storeId = dependencyResolver.resolveDependency( NeoStoreDataSource.class ).getStoreId();
return AuthUtils.uniqueIdentifier( storeId );
};
return new BasicAuthentication( dependencyResolver.resolveDependency( BasicAuthManager.class ), logging.getUserLogProvider(), identifier );
} }
else else
{ {
Expand Down
Expand Up @@ -26,8 +26,6 @@
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;


import java.util.function.Supplier;

import org.neo4j.kernel.api.exceptions.Status; import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.logging.Log; import org.neo4j.logging.Log;
import org.neo4j.logging.LogProvider; import org.neo4j.logging.LogProvider;
Expand All @@ -47,15 +45,14 @@ public class BasicAuthenticationTest
@Rule @Rule
public ExpectedException exception = ExpectedException.none(); public ExpectedException exception = ExpectedException.none();


private final Supplier<String> identifier = () -> "UNIQUE";


@Test @Test
public void shouldNotDoAnythingOnSuccess() throws AuthenticationException public void shouldNotDoAnythingOnSuccess() throws AuthenticationException
{ {
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS );


Expand All @@ -74,7 +71,7 @@ public void shouldThrowAndLogOnFailure() throws AuthenticationException
Log log = mock( Log.class ); Log log = mock( Log.class );
LogProvider logProvider = mock( LogProvider.class ); LogProvider logProvider = mock( LogProvider.class );
when( logProvider.getLog( BasicAuthentication.class ) ).thenReturn( log ); when( logProvider.getLog( BasicAuthentication.class ) ).thenReturn( log );
BasicAuthentication authentication = new BasicAuthentication( manager, logProvider, identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, logProvider );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.FAILURE ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.FAILURE );


Expand All @@ -96,7 +93,7 @@ public void shouldIndicateThatCredentialsExpired() throws AuthenticationExceptio
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.PASSWORD_CHANGE_REQUIRED ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.PASSWORD_CHANGE_REQUIRED );


Expand All @@ -116,7 +113,7 @@ public void shouldFailWhenTooManyAttempts() throws AuthenticationException
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.TOO_MANY_ATTEMPTS ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.TOO_MANY_ATTEMPTS );


Expand All @@ -135,7 +132,7 @@ public void shouldBeAbleToUpdateCredentials() throws AuthenticationException
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS );


Expand All @@ -152,7 +149,7 @@ public void shouldBeAbleToUpdateExpiredCredentials() throws AuthenticationExcept
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.PASSWORD_CHANGE_REQUIRED ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.PASSWORD_CHANGE_REQUIRED );


Expand All @@ -169,7 +166,7 @@ public void shouldNotBeAbleToUpdateCredentialsIfOldCredentialsAreInvalid() throw
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.FAILURE ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.FAILURE );


Expand All @@ -190,7 +187,7 @@ public void shouldFailOnUnknownScheme() throws AuthenticationException
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS );


Expand All @@ -209,7 +206,7 @@ public void shouldFailOnMalformedToken() throws AuthenticationException
// Given // Given
BasicAuthManager manager = mock( BasicAuthManager.class ); BasicAuthManager manager = mock( BasicAuthManager.class );
BasicAuthSubject authSubject = mock( BasicAuthSubject.class ); BasicAuthSubject authSubject = mock( BasicAuthSubject.class );
BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ), identifier ); BasicAuthentication authentication = new BasicAuthentication( manager, mock( LogProvider.class ) );
when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject ); when( manager.login( anyString(), anyString() ) ).thenReturn( authSubject );
when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS ); when( authSubject.getAuthenticationResult() ).thenReturn( AuthenticationResult.SUCCESS );


Expand Down
Expand Up @@ -117,7 +117,7 @@ public void shouldFailIfWrongCredentials() throws Throwable
// Then // Then
assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) );
assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized, assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized,
String.format( "The client is unauthorized due to authentication failure. (ID:%s)", server.uniqueIdentier()) ) ) ); "The client is unauthorized due to authentication failure." ) ) );
} }


@Test @Test
Expand All @@ -143,7 +143,7 @@ public void shouldBeAbleToUpdateCredentials() throws Throwable
map( "principal", "neo4j", "credentials", "neo4j", "scheme", "basic" ) ) ) ); map( "principal", "neo4j", "credentials", "neo4j", "scheme", "basic" ) ) ) );
assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) );
assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized, assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized,
String.format( "The client is unauthorized due to authentication failure. (ID:%s)", server.uniqueIdentier()) ) ) ); "The client is unauthorized due to authentication failure." ) ) );


// But the new password works fine // But the new password works fine
reconnect(); reconnect();
Expand Down Expand Up @@ -188,7 +188,7 @@ public void shouldBeAbleToChangePasswordUsingBuiltInProcedure() throws Throwable
map( "principal", "neo4j", "credentials", "neo4j", "scheme", "basic" ) ) ) ); map( "principal", "neo4j", "credentials", "neo4j", "scheme", "basic" ) ) ) );
assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) ); assertThat( client, eventuallyRecieves( new byte[]{0, 0, 0, 1} ) );
assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized, assertThat( client, eventuallyRecieves( msgFailure( Status.Security.Unauthorized,
String.format( "The client is unauthorized due to authentication failure. (ID:%s)", server.uniqueIdentier()) ) ) ); "The client is unauthorized due to authentication failure." ) ) );


// But the new password works fine // But the new password works fine
reconnect(); reconnect();
Expand Down
Expand Up @@ -31,7 +31,6 @@
import java.util.function.Consumer; import java.util.function.Consumer;


import org.neo4j.bolt.BoltKernelExtension; import org.neo4j.bolt.BoltKernelExtension;
import org.neo4j.bolt.security.auth.AuthUtils;
import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.config.Setting; import org.neo4j.graphdb.config.Setting;
import org.neo4j.kernel.impl.factory.GraphDatabaseFacade; import org.neo4j.kernel.impl.factory.GraphDatabaseFacade;
Expand All @@ -56,12 +55,6 @@ public Neo4jWithSocket( Consumer<Map<Setting<?>, String>> configure )
this.configure = configure; this.configure = configure;
} }


public String uniqueIdentier()
{
return AuthUtils.uniqueIdentifier( storeId );
}


@Override @Override
public Statement apply( final Statement statement, Description description ) public Statement apply( final Statement statement, Description description )
{ {
Expand Down

0 comments on commit 07b4332

Please sign in to comment.