Search in sources :

Example 1 with SaslMechanismsListedEvent

use of com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandlerTest method decodesSuccessfulSaslMechsList.

@Test
void decodesSuccessfulSaslMechsList() {
    SaslListMechanismsHandler handler = new SaslListMechanismsHandler(endpointContext);
    channel.pipeline().addLast(handler);
    assertEquals(handler, channel.pipeline().get(SaslListMechanismsHandler.class));
    ChannelFuture connectFuture = channel.connect(new InetSocketAddress("1.2.3.4", 1234));
    assertFalse(connectFuture.isDone());
    channel.pipeline().fireChannelActive();
    channel.runPendingTasks();
    ByteBuf writtenRequest = channel.readOutbound();
    verifyRequest(writtenRequest, MemcacheProtocol.Opcode.SASL_LIST_MECHS.opcode(), false, false, false);
    assertNotNull(channel.pipeline().get(SaslListMechanismsHandler.class));
    ReferenceCountUtil.release(writtenRequest);
    ByteBuf response = decodeHexDump(readResource("success_sasl_list_mechs_response.txt", ErrorMapLoadingHandlerTest.class));
    channel.writeInbound(response);
    channel.runPendingTasks();
    assertTrue(connectFuture.isSuccess());
    Set<SaslMechanism> saslMechanisms = channel.attr(ChannelAttributes.SASL_MECHS_KEY).get();
    assertEquals(saslMechanisms, EnumSet.of(SaslMechanism.PLAIN, SaslMechanism.SCRAM_SHA1, SaslMechanism.SCRAM_SHA256, SaslMechanism.SCRAM_SHA512));
    assertTrue(eventBus.publishedEvents().get(0) instanceof SaslMechanismsListedEvent);
}
Also used : ChannelFuture(com.couchbase.client.core.deps.io.netty.channel.ChannelFuture) SaslMechanismsListedEvent(com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent) InetSocketAddress(java.net.InetSocketAddress) SaslMechanism(com.couchbase.client.core.env.SaslMechanism) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 2 with SaslMechanismsListedEvent

use of com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandler method channelRead.

/**
 * As soon as we get a response, turn it into a list of SASL mechanisms the server supports.
 * <p>
 * If the server responds with an empty list this is an issue and as a result we need to fail the connection
 * immediately.
 *
 * @param ctx the {@link ChannelHandlerContext} for which the channel read operation is made.
 * @param msg the incoming msg that needs to be parsed.
 */
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    if (msg instanceof ByteBuf) {
        Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
        if (successful((ByteBuf) msg)) {
            String[] rawMechansisms = body((ByteBuf) msg).orElse(Unpooled.EMPTY_BUFFER).toString(UTF_8).split(" ");
            boolean isEmpty = rawMechansisms.length == 1 && rawMechansisms[0].isEmpty();
            if (rawMechansisms.length > 0 && !isEmpty) {
                final Set<SaslMechanism> serverMechanisms = decodeMechanisms(rawMechansisms);
                ioContext.environment().eventBus().publish(new SaslMechanismsListedEvent(ioContext, serverMechanisms, latency.orElse(Duration.ZERO)));
                ctx.channel().attr(ChannelAttributes.SASL_MECHS_KEY).set(serverMechanisms);
                interceptedConnectPromise.trySuccess();
                ctx.pipeline().remove(this);
            } else {
                failConnection("Received empty mechanism list from server", status((ByteBuf) msg), latency);
            }
        } else {
            failConnection("Received non-success status from server", status((ByteBuf) msg), latency);
        }
    } else {
        interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response " + "type on channel read, this is a bug - please report. " + msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : SaslMechanismsListedEvent(com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) SaslMechanism(com.couchbase.client.core.env.SaslMechanism) Duration(java.time.Duration) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Aggregations

SaslMechanismsListedEvent (com.couchbase.client.core.cnc.events.io.SaslMechanismsListedEvent)2 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)2 SaslMechanism (com.couchbase.client.core.env.SaslMechanism)2 ChannelFuture (com.couchbase.client.core.deps.io.netty.channel.ChannelFuture)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 InetSocketAddress (java.net.InetSocketAddress)1 Duration (java.time.Duration)1 Test (org.junit.jupiter.api.Test)1