Skip to content

Commit 61c6662

Browse files
committed
Merge pull request #10251 from martingreber:add-keystoretype
* pr/10251: Polish "Added keystore type and truststore type to rabbit properties" Added keystore type and truststore type to rabbit properties
2 parents f04fa32 + 4c53755 commit 61c6662

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

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
@@ -113,8 +113,10 @@ public CachingConnectionFactory rabbitConnectionFactory(RabbitProperties config)
113113
if (ssl.getAlgorithm() != null) {
114114
factory.setSslAlgorithm(ssl.getAlgorithm());
115115
}
116+
factory.setKeyStoreType(ssl.getKeyStoreType());
116117
factory.setKeyStore(ssl.getKeyStore());
117118
factory.setKeyStorePassphrase(ssl.getKeyStorePassword());
119+
factory.setTrustStoreType(ssl.getTrustStoreType());
118120
factory.setTrustStore(ssl.getTrustStore());
119121
factory.setTrustStorePassphrase(ssl.getTrustStorePassword());
120122
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ public static class Ssl {
314314
*/
315315
private String keyStore;
316316

317+
/**
318+
* Key store type.
319+
*/
320+
private String keyStoreType = "PKCS12";
321+
317322
/**
318323
* Password used to access the key store.
319324
*/
@@ -324,6 +329,11 @@ public static class Ssl {
324329
*/
325330
private String trustStore;
326331

332+
/**
333+
* Trust store type.
334+
*/
335+
private String trustStoreType = "JKS";
336+
327337
/**
328338
* Password used to access the trust store.
329339
*/
@@ -351,6 +361,14 @@ public void setKeyStore(String keyStore) {
351361
this.keyStore = keyStore;
352362
}
353363

364+
public String getKeyStoreType() {
365+
return this.keyStoreType;
366+
}
367+
368+
public void setKeyStoreType(String keyStoreType) {
369+
this.keyStoreType = keyStoreType;
370+
}
371+
354372
public String getKeyStorePassword() {
355373
return this.keyStorePassword;
356374
}
@@ -367,6 +385,14 @@ public void setTrustStore(String trustStore) {
367385
this.trustStore = trustStore;
368386
}
369387

388+
public String getTrustStoreType() {
389+
return this.trustStoreType;
390+
}
391+
392+
public void setTrustStoreType(String trustStoreType) {
393+
this.trustStoreType = trustStoreType;
394+
}
395+
370396
public String getTrustStorePassword() {
371397
return this.trustStorePassword;
372398
}

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

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import java.security.NoSuchAlgorithmException;
20+
1921
import javax.net.SocketFactory;
2022
import javax.net.ssl.SSLSocketFactory;
2123

@@ -540,21 +542,80 @@ public void enableSsl() {
540542

541543
@Test
542544
// Make sure that we at least attempt to load the store
543-
public void enableSslWithExtraConfig() {
544-
this.contextRunner.withUserConfiguration(TestConfiguration.class)
545+
public void enableSslWithNonExistingKeystoreShouldFail() {
546+
this.contextRunner
547+
.withUserConfiguration(TestConfiguration.class)
545548
.withPropertyValues("spring.rabbitmq.ssl.enabled:true",
546549
"spring.rabbitmq.ssl.keyStore=foo",
547-
"spring.rabbitmq.ssl.keyStorePassword=secret",
550+
"spring.rabbitmq.ssl.keyStorePassword=secret")
551+
.run(context -> {
552+
assertThat(context).hasFailed();
553+
assertThat(context).getFailure().hasMessageContaining("foo");
554+
assertThat(context).getFailure().hasMessageContaining("does not exist");
555+
});
556+
}
557+
558+
@Test
559+
// Make sure that we at least attempt to load the store
560+
public void enableSslWithNonExistingTrustStoreShouldFail() {
561+
this.contextRunner
562+
.withUserConfiguration(TestConfiguration.class)
563+
.withPropertyValues(
564+
"spring.rabbitmq.ssl.enabled:true",
548565
"spring.rabbitmq.ssl.trustStore=bar",
549566
"spring.rabbitmq.ssl.trustStorePassword=secret")
550567
.run((context) -> {
551568
assertThat(context).hasFailed();
552-
assertThat(context).getFailure().hasMessageContaining("foo");
553-
assertThat(context).getFailure()
554-
.hasMessageContaining("does not exist");
569+
assertThat(context).getFailure().hasMessageContaining("bar");
570+
assertThat(context).getFailure().hasMessageContaining("does not exist");
555571
});
556572
}
557573

574+
@Test
575+
public void enableSslWithInvalidKeystoreTypeShouldFail() throws Exception {
576+
this.contextRunner
577+
.withUserConfiguration(TestConfiguration.class)
578+
.withPropertyValues(
579+
"spring.rabbitmq.ssl.enabled:true",
580+
"spring.rabbitmq.ssl.keyStore=foo",
581+
"spring.rabbitmq.ssl.keyStoreType=fooType")
582+
.run(context -> {
583+
assertThat(context).hasFailed();
584+
assertThat(context).getFailure().hasMessageContaining("fooType");
585+
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
586+
});
587+
}
588+
589+
@Test
590+
public void enableSslWithInvalidTrustStoreTypeShouldFail() throws Exception {
591+
this.contextRunner
592+
.withUserConfiguration(TestConfiguration.class)
593+
.withPropertyValues(
594+
"spring.rabbitmq.ssl.enabled:true",
595+
"spring.rabbitmq.ssl.trustStore=bar",
596+
"spring.rabbitmq.ssl.trustStoreType=barType")
597+
.run(context -> {
598+
assertThat(context).hasFailed();
599+
assertThat(context).getFailure().hasMessageContaining("barType");
600+
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
601+
});
602+
}
603+
604+
@Test
605+
public void enableSslWithKeystoreTypeAndTrustStoreTypeShouldWork() throws Exception {
606+
this.contextRunner
607+
.withUserConfiguration(TestConfiguration.class)
608+
.withPropertyValues(
609+
"spring.rabbitmq.ssl.enabled:true",
610+
"spring.rabbitmq.ssl.keyStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
611+
"spring.rabbitmq.ssl.keyStoreType=jks",
612+
"spring.rabbitmq.ssl.keyStorePassword=secret",
613+
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
614+
"spring.rabbitmq.ssl.trustStoreType=jks",
615+
"spring.rabbitmq.ssl.trustStorePassword=secret")
616+
.run(context -> assertThat(context).hasNotFailed());
617+
}
618+
558619
private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory(
559620
AssertableApplicationContext context) {
560621
CachingConnectionFactory connectionFactory = context

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,8 +1052,10 @@ content into your application; rather pick only the properties that you need.
10521052
spring.rabbitmq.ssl.enabled=false # Enable SSL support.
10531053
spring.rabbitmq.ssl.key-store= # Path to the key store that holds the SSL certificate.
10541054
spring.rabbitmq.ssl.key-store-password= # Password used to access the key store.
1055+
spring.rabbitmq.ssl.key-store-type=PKCS12 # Key store type.
10551056
spring.rabbitmq.ssl.trust-store= # Trust store that holds SSL certificates.
10561057
spring.rabbitmq.ssl.trust-store-password= # Password used to access the trust store.
1058+
spring.rabbitmq.ssl.trust-store-type=JKS # Trust store type.
10571059
spring.rabbitmq.ssl.algorithm= # SSL algorithm to use. By default configure by the rabbit client library.
10581060
spring.rabbitmq.template.mandatory=false # Enable mandatory messages.
10591061
spring.rabbitmq.template.receive-timeout=0 # Timeout for `receive()` methods.

0 commit comments

Comments
 (0)