Search in sources :

Example 46 with ProtonClient

use of io.vertx.proton.ProtonClient in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword.

/**
 * Verifies that the factory does not enable SASL_PLAIN if the username and password are empty
 * strings.
 */
@Test
public void testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword() {
    // GIVEN a factory configured to connect to a server
    final ProtonClientOptions options = new ProtonClientOptions();
    final ProtonClient client = mock(ProtonClient.class);
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
    factory.setProtonClient(client);
    // WHEN connecting to the server using empty strings for username and password
    factory.connect(options, "", "", null, null, c -> {
    });
    // THEN the factory does not enable the SASL_PLAIN mechanism when establishing
    // the connection
    final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
    verify(client).connect(optionsCaptor.capture(), anyString(), anyInt(), eq(""), eq(""), VertxMockSupport.anyHandler());
    assertFalse(optionsCaptor.getValue().getEnabledSaslMechanisms().contains("PLAIN"));
}
Also used : ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.jupiter.api.Test)

Example 47 with ProtonClient

use of io.vertx.proton.ProtonClient in project hono by eclipse.

the class ConnectionFactoryImplTest method testConnectEnablesSslIfExplicitlyConfigured.

/**
 * Verifies that the factory uses TLS when connecting to the peer if no trust store
 * is configured but TLS has been enabled explicitly.
 */
@Test
public void testConnectEnablesSslIfExplicitlyConfigured() {
    // GIVEN a factory configured to connect to a server using TLS
    final ClientConfigProperties config = new ClientConfigProperties();
    config.setHost("remote.host");
    config.setTlsEnabled(true);
    final ProtonClient client = mock(ProtonClient.class);
    final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, config);
    factory.setProtonClient(client);
    // WHEN connecting to the server
    factory.connect(null, null, null, c -> {
    });
    // THEN the factory uses TLS when establishing the connection
    final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
    verify(client).connect(optionsCaptor.capture(), eq("remote.host"), anyInt(), any(), any(), VertxMockSupport.anyHandler());
    assertTrue(optionsCaptor.getValue().isSsl());
}
Also used : ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.jupiter.api.Test)

Example 48 with ProtonClient

use of io.vertx.proton.ProtonClient in project hono by eclipse.

the class AmqpCliClient method connectToAdapter.

/**
 * Connects to the AMQP org.eclipse.hono.cli.app.adapter.
 *
 * @return A future containing the established connection. The future will
 *         be succeeded once the connection is open.
 */
protected Future<ProtonConnection> connectToAdapter() {
    final Promise<ProtonConnection> connectAttempt = Promise.promise();
    final ProtonClientOptions options = new ProtonClientOptions();
    final ProtonClient client = ProtonClient.create(vertx);
    options.setConnectTimeout(properties.getConnectTimeout());
    options.setHeartbeat(properties.getHeartbeatInterval());
    options.setMaxFrameSize(properties.getMaxFrameSize());
    Optional.ofNullable(properties.getAmqpHostname()).ifPresent(s -> options.setVirtualHost(s));
    addTlsTrustOptions(options, properties);
    if (!Strings.isNullOrEmpty(properties.getUsername()) && !Strings.isNullOrEmpty(properties.getPassword())) {
        // SASL PLAIN auth
        options.addEnabledSaslMechanism(ProtonSaslPlainImpl.MECH_NAME);
        log.info("connecting to AMQP org.eclipse.hono.cli.app.adapter using SASL PLAIN [host: {}, port: {}, username: {}]", properties.getHost(), properties.getPort(), properties.getUsername());
        client.connect(options, properties.getHost(), properties.getPort(), properties.getUsername(), properties.getPassword(), connectAttempt);
    } else {
        if (properties.getKeyCertOptions() != null && properties.getTrustOptions() != null) {
            // SASL EXTERNAL auth
            options.setKeyCertOptions(properties.getKeyCertOptions());
        } else {
        // SASL ANONYMOUS auth
        }
        log.info("connecting to AMQP org.eclipse.hono.cli.app.adapter [host: {}, port: {}]", properties.getHost(), properties.getPort());
        client.connect(options, properties.getHost(), properties.getPort(), connectAttempt);
    }
    return connectAttempt.future().compose(unopenedConnection -> {
        final Promise<ProtonConnection> con = Promise.promise();
        unopenedConnection.openHandler(con);
        unopenedConnection.open();
        return con.future();
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient)

Example 49 with ProtonClient

use of io.vertx.proton.ProtonClient in project hono by eclipse.

the class AmqpAdapterTestBase method connectToAdapter.

/**
 * Connects to the AMQP protocol adapter using a username and password.
 *
 * @param tlsVersion The TLS protocol version to use for connecting to the adapter.
 * @param cipherSuite The TLS cipher suite to use for connecting to the adapter or {@code null} if the
 *                    cipher suite should not be restricted.
 * @param username The username to use for authentication.
 * @param password The password to use for authentication.
 * @return A succeeded future containing the established connection.
 */
protected Future<ProtonConnection> connectToAdapter(final String tlsVersion, final String cipherSuite, final String username, final String password) {
    final Promise<ProtonConnection> result = Promise.promise();
    final ProtonClient client = ProtonClient.create(vertx);
    final ProtonClientOptions options = new ProtonClientOptions(defaultOptions);
    options.addEnabledSaslMechanism(ProtonSaslPlainImpl.MECH_NAME);
    options.setEnabledSecureTransportProtocols(Set.of(tlsVersion));
    Optional.ofNullable(cipherSuite).ifPresent(options::addEnabledCipherSuite);
    client.connect(options, IntegrationTestSupport.AMQP_HOST, IntegrationTestSupport.AMQPS_PORT, username, password, result);
    return result.future().compose(con -> handleConnectAttempt(con, IntegrationTestSupport.AMQP_HOST));
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient)

Example 50 with ProtonClient

use of io.vertx.proton.ProtonClient in project hono by eclipse.

the class AmqpAdapterTestBase method connectToAdapter.

/**
 * Connects to the AMQP protocol adapter using a client certificate.
 *
 * @param hostname The name of the host to connect to.
 * @param clientCertificate The certificate to use for authentication.
 * @param tlsProtocolVersion The TLS protocol version to use for connecting to the host.
 * @return A succeeded future containing the established connection.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
protected Future<ProtonConnection> connectToAdapter(final String hostname, final SelfSignedCertificate clientCertificate, final String tlsProtocolVersion) {
    Objects.requireNonNull(hostname);
    Objects.requireNonNull(clientCertificate);
    Objects.requireNonNull(tlsProtocolVersion);
    final Promise<ProtonConnection> result = Promise.promise();
    final ProtonClient client = ProtonClient.create(vertx);
    final ProtonClientOptions secureOptions = new ProtonClientOptions(defaultOptions);
    secureOptions.setKeyCertOptions(clientCertificate.keyCertOptions());
    secureOptions.addEnabledSaslMechanism(ProtonSaslExternalImpl.MECH_NAME);
    secureOptions.setEnabledSecureTransportProtocols(Set.of(tlsProtocolVersion));
    client.connect(secureOptions, hostname, IntegrationTestSupport.AMQPS_PORT, result);
    return result.future().compose(con -> handleConnectAttempt(con, hostname));
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient)

Aggregations

ProtonClient (io.vertx.proton.ProtonClient)55 ProtonConnection (io.vertx.proton.ProtonConnection)42 Handler (io.vertx.core.Handler)27 Message (org.apache.qpid.proton.message.Message)27 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)24 AsyncResult (io.vertx.core.AsyncResult)23 Test (org.junit.Test)22 Arrays (java.util.Arrays)21 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)20 ProtonStreams (io.vertx.proton.streams.ProtonStreams)20 Section (org.apache.qpid.proton.amqp.messaging.Section)20 Async (io.vertx.ext.unit.Async)19 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Logger (io.vertx.core.impl.logging.Logger)18 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)18 TestContext (io.vertx.ext.unit.TestContext)18 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)18 FutureHandler (io.vertx.proton.FutureHandler)18 MockServerTestBase (io.vertx.proton.MockServerTestBase)18