use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class VerifiedConnection method connectionGetConsumeProtocols.
@Test
public void connectionGetConsumeProtocols() throws Exception {
Collection<String> availableProtocols = TlsTestUtils.availableTlsProtocols();
Collection<String> protocols = Stream.of("TLSv1.2", "TLSv1.3").filter(p -> availableProtocols.contains(p)).collect(Collectors.toList());
for (String protocol : protocols) {
SSLContext sslContext = SSLContext.getInstance(protocol);
ConnectionFactory cf = TestUtils.connectionFactory();
cf.useSslProtocol(TlsTestUtils.verifiedSslContext(() -> sslContext));
AtomicReference<Supplier<String[]>> protocolsSupplier = new AtomicReference<>();
if (TestUtils.USE_NIO) {
cf.useNio();
cf.setNioParams(new NioParams().setSslEngineConfigurator(sslEngine -> {
protocolsSupplier.set(() -> sslEngine.getEnabledProtocols());
}));
} else {
cf.setSocketConfigurator(socket -> {
SSLSocket s = (SSLSocket) socket;
protocolsSupplier.set(() -> s.getEnabledProtocols());
});
}
try (Connection c = cf.newConnection()) {
CountDownLatch latch = new CountDownLatch(1);
TestUtils.basicGetBasicConsume(c, VerifiedConnection.class.getName(), latch, 100);
boolean messagesReceived = latch.await(5, TimeUnit.SECONDS);
assertTrue("Message has not been received", messagesReceived);
assertThat(protocolsSupplier.get()).isNotNull();
assertThat(protocolsSupplier.get().get()).contains(protocol);
}
}
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class LoopbackUsers method getFactory.
private ConnectionFactory getFactory(String name, String addr) {
ConnectionFactory factory = TestUtils.connectionFactory();
factory.setUsername(name);
factory.setPassword(name);
factory.setHost(addr);
return factory;
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class NioTlsUnverifiedConnection method connectionShouldEnforceConnectionTimeout.
@Test
public void connectionShouldEnforceConnectionTimeout() throws Exception {
// assumes RabbitMQ server running on localhost;
int amqpPort = 5671;
int amqpProxyPort = TestUtils.randomNetworkPort();
int connectionTimeout = 3_000;
int handshakeTimeout = 1_000;
try (NioReactor reactor = new NioReactor();
TcpCrusher tcpProxy = TcpCrusherBuilder.builder().withReactor(reactor).withBindAddress(new InetSocketAddress(amqpProxyPort)).withConnectAddress("localhost", amqpPort).build()) {
tcpProxy.open();
tcpProxy.freeze();
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(amqpProxyPort);
factory.useSslProtocol();
factory.useNio();
factory.setConnectionTimeout(connectionTimeout);
factory.setHandshakeTimeout(handshakeTimeout);
ExecutorService executorService = Executors.newSingleThreadExecutor();
try {
CountDownLatch latch = new CountDownLatch(1);
executorService.submit(() -> {
try {
factory.newConnection();
latch.countDown();
} catch (SocketTimeoutException e) {
latch.countDown();
} catch (Exception e) {
// not supposed to happen
}
});
boolean connectionCreatedTimedOut = latch.await(10, TimeUnit.SECONDS);
assertThat(connectionCreatedTimedOut).isTrue();
} finally {
executorService.shutdownNow();
}
}
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class TlsConnectionLogging method certificateInfoAreProperlyExtracted.
@Test
public void certificateInfoAreProperlyExtracted() throws Exception {
SSLContext sslContext = TlsTestUtils.getSSLContext();
sslContext.init(null, new TrustManager[] { new AlwaysTrustTrustManager() }, null);
ConnectionFactory connectionFactory = TestUtils.connectionFactory();
connectionFactory.useSslProtocol(sslContext);
Supplier<SSLSession> sslSessionSupplier = configurer.apply(connectionFactory);
try (Connection ignored = connectionFactory.newConnection()) {
SSLSession session = sslSessionSupplier.get();
assertNotNull(session);
String info = TlsUtils.peerCertificateInfo(session.getPeerCertificates()[0], "some prefix");
Assertions.assertThat(info).contains("some prefix").contains("CN=").contains("X.509 usage extensions").contains("KeyUsage");
}
}
use of com.rabbitmq.client.ConnectionFactory in project rabbitmq-java-client by rabbitmq.
the class SaslMechanisms method connectionWithoutCapabilities.
// start a connection without capabilities, causing authentication failures
// to be reported by the broker by closing the connection
private Connection connectionWithoutCapabilities(String username, String password) throws IOException, TimeoutException {
ConnectionFactory customFactory = TestUtils.connectionFactory();
customFactory.setUsername(username);
customFactory.setPassword(password);
Map<String, Object> customProperties = AMQConnection.defaultClientProperties();
customProperties.remove("capabilities");
customFactory.setClientProperties(customProperties);
return customFactory.newConnection();
}
Aggregations