Example usage for com.rabbitmq.client DefaultSaslConfig EXTERNAL

List of usage examples for com.rabbitmq.client DefaultSaslConfig EXTERNAL

Introduction

In this page you can find the example usage for com.rabbitmq.client DefaultSaslConfig EXTERNAL.

Prototype

DefaultSaslConfig EXTERNAL

To view the source code for com.rabbitmq.client DefaultSaslConfig EXTERNAL.

Click Source Link

Usage

From source file:com.hp.ov.sdk.messaging.core.RabbitMqClientConnectionFactory.java

License:Apache License

public static ConnectionFactory getConnectionFactory(final SSLContext sslContext, final RestParams params) {

    final ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(params.getHostname());
    factory.setPort(params.getAmqpPort());

    // Set Auth mechanism to "EXTERNAL" so that commonName of the client
    // certificate is mapped to AMQP user name. Hence, No need to set
    // userId/Password here.
    factory.setSaslConfig(DefaultSaslConfig.EXTERNAL);
    factory.useSslProtocol(sslContext);//from   www.  j  a  v  a  2  s  .  c  om
    factory.setAutomaticRecoveryEnabled(true);

    return factory;
}

From source file:org.apache.airavata.gfac.monitor.util.AMQPConnectionUtil.java

License:Apache License

public static Connection connect(String host, String vhost, String proxyFile) {
    Connection connection;//  w  w w . j  av a2 s.co m
    try {
        String keyPassPhrase = "test123";
        KeyStore ks = X509Helper.keyStoreFromPEM(proxyFile, keyPassPhrase);
        KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
        kmf.init(ks, keyPassPhrase.toCharArray());

        KeyStore tks = X509Helper.trustKeyStoreFromCertDir();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
        tmf.init(tks);

        SSLContext c = SSLContext.getInstance("SSLv3");
        c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(host);
        factory.setPort(5671);
        factory.useSslProtocol(c);
        factory.setVirtualHost(vhost);
        factory.setSaslConfig(DefaultSaslConfig.EXTERNAL);

        connection = factory.newConnection();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return connection;
}

From source file:org.apache.nifi.amqp.processors.AbstractAMQPProcessor.java

License:Apache License

/**
 * Creates {@link Connection} to AMQP system.
 *//*from  ww  w  . j a  v a2 s  .co  m*/
private Connection createConnection(ProcessContext context) {
    ConnectionFactory cf = new ConnectionFactory();
    cf.setHost(context.getProperty(HOST).getValue());
    cf.setPort(Integer.parseInt(context.getProperty(PORT).getValue()));
    cf.setUsername(context.getProperty(USER).getValue());
    cf.setPassword(context.getProperty(PASSWORD).getValue());
    String vHost = context.getProperty(V_HOST).getValue();
    if (vHost != null) {
        cf.setVirtualHost(vHost);
    }

    // handles TLS/SSL aspects
    final Boolean useCertAuthentication = context.getProperty(USE_CERT_AUTHENTICATION).asBoolean();
    final SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE)
            .asControllerService(SSLContextService.class);
    // if the property to use cert authentication is set but the SSL service hasn't been configured, throw an exception.
    if (useCertAuthentication && sslService == null) {
        throw new ProviderCreationException("This processor is configured to use cert authentication, "
                + "but the SSL Context Service hasn't been configured. You need to configure the SSL Context Service.");
    }
    final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();

    if (sslService != null) {
        final SSLContextService.ClientAuth clientAuth;
        if (StringUtils.isBlank(rawClientAuth)) {
            clientAuth = SSLContextService.ClientAuth.REQUIRED;
        } else {
            try {
                clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
            } catch (final IllegalArgumentException iae) {
                throw new ProviderCreationException(
                        String.format("Unrecognized client auth '%s'. Possible values are [%s]", rawClientAuth,
                                StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
            }
        }
        final SSLContext sslContext = sslService.createSSLContext(clientAuth);
        cf.useSslProtocol(sslContext);

        if (useCertAuthentication) {
            // this tells the factory to use the cert common name for authentication and not user name and password
            // REF: https://github.com/rabbitmq/rabbitmq-auth-mechanism-ssl
            cf.setSaslConfig(DefaultSaslConfig.EXTERNAL);
        }
    }

    try {
        Connection connection = cf.newConnection();
        return connection;
    } catch (Exception e) {
        throw new IllegalStateException("Failed to establish connection with AMQP Broker: " + cf.toString(), e);
    }
}