Search in sources :

Example 1 with AuthenticationException

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;
}
Also used : ClientEngineImpl(com.hazelcast.client.impl.ClientEngineImpl) AuthenticationException(com.hazelcast.client.AuthenticationException) ClientEndpoint(com.hazelcast.client.ClientEndpoint) ClientPrincipal(com.hazelcast.client.impl.client.ClientPrincipal)

Example 2 with AuthenticationException

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;
}
Also used : Address(com.hazelcast.nio.Address) InetSocketAddress(java.net.InetSocketAddress) AuthenticationException(com.hazelcast.client.AuthenticationException) InetSocketAddress(java.net.InetSocketAddress) Connection(com.hazelcast.nio.Connection) Level(java.util.logging.Level) AuthenticationException(com.hazelcast.client.AuthenticationException) TargetDisconnectedException(com.hazelcast.spi.exception.TargetDisconnectedException)

Example 3 with AuthenticationException

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);
        }
    });
}
Also used : AuthenticationException(com.hazelcast.client.AuthenticationException) SerializationService(com.hazelcast.spi.serialization.SerializationService) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientPrincipal(com.hazelcast.client.impl.client.ClientPrincipal) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) ClientClusterServiceImpl(com.hazelcast.client.spi.impl.ClientClusterServiceImpl)

Example 4 with AuthenticationException

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;
    }
}
Also used : AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) ClientNotAllowedInClusterException(com.hazelcast.client.ClientNotAllowedInClusterException) AuthenticationException(com.hazelcast.client.AuthenticationException) ClientPartitionServiceImpl(com.hazelcast.client.impl.spi.impl.ClientPartitionServiceImpl)

Example 5 with AuthenticationException

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);
}
Also used : HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) RetryableHazelcastException(com.hazelcast.spi.exception.RetryableHazelcastException) AuthenticationException(com.hazelcast.client.AuthenticationException) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) RetryableHazelcastException(com.hazelcast.spi.exception.RetryableHazelcastException) MemberLeftException(com.hazelcast.core.MemberLeftException) AuthenticationException(com.hazelcast.client.AuthenticationException) AccessControlException(java.security.AccessControlException)

Aggregations

AuthenticationException (com.hazelcast.client.AuthenticationException)5 ClientPrincipal (com.hazelcast.client.impl.client.ClientPrincipal)2 AuthenticationStatus (com.hazelcast.client.impl.protocol.AuthenticationStatus)2 ClientEndpoint (com.hazelcast.client.ClientEndpoint)1 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)1 ClientEngineImpl (com.hazelcast.client.impl.ClientEngineImpl)1 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)1 ClientPartitionServiceImpl (com.hazelcast.client.impl.spi.impl.ClientPartitionServiceImpl)1 ClientClusterServiceImpl (com.hazelcast.client.spi.impl.ClientClusterServiceImpl)1 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)1 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)1 MemberLeftException (com.hazelcast.core.MemberLeftException)1 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 Address (com.hazelcast.nio.Address)1 Connection (com.hazelcast.nio.Connection)1 RetryableHazelcastException (com.hazelcast.spi.exception.RetryableHazelcastException)1 TargetDisconnectedException (com.hazelcast.spi.exception.TargetDisconnectedException)1 SerializationService (com.hazelcast.spi.serialization.SerializationService)1 InetSocketAddress (java.net.InetSocketAddress)1