Skip to content

Commit

Permalink
Only mention roles file in error msg if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Lojjs committed Sep 20, 2018
1 parent 4d2d67c commit 7524480
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Expand Up @@ -28,7 +28,6 @@
import org.neo4j.commandline.admin.IncorrectUsage;
import org.neo4j.commandline.admin.OutsideWorld;
import org.neo4j.commandline.arguments.Arguments;
import org.neo4j.helpers.Args;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.logging.NullLogProvider;
Expand Down Expand Up @@ -81,18 +80,16 @@ public void execute( String[] args ) throws IncorrectUsage, CommandFailed
private void setPassword( String password ) throws Throwable
{
Config config = loadNeo4jConfig();
FileSystemAbstraction fileSystem = outsideWorld.fileSystem();
File authFile = CommunitySecurityModule.getUserRepositoryFile( config );

if ( realUsersExist( authFile ) )
{
throw new CommandFailed( "the provided initial password was not set because existing Neo4j users were detected at `" +
authFile.getAbsolutePath() + "`. Please remove the existing `auth` and `roles` files if you want to reset your database " +
"to only have a default user with the provided password." );
throw new CommandFailed( realUsersExistErrorMsg( fileSystem, authFile ) );
}
else
{
File file = CommunitySecurityModule.getInitialUserRepositoryFile( config );
FileSystemAbstraction fileSystem = outsideWorld.fileSystem();
if ( fileSystem.fileExists( file ) )
{
fileSystem.deleteFile( file );
Expand All @@ -116,6 +113,26 @@ private boolean realUsersExist( File authFile )
return outsideWorld.fileSystem().fileExists( authFile );
}

private String realUsersExistErrorMsg( FileSystemAbstraction fileSystem, File authFile )
{
String files;
File parentFile = authFile.getParentFile();
File roles = new File( parentFile, "roles" );

if ( fileSystem.fileExists( roles ) )
{
files = "`auth` and `roles` files";
}
else
{
files = "`auth` file";
}

return "the provided initial password was not set because existing Neo4j users were detected at `" +
authFile.getAbsolutePath() + "`. Please remove the existing " + files + " if you want to reset your database " +
"to only have a default user with the provided password.";
}

Config loadNeo4jConfig()
{
return ConfigLoader.loadConfigWithConnectorsDisabled(
Expand Down
Expand Up @@ -138,7 +138,7 @@ public void shouldGetUsageOnWrongArguments2() throws Throwable
}

@Test
public void shouldErrorIfRealUsersAlreadyExist() throws Throwable
public void shouldErrorIfRealUsersAlreadyExistCommunity() throws Throwable
{
// Given
File authFile = getAuthFile( "auth" );
Expand All @@ -148,6 +148,30 @@ public void shouldErrorIfRealUsersAlreadyExist() throws Throwable
// When
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" );

// Then
assertNoAuthIniFile();
verify( out, times( 1 ) )
.stdErrLine( "command failed: the provided initial password was not set because existing Neo4j users were " +
"detected at `" + authFile.getAbsolutePath() + "`. Please remove the existing `auth` file if you " +
"want to reset your database to only have a default user with the provided password." );
verify( out ).exit( 1 );
verify( out, times( 0 ) ).stdOutLine( anyString() );
}

@Test
public void shouldErrorIfRealUsersAlreadyExistEnterprise() throws Throwable
{
// Given
File authFile = getAuthFile( "auth" );
File rolesFile = getAuthFile( "roles" );

fileSystem.mkdirs( authFile.getParentFile() );
fileSystem.create( authFile );
fileSystem.create( rolesFile );

// When
tool.execute( homeDir.toPath(), confDir.toPath(), SET_PASSWORD, "will-be-ignored" );

// Then
assertNoAuthIniFile();
verify( out, times( 1 ) )
Expand Down

0 comments on commit 7524480

Please sign in to comment.