Skip to content

Commit 18e3091

Browse files
committed
Merge branch '4.0.x'
Closes gh-50175
2 parents 51ffc52 + f8bb816 commit 18e3091

3 files changed

Lines changed: 79 additions & 22 deletions

File tree

module/spring-boot-amqp/src/main/java/org/springframework/boot/amqp/autoconfigure/RabbitConnectionFactoryBeanConfigurer.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,15 @@ public void configure(RabbitConnectionFactoryBean factory) {
130130
.to(factory::setRequestedHeartbeat);
131131
map.from(this.rabbitProperties::getRequestedChannelMax).to(factory::setRequestedChannelMax);
132132
SslBundle sslBundle = this.connectionDetails.getSslBundle();
133-
if (sslBundle != null) {
134-
applySslBundle(factory, sslBundle);
135-
}
136-
else {
137-
RabbitProperties.Ssl ssl = this.rabbitProperties.getSsl();
138-
if (ssl.determineEnabled()) {
139-
factory.setUseSSL(true);
140-
map.from(ssl::getAlgorithm).to(factory::setSslAlgorithm);
141-
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
142-
map.from(ssl::getKeyStore).to(factory::setKeyStore);
143-
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
144-
map.from(ssl::getKeyStoreAlgorithm).to(factory::setKeyStoreAlgorithm);
145-
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
146-
map.from(ssl::getTrustStore).to(factory::setTrustStore);
147-
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
148-
map.from(ssl::getTrustStoreAlgorithm).to(factory::setTrustStoreAlgorithm);
149-
map.from(ssl::isValidateServerCertificate)
150-
.to((validate) -> factory.setSkipServerCertificateValidation(!validate));
151-
map.from(ssl::isVerifyHostname).to(factory::setEnableHostnameVerification);
133+
RabbitProperties.Ssl ssl = this.rabbitProperties.getSsl();
134+
if (sslBundle != null || ssl.determineEnabled()) {
135+
factory.setUseSSL(true);
136+
map.from(ssl::isVerifyHostname).to(factory::setEnableHostnameVerification);
137+
if (sslBundle != null) {
138+
applySslBundle(factory, sslBundle);
139+
}
140+
else {
141+
applySslProperties(factory, map, ssl);
152142
}
153143
}
154144
map.from(this.rabbitProperties::getConnectionTimeout)
@@ -164,11 +154,24 @@ public void configure(RabbitConnectionFactoryBean factory) {
164154
.to(factory::setMaxInboundMessageBodySize);
165155
}
166156

167-
private static void applySslBundle(RabbitConnectionFactoryBean factory, SslBundle bundle) {
168-
factory.setUseSSL(true);
157+
private void applySslBundle(RabbitConnectionFactoryBean factory, SslBundle bundle) {
169158
if (factory instanceof SslBundleRabbitConnectionFactoryBean sslFactory) {
170159
sslFactory.setSslBundle(bundle);
171160
}
172161
}
173162

163+
private void applySslProperties(RabbitConnectionFactoryBean factory, PropertyMapper map, RabbitProperties.Ssl ssl) {
164+
map.from(ssl::getAlgorithm).to(factory::setSslAlgorithm);
165+
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
166+
map.from(ssl::getKeyStore).to(factory::setKeyStore);
167+
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
168+
map.from(ssl::getKeyStoreAlgorithm).to(factory::setKeyStoreAlgorithm);
169+
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
170+
map.from(ssl::getTrustStore).to(factory::setTrustStore);
171+
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
172+
map.from(ssl::getTrustStoreAlgorithm).to(factory::setTrustStoreAlgorithm);
173+
map.from(ssl::isValidateServerCertificate)
174+
.to((validate) -> factory.setSkipServerCertificateValidation(!validate));
175+
}
176+
174177
}

module/spring-boot-amqp/src/main/java/org/springframework/boot/amqp/autoconfigure/SslBundleRabbitConnectionFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SslBundleRabbitConnectionFactoryBean extends RabbitConnectionFactoryBean {
3131

3232
private @Nullable SslBundle sslBundle;
3333

34-
private boolean enableHostnameVerification;
34+
private boolean enableHostnameVerification = true;
3535

3636
@Override
3737
protected void setUpSSL() {

module/spring-boot-amqp/src/test/java/org/springframework/boot/amqp/autoconfigure/RabbitAutoConfigurationTests.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@
1616

1717
package org.springframework.boot.amqp.autoconfigure;
1818

19+
import java.io.IOException;
1920
import java.security.NoSuchAlgorithmException;
2021
import java.util.Collection;
2122
import java.util.List;
2223
import java.util.concurrent.ThreadFactory;
2324
import java.util.concurrent.atomic.AtomicInteger;
2425
import java.util.function.BiFunction;
2526

27+
import javax.net.ssl.SSLEngine;
28+
import javax.net.ssl.SSLParameters;
2629
import javax.net.ssl.SSLSocketFactory;
2730

2831
import com.rabbitmq.client.Address;
2932
import com.rabbitmq.client.Connection;
3033
import com.rabbitmq.client.JDKSaslConfig;
34+
import com.rabbitmq.client.SslEngineConfigurator;
3135
import com.rabbitmq.client.impl.CredentialsProvider;
3236
import com.rabbitmq.client.impl.CredentialsRefreshService;
3337
import com.rabbitmq.client.impl.DefaultCredentialsProvider;
@@ -39,6 +43,7 @@
3943
import org.junit.jupiter.api.extension.ExtendWith;
4044
import org.junit.jupiter.params.ParameterizedTest;
4145
import org.junit.jupiter.params.provider.ValueSource;
46+
import org.mockito.ArgumentCaptor;
4247
import org.mockito.InOrder;
4348

4449
import org.springframework.amqp.core.AcknowledgeMode;
@@ -875,6 +880,20 @@ void enableSsl() {
875880
assertThat(rabbitConnectionFactory.isSSL()).isTrue();
876881
assertThat(rabbitConnectionFactory.getSocketFactory()).as("SocketFactory must use SSL")
877882
.isInstanceOf(SSLSocketFactory.class);
883+
assertThatHostnameVerificationIsEnabled(rabbitConnectionFactory);
884+
});
885+
}
886+
887+
@Test
888+
void enableSslWithoutHostnameVerification() {
889+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
890+
.withPropertyValues("spring.rabbitmq.ssl.enabled:true", "spring.rabbitmq.ssl.verify-hostname:false")
891+
.run((context) -> {
892+
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
893+
assertThat(rabbitConnectionFactory.isSSL()).isTrue();
894+
assertThat(rabbitConnectionFactory.getSocketFactory()).as("SocketFactory must use SSL")
895+
.isInstanceOf(SSLSocketFactory.class);
896+
assertThatHostnameVerificationIsDisabled(rabbitConnectionFactory);
878897
});
879898
}
880899

@@ -947,6 +966,20 @@ void enableSslWithBundle() {
947966
.run((context) -> {
948967
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
949968
assertThat(rabbitConnectionFactory.isSSL()).isTrue();
969+
assertThatHostnameVerificationIsEnabled(rabbitConnectionFactory);
970+
});
971+
}
972+
973+
@Test
974+
void enableSslWithBundleAndWithoutHostnameVerification() {
975+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
976+
.withPropertyValues("spring.rabbitmq.ssl.bundle=test-bundle", "spring.rabbitmq.ssl.verify-hostname=false",
977+
"spring.ssl.bundle.jks.test-bundle.keystore.location=classpath:org/springframework/boot/amqp/autoconfigure/test.jks",
978+
"spring.ssl.bundle.jks.test-bundle.keystore.password=secret")
979+
.run((context) -> {
980+
com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory = getTargetConnectionFactory(context);
981+
assertThat(rabbitConnectionFactory.isSSL()).isTrue();
982+
assertThatHostnameVerificationIsDisabled(rabbitConnectionFactory);
950983
});
951984
}
952985

@@ -1110,6 +1143,27 @@ void whenADirectContainerCustomizerIsDefinedThenItIsCalledToConfigureTheContaine
11101143
.configure(any(DirectMessageListenerContainer.class)));
11111144
}
11121145

1146+
@SuppressWarnings("deprecation")
1147+
private void assertThatHostnameVerificationIsEnabled(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
1148+
throws IOException {
1149+
SslEngineConfigurator sslEngineConfigurator = rabbitConnectionFactory.getNioParams().getSslEngineConfigurator();
1150+
SSLEngine engine = mock(SSLEngine.class);
1151+
sslEngineConfigurator.configure(engine);
1152+
ArgumentCaptor<SSLParameters> sslParametersCaptor = ArgumentCaptor.forClass(SSLParameters.class);
1153+
then(engine).should().setSSLParameters(sslParametersCaptor.capture());
1154+
SSLParameters sslParameters = sslParametersCaptor.getValue();
1155+
assertThat(sslParameters.getEndpointIdentificationAlgorithm()).isEqualTo("HTTPS");
1156+
}
1157+
1158+
@SuppressWarnings("deprecation")
1159+
private void assertThatHostnameVerificationIsDisabled(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
1160+
throws IOException {
1161+
SslEngineConfigurator sslEngineConfigurator = rabbitConnectionFactory.getNioParams().getSslEngineConfigurator();
1162+
SSLEngine engine = mock(SSLEngine.class);
1163+
sslEngineConfigurator.configure(engine);
1164+
then(engine).shouldHaveNoMoreInteractions();
1165+
}
1166+
11131167
private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(AssertableApplicationContext context) {
11141168
CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
11151169
return connectionFactory.getRabbitConnectionFactory();

0 commit comments

Comments
 (0)