Use TLS port for Docker Compose RabbitMQ connection when SSL is configured

When a rabbitmq Docker Compose service has SSL bundle labels,
RabbitDockerComposeConnectionDetailsFactory created an SslBundle but
still resolved the address from container port 5672, the plain AMQP
listener. As the connection factory enables SSL when an SslBundle is
present, the TLS handshake was attempted against the non-TLS listener
and the connection failed.

The address is now resolved from container port 5671 when an SslBundle
is present, matching RabbitStreamDockerComposeConnectionDetailsFactory
and the Testcontainers-based RabbitContainerConnectionDetailsFactory.
The SSL integration test now opens a connection using the resolved
address and SslBundle so that it fails without this fix.

Signed-off-by: ohchanKyu <okc0202@naver.com>

See gh-51666
This commit is contained in:
ohchanKyu
2026-09-11 12:35:40 +01:00
committed by Andy Wilkinson
parent f956f588d5
commit 458ed72f55
2 changed files with 24 additions and 2 deletions
@@ -16,6 +16,9 @@
package org.springframework.boot.amqp.docker.compose;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.boot.amqp.autoconfigure.RabbitConnectionDetails;
import org.springframework.boot.amqp.autoconfigure.RabbitConnectionDetails.Address;
import org.springframework.boot.docker.compose.service.connection.test.DockerComposeTest;
@@ -42,10 +45,26 @@ class RabbitDockerComposeConnectionDetailsFactoryIntegrationTests {
@DockerComposeTest(composeFile = "rabbit-ssl-compose.yaml", image = TestImage.RABBITMQ,
additionalResources = { "ca.crt", "server.crt", "server.key", "client.crt", "client.key", "rabbitmq.conf" })
void runWithSslCreatesConnectionDetails(RabbitConnectionDetails connectionDetails) {
void runWithSslCreatesConnectionDetails(RabbitConnectionDetails connectionDetails) throws Exception {
assertConnectionDetails(connectionDetails);
SslBundle sslBundle = connectionDetails.getSslBundle();
assertThat(sslBundle).isNotNull();
assertThatSslConnectionCanBeMade(connectionDetails, sslBundle);
}
private void assertThatSslConnectionCanBeMade(RabbitConnectionDetails connectionDetails, SslBundle sslBundle)
throws Exception {
ConnectionFactory connectionFactory = new ConnectionFactory();
Address address = connectionDetails.getFirstAddress();
connectionFactory.setHost(address.host());
connectionFactory.setPort(address.port());
connectionFactory.setUsername(connectionDetails.getUsername());
connectionFactory.setPassword(connectionDetails.getPassword());
connectionFactory.setVirtualHost(connectionDetails.getVirtualHost());
connectionFactory.useSslProtocol(sslBundle.createSslContext());
try (Connection connection = connectionFactory.newConnection()) {
assertThat(connection.isOpen()).isTrue();
}
}
private void assertConnectionDetails(RabbitConnectionDetails connectionDetails) {
@@ -40,6 +40,8 @@ class RabbitDockerComposeConnectionDetailsFactory
private static final int RABBITMQ_PORT = 5672;
private static final int RABBITMQ_TLS_PORT = 5671;
protected RabbitDockerComposeConnectionDetailsFactory() {
super("rabbitmq");
}
@@ -66,7 +68,8 @@ class RabbitDockerComposeConnectionDetailsFactory
super(service);
this.environment = new RabbitEnvironment(service.env());
this.sslBundle = getSslBundle(service);
this.addresses = List.of(new Address(service.host(), service.ports().get(RABBITMQ_PORT)));
int containerPort = (this.sslBundle != null) ? RABBITMQ_TLS_PORT : RABBITMQ_PORT;
this.addresses = List.of(new Address(service.host(), service.ports().get(containerPort)));
}
@Override