use of com.hazelcast.client.AuthenticationException in project hazelcast by hazelcast.
the class ClientReAuthOperation method run.
@Override
public void run() throws Exception {
ClientEngineImpl engine = getService();
String memberUuid = getCallerUuid();
if (!engine.trySetLastAuthenticationCorrelationId(clientUuid, authCorrelationId)) {
String message = "Server already processed a newer authentication from client with uuid " + clientUuid + ". Not applying requested ownership change to " + memberUuid;
getLogger().info(message);
throw new AuthenticationException(message);
}
Set<ClientEndpoint> endpoints = engine.getEndpointManager().getEndpoints(clientUuid);
for (ClientEndpoint endpoint : endpoints) {
ClientPrincipal principal = new ClientPrincipal(clientUuid, memberUuid);
endpoint.authenticated(principal);
}
String previousMemberUuid = engine.addOwnershipMapping(clientUuid, memberUuid);
clientDisconnectOperationRun = previousMemberUuid == null;
}
use of com.hazelcast.client.AuthenticationException in project hazelcast by hazelcast.
the class ClusterListenerSupport method connect.
private boolean connect(Set<InetSocketAddress> triedAddresses) throws Exception {
final Collection<InetSocketAddress> socketAddresses = getSocketAddresses();
for (InetSocketAddress inetSocketAddress : socketAddresses) {
if (!client.getLifecycleService().isRunning()) {
if (logger.isFinestEnabled()) {
logger.finest("Giving up on retrying to connect to cluster since client is shutdown");
}
break;
}
Connection connection = null;
try {
triedAddresses.add(inetSocketAddress);
Address address = new Address(inetSocketAddress);
logger.info("Trying to connect to " + address + " as owner member");
connection = connectionManager.getOrConnect(address, true);
clientMembershipListener.listenMembershipEvents(ownerConnectionAddress);
fireConnectionEvent(LifecycleEvent.LifecycleState.CLIENT_CONNECTED);
return true;
} catch (Exception e) {
Level level = e instanceof AuthenticationException ? Level.WARNING : Level.FINEST;
logger.log(level, "Exception during initial connection to " + inetSocketAddress, e);
if (null != connection) {
connection.close("Could not connect to " + inetSocketAddress + " as owner", e);
}
}
}
return false;
}
use of com.hazelcast.client.AuthenticationException in project hazelcast by hazelcast.
the class ClientConnectionManagerImpl method authenticate.
private void authenticate(final Address target, final ClientConnection connection, final boolean asOwner, final AuthenticationFuture callback) {
SerializationService ss = client.getSerializationService();
final ClientClusterServiceImpl clusterService = (ClientClusterServiceImpl) client.getClientClusterService();
final ClientPrincipal principal = clusterService.getPrincipal();
byte serializationVersion = ((InternalSerializationService) client.getSerializationService()).getVersion();
String uuid = null;
String ownerUuid = null;
if (principal != null) {
uuid = principal.getUuid();
ownerUuid = principal.getOwnerUuid();
}
ClientMessage clientMessage = encodeAuthenticationRequest(asOwner, ss, serializationVersion, uuid, ownerUuid);
ClientInvocation clientInvocation = new ClientInvocation(client, clientMessage, connection);
ClientInvocationFuture future = clientInvocation.invokeUrgent();
if (asOwner && clientInvocation.getSendConnection() != null) {
correlationIddOfLastAuthentication.set(clientInvocation.getClientMessage().getCorrelationId());
}
future.andThen(new ExecutionCallback<ClientMessage>() {
@Override
public void onResponse(ClientMessage response) {
ClientAuthenticationCodec.ResponseParameters result = ClientAuthenticationCodec.decodeResponse(response);
AuthenticationStatus authenticationStatus = AuthenticationStatus.getById(result.status);
switch(authenticationStatus) {
case AUTHENTICATED:
connection.setConnectedServerVersion(result.serverHazelcastVersion);
connection.setRemoteEndpoint(result.address);
if (asOwner) {
if (!(correlationIddOfLastAuthentication.get() == response.getCorrelationId())) {
//if not same, client already gave up on this and send another authentication.
onFailure(new AuthenticationException("Owner authentication response from address " + target + " is late. Dropping the response. Principal : " + principal));
return;
}
connection.setIsAuthenticatedAsOwner();
ClientPrincipal principal = new ClientPrincipal(result.uuid, result.ownerUuid);
clusterService.setPrincipal(principal);
clusterService.setOwnerConnectionAddress(connection.getEndPoint());
logger.info("Setting " + connection + " as owner with principal " + principal);
}
onAuthenticated(target, connection);
callback.onSuccess(connection, asOwner);
break;
case CREDENTIALS_FAILED:
onFailure(new AuthenticationException("Invalid credentials! Principal: " + principal));
break;
default:
onFailure(new AuthenticationException("Authentication status code not supported. status: " + authenticationStatus));
}
}
@Override
public void onFailure(Throwable t) {
onAuthenticationFailed(target, connection, t);
callback.onFailure(t);
}
});
}
use of com.hazelcast.client.AuthenticationException in project hazelcast by hazelcast.
the class TcpClientConnectionManager method checkAuthenticationResponse.
/**
* Checks the response from the server to see if authentication needs to be continued,
* closes the connection and throws exception if the authentication needs to be cancelled.
*/
private void checkAuthenticationResponse(TcpClientConnection connection, ClientAuthenticationCodec.ResponseParameters response) {
AuthenticationStatus authenticationStatus = AuthenticationStatus.getById(response.status);
if (failoverConfigProvided && !response.failoverSupported) {
logger.warning("Cluster does not support failover. This feature is available in Hazelcast Enterprise");
authenticationStatus = NOT_ALLOWED_IN_CLUSTER;
}
switch(authenticationStatus) {
case AUTHENTICATED:
break;
case CREDENTIALS_FAILED:
AuthenticationException authException = new AuthenticationException("Authentication failed. The configured " + "cluster name on the client (see ClientConfig.setClusterName()) does not match the one configured " + "in the cluster or the credentials set in the Client security config could not be authenticated");
connection.close("Failed to authenticate connection", authException);
throw authException;
case NOT_ALLOWED_IN_CLUSTER:
ClientNotAllowedInClusterException notAllowedException = new ClientNotAllowedInClusterException("Client is not allowed in the cluster");
connection.close("Failed to authenticate connection", notAllowedException);
throw notAllowedException;
default:
AuthenticationException exception = new AuthenticationException("Authentication status code not supported. status: " + authenticationStatus);
connection.close("Failed to authenticate connection", exception);
throw exception;
}
ClientPartitionServiceImpl partitionService = (ClientPartitionServiceImpl) client.getClientPartitionService();
if (!partitionService.checkAndSetPartitionCount(response.partitionCount)) {
ClientNotAllowedInClusterException exception = new ClientNotAllowedInClusterException("Client can not work with this cluster" + " because it has a different partition count. " + "Expected partition count: " + partitionService.getPartitionCount() + ", Member partition count: " + response.partitionCount);
connection.close("Failed to authenticate connection", exception);
throw exception;
}
}
use of com.hazelcast.client.AuthenticationException in project hazelcast by hazelcast.
the class AbstractMessageTask method handleAuthenticationFailure.
private void handleAuthenticationFailure() {
Exception exception;
if (nodeEngine.isRunning()) {
String message = "Client " + endpoint + " must authenticate before any operation.";
logger.severe(message);
exception = new RetryableHazelcastException(new AuthenticationException(message));
} else {
exception = new HazelcastInstanceNotActiveException();
}
sendClientMessage(exception);
connection.close("Authentication failed. " + exception.getMessage(), null);
}
Aggregations