Search in sources :

Example 1 with FailureMessage

use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.

the class AuthenticationIT method shouldFailDifferentlyIfTooManyFailedAuthAttempts.

@ParameterizedTest(name = "{displayName} {2}")
@MethodSource("argumentsProvider")
public void shouldFailDifferentlyIfTooManyFailedAuthAttempts(Class<? extends TransportConnection> connectionClass, Neo4jPack neo4jPack, String name) throws Exception {
    initParameters(connectionClass, neo4jPack, name);
    // Given
    final long timeout = System.currentTimeMillis() + 60_000;
    FailureMessage failureMessage = null;
    // When
    while (failureMessage == null) {
        if (System.currentTimeMillis() > timeout) {
            fail("Timed out waiting for the authentication failure to occur.");
        }
        ExecutorService executor = Executors.newFixedThreadPool(10);
        // Fire up some parallel connections that all send wrong authentication tokens
        List<CompletableFuture<FailureMessage>> futures = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            futures.add(CompletableFuture.supplyAsync(this::collectAuthFailureOnFailedAuth, executor));
        }
        try {
            // Wait for all tasks to complete
            CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(30, SECONDS);
            // We want at least one of the futures to fail with our expected code
            for (int i = 0; i < futures.size(); i++) {
                FailureMessage recordedMessage = futures.get(i).get();
                if (recordedMessage != null) {
                    failureMessage = recordedMessage;
                    break;
                }
            }
        } catch (TimeoutException ex) {
        // if jobs did not complete, let's try again
        // do nothing
        } finally {
            executor.shutdown();
        }
    }
    assertThat(failureMessage.status()).isEqualTo(Status.Security.AuthenticationRateLimit);
    assertThat(failureMessage.message()).contains("The client has provided incorrect authentication details too many times in a row.");
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) TimeoutException(java.util.concurrent.TimeoutException) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with FailureMessage

use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.

the class FailureMessageEncoderTest method shouldEncodeFailureMessage.

@Test
void shouldEncodeFailureMessage() throws Throwable {
    // Given
    Neo4jPack.Packer packer = mock(Neo4jPack.Packer.class);
    Log log = mock(Log.class);
    FailureMessageEncoder encoder = new FailureMessageEncoder(log);
    // When
    encoder.encode(packer, new FailureMessage(Status.General.UnknownError, "I am an error message"));
    // Then
    verify(log, never()).debug(anyString(), any(FailureMessage.class));
    verify(packer).packStructHeader(anyInt(), eq(FailureMessage.SIGNATURE));
    verify(packer).packMapHeader(2);
    verify(packer).pack("code");
    verify(packer).pack("message");
}
Also used : Log(org.neo4j.logging.Log) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) FatalFailureMessage(org.neo4j.bolt.v3.messaging.response.FatalFailureMessage) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) Test(org.junit.jupiter.api.Test)

Example 3 with FailureMessage

use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.

the class BoltResponseMessageReader method read.

public void read(BoltResponseMessageWriter messageWriter) throws IOException {
    try {
        unpacker.unpackStructHeader();
        final int signature = unpacker.unpackStructSignature();
        BoltResponseMessage message = BoltResponseMessage.withSignature(signature);
        try {
            switch(message) {
                case SUCCESS:
                    MapValue successMetadata = unpacker.unpackMap();
                    messageWriter.write(new SuccessMessage(successMetadata));
                    break;
                case RECORD:
                    long length = unpacker.unpackListHeader();
                    final AnyValue[] fields = new AnyValue[(int) length];
                    for (int i = 0; i < length; i++) {
                        fields[i] = unpacker.unpack();
                    }
                    messageWriter.write(new RecordMessage(fields));
                    break;
                case IGNORED:
                    messageWriter.write(IgnoredMessage.IGNORED_MESSAGE);
                    break;
                case FAILURE:
                    MapValue failureMetadata = unpacker.unpackMap();
                    String code = failureMetadata.containsKey("code") ? ((StringValue) failureMetadata.get("code")).stringValue() : Status.General.UnknownError.name();
                    AnyValue msgValue = failureMetadata.get("message");
                    String msg = msgValue != NO_VALUE ? ((StringValue) msgValue).stringValue() : "<No message supplied>";
                    messageWriter.write(new FailureMessage(Neo4jError.codeFromString(code), msg));
                    break;
                default:
                    throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not supported.", Integer.toHexString(signature)));
            }
        } catch (IllegalArgumentException e) {
            throw new BoltIOException(Status.Request.InvalidFormat, String.format("Message 0x%s is not a valid message signature.", Integer.toHexString(signature)));
        }
    } catch (PackStream.PackStreamException e) {
        throw new BoltIOException(Status.Request.InvalidFormat, String.format("Unable to read message type. Error was: %s.", e.getMessage()), e);
    }
}
Also used : MapValue(org.neo4j.values.virtual.MapValue) PackStream(org.neo4j.bolt.packstream.PackStream) SuccessMessage(org.neo4j.bolt.v3.messaging.response.SuccessMessage) AnyValue(org.neo4j.values.AnyValue) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage)

Example 4 with FailureMessage

use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.

the class BoltResponseMessageTest method shouldHandleCommonMessages.

@Test
void shouldHandleCommonMessages() throws Throwable {
    assertSerializes(new RecordMessage(new AnyValue[] { longValue(1L), stringValue("b"), longValue(2L) }));
    assertSerializes(new SuccessMessage(VirtualValues.EMPTY_MAP));
    assertSerializes(new FailureMessage(Status.General.UnknownError, "Err"));
    assertSerializes(IGNORED_MESSAGE);
}
Also used : SuccessMessage(org.neo4j.bolt.v3.messaging.response.SuccessMessage) AnyValue(org.neo4j.values.AnyValue) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage) Test(org.junit.jupiter.api.Test)

Example 5 with FailureMessage

use of org.neo4j.bolt.v3.messaging.response.FailureMessage in project neo4j by neo4j.

the class BoltResponseMessageWriterV3Test method shouldWriteFailureMessage.

@Test
void shouldWriteFailureMessage() throws Exception {
    PackOutput output = mock(PackOutput.class);
    Neo4jPack.Packer packer = mock(Neo4jPack.Packer.class);
    var writer = newWriter(output, packer);
    Status.Transaction errorStatus = Status.Transaction.DeadlockDetected;
    String errorMessage = "Hi Deadlock!";
    writer.write(new FailureMessage(errorStatus, errorMessage));
    InOrder inOrder = inOrder(output, packer);
    inOrder.verify(output).beginMessage();
    inOrder.verify(packer).pack(errorStatus.code().serialize());
    inOrder.verify(packer).pack(errorMessage);
    inOrder.verify(output).messageSucceeded();
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) InOrder(org.mockito.InOrder) FailureMessage(org.neo4j.bolt.v3.messaging.response.FailureMessage) PackOutput(org.neo4j.bolt.packstream.PackOutput) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) Test(org.junit.jupiter.api.Test)

Aggregations

FailureMessage (org.neo4j.bolt.v3.messaging.response.FailureMessage)5 Test (org.junit.jupiter.api.Test)3 Neo4jPack (org.neo4j.bolt.packstream.Neo4jPack)2 RecordMessage (org.neo4j.bolt.v3.messaging.response.RecordMessage)2 SuccessMessage (org.neo4j.bolt.v3.messaging.response.SuccessMessage)2 AnyValue (org.neo4j.values.AnyValue)2 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 InOrder (org.mockito.InOrder)1 PackOutput (org.neo4j.bolt.packstream.PackOutput)1 PackStream (org.neo4j.bolt.packstream.PackStream)1 FatalFailureMessage (org.neo4j.bolt.v3.messaging.response.FatalFailureMessage)1 Status (org.neo4j.kernel.api.exceptions.Status)1 Log (org.neo4j.logging.Log)1 MapValue (org.neo4j.values.virtual.MapValue)1