Skip to content

Commit eb14f06

Browse files
committed
Merge pull request #24076 from topikachu
* pr/24076: Polish "Add configuration for Rabbit's key store and trust store algorithm" Add configuration for Rabbit's key store and trust store algorithm Closes gh-24076
2 parents 315067b + 5991033 commit eb14f06

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,47 @@ void enableSslWithValidateServerCertificateDefault() throws Exception {
738738
});
739739
}
740740

741+
@Test
742+
void enableSslWithValidStoreAlgorithmShouldWork() {
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 enableSslWithInvalidKeyStoreAlgorithmShouldFail() {
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=test-invalid-algo")
761+
.run((context) -> {
762+
assertThat(context).hasFailed();
763+
assertThat(context).getFailure().hasMessageContaining("test-invalid-algo");
764+
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
765+
});
766+
}
767+
768+
@Test
769+
void enableSslWithInvalidTrustStoreAlgorithmShouldFail() {
770+
this.contextRunner.withUserConfiguration(TestConfiguration.class)
771+
.withPropertyValues("spring.rabbitmq.ssl.enabled:true",
772+
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
773+
"spring.rabbitmq.ssl.trustStoreType=jks", "spring.rabbitmq.ssl.trustStorePassword=secret",
774+
"spring.rabbitmq.ssl.trustStoreAlgorithm=test-invalid-algo")
775+
.run((context) -> {
776+
assertThat(context).hasFailed();
777+
assertThat(context).getFailure().hasMessageContaining("test-invalid-algo");
778+
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
779+
});
780+
}
781+
741782
@Test
742783
void whenACredentialsProviderIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception {
743784
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)

0 commit comments

Comments
 (0)