Search in sources :

Example 1 with AbstractCheckedFuture

use of com.google.common.util.concurrent.AbstractCheckedFuture in project drill by apache.

the class UserClient method authenticate.

private CheckedFuture<Void, SaslException> authenticate(final DrillProperties properties) {
    final Map<String, String> propertiesMap = properties.stringPropertiesAsMap();
    // Set correct QOP property and Strength based on server needs encryption or not.
    // If ChunkMode is enabled then negotiate for buffer size equal to wrapChunkSize,
    // If ChunkMode is disabled then negotiate for MAX_WRAPPED_SIZE buffer size.
    propertiesMap.putAll(SaslProperties.getSaslProperties(connection.isEncryptionEnabled(), connection.getMaxWrappedSize()));
    // use handleAuthFailure to setException
    final SettableFuture<Void> authSettable = SettableFuture.create();
    final CheckedFuture<Void, SaslException> authFuture = new AbstractCheckedFuture<Void, SaslException>(authSettable) {

        @Override
        protected SaslException mapException(Exception e) {
            if (e instanceof ExecutionException) {
                final Throwable cause = Throwables.getRootCause(e);
                if (cause instanceof SaslException) {
                    return new SaslException(String.format("Authentication failed. [Details: %s, Error %s]", connection.getEncryptionCtxtString(), cause.getMessage()), cause);
                }
            }
            return new SaslException(String.format("Authentication failed unexpectedly. [Details: %s, Error %s]", connection.getEncryptionCtxtString(), e.getMessage()), e);
        }
    };
    final AuthenticatorFactory factory;
    final String mechanismName;
    final UserGroupInformation ugi;
    final SaslClient saslClient;
    try {
        factory = getAuthenticatorFactory(properties);
        mechanismName = factory.getSimpleName();
        logger.trace("Will try to authenticate to server using {} mechanism with encryption context {}", mechanismName, connection.getEncryptionCtxtString());
        ugi = factory.createAndLoginUser(propertiesMap);
        saslClient = factory.createSaslClient(ugi, propertiesMap);
        if (saslClient == null) {
            throw new SaslException(String.format("Cannot initiate authentication using %s mechanism. Insufficient " + "credentials or selected mechanism doesn't support configured security layers?", factory.getSimpleName()));
        }
        connection.setSaslClient(saslClient);
    } catch (final IOException e) {
        authSettable.setException(e);
        return authFuture;
    }
    logger.trace("Initiating SASL exchange.");
    new AuthenticationOutcomeListener<>(this, connection, RpcType.SASL_MESSAGE, ugi, new RpcOutcomeListener<Void>() {

        @Override
        public void failed(RpcException ex) {
            authSettable.setException(ex);
        }

        @Override
        public void success(Void value, ByteBuf buffer) {
            authComplete = true;
            authSettable.set(null);
        }

        @Override
        public void interrupted(InterruptedException e) {
            authSettable.setException(e);
        }
    }).initiate(mechanismName);
    return authFuture;
}
Also used : IOException(java.io.IOException) AbstractCheckedFuture(com.google.common.util.concurrent.AbstractCheckedFuture) SaslException(javax.security.sasl.SaslException) AuthenticatorFactory(org.apache.drill.exec.rpc.security.AuthenticatorFactory) ByteBuf(io.netty.buffer.ByteBuf) RpcException(org.apache.drill.exec.rpc.RpcException) SaslException(javax.security.sasl.SaslException) NonTransientRpcException(org.apache.drill.exec.rpc.NonTransientRpcException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SaslClient(javax.security.sasl.SaslClient) RpcException(org.apache.drill.exec.rpc.RpcException) NonTransientRpcException(org.apache.drill.exec.rpc.NonTransientRpcException) ExecutionException(java.util.concurrent.ExecutionException) UserGroupInformation(org.apache.hadoop.security.UserGroupInformation) RpcOutcomeListener(org.apache.drill.exec.rpc.RpcOutcomeListener)

Example 2 with AbstractCheckedFuture

use of com.google.common.util.concurrent.AbstractCheckedFuture in project drill by apache.

the class UserClient method connect.

private CheckedFuture<Void, RpcException> connect(final UserToBitHandshake handshake, final DrillbitEndpoint endpoint) {
    final SettableFuture<Void> connectionSettable = SettableFuture.create();
    final CheckedFuture<Void, RpcException> connectionFuture = new AbstractCheckedFuture<Void, RpcException>(connectionSettable) {

        @Override
        protected RpcException mapException(Exception e) {
            return RpcException.mapException(e);
        }
    };
    final RpcConnectionHandler<UserToBitConnection> connectionHandler = new RpcConnectionHandler<UserToBitConnection>() {

        @Override
        public void connectionSucceeded(UserToBitConnection connection) {
            connectionSettable.set(null);
        }

        @Override
        public void connectionFailed(FailureType type, Throwable t) {
            connectionSettable.setException(new RpcException(String.format("%s : %s", type.name(), t.getMessage()), t));
        }
    };
    connectAsClient(queryResultHandler.getWrappedConnectionHandler(connectionHandler), handshake, endpoint.getAddress(), endpoint.getUserPort());
    return connectionFuture;
}
Also used : RpcException(org.apache.drill.exec.rpc.RpcException) NonTransientRpcException(org.apache.drill.exec.rpc.NonTransientRpcException) AbstractCheckedFuture(com.google.common.util.concurrent.AbstractCheckedFuture) RpcConnectionHandler(org.apache.drill.exec.rpc.RpcConnectionHandler) RpcException(org.apache.drill.exec.rpc.RpcException) SaslException(javax.security.sasl.SaslException) NonTransientRpcException(org.apache.drill.exec.rpc.NonTransientRpcException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

AbstractCheckedFuture (com.google.common.util.concurrent.AbstractCheckedFuture)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 SaslException (javax.security.sasl.SaslException)2 NonTransientRpcException (org.apache.drill.exec.rpc.NonTransientRpcException)2 RpcException (org.apache.drill.exec.rpc.RpcException)2 ByteBuf (io.netty.buffer.ByteBuf)1 SaslClient (javax.security.sasl.SaslClient)1 RpcConnectionHandler (org.apache.drill.exec.rpc.RpcConnectionHandler)1 RpcOutcomeListener (org.apache.drill.exec.rpc.RpcOutcomeListener)1 AuthenticatorFactory (org.apache.drill.exec.rpc.security.AuthenticatorFactory)1 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)1