Skip to content

Commit a6b7f70

Browse files
bijukunjummenspencergibb
authored andcommitted
Show better message for missing config keystore props (spring-projects#232)
* Show better message for missing config keystore props * Removes unused ExpectedException Fixes spring-projects#230
1 parent 54d5501 commit a6b7f70

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

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

+13-8
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,20 @@ protected static class RsaEncryptionConfiguration {
6363
@ConditionalOnMissingBean(TextEncryptor.class)
6464
public TextEncryptor textEncryptor() {
6565
KeyStore keyStore = this.key.getKeyStore();
66-
if (keyStore.getLocation() != null && keyStore.getLocation().exists()) {
67-
return new RsaSecretEncryptor(
68-
new KeyStoreKeyFactory(keyStore.getLocation(),
69-
keyStore.getPassword().toCharArray()).getKeyPair(
70-
keyStore.getAlias(),
71-
keyStore.getSecret().toCharArray()),
72-
this.key.getRsa().getAlgorithm(), this.key.getRsa().getSalt(),
73-
this.key.getRsa().isStrong());
66+
if (keyStore.getLocation() != null) {
67+
if (keyStore.getLocation().exists()) {
68+
return new RsaSecretEncryptor(
69+
new KeyStoreKeyFactory(keyStore.getLocation(),
70+
keyStore.getPassword().toCharArray()).getKeyPair(
71+
keyStore.getAlias(),
72+
keyStore.getSecret().toCharArray()),
73+
this.key.getRsa().getAlgorithm(), this.key.getRsa().getSalt(),
74+
this.key.getRsa().isStrong());
75+
}
76+
77+
throw new IllegalStateException("Invalid keystore location");
7478
}
79+
7580
return new EncryptorFactory().create(this.key.getKey());
7681
}
7782

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

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.springframework.cloud.bootstrap.encrypt;
22

3-
import static org.junit.Assert.assertEquals;
4-
53
import org.junit.Test;
64
import org.springframework.boot.builder.SpringApplicationBuilder;
75
import org.springframework.context.ConfigurableApplicationContext;
86
import org.springframework.security.crypto.encrypt.TextEncryptor;
97

8+
import static org.assertj.core.api.Assertions.assertThat;
9+
import static org.junit.Assert.assertEquals;
10+
1011
public class EncryptionBootstrapConfigurationTests {
1112

1213
@Test
@@ -35,4 +36,23 @@ public void rsaKeyStoreWithRelaxedProperties() {
3536
context.close();
3637
}
3738

39+
@Test
40+
public void nonExistentKeystoreLocationShouldNotBeAllowed() {
41+
try {
42+
new SpringApplicationBuilder(EncryptionBootstrapConfiguration.class)
43+
.web(false)
44+
.properties("encrypt.key-store.location:classpath:/server.jks1",
45+
"encrypt.key-store.password:letmein",
46+
"encrypt.key-store.alias:mytestkey",
47+
"encrypt.key-store.secret:changeme")
48+
.run();
49+
assertThat(false)
50+
.as("Should not create an application context with invalid keystore location")
51+
.isTrue();
52+
}
53+
catch (Exception e) {
54+
assertThat(e).hasRootCauseInstanceOf(IllegalStateException.class);
55+
}
56+
}
57+
3858
}

0 commit comments

Comments
 (0)