Search in sources :

Example 1 with SaslAuthenticationFailedEvent

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

the class SaslAuthenticationHandler method failConnect.

/**
 * Refactored method which is called from many places to fail the connection
 * process because of an issue during SASL auth.
 *
 * <p>Usually errors during auth are very problematic and as a result we cannot continue
 * with this channel connect attempt.</p>
 *
 * @param ctx the channel context.
 */
private void failConnect(final ChannelHandlerContext ctx, final String message, final ByteBuf lastPacket, final Throwable cause, final short status) {
    Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
    byte[] packetCopy = Bytes.EMPTY_BYTE_ARRAY;
    Map<String, Object> serverContext = null;
    if (lastPacket != null) {
        if (MemcacheProtocol.verifyResponse(lastPacket)) {
            // This is a proper response, try to extract server context
            Optional<ByteBuf> body = MemcacheProtocol.body(lastPacket);
            if (body.isPresent()) {
                byte[] content = ByteBufUtil.getBytes(body.get());
                try {
                    serverContext = Mapper.decodeInto(content, new TypeReference<Map<String, Object>>() {
                    });
                } catch (Exception ex) {
                // Ignore, no displayable content
                }
            }
        } else {
            // This is not a proper memcache response, store the raw packet for debugging purposes
            int ridx = lastPacket.readerIndex();
            lastPacket.readerIndex(lastPacket.writerIndex());
            packetCopy = new byte[lastPacket.readableBytes()];
            lastPacket.readBytes(packetCopy);
            lastPacket.readerIndex(ridx);
        }
    }
    KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, serverContext);
    endpointContext.environment().eventBus().publish(new SaslAuthenticationFailedEvent(latency.orElse(Duration.ZERO), errorContext, message, packetCopy));
    interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, cause));
    ctx.pipeline().remove(this);
}
Also used : Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) TimeoutException(java.util.concurrent.TimeoutException) SaslException(javax.security.sasl.SaslException) SaslAuthenticationFailedEvent(com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) TypeReference(com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)

Aggregations

SaslAuthenticationFailedEvent (com.couchbase.client.core.cnc.events.io.SaslAuthenticationFailedEvent)1 TypeReference (com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)1 ByteBuf (com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)1 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)1 KeyValueIoErrorContext (com.couchbase.client.core.error.context.KeyValueIoErrorContext)1 Duration (java.time.Duration)1 TimeoutException (java.util.concurrent.TimeoutException)1 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)1 SaslException (javax.security.sasl.SaslException)1