From b70ac99bcbbe1abdb966b69d894a609960f366e7 Mon Sep 17 00:00:00 2001 From: Martin Greber Date: Mon, 11 Sep 2017 23:02:35 +1000 Subject: [PATCH 1/2] Added keystore type and truststore type to rabbit properties See gh-10251 --- .../amqp/RabbitAutoConfiguration.java | 6 ++ .../autoconfigure/amqp/RabbitProperties.java | 26 +++++++ .../amqp/RabbitAutoConfigurationTests.java | 73 ++++++++++++++++-- .../boot/autoconfigure/amqp/test.jks | Bin 0 -> 1294 bytes .../appendix-application-properties.adoc | 2 + 5 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/amqp/test.jks diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 0ea5a3e4e3..2b849b5066 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -113,8 +113,14 @@ public class RabbitAutoConfiguration { if (ssl.getAlgorithm() != null) { factory.setSslAlgorithm(ssl.getAlgorithm()); } + if (ssl.getKeyStoreType() != null) { + factory.setKeyStoreType(ssl.getKeyStoreType()); + } factory.setKeyStore(ssl.getKeyStore()); factory.setKeyStorePassphrase(ssl.getKeyStorePassword()); + if (ssl.getTrustStoreType() != null) { + factory.setTrustStoreType(ssl.getTrustStoreType()); + } factory.setTrustStore(ssl.getTrustStore()); factory.setTrustStorePassphrase(ssl.getTrustStorePassword()); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 66e7f483be..2b57df1cd1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -314,6 +314,11 @@ public class RabbitProperties { */ private String keyStore; + /** + * Set the key store type (jks, pkcs12, etc). + */ + private String keyStoreType; + /** * Password used to access the key store. */ @@ -324,6 +329,11 @@ public class RabbitProperties { */ private String trustStore; + /** + * Set the trust store type (jks, pkcs12, etc). + */ + private String trustStoreType; + /** * Password used to access the trust store. */ @@ -351,6 +361,14 @@ public class RabbitProperties { this.keyStore = keyStore; } + public String getKeyStoreType() { + return this.keyStoreType; + } + + public void setKeyStoreType(String keyStoreType) { + this.keyStoreType = keyStoreType; + } + public String getKeyStorePassword() { return this.keyStorePassword; } @@ -367,6 +385,14 @@ public class RabbitProperties { this.trustStore = trustStore; } + public String getTrustStoreType() { + return this.trustStoreType; + } + + public void setTrustStoreType(String trustStoreType) { + this.trustStoreType = trustStoreType; + } + public String getTrustStorePassword() { return this.trustStorePassword; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 9132448dc8..782c426b28 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.amqp; +import java.security.NoSuchAlgorithmException; + import javax.net.SocketFactory; import javax.net.ssl.SSLSocketFactory; @@ -540,21 +542,80 @@ public class RabbitAutoConfigurationTests { @Test // Make sure that we at least attempt to load the store - public void enableSslWithExtraConfig() { - this.contextRunner.withUserConfiguration(TestConfiguration.class) + public void enableSslWithNonexistingKeystoreShouldFail() { + this.contextRunner + .withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.rabbitmq.ssl.enabled:true", "spring.rabbitmq.ssl.keyStore=foo", - "spring.rabbitmq.ssl.keyStorePassword=secret", + "spring.rabbitmq.ssl.keyStorePassword=secret") + .run(context -> { + assertThat(context).hasFailed(); + assertThat(context).getFailure().hasMessageContaining("foo"); + assertThat(context).getFailure().hasMessageContaining("does not exist"); + }); + } + + @Test + // Make sure that we at least attempt to load the store + public void enableSslWithNonexistingTruststoreShouldFail() { + this.contextRunner + .withUserConfiguration(TestConfiguration.class) + .withPropertyValues( + "spring.rabbitmq.ssl.enabled:true", "spring.rabbitmq.ssl.trustStore=bar", "spring.rabbitmq.ssl.trustStorePassword=secret") .run((context) -> { assertThat(context).hasFailed(); - assertThat(context).getFailure().hasMessageContaining("foo"); - assertThat(context).getFailure() - .hasMessageContaining("does not exist"); + assertThat(context).getFailure().hasMessageContaining("bar"); + assertThat(context).getFailure().hasMessageContaining("does not exist"); }); } + @Test + public void enableSslWithInvalidKeystoreTypeShouldFail() throws Exception { + this.contextRunner + .withUserConfiguration(TestConfiguration.class) + .withPropertyValues( + "spring.rabbitmq.ssl.enabled:true", + "spring.rabbitmq.ssl.keyStore=foo", + "spring.rabbitmq.ssl.keyStoreType=fooType") + .run(context -> { + assertThat(context).hasFailed(); + assertThat(context).getFailure().hasMessageContaining("fooType"); + assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class); + }); + } + + @Test + public void enableSslWithInvalidTruststoreTypeShouldFail() throws Exception { + this.contextRunner + .withUserConfiguration(TestConfiguration.class) + .withPropertyValues( + "spring.rabbitmq.ssl.enabled:true", + "spring.rabbitmq.ssl.trustStore=bar", + "spring.rabbitmq.ssl.trustStoreType=barType") + .run(context -> { + assertThat(context).hasFailed(); + assertThat(context).getFailure().hasMessageContaining("barType"); + assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class); + }); + } + + @Test + public void enableSslWithKeystoreTypeAndTrustStoreTypeShouldWork() throws Exception { + this.contextRunner + .withUserConfiguration(TestConfiguration.class) + .withPropertyValues( + "spring.rabbitmq.ssl.enabled:true", + "spring.rabbitmq.ssl.keyStore=/org/springframework/boot/autoconfigure/amqp/test.jks", + "spring.rabbitmq.ssl.keyStoreType=jks", + "spring.rabbitmq.ssl.keyStorePassword=secret", + "spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks", + "spring.rabbitmq.ssl.trustStoreType=jks", + "spring.rabbitmq.ssl.trustStorePassword=secret") + .run(context -> assertThat(context).hasNotFailed()); + } + private com.rabbitmq.client.ConnectionFactory getTargetConnectionFactory( AssertableApplicationContext context) { CachingConnectionFactory connectionFactory = context diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/amqp/test.jks b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/amqp/test.jks new file mode 100644 index 0000000000000000000000000000000000000000..8413be810956262d0da0387e8781fc4d27f6b027 GIT binary patch literal 1294 zcmezO_TO6u1_mY|W&~r_+{*0KN+2&TwQlnkAl+}!#Mo`X$Ht}2#>m2`#U#kc$jZRd z#8^J5!~Wmp7|YE9c_o202CEfvPJAt&F-z+E+Y5Je-P@SM3x!M{?<`L$d7kPy>HgY< z0rl+7ddFPf9_^UC?)~PM8U_ciTzNlZu6ENGso!d;PtM$lt<~ROC~|P;oYPIU`ifCm z3xrl>nF}qFRax}#!Gf-mKc9ryWTqXD5RA>>WDIzayKC#wV=9~F(x>fBQsZQ6{U|+U z!WW$>x*Dgfn4=QHB86`zaB53NZvW1sG()7e{`TIV! z&3V4Ogyo;agqT?@rcVEe)EOO%0lu zJ}+QqVq{`s(UY*NH{fPt*J|@PXTieE%3zRVC;*H-=1>+kVfN6x?7aN)JeUXvh6p=` z2p5J3H--o^T*N?5oY&CYz!C_}4NXl=qCi|jBLfR4m(H$fV%z~ty*jYqU<7jHnHn1z z{?-RK%9aXg2Hp3Xqxa^?^KXLNgKXZrFdUY%Z`c;7mgySnpcME!(zZ9^*Z!klcNu&; zJv%h#dZTrNtV#Z-w1n;1GXs_`nLqypOSOQ~A^+Vgum9VAO!|j)`14~cmWlGS{BHhs z`Ka7sCesk%D6xq~-%3BP(@wmu_5PC*v*g^19*oDCL>ZQ_-Kd;Z$Hmz)N^9lLmRQG?vR>ZjR%X@$!Tw1Ryr|naXt4hMbo5@zmuvM zRjkh}`M7UI;DpW<&G#QhJ)O@ssXHyMp*mDGh0(|Q-+50h=EhbAkUKiu|LR|O{i|~8 zG#9?b`_jG2zRtb(d;J9c=WfRw1SLMc;#t4^)bdH$I%1_8mU!~AelPGZ&F0$q@R`9O zTlJYe_wAPLtUcO4Q%WhRoN3F19Qh9Ow1TBSOuwH0IP>TZsa20}Jy&_$o4sju^m418 zzVifG8;&#`T)}fG(e3OcJ!7?pmWGQJ4de}Ef$3J3k420{WPN8vSnXA Date: Mon, 18 Sep 2017 16:43:49 +0200 Subject: [PATCH 2/2] Polish "Added keystore type and truststore type to rabbit properties" Closes gh-10251 --- .../boot/autoconfigure/amqp/RabbitAutoConfiguration.java | 8 ++------ .../boot/autoconfigure/amqp/RabbitProperties.java | 8 ++++---- .../autoconfigure/amqp/RabbitAutoConfigurationTests.java | 6 +++--- .../main/asciidoc/appendix-application-properties.adoc | 4 ++-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java index 2b849b5066..38532b0adc 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration.java @@ -113,14 +113,10 @@ public class RabbitAutoConfiguration { if (ssl.getAlgorithm() != null) { factory.setSslAlgorithm(ssl.getAlgorithm()); } - if (ssl.getKeyStoreType() != null) { - factory.setKeyStoreType(ssl.getKeyStoreType()); - } + factory.setKeyStoreType(ssl.getKeyStoreType()); factory.setKeyStore(ssl.getKeyStore()); factory.setKeyStorePassphrase(ssl.getKeyStorePassword()); - if (ssl.getTrustStoreType() != null) { - factory.setTrustStoreType(ssl.getTrustStoreType()); - } + factory.setTrustStoreType(ssl.getTrustStoreType()); factory.setTrustStore(ssl.getTrustStore()); factory.setTrustStorePassphrase(ssl.getTrustStorePassword()); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java index 2b57df1cd1..8d2fe9afd1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/amqp/RabbitProperties.java @@ -315,9 +315,9 @@ public class RabbitProperties { private String keyStore; /** - * Set the key store type (jks, pkcs12, etc). + * Key store type. */ - private String keyStoreType; + private String keyStoreType = "PKCS12"; /** * Password used to access the key store. @@ -330,9 +330,9 @@ public class RabbitProperties { private String trustStore; /** - * Set the trust store type (jks, pkcs12, etc). + * Trust store type. */ - private String trustStoreType; + private String trustStoreType = "JKS"; /** * Password used to access the trust store. diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java index 782c426b28..4e116b6025 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/amqp/RabbitAutoConfigurationTests.java @@ -542,7 +542,7 @@ public class RabbitAutoConfigurationTests { @Test // Make sure that we at least attempt to load the store - public void enableSslWithNonexistingKeystoreShouldFail() { + public void enableSslWithNonExistingKeystoreShouldFail() { this.contextRunner .withUserConfiguration(TestConfiguration.class) .withPropertyValues("spring.rabbitmq.ssl.enabled:true", @@ -557,7 +557,7 @@ public class RabbitAutoConfigurationTests { @Test // Make sure that we at least attempt to load the store - public void enableSslWithNonexistingTruststoreShouldFail() { + public void enableSslWithNonExistingTrustStoreShouldFail() { this.contextRunner .withUserConfiguration(TestConfiguration.class) .withPropertyValues( @@ -587,7 +587,7 @@ public class RabbitAutoConfigurationTests { } @Test - public void enableSslWithInvalidTruststoreTypeShouldFail() throws Exception { + public void enableSslWithInvalidTrustStoreTypeShouldFail() throws Exception { this.contextRunner .withUserConfiguration(TestConfiguration.class) .withPropertyValues( diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 3e5e7acf3c..8c0e997ac3 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -1051,11 +1051,11 @@ content into your application; rather pick only the properties that you need. spring.rabbitmq.requested-heartbeat= # Requested heartbeat timeout, in seconds; zero for none. spring.rabbitmq.ssl.enabled=false # Enable SSL support. spring.rabbitmq.ssl.key-store= # Path to the key store that holds the SSL certificate. - spring.rabbitmq.ssl.key-store-type= # Type of key store (jks, pkcs12,..). Defaults to pkcs12 if not set spring.rabbitmq.ssl.key-store-password= # Password used to access the key store. + spring.rabbitmq.ssl.key-store-type=PKCS12 # Key store type. spring.rabbitmq.ssl.trust-store= # Trust store that holds SSL certificates. - spring.rabbitmq.ssl.trust-store-type= # Type of trust store (jks, pkcs12,..). Defaults to jks if not set spring.rabbitmq.ssl.trust-store-password= # Password used to access the trust store. + spring.rabbitmq.ssl.trust-store-type=JKS # Trust store type. spring.rabbitmq.ssl.algorithm= # SSL algorithm to use. By default configure by the rabbit client library. spring.rabbitmq.template.mandatory=false # Enable mandatory messages. spring.rabbitmq.template.receive-timeout=0 # Timeout for `receive()` methods.