Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cypher-shell/src/main/java/org/neo4j/shell/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static org.neo4j.shell.ShellRunner.isInputInteractive;
import static org.neo4j.shell.ShellRunner.isOutputInteractive;
import static org.neo4j.shell.util.Versions.isPasswordChangeRequiredException;

public class Main {
static final String NEO_CLIENT_ERROR_SECURITY_UNAUTHORIZED = "Neo.ClientError.Security.Unauthorized";
Expand Down Expand Up @@ -172,7 +173,7 @@ private void connectMaybeInteractively( @Nonnull CypherShell shell,
promptForUsernameAndPassword(connectionConfig, outputInteractive);
didPrompt = true;
} catch (Neo4jException e) {
if (passwordChangeRequiredException(e)) {
if (isPasswordChangeRequiredException(e)) {
promptForPasswordChange(connectionConfig, outputInteractive);
shell.changePassword(connectionConfig);
didPrompt = true;
Expand All @@ -183,10 +184,6 @@ private void connectMaybeInteractively( @Nonnull CypherShell shell,
}
}

private boolean passwordChangeRequiredException(Neo4jException e) {
return "Neo.ClientError.Security.CredentialsExpired".equalsIgnoreCase(e.code());
}

private void promptForUsernameAndPassword(ConnectionConfig connectionConfig, boolean outputInteractive) throws Exception {
OutputStream promptOutputStream = getOutputStreamForInteractivePrompt();
ConsoleReader consoleReader = new ConsoleReader(in, promptOutputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.neo4j.driver.Value;
import org.neo4j.driver.Values;
import org.neo4j.driver.exceptions.ClientException;
import org.neo4j.driver.exceptions.Neo4jException;
import org.neo4j.driver.exceptions.SessionExpiredException;
import org.neo4j.driver.internal.Scheme;
import org.neo4j.driver.summary.DatabaseInfo;
Expand All @@ -38,6 +39,7 @@
import org.neo4j.shell.exception.CommandException;
import org.neo4j.shell.log.NullLogging;

import static org.neo4j.shell.util.Versions.isPasswordChangeRequiredException;
import static org.neo4j.shell.util.Versions.majorVersion;

/**
Expand Down Expand Up @@ -218,10 +220,32 @@ else if ( systemBookmark != null )

session = driver.session( builder.build() );

ThrowingAction<CommandException> action = command != null ? command : getPing();

resetActualDbName(); // Set this to null first in case run throws an exception
action.apply();
connect(command);
}

private void connect( ThrowingAction<CommandException> command) throws CommandException
{
ThrowingAction<CommandException> toCall = command == null ? getPing() : () ->
{
try
{
command.apply();
}
catch ( Neo4jException e )
{
//If we need to update password we need to call the apply
//to set the server version and such.
if (isPasswordChangeRequiredException( e ))
{
getPing().apply();
}
throw e;
}
};

//execute
toCall.apply();
}

private ThrowingAction<CommandException> getPing() {
Expand Down Expand Up @@ -401,6 +425,7 @@ public void reset() {
public void disconnect() {
reset();
silentDisconnect();
version = null;
}

List<Query> getTransactionStatements() {
Expand Down
6 changes: 6 additions & 0 deletions cypher-shell/src/main/java/org/neo4j/shell/util/Versions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.neo4j.shell.util;

import org.neo4j.driver.exceptions.Neo4jException;

import static java.lang.Integer.parseInt;
import static java.lang.String.format;

Expand Down Expand Up @@ -47,4 +49,8 @@ public static Version version(String version) {
format("%s is not a proper version string, it should be of the form X.Y.Z ", version));
}
}

public static boolean isPasswordChangeRequiredException( Neo4jException e) {
return "Neo.ClientError.Security.CredentialsExpired".equalsIgnoreCase(e.code());
}
}