Skip to content

Commit 6ec0cf2

Browse files
committed
Add configuration option to configure RabbitConnectionFactory key store and trust store algorithm
Add keyStoreAlgorithm and trustStoreAlgorithm to RabbitProperties and adopt at Rabbit AutoConfig.
1 parent dfe3058 commit 6ec0cf2

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(RabbitPropert
140140
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
141141
map.from(ssl::getKeyStore).to(factory::setKeyStore);
142142
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
143+
map.from(ssl::getKeyStoreAlgorithm).whenNonNull().to(factory::setKeyStoreAlgorithm);
143144
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
144145
map.from(ssl::getTrustStore).to(factory::setTrustStore);
145146
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
147+
map.from(ssl::getTrustStoreAlgorithm).whenNonNull().to(factory::setTrustStoreAlgorithm);
146148
map.from(ssl::isValidateServerCertificate)
147149
.to((validate) -> factory.setSkipServerCertificateValidation(!validate));
148150
map.from(ssl::getVerifyHostname).to(factory::setEnableHostnameVerification);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ public Template getTemplate() {
363363

364364
public class Ssl {
365365

366+
private static final String SUN_X509 = "SunX509";
367+
366368
/**
367369
* Whether to enable SSL support. Determined automatically if an address is
368370
* provided with the protocol (amqp:// vs. amqps://).
@@ -384,6 +386,11 @@ public class Ssl {
384386
*/
385387
private String keyStorePassword;
386388

389+
/**
390+
* Key store algorithm.
391+
*/
392+
private String keyStoreAlgorithm = SUN_X509;
393+
387394
/**
388395
* Trust store that holds SSL certificates.
389396
*/
@@ -399,6 +406,11 @@ public class Ssl {
399406
*/
400407
private String trustStorePassword;
401408

409+
/**
410+
* Trust store algorithm.
411+
*/
412+
private String trustStoreAlgorithm = SUN_X509;
413+
402414
/**
403415
* SSL algorithm to use. By default, configured by the Rabbit client library.
404416
*/
@@ -462,6 +474,14 @@ public void setKeyStorePassword(String keyStorePassword) {
462474
this.keyStorePassword = keyStorePassword;
463475
}
464476

477+
public String getKeyStoreAlgorithm() {
478+
return this.keyStoreAlgorithm;
479+
}
480+
481+
public void setKeyStoreAlgorithm(String keyStoreAlgorithm) {
482+
this.keyStoreAlgorithm = keyStoreAlgorithm;
483+
}
484+
465485
public String getTrustStore() {
466486
return this.trustStore;
467487
}
@@ -486,6 +506,14 @@ public void setTrustStorePassword(String trustStorePassword) {
486506
this.trustStorePassword = trustStorePassword;
487507
}
488508

509+
public String getTrustStoreAlgorithm() {
510+
return this.trustStoreAlgorithm;
511+
}
512+
513+
public void setTrustStoreAlgorithm(String trustStoreAlgorithm) {
514+
this.trustStoreAlgorithm = trustStoreAlgorithm;
515+
}
516+
489517
public String getAlgorithm() {
490518
return this.algorithm;
491519
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,36 @@ void enableSslWithValidateServerCertificateDefault() throws Exception {
738738
});
739739
}
740740

741+
@Test
742+
void enableSslWithValidStoreAlgorithmShouldWork() throws Exception {
743+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
744+
.withPropertyValues("spring.rabbitmq.ssl.enabled:true",
745+
"spring.rabbitmq.ssl.keyStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
746+
"spring.rabbitmq.ssl.keyStoreType=jks", "spring.rabbitmq.ssl.keyStorePassword=secret",
747+
"spring.rabbitmq.ssl.keyStoreAlgorithm=PKIX",
748+
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
749+
"spring.rabbitmq.ssl.trustStoreType=jks", "spring.rabbitmq.ssl.trustStorePassword=secret",
750+
"spring.rabbitmq.ssl.trustStoreAlgorithm=PKIX")
751+
.run((context) -> assertThat(context).hasNotFailed());
752+
}
753+
754+
@Test
755+
void enableSslWithInvalidStoreAlgorithmShouldFail() throws Exception {
756+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
757+
.withPropertyValues("spring.rabbitmq.ssl.enabled:true",
758+
"spring.rabbitmq.ssl.keyStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
759+
"spring.rabbitmq.ssl.keyStoreType=jks", "spring.rabbitmq.ssl.keyStorePassword=secret",
760+
"spring.rabbitmq.ssl.keyStoreAlgorithm=foo",
761+
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
762+
"spring.rabbitmq.ssl.trustStoreType=jks", "spring.rabbitmq.ssl.trustStorePassword=secret",
763+
"spring.rabbitmq.ssl.trustStoreAlgorithm=foo")
764+
.run((context) -> {
765+
assertThat(context).hasFailed();
766+
assertThat(context).getFailure().hasMessageContaining("foo");
767+
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
768+
});
769+
}
770+
741771
@Test
742772
void whenACredentialsProviderIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception {
743773
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)

0 commit comments

Comments
 (0)