Example usage for javax.security.sasl Sasl SERVER_AUTH

List of usage examples for javax.security.sasl Sasl SERVER_AUTH

Introduction

In this page you can find the example usage for javax.security.sasl Sasl SERVER_AUTH.

Prototype

String SERVER_AUTH

To view the source code for javax.security.sasl Sasl SERVER_AUTH.

Click Source Link

Document

The name of a property that specifies whether the server must authenticate to the client.

Usage

From source file:backtype.storm.security.auth.kerberos.KerberosSaslTransportPlugin.java

public TTransportFactory getServerTransportFactory() throws IOException {
    // create an authentication callback handler
    CallbackHandler server_callback_handler = new ServerCallbackHandler(login_conf, storm_conf);

    // login our principal
    Subject subject = null;//from  w  w  w .  j  a v  a2  s.c om
    try {
        // specify a configuration object to be used
        Configuration.setConfiguration(login_conf);
        // now login
        Login login = new Login(AuthUtils.LOGIN_CONTEXT_SERVER, server_callback_handler);
        subject = login.getSubject();
    } catch (LoginException ex) {
        LOG.error("Server failed to login in principal:" + ex, ex);
        throw new RuntimeException(ex);
    }

    // check the credential of our principal
    if (subject.getPrivateCredentials(KerberosTicket.class).isEmpty()) {
        throw new RuntimeException("Fail to verify user principal with section \""
                + AuthUtils.LOGIN_CONTEXT_SERVER + "\" in login configuration file " + login_conf);
    }

    String principal = AuthUtils.get(login_conf, AuthUtils.LOGIN_CONTEXT_SERVER, "principal");
    LOG.debug("principal:" + principal);
    KerberosName serviceKerberosName = new KerberosName(principal);
    String serviceName = serviceKerberosName.getServiceName();
    String hostName = serviceKerberosName.getHostName();
    Map<String, String> props = new TreeMap<String, String>();
    props.put(Sasl.QOP, "auth");
    props.put(Sasl.SERVER_AUTH, "false");

    // create a transport factory that will invoke our auth callback for digest
    TSaslServerTransport.Factory factory = new TSaslServerTransport.Factory();
    factory.addServerDefinition(KERBEROS, serviceName, hostName, props, server_callback_handler);

    // create a wrap transport factory so that we could apply user credential during connections
    TUGIAssumingTransportFactory wrapFactory = new TUGIAssumingTransportFactory(factory, subject);

    LOG.info("SASL GSSAPI transport factory will be used");
    return wrapFactory;
}

From source file:backtype.storm.security.auth.kerberos.KerberosSaslTransportPlugin.java

@Override
public TTransport connect(TTransport transport, String serverHost, String asUser)
        throws TTransportException, IOException {
    // create an authentication callback handler
    ClientCallbackHandler client_callback_handler = new ClientCallbackHandler(login_conf);

    // login our user
    Login login = null;/*from   w  ww .  ja  va  2s  . c om*/
    try {
        // specify a configuration object to be used
        Configuration.setConfiguration(login_conf);
        // now login
        login = new Login(AuthUtils.LOGIN_CONTEXT_CLIENT, client_callback_handler);
    } catch (LoginException ex) {
        LOG.error("Server failed to login in principal:" + ex, ex);
        throw new RuntimeException(ex);
    }

    final Subject subject = login.getSubject();
    if (subject.getPrivateCredentials(KerberosTicket.class).isEmpty()) { // error
        throw new RuntimeException("Fail to verify user principal with section \""
                + AuthUtils.LOGIN_CONTEXT_CLIENT + "\" in login configuration file " + login_conf);
    }

    final String principal = StringUtils.isBlank(asUser) ? getPrincipal(subject) : asUser;
    String serviceName = AuthUtils.get(login_conf, AuthUtils.LOGIN_CONTEXT_CLIENT, "serviceName");
    if (serviceName == null) {
        serviceName = AuthUtils.SERVICE;
    }
    Map<String, String> props = new TreeMap<String, String>();
    props.put(Sasl.QOP, "auth");
    props.put(Sasl.SERVER_AUTH, "false");

    LOG.debug("SASL GSSAPI client transport is being established");
    final TTransport sasalTransport = new TSaslClientTransport(KERBEROS, principal, serviceName, serverHost,
            props, null, transport);

    // open Sasl transport with the login credential
    try {
        Subject.doAs(subject, new PrivilegedExceptionAction<Void>() {
            public Void run() {
                try {
                    LOG.debug("do as:" + principal);
                    sasalTransport.open();
                } catch (Exception e) {
                    LOG.error(
                            "Client failed to open SaslClientTransport to interact with a server during session initiation: "
                                    + e,
                            e);
                }
                return null;
            }
        });
    } catch (PrivilegedActionException e) {
        throw new RuntimeException(e);
    }

    return sasalTransport;
}

From source file:org.apache.ambari.view.hive.client.Connection.java

/**
 * Based on JDBC implementation of HiveConnection.createBinaryTransport
 *
 * @return transport//from  w  ww  . j  a  va 2s  .co m
 * @throws HiveClientException
 */
protected TTransport createBinaryTransport()
        throws HiveClientException, TTransportException, HiveAuthRequiredException {
    TTransport transport;
    boolean assumeSubject = Utils.HiveAuthenticationParams.AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT
            .equals(authParams.get(Utils.HiveAuthenticationParams.AUTH_KERBEROS_AUTH_TYPE));
    try {
        if (!Utils.HiveAuthenticationParams.AUTH_SIMPLE
                .equalsIgnoreCase(authParams.get(Utils.HiveAuthenticationParams.AUTH_TYPE))) {
            // If Kerberos
            Map<String, String> saslProps = new HashMap<String, String>();
            SaslQOP saslQOP = SaslQOP.AUTH;
            if (authParams.containsKey(Utils.HiveAuthenticationParams.AUTH_PRINCIPAL)) {
                if (authParams.containsKey(Utils.HiveAuthenticationParams.AUTH_QOP)) {
                    try {
                        saslQOP = SaslQOP.fromString(authParams.get(Utils.HiveAuthenticationParams.AUTH_QOP));
                    } catch (IllegalArgumentException e) {
                        throw new HiveClientException("H040 Invalid " + Utils.HiveAuthenticationParams.AUTH_QOP
                                + " parameter. " + e.getMessage(), e);
                    }
                }
                saslProps.put(Sasl.QOP, saslQOP.toString());
                saslProps.put(Sasl.SERVER_AUTH, "true");

                Configuration conf = new Configuration();
                conf.set("hadoop.security.authentication", "kerberos");
                UserGroupInformation.setConfiguration(conf);

                transport = KerberosSaslHelper.getKerberosTransport(
                        authParams.get(Utils.HiveAuthenticationParams.AUTH_PRINCIPAL), host,
                        HiveAuthFactory.getSocketTransport(host, port, 10000), saslProps, assumeSubject);
            } else {
                // If there's a delegation token available then use token based connection
                String tokenStr = getClientDelegationToken(authParams);
                if (tokenStr != null) {
                    transport = KerberosSaslHelper.getTokenTransport(tokenStr, host,
                            HiveAuthFactory.getSocketTransport(host, port, 10000), saslProps);
                } else {
                    // we are using PLAIN Sasl connection with user/password
                    String userName = getAuthParamDefault(Utils.HiveAuthenticationParams.AUTH_USER,
                            getUsername());
                    String passwd = getPassword();
                    // Note: Thrift returns an SSL socket that is already bound to the specified host:port
                    // Therefore an open called on this would be a no-op later
                    // Hence, any TTransportException related to connecting with the peer are thrown here.
                    // Bubbling them up the call hierarchy so that a retry can happen in openTransport,
                    // if dynamic service discovery is configured.
                    if (isSslConnection()) {
                        // get SSL socket
                        String sslTrustStore = authParams.get(Utils.HiveAuthenticationParams.SSL_TRUST_STORE);
                        String sslTrustStorePassword = authParams
                                .get(Utils.HiveAuthenticationParams.SSL_TRUST_STORE_PASSWORD);
                        if (sslTrustStore == null || sslTrustStore.isEmpty()) {
                            transport = HiveAuthFactory.getSSLSocket(host, port, 10000);
                        } else {
                            transport = HiveAuthFactory.getSSLSocket(host, port, 10000, sslTrustStore,
                                    sslTrustStorePassword);
                        }
                    } else {
                        // get non-SSL socket transport
                        transport = HiveAuthFactory.getSocketTransport(host, port, 10000);
                    }
                    // Overlay the SASL transport on top of the base socket transport (SSL or non-SSL)
                    transport = PlainSaslHelper.getPlainTransport(userName, passwd, transport);
                }
            }
        } else {
            //NOSASL
            return HiveAuthFactory.getSocketTransport(host, port, 10000);
        }
    } catch (SaslException e) {
        throw new HiveClientException(
                "H040 Could not create secure connection to " + host + ": " + e.getMessage(), e);
    }
    return transport;
}

From source file:com.udps.hive.jdbc.HiveConnection.java

/**
 * Create transport per the connection options Supported transport options
 * are: - SASL based transports over + Kerberos + Delegation token + SSL +
 * non-SSL - Raw (non-SASL) socket//from   ww  w .ja v a2s .c o  m
 *
 * Kerberos and Delegation token supports SASL QOP configurations
 */
private TTransport createBinaryTransport() throws SQLException {
    try {
        // handle secure connection if specified
        if (!HIVE_AUTH_SIMPLE.equals(sessConfMap.get(HIVE_AUTH_TYPE))) {
            // If Kerberos
            Map<String, String> saslProps = new HashMap<String, String>();
            SaslQOP saslQOP = SaslQOP.AUTH;
            if (sessConfMap.containsKey(HIVE_AUTH_PRINCIPAL)) {
                if (sessConfMap.containsKey(HIVE_AUTH_QOP)) {
                    try {
                        saslQOP = SaslQOP.fromString(sessConfMap.get(HIVE_AUTH_QOP));
                    } catch (IllegalArgumentException e) {
                        throw new SQLException("Invalid " + HIVE_AUTH_QOP + " parameter. " + e.getMessage(),
                                "42000", e);
                    }
                }
                saslProps.put(Sasl.QOP, saslQOP.toString());
                saslProps.put(Sasl.SERVER_AUTH, "true");
                boolean assumeSubject = HIVE_AUTH_KERBEROS_AUTH_TYPE_FROM_SUBJECT
                        .equals(sessConfMap.get(HIVE_AUTH_KERBEROS_AUTH_TYPE));
                transport = KerberosSaslHelper.getKerberosTransport(sessConfMap.get(HIVE_AUTH_PRINCIPAL), host,
                        HiveAuthFactory.getSocketTransport(host, port, loginTimeout), saslProps, assumeSubject);
            } else {
                // If there's a delegation token available then use token
                // based connection
                String tokenStr = getClientDelegationToken(sessConfMap);
                if (tokenStr != null) {
                    transport = KerberosSaslHelper.getTokenTransport(tokenStr, host,
                            HiveAuthFactory.getSocketTransport(host, port, loginTimeout), saslProps);
                } else {
                    // we are using PLAIN Sasl connection with user/password
                    String userName = getUserName();
                    String passwd = getPassword();
                    if (isSslConnection()) {
                        // get SSL socket
                        String sslTrustStore = sessConfMap.get(HIVE_SSL_TRUST_STORE);
                        String sslTrustStorePassword = sessConfMap.get(HIVE_SSL_TRUST_STORE_PASSWORD);
                        if (sslTrustStore == null || sslTrustStore.isEmpty()) {
                            transport = HiveAuthFactory.getSSLSocket(host, port, loginTimeout);
                        } else {
                            transport = HiveAuthFactory.getSSLSocket(host, port, loginTimeout, sslTrustStore,
                                    sslTrustStorePassword);
                        }
                    } else {
                        // get non-SSL socket transport
                        transport = HiveAuthFactory.getSocketTransport(host, port, loginTimeout);
                    }
                    // Overlay the SASL transport on top of the base socket
                    // transport (SSL or non-SSL)
                    transport = PlainSaslHelper.getPlainTransport(userName, passwd, transport);
                }
            }
        } else {
            // Raw socket connection (non-sasl)
            transport = HiveAuthFactory.getSocketTransport(host, port, loginTimeout);
        }
    } catch (SaslException e) {
        throw new SQLException("Could not create secure connection to " + jdbcURI + ": " + e.getMessage(),
                " 08S01", e);
    } catch (TTransportException e) {
        throw new SQLException("Could not create connection to " + jdbcURI + ": " + e.getMessage(), " 08S01",
                e);
    }
    return transport;
}

From source file:org.apache.directory.studio.connection.core.io.jndi.JNDIConnectionWrapper.java

private void doBind(final StudioProgressMonitor monitor) throws NamingException {
    if (context != null && isConnected) {
        // setup authentication methdod
        authMethod = AUTHMETHOD_NONE;//from  w w  w.jav a 2s. c  o m
        if (connection.getConnectionParameter()
                .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SIMPLE) {
            authMethod = AUTHMETHOD_SIMPLE;
        } else if (connection.getConnectionParameter()
                .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SASL_DIGEST_MD5) {
            authMethod = AUTHMETHOD_DIGEST_MD5;
            saslRealm = connection.getConnectionParameter().getSaslRealm();
        } else if (connection.getConnectionParameter()
                .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SASL_CRAM_MD5) {
            authMethod = AUTHMETHOD_CRAM_MD5;
        } else if (connection.getConnectionParameter()
                .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SASL_GSSAPI) {
            authMethod = AUTHMETHOD_GSSAPI;
        }

        // No Authentication
        if (authMethod == AUTHMETHOD_NONE) {
            bindPrincipal = ""; //$NON-NLS-1$
            bindCredentials = ""; //$NON-NLS-1$
        } else {
            // setup credentials
            IAuthHandler authHandler = ConnectionCorePlugin.getDefault().getAuthHandler();
            if (authHandler == null) {
                NamingException namingException = new NamingException(Messages.model__no_auth_handler);
                monitor.reportError(Messages.model__no_auth_handler, namingException);
                throw namingException;
            }
            ICredentials credentials = authHandler.getCredentials(connection.getConnectionParameter());
            if (credentials == null) {
                CancelException cancelException = new CancelException();
                monitor.setCanceled(true);
                monitor.reportError(Messages.model__no_credentials, cancelException);
                throw cancelException;
            }
            if (credentials.getBindPrincipal() == null || credentials.getBindPassword() == null) {
                NamingException namingException = new NamingException(Messages.model__no_credentials);
                monitor.reportError(Messages.model__no_credentials, namingException);
                throw namingException;
            }
            bindPrincipal = credentials.getBindPrincipal();
            bindCredentials = credentials.getBindPassword();
        }

        InnerRunnable runnable = new InnerRunnable() {
            public void run() {
                try {
                    context.removeFromEnvironment(Context.SECURITY_AUTHENTICATION);
                    context.removeFromEnvironment(Context.SECURITY_PRINCIPAL);
                    context.removeFromEnvironment(Context.SECURITY_CREDENTIALS);
                    context.removeFromEnvironment(JAVA_NAMING_SECURITY_SASL_REALM);

                    context.addToEnvironment(Context.SECURITY_AUTHENTICATION, authMethod);

                    // SASL options
                    if (connection.getConnectionParameter()
                            .getAuthMethod() == AuthenticationMethod.SASL_CRAM_MD5
                            || connection.getConnectionParameter()
                                    .getAuthMethod() == AuthenticationMethod.SASL_DIGEST_MD5
                            || connection.getConnectionParameter()
                                    .getAuthMethod() == AuthenticationMethod.SASL_GSSAPI) {
                        // Request quality of protection
                        switch (connection.getConnectionParameter().getSaslQop()) {
                        case AUTH:
                            context.addToEnvironment(Sasl.QOP, SaslQoP.AUTH.getValue());
                            break;
                        case AUTH_INT:
                            context.addToEnvironment(Sasl.QOP, SaslQoP.AUTH_INT.getValue());
                            break;
                        case AUTH_CONF:
                            context.addToEnvironment(Sasl.QOP, SaslQoP.AUTH_CONF.getValue());
                            break;
                        }

                        // Request mutual authentication
                        if (connection.getConnectionParameter().isSaslMutualAuthentication()) {
                            context.addToEnvironment(Sasl.SERVER_AUTH, "true"); //$NON-NLS-1$
                        } else {
                            context.removeFromEnvironment(Sasl.SERVER_AUTH);
                        }

                        // Request cryptographic protection strength
                        switch (connection.getConnectionParameter().getSaslSecurityStrength()) {
                        case HIGH:
                            context.addToEnvironment(Sasl.STRENGTH, SaslSecurityStrength.HIGH.getValue());
                            break;
                        case MEDIUM:
                            context.addToEnvironment(Sasl.STRENGTH, SaslSecurityStrength.MEDIUM.getValue());
                            break;
                        case LOW:
                            context.addToEnvironment(Sasl.STRENGTH, SaslSecurityStrength.LOW.getValue());
                            break;
                        }
                    }

                    // Bind
                    if (connection.getConnectionParameter()
                            .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SASL_GSSAPI) {
                        // GSSAPI
                        doGssapiBind(this);
                    } else {
                        // no GSSAPI
                        context.addToEnvironment(Context.SECURITY_PRINCIPAL, bindPrincipal);
                        context.addToEnvironment(Context.SECURITY_CREDENTIALS, bindCredentials);

                        if (connection.getConnectionParameter()
                                .getAuthMethod() == ConnectionParameter.AuthenticationMethod.SASL_DIGEST_MD5
                                && StringUtils.isNotEmpty(saslRealm)) {
                            context.addToEnvironment(JAVA_NAMING_SECURITY_SASL_REALM, saslRealm);
                        }

                        context.reconnect(context.getConnectControls());
                    }
                } catch (NamingException ne) {
                    namingException = ne;
                }
            }
        };

        runAndMonitor(runnable, monitor);

        if (runnable.getException() != null) {
            throw runnable.getException();
        } else if (context != null) {
            // all OK
        } else {
            throw new NamingException("???"); //$NON-NLS-1$
        }
    } else {
        throw new NamingException(NO_CONNECTION);
    }
}

From source file:org.apache.hadoop.hbase.io.asyncfs.FanOutOneBlockAsyncDFSOutputSaslHelper.java

private static Map<String, String> createSaslPropertiesForEncryption(String encryptionAlgorithm) {
    Map<String, String> saslProps = Maps.newHashMapWithExpectedSize(3);
    saslProps.put(Sasl.QOP, QualityOfProtection.PRIVACY.getSaslQop());
    saslProps.put(Sasl.SERVER_AUTH, "true");
    saslProps.put("com.sun.security.sasl.digest.cipher", encryptionAlgorithm);
    return saslProps;
}

From source file:org.apache.hadoop.hbase.security.SaslUtil.java

static void initSaslProperties(String rpcProtection) {
    QualityOfProtection saslQOP = QualityOfProtection.AUTHENTICATION;
    if (QualityOfProtection.INTEGRITY.name().toLowerCase().equals(rpcProtection)) {
        saslQOP = QualityOfProtection.INTEGRITY;
    } else if (QualityOfProtection.PRIVACY.name().toLowerCase().equals(rpcProtection)) {
        saslQOP = QualityOfProtection.PRIVACY;
    }/*  ww w.j  av  a2 s  .c o m*/

    SaslUtil.SASL_PROPS.put(Sasl.QOP, saslQOP.getSaslQop());
    SaslUtil.SASL_PROPS.put(Sasl.SERVER_AUTH, "true");
}

From source file:org.apache.hadoop.hdfs.protocol.datatransfer.sasl.DataTransferSaslUtil.java

/**
 * Creates SASL properties required for an encrypted SASL negotiation.
 *
 * @param encryptionAlgorithm to use for SASL negotation
 * @return properties of encrypted SASL negotiation
 *//*ww  w .  j av  a  2  s .  co  m*/
public static Map<String, String> createSaslPropertiesForEncryption(String encryptionAlgorithm) {
    Map<String, String> saslProps = Maps.newHashMapWithExpectedSize(3);
    saslProps.put(Sasl.QOP, QualityOfProtection.PRIVACY.getSaslQop());
    saslProps.put(Sasl.SERVER_AUTH, "true");
    saslProps.put("com.sun.security.sasl.digest.cipher", encryptionAlgorithm);
    return saslProps;
}

From source file:org.apache.hadoop.security.SaslRpcServer.java

public static void init(Configuration conf) {
    QualityOfProtection saslQOP = QualityOfProtection.AUTHENTICATION;
    String rpcProtection = conf.get("hadoop.rpc.protection",
            QualityOfProtection.AUTHENTICATION.name().toLowerCase());
    if (QualityOfProtection.INTEGRITY.name().toLowerCase().equals(rpcProtection)) {
        saslQOP = QualityOfProtection.INTEGRITY;
    } else if (QualityOfProtection.PRIVACY.name().toLowerCase().equals(rpcProtection)) {
        saslQOP = QualityOfProtection.PRIVACY;
    }/*from   ww w . j a  v  a2s  .  c om*/

    SASL_PROPS.put(Sasl.QOP, saslQOP.getSaslQop());
    SASL_PROPS.put(Sasl.SERVER_AUTH, "true");
}