Search in sources :

Example 1 with Neo4jException

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);
    }
}
Also used : PrintStream(java.io.PrintStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) Test(org.junit.Test)

Example 2 with Neo4jException

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();
}
Also used : Neo4jException(org.neo4j.driver.exceptions.Neo4jException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) DiscoveryException(org.neo4j.driver.exceptions.DiscoveryException)

Example 3 with Neo4jException

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);
    }
}
Also used : Value(org.neo4j.driver.Value) AuthToken(org.neo4j.driver.AuthToken) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException) Result(org.neo4j.driver.Result)

Aggregations

Neo4jException (org.neo4j.driver.exceptions.Neo4jException)3 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 PrintStream (java.io.PrintStream)1 Test (org.junit.Test)1 AuthToken (org.neo4j.driver.AuthToken)1 Result (org.neo4j.driver.Result)1 Value (org.neo4j.driver.Value)1 ClientException (org.neo4j.driver.exceptions.ClientException)1 DiscoveryException (org.neo4j.driver.exceptions.DiscoveryException)1 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)1 CommandException (org.neo4j.shell.exception.CommandException)1 Versions.isPasswordChangeRequiredException (org.neo4j.shell.util.Versions.isPasswordChangeRequiredException)1