use of org.neo4j.driver.exceptions.Neo4jException in project neo4j by neo4j.
the class MainTest method triesOnlyOnceIfUserPassExists.
@Test
public void triesOnlyOnceIfUserPassExists() throws Exception {
doThrow(authException).doThrow(new RuntimeException("second try")).when(shell).connect(connectionConfig, null);
doReturn("bob").when(connectionConfig).username();
doReturn("secret").when(connectionConfig).password();
InputStream inputStream = new ByteArrayInputStream("".getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
Main main = new Main(inputStream, ps);
try {
main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
fail("Expected an exception");
} catch (Neo4jException e) {
assertEquals(authException.code(), e.code());
verify(shell, times(1)).connect(connectionConfig, null);
}
}
use of org.neo4j.driver.exceptions.Neo4jException in project neo4j by neo4j.
the class CypherShell method getErrorCode.
private static String getErrorCode(Neo4jException e) {
Neo4jException statusException = e;
// If we encountered a later suppressed Neo4jException we use that as the basis for the status instead
Throwable[] suppressed = e.getSuppressed();
for (Throwable s : suppressed) {
if (s instanceof Neo4jException) {
statusException = (Neo4jException) s;
break;
}
}
if (statusException instanceof ServiceUnavailableException || statusException instanceof DiscoveryException) {
// Treat this the same way as a DatabaseUnavailable error for now.
return DATABASE_UNAVAILABLE_ERROR_CODE;
}
return statusException.code();
}
use of org.neo4j.driver.exceptions.Neo4jException in project neo4j by neo4j.
the class BoltStateHandler method changePassword.
public void changePassword(@Nonnull ConnectionConfig connectionConfig) {
if (!connectionConfig.passwordChangeRequired()) {
return;
}
if (isConnected()) {
silentDisconnect();
}
final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
try {
driver = getDriver(connectionConfig, authToken);
activeDatabaseNameAsSetByUser = SYSTEM_DB_NAME;
// Supply empty command, so that we do not run ping.
reconnect(SYSTEM_DB_NAME, SYSTEM_DB_NAME, () -> {
});
try {
String command = "ALTER CURRENT USER SET PASSWORD FROM $o TO $n";
Value parameters = Values.parameters("o", connectionConfig.password(), "n", connectionConfig.newPassword());
Result run = session.run(command, parameters);
run.consume();
} catch (Neo4jException e) {
if (isPasswordChangeRequiredException(e)) {
// In < 4.0 versions use legacy method.
String oldCommand = "CALL dbms.security.changePassword($n)";
Value oldParameters = Values.parameters("n", connectionConfig.newPassword());
Result run = session.run(oldCommand, oldParameters);
run.consume();
} else {
throw e;
}
}
// If successful, use the new password when reconnecting
connectionConfig.setPassword(connectionConfig.newPassword());
connectionConfig.setNewPassword(null);
silentDisconnect();
} catch (Throwable t) {
try {
silentDisconnect();
} catch (Exception e) {
t.addSuppressed(e);
}
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
}
// we cannot get that since we supply an empty command.
throw new RuntimeException(t);
}
}
Aggregations