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"));
}
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());
}
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);
}
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();
});
}
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));
}
Aggregations