Example usage for com.rabbitmq.client ConnectionFactory setSaslConfig

List of usage examples for com.rabbitmq.client ConnectionFactory setSaslConfig

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory setSaslConfig.

Prototype

public void setSaslConfig(SaslConfig saslConfig) 

Source Link

Document

Sets the sasl config to use when authenticating

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);//w ww. j  a va2  s  .  c om
    factory.setAutomaticRecoveryEnabled(true);

    return factory;
}

From source file:io.bigwig.sasl.PublicKeyITest.java

License:Mozilla Public License

@Test
public void testDefaultUser() throws Exception {

    // Read in a private key from the file system and pass it to the SASL configuration

    InputStream is = getClass().getClassLoader().getResourceAsStream("ecdsa.pem");
    PublicKeyConfig saslConfig = new PublicKeyConfig(new InputStreamReader(is));

    ConnectionFactory factory = new ConnectionFactory();
    factory.setSaslConfig(saslConfig);

    // Override any other non-default parameters for connecting to Bigwig, for example:
    // factory.setUsername("gljJvcFZwoMW9");
    // factory.setPort(10160);
    // factory.setVirtualHost("B6F90F1uhpNv");

    Connection connection = factory.newConnection();

    assertTrue("Could not open connection to Bigwig using public key authentication", connection.isOpen());

    connection.close();/*  www .j a  v  a  2s  .  co  m*/
}

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;/* ww w. j a v  a 2 s .c  o  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   w w  w. j a v a 2s .  c om
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);
    }
}

From source file:org.geoserver.notification.common.sender.RabbitMQSender.java

License:Open Source License

public void initialize() throws Exception {

    if (uri == null) {
        if (this.username != null && !this.username.isEmpty() && this.password != null
                && !this.password.isEmpty()) {
            this.uri = "amqp://" + this.username + ":" + this.password + "@" + this.host + ":" + this.port;
        } else {/*from  ww w  . j a v a  2 s  . com*/
            this.uri = "amqp://" + this.host + ":" + this.port;
        }

    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setUri(this.uri);
    String vHost = (this.virtualHost != null && !this.virtualHost.isEmpty() ? this.virtualHost : "/");
    factory.setVirtualHost(vHost);
    factory.setSaslConfig(new CustomSaslConfig());
    conn = factory.newConnection();
    channel = conn.createChannel();
}

From source file:org.geoserver.notification.support.Receiver.java

License:Open Source License

public void receive(ReceiverService service) throws Exception {
    // let's setup evrything and start listening
    this.service = service;
    ConnectionFactory factory = createConnectionFactory();
    factory.setSaslConfig(new CustomSaslConfig());
    connection = factory.newConnection();
    channel = connection.createChannel();
    channel.exchangeDeclare("testExchange", "fanout");
    channel.queueDeclare(QUEUE_NAME, false, true, false, null);
    channel.queueBind(QUEUE_NAME, "testExchange", "testRouting");
    channel.basicConsume(QUEUE_NAME, true, newConsumer(channel));
}

From source file:ws.ament.hammock.rabbitmq.ConnectionFactoryProducer.java

License:Apache License

@Produces
@ApplicationScoped//from   w  ww  . ja  v  a  2 s . com
public ConnectionFactory createConnectionFactory(RabbitMQConfiguration rabbitMQConfiguration) {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.setAutomaticRecoveryEnabled(rabbitMQConfiguration.isAutomaticRecovery());
    connectionFactory.setClientProperties(rabbitMQConfiguration.getClientProperties());
    connectionFactory.setConnectionTimeout(rabbitMQConfiguration.getConnectionTimeout());
    connectionFactory.setExceptionHandler(rabbitMQConfiguration.getExceptionHandler());
    connectionFactory.setHandshakeTimeout(rabbitMQConfiguration.getHandshakeTimeout());
    connectionFactory.setHeartbeatExecutor(rabbitMQConfiguration.getHeartbeatExecutor());
    connectionFactory.setMetricsCollector(rabbitMQConfiguration.getMetricsCollector());
    connectionFactory.setHost(rabbitMQConfiguration.getHost());
    connectionFactory.setNetworkRecoveryInterval(rabbitMQConfiguration.getNetworkRecoveryInterval());
    if (rabbitMQConfiguration.isNio()) {
        connectionFactory.useNio();
        connectionFactory.setNioParams(rabbitMQConfiguration.getNioParams());
    }
    connectionFactory.setPassword(rabbitMQConfiguration.getPassword());
    connectionFactory.setPort(rabbitMQConfiguration.getPort());
    connectionFactory.setRequestedChannelMax(rabbitMQConfiguration.getRequestedChannelMax());
    connectionFactory.setRequestedFrameMax(rabbitMQConfiguration.getRequestedFrameMax());
    connectionFactory.setRequestedHeartbeat(rabbitMQConfiguration.getRequestedHeartbeat());
    connectionFactory.setSaslConfig(rabbitMQConfiguration.getSaslConfig());
    connectionFactory.setSharedExecutor(rabbitMQConfiguration.getSharedExecutor());
    connectionFactory.setShutdownExecutor(rabbitMQConfiguration.getShutdownExecutor());
    connectionFactory.setShutdownTimeout(rabbitMQConfiguration.getShutdownTimeout());
    connectionFactory.setSocketConfigurator(rabbitMQConfiguration.getSocketConf());
    connectionFactory.setSocketFactory(rabbitMQConfiguration.getFactory());
    connectionFactory.setThreadFactory(rabbitMQConfiguration.getThreadFactory());
    connectionFactory.setTopologyRecoveryEnabled(rabbitMQConfiguration.isTopologyRecovery());
    try {
        connectionFactory.setUri(rabbitMQConfiguration.getUri());
    } catch (Exception e) {
        throw new RuntimeException("Unable to populate URI ", e);
    }
    connectionFactory.setUsername(rabbitMQConfiguration.getUsername());
    connectionFactory.setVirtualHost(rabbitMQConfiguration.getVirtualHost());
    if (rabbitMQConfiguration.getSslContext() != null) {
        connectionFactory.useSslProtocol(rabbitMQConfiguration.getSslContext());
    }
    return connectionFactory;
}