Search in sources :

Example 1 with AuthenticationStatus

use of com.hazelcast.client.impl.protocol.AuthenticationStatus in project hazelcast by hazelcast.

the class AuthenticationBaseMessageTask method processMessage.

@Override
public void processMessage() throws Throwable {
    byte serializationServiceVersion = serializationService.getVersion();
    AuthenticationStatus authenticationStatus;
    if (clientSerializationVersion != serializationServiceVersion) {
        sendClientMessage(prepareSerializationVersionMismatchClientMessage());
        return;
    }
    authenticationStatus = authenticate();
    if (authenticationStatus == AuthenticationStatus.CREDENTIALS_FAILED) {
        sendClientMessage(prepareUnauthenticatedClientMessage());
        return;
    }
    if (!isOwnerConnection()) {
        prepareAndSendResponse(authenticationStatus);
        return;
    }
    String uuid = getUuid();
    String localMemberUUID = clientEngine.getThisUuid();
    principal = new ClientPrincipal(uuid, localMemberUUID);
    super.processMessage();
}
Also used : AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) ClientPrincipal(com.hazelcast.client.impl.client.ClientPrincipal)

Example 2 with AuthenticationStatus

use of com.hazelcast.client.impl.protocol.AuthenticationStatus in project hazelcast by hazelcast.

the class AuthenticationBaseMessageTask method authenticate.

private AuthenticationStatus authenticate() {
    ILogger logger = clientEngine.getLogger(getClass());
    AuthenticationStatus status;
    if (credentials == null) {
        status = AuthenticationStatus.CREDENTIALS_FAILED;
        logger.severe("Could not retrieve Credentials object!");
    } else if (clientEngine.getSecurityContext() != null) {
        status = authenticate(clientEngine.getSecurityContext());
    } else if (credentials instanceof UsernamePasswordCredentials) {
        UsernamePasswordCredentials usernamePasswordCredentials = (UsernamePasswordCredentials) credentials;
        status = authenticate(usernamePasswordCredentials);
    } else {
        status = AuthenticationStatus.CREDENTIALS_FAILED;
        logger.severe("Hazelcast security is disabled.\nUsernamePasswordCredentials or cluster " + "group-name and group-password should be used for authentication!\n" + "Current credentials type is: " + credentials.getClass().getName());
    }
    return status;
}
Also used : AuthenticationStatus(com.hazelcast.client.impl.protocol.AuthenticationStatus) ILogger(com.hazelcast.logging.ILogger) UsernamePasswordCredentials(com.hazelcast.security.UsernamePasswordCredentials)

Example 3 with AuthenticationStatus

use of com.hazelcast.client.impl.protocol.AuthenticationStatus 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 AuthenticationStatus

use of com.hazelcast.client.impl.protocol.AuthenticationStatus 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)

Aggregations

AuthenticationStatus (com.hazelcast.client.impl.protocol.AuthenticationStatus)4 AuthenticationException (com.hazelcast.client.AuthenticationException)2 ClientPrincipal (com.hazelcast.client.impl.client.ClientPrincipal)2 ClientNotAllowedInClusterException (com.hazelcast.client.ClientNotAllowedInClusterException)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 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)1 ILogger (com.hazelcast.logging.ILogger)1 UsernamePasswordCredentials (com.hazelcast.security.UsernamePasswordCredentials)1 SerializationService (com.hazelcast.spi.serialization.SerializationService)1