Search in sources :

Example 31 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 32 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 33 with ProtonClient

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

the class ConnectionFactoryImplTest method testConnectSetsMaxFrameSize.

/**
 * Verifies that the factory sets the maximum frame size on the connection to the value from the client configuration.
 */
@Test
public void testConnectSetsMaxFrameSize() {
    // GIVEN a factory configured with a max-message-size
    final ClientConfigProperties config = new ClientConfigProperties();
    config.setHost("remote.host");
    config.setMaxFrameSize(64 * 1024);
    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 sets the max-message-size 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());
    assertThat(optionsCaptor.getValue().getMaxFrameSize()).isEqualTo(64 * 1024);
}
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 34 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 35 with ProtonClient

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

the class ConnectionFactoryImpl method connect.

@Override
public void connect(final ProtonClientOptions options, final String username, final String password, final String containerId, final Handler<AsyncResult<ProtonConnection>> closeHandler, final Handler<ProtonConnection> disconnectHandler, final Handler<AsyncResult<ProtonConnection>> connectionResultHandler) {
    Objects.requireNonNull(connectionResultHandler);
    final ProtonClientOptions clientOptions = options != null ? options : createClientOptions();
    final String effectiveUsername = username == null ? config.getUsername() : username;
    final String effectivePassword = password == null ? config.getPassword() : password;
    addOptions(clientOptions, effectiveUsername, effectivePassword);
    final String effectiveContainerId = containerId == null ? getContainerIdDefault() : containerId;
    final ProtonClient client = protonClient != null ? protonClient : ProtonClient.create(vertx);
    logger.debug("connecting to AMQP 1.0 container [{}://{}:{}, role: {}]", clientOptions.isSsl() ? PROTOCOL_AMQPS : PROTOCOL_AMQP, config.getHost(), config.getPort(), config.getServerRole());
    final AtomicBoolean connectionTimeoutReached = new AtomicBoolean(false);
    final Long connectionTimeoutTimerId = config.getConnectTimeout() > 0 ? vertx.setTimer(config.getConnectTimeout(), id -> {
        if (connectionTimeoutReached.compareAndSet(false, true)) {
            failConnectionAttempt(clientOptions, connectionResultHandler, new ConnectTimeoutException("connection attempt timed out after " + config.getConnectTimeout() + "ms"));
        }
    }) : null;
    client.connect(clientOptions, config.getHost(), config.getPort(), effectiveUsername, effectivePassword, conAttempt -> handleConnectionAttemptResult(conAttempt, effectiveContainerId, connectionTimeoutTimerId, connectionTimeoutReached, clientOptions, closeHandler, disconnectHandler, connectionResultHandler));
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException) OpenSSLEngineOptions(io.vertx.core.net.OpenSSLEngineOptions) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) KeyCertOptions(io.vertx.core.net.KeyCertOptions) ProtonSaslExternalImpl(io.vertx.proton.sasl.impl.ProtonSaslExternalImpl) AsyncResult(io.vertx.core.AsyncResult) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) LinkedHashSet(java.util.LinkedHashSet) Strings(org.eclipse.hono.util.Strings) OpenSsl(io.netty.handler.ssl.OpenSsl) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) Set(java.util.Set) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) Future(io.vertx.core.Future) Objects(java.util.Objects) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) TrustOptions(io.vertx.core.net.TrustOptions) Handler(io.vertx.core.Handler) ProtonSaslPlainImpl(io.vertx.proton.sasl.impl.ProtonSaslPlainImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) ConnectTimeoutException(org.eclipse.hono.connection.ConnectTimeoutException)

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