Skip to content

Commit

Permalink
Add configuration option to configure RabbitConnectionFactory key sto…
Browse files Browse the repository at this point in the history
…re and trust store algorithm

Add keyStoreAlgorithm and trustStoreAlgorithm to RabbitProperties and adopt at Rabbit AutoConfig.
  • Loading branch information
topikachu committed Nov 9, 2020
1 parent dfe3058 commit 6ec0cf2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ private RabbitConnectionFactoryBean getRabbitConnectionFactoryBean(RabbitPropert
map.from(ssl::getKeyStoreType).to(factory::setKeyStoreType);
map.from(ssl::getKeyStore).to(factory::setKeyStore);
map.from(ssl::getKeyStorePassword).to(factory::setKeyStorePassphrase);
map.from(ssl::getKeyStoreAlgorithm).whenNonNull().to(factory::setKeyStoreAlgorithm);
map.from(ssl::getTrustStoreType).to(factory::setTrustStoreType);
map.from(ssl::getTrustStore).to(factory::setTrustStore);
map.from(ssl::getTrustStorePassword).to(factory::setTrustStorePassphrase);
map.from(ssl::getTrustStoreAlgorithm).whenNonNull().to(factory::setTrustStoreAlgorithm);
map.from(ssl::isValidateServerCertificate)
.to((validate) -> factory.setSkipServerCertificateValidation(!validate));
map.from(ssl::getVerifyHostname).to(factory::setEnableHostnameVerification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ public Template getTemplate() {

public class Ssl {

private static final String SUN_X509 = "SunX509";

/**
* Whether to enable SSL support. Determined automatically if an address is
* provided with the protocol (amqp:// vs. amqps://).
Expand All @@ -384,6 +386,11 @@ public class Ssl {
*/
private String keyStorePassword;

/**
* Key store algorithm.
*/
private String keyStoreAlgorithm = SUN_X509;

/**
* Trust store that holds SSL certificates.
*/
Expand All @@ -399,6 +406,11 @@ public class Ssl {
*/
private String trustStorePassword;

/**
* Trust store algorithm.
*/
private String trustStoreAlgorithm = SUN_X509;

/**
* SSL algorithm to use. By default, configured by the Rabbit client library.
*/
Expand Down Expand Up @@ -462,6 +474,14 @@ public void setKeyStorePassword(String keyStorePassword) {
this.keyStorePassword = keyStorePassword;
}

public String getKeyStoreAlgorithm() {
return this.keyStoreAlgorithm;
}

public void setKeyStoreAlgorithm(String keyStoreAlgorithm) {
this.keyStoreAlgorithm = keyStoreAlgorithm;
}

public String getTrustStore() {
return this.trustStore;
}
Expand All @@ -486,6 +506,14 @@ public void setTrustStorePassword(String trustStorePassword) {
this.trustStorePassword = trustStorePassword;
}

public String getTrustStoreAlgorithm() {
return this.trustStoreAlgorithm;
}

public void setTrustStoreAlgorithm(String trustStoreAlgorithm) {
this.trustStoreAlgorithm = trustStoreAlgorithm;
}

public String getAlgorithm() {
return this.algorithm;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,36 @@ void enableSslWithValidateServerCertificateDefault() throws Exception {
});
}

@Test
void enableSslWithValidStoreAlgorithmShouldWork() 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.keyStoreAlgorithm=PKIX",
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
"spring.rabbitmq.ssl.trustStoreType=jks", "spring.rabbitmq.ssl.trustStorePassword=secret",
"spring.rabbitmq.ssl.trustStoreAlgorithm=PKIX")
.run((context) -> assertThat(context).hasNotFailed());
}

@Test
void enableSslWithInvalidStoreAlgorithmShouldFail() 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.keyStoreAlgorithm=foo",
"spring.rabbitmq.ssl.trustStore=/org/springframework/boot/autoconfigure/amqp/test.jks",
"spring.rabbitmq.ssl.trustStoreType=jks", "spring.rabbitmq.ssl.trustStorePassword=secret",
"spring.rabbitmq.ssl.trustStoreAlgorithm=foo")
.run((context) -> {
assertThat(context).hasFailed();
assertThat(context).getFailure().hasMessageContaining("foo");
assertThat(context).getFailure().hasRootCauseInstanceOf(NoSuchAlgorithmException.class);
});
}

@Test
void whenACredentialsProviderIsAvailableThenConnectionFactoryIsConfiguredToUseIt() throws Exception {
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)
Expand Down

0 comments on commit 6ec0cf2

Please sign in to comment.