use of com.couchbase.client.core.error.AuthenticationFailureException 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);
}
use of com.couchbase.client.core.error.AuthenticationFailureException in project connectors-se by Talend.
the class CouchbaseService method healthCheck.
@HealthCheck("healthCheck")
public HealthCheckStatus healthCheck(@Option("configuration.dataset.connection") final CouchbaseDataStore datastore) {
try {
openConnection(datastore);
LOG.debug(i18n.connectionOK());
return new HealthCheckStatus(HealthCheckStatus.Status.OK, "Connection OK");
} catch (Exception exception) {
String message = "";
if (exception.getCause() instanceof AuthenticationFailureException) {
message = i18n.invalidPassword();
} else if (exception.getCause() instanceof RuntimeException && exception.getCause().getCause() instanceof TimeoutException) {
message = i18n.destinationUnreachable();
} else {
message = i18n.connectionKODetailed(exception.getMessage());
}
LOG.error(message, exception);
return new HealthCheckStatus(HealthCheckStatus.Status.KO, message);
} finally {
closeConnection(datastore);
}
}
use of com.couchbase.client.core.error.AuthenticationFailureException in project couchbase-jdbc-driver by couchbaselabs.
the class ConnectionHandle method clusterVersion.
/**
* Retrieves the raw cluster version as a string.
*
* @return the cluster version as a string if successful.
* @throws SQLException if fetching the cluster version failed.
*/
public String clusterVersion() throws SQLException {
CoreHttpClient client = cluster.core().httpClient(RequestTarget.manager());
CompletableFuture<CoreHttpResponse> exec = client.get(path("/pools"), CoreCommonOptions.DEFAULT).build().exec(cluster.core());
try {
JsonNode root = Mapper.decodeIntoTree(exec.get().content());
return root.get("implementationVersion").asText();
} catch (ExecutionException ex) {
if (ex.getCause() instanceof AuthenticationFailureException) {
throw authError(ex);
} else {
throw new SQLException("Failed to fetch cluster version", ex);
}
} catch (Exception e) {
throw new SQLException("Failed to fetch cluster version", e);
}
}
use of com.couchbase.client.core.error.AuthenticationFailureException 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);
}
use of com.couchbase.client.core.error.AuthenticationFailureException 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));
}
Aggregations