Search in sources :

Example 1 with KeyValueIoErrorContext

use of com.couchbase.client.core.error.context.KeyValueIoErrorContext 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)

Example 2 with KeyValueIoErrorContext

use of com.couchbase.client.core.error.context.KeyValueIoErrorContext in project couchbase-jvm-clients by couchbase.

the class SelectBucketHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) {
    Optional<Duration> latency = ConnectTimings.stop(ctx.channel(), this.getClass(), false);
    if (msg instanceof ByteBuf) {
        short status = status((ByteBuf) msg);
        if (status == MemcacheProtocol.Status.SUCCESS.status()) {
            endpointContext.environment().eventBus().publish(new SelectBucketCompletedEvent(latency.orElse(Duration.ZERO), ioContext, bucketName));
            interceptedConnectPromise.trySuccess();
            ctx.pipeline().remove(this);
            ctx.fireChannelActive();
        } else if (status == MemcacheProtocol.Status.ACCESS_ERROR.status()) {
            // If the promise has been already completed with an error from a downstream handler, there is no
            // need to WARN because it will just spam and reduce the visibility of the actual source error.
            Event.Severity severity = interceptedConnectPromise.isDone() && !interceptedConnectPromise.isSuccess() ? Event.Severity.DEBUG : Event.Severity.WARN;
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(severity, ioContext, status));
            interceptedConnectPromise.tryFailure(new AuthenticationFailureException("Either the bucket with name \"" + redactMeta(bucketName) + "\" is not present or the user does not have " + "the right privileges to access it", new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, null), null));
        } else if (status == MemcacheProtocol.Status.NOT_FOUND.status()) {
            // NOT_FOUND severity is lowered to debug because it shows up when bootstrapping against
            // a node in a MDS setup without the kv service / bucket enabled. If the bucket is legit not
            // present, we'd get the access error from above (which is an error).
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.DEBUG, ioContext, status));
            interceptedConnectPromise.tryFailure(BucketNotFoundException.forBucket(bucketName));
        } else {
            endpointContext.environment().eventBus().publish(new SelectBucketFailedEvent(Event.Severity.ERROR, ioContext, status));
            interceptedConnectPromise.tryFailure(new CouchbaseException("Select bucket failed with unexpected status code 0x" + Integer.toHexString(status)));
        }
    } else {
        interceptedConnectPromise.tryFailure(new CouchbaseException("Unexpected response type on channel read, this is a bug " + "- please report." + msg));
    }
    ReferenceCountUtil.release(msg);
}
Also used : SelectBucketFailedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketFailedEvent) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) SelectBucketCompletedEvent(com.couchbase.client.core.cnc.events.io.SelectBucketCompletedEvent) Duration(java.time.Duration) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) ByteBuf(com.couchbase.client.core.deps.io.netty.buffer.ByteBuf)

Example 3 with KeyValueIoErrorContext

use of com.couchbase.client.core.error.context.KeyValueIoErrorContext in project couchbase-jvm-clients by couchbase.

the class SaslListMechanismsHandler method failConnection.

/**
 * Helper method to fail the connection due to an unsuccessful sasl list mechs event.
 *
 * @param message the message that should be part of the error
 * @param status the status code from the memcached response
 * @param duration the duration how long it took overall
 */
private void failConnection(final String message, final short status, final Optional<Duration> duration) {
    KeyValueIoErrorContext errorContext = new KeyValueIoErrorContext(MemcacheProtocol.decodeStatus(status), endpointContext, null);
    ioContext.environment().eventBus().publish(new SaslMechanismsListingFailedEvent(duration.orElse(Duration.ZERO), errorContext, message));
    interceptedConnectPromise.tryFailure(new AuthenticationFailureException(message, errorContext, null));
}
Also used : KeyValueIoErrorContext(com.couchbase.client.core.error.context.KeyValueIoErrorContext) SaslMechanismsListingFailedEvent(com.couchbase.client.core.cnc.events.io.SaslMechanismsListingFailedEvent) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException)

Aggregations

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