Browse Source

Show better message for missing config keystore props (#232)

* Show better message for missing config keystore props

* Removes unused ExpectedException

Fixes #230
pull/241/head
Biju Kunjummen 8 years ago committed by Spencer Gibb
parent
commit
a6b7f70b8e
  1. 21
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java
  2. 24
      spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java

21
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java

@ -63,15 +63,20 @@ public class EncryptionBootstrapConfiguration { @@ -63,15 +63,20 @@ public class EncryptionBootstrapConfiguration {
@ConditionalOnMissingBean(TextEncryptor.class)
public TextEncryptor textEncryptor() {
KeyStore keyStore = this.key.getKeyStore();
if (keyStore.getLocation() != null && keyStore.getLocation().exists()) {
return new RsaSecretEncryptor(
new KeyStoreKeyFactory(keyStore.getLocation(),
keyStore.getPassword().toCharArray()).getKeyPair(
keyStore.getAlias(),
keyStore.getSecret().toCharArray()),
this.key.getRsa().getAlgorithm(), this.key.getRsa().getSalt(),
this.key.getRsa().isStrong());
if (keyStore.getLocation() != null) {
if (keyStore.getLocation().exists()) {
return new RsaSecretEncryptor(
new KeyStoreKeyFactory(keyStore.getLocation(),
keyStore.getPassword().toCharArray()).getKeyPair(
keyStore.getAlias(),
keyStore.getSecret().toCharArray()),
this.key.getRsa().getAlgorithm(), this.key.getRsa().getSalt(),
this.key.getRsa().isStrong());
}
throw new IllegalStateException("Invalid keystore location");
}
return new EncryptorFactory().create(this.key.getKey());
}

24
spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfigurationTests.java

@ -1,12 +1,13 @@ @@ -1,12 +1,13 @@
package org.springframework.cloud.bootstrap.encrypt;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class EncryptionBootstrapConfigurationTests {
@Test
@ -35,4 +36,23 @@ public class EncryptionBootstrapConfigurationTests { @@ -35,4 +36,23 @@ public class EncryptionBootstrapConfigurationTests {
context.close();
}
@Test
public void nonExistentKeystoreLocationShouldNotBeAllowed() {
try {
new SpringApplicationBuilder(EncryptionBootstrapConfiguration.class)
.web(false)
.properties("encrypt.key-store.location:classpath:/server.jks1",
"encrypt.key-store.password:letmein",
"encrypt.key-store.alias:mytestkey",
"encrypt.key-store.secret:changeme")
.run();
assertThat(false)
.as("Should not create an application context with invalid keystore location")
.isTrue();
}
catch (Exception e) {
assertThat(e).hasRootCauseInstanceOf(IllegalStateException.class);
}
}
}

Loading…
Cancel
Save