Add encrypt.salt optional configuration

This commit is contained in:
Dave Syer
2018-02-22 11:08:32 +00:00
parent 40880bf7bd
commit 33e85bdf58
3 changed files with 47 additions and 21 deletions

2
.gitignore vendored
View File

@@ -10,6 +10,8 @@ _site/
.classpath
.project
.settings
.sts4-cache/
.attach_pid*
.springBeans
.DS_Store
*.sw*

View File

@@ -42,9 +42,9 @@ import org.springframework.security.rsa.crypto.RsaSecretEncryptor;
import org.springframework.util.StringUtils;
/**
* Auto configuration for text encryptors and environment encryptors (non-web
* stuff). Users can provide beans of the same type as any or all of the beans
* defined here in application code to override the default behaviour.
* Auto configuration for text encryptors and environment encryptors (non-web stuff).
* Users can provide beans of the same type as any or all of the beans defined here in
* application code to override the default behaviour.
*
* @author Bartosz Wojtkiewicz
* @author Rafal Zukowski
@@ -53,7 +53,8 @@ import org.springframework.util.StringUtils;
*/
@Configuration
@EnableConfigurationProperties(KeyProperties.class)
@Import({SingleTextEncryptorConfiguration.class, DefaultTextEncryptorConfiguration.class})
@Import({ SingleTextEncryptorConfiguration.class,
DefaultTextEncryptorConfiguration.class })
public class EncryptionAutoConfiguration {
@Configuration
@@ -91,7 +92,8 @@ public class EncryptionAutoConfiguration {
public TextEncryptorLocator textEncryptorLocator() {
KeyStore keyStore = this.key.getKeyStore();
KeyStoreTextEncryptorLocator locator = new KeyStoreTextEncryptorLocator(
new KeyStoreKeyFactory(keyStore.getLocation(), keyStore.getPassword().toCharArray()),
new KeyStoreKeyFactory(keyStore.getLocation(),
keyStore.getPassword().toCharArray()),
keyStore.getSecret(), keyStore.getAlias());
RsaAlgorithm algorithm = this.key.getRsa().getAlgorithm();
locator.setRsaAlgorithm(algorithm);
@@ -104,7 +106,6 @@ public class EncryptionAutoConfiguration {
}
@ConditionalOnBean(TextEncryptor.class)
@ConditionalOnMissingBean(TextEncryptorLocator.class)
@Configuration
@@ -136,7 +137,7 @@ class DefaultTextEncryptorConfiguration {
return new LocatorTextEncryptor(locator);
}
if (StringUtils.hasText(this.key.getKey())) {
return new EncryptorFactory().create(this.key.getKey());
return new EncryptorFactory(this.key.getSalt()).create(this.key.getKey());
}
return Encryptors.noOpText();
}

View File

@@ -16,29 +16,49 @@
package org.springframework.cloud.config.server.encryption;
import static java.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.context.encrypt.EncryptorFactory;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import static java.util.UUID.randomUUID;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class CipherEnvironmentEncryptorTests {
TextEncryptor textEncryptor = new EncryptorFactory().create("foo");
EnvironmentEncryptor encryptor = new CipherEnvironmentEncryptor(new TextEncryptorLocator() {
EnvironmentEncryptor encryptor;
@Override
public TextEncryptor locate(Map<String, String> keys) {
return CipherEnvironmentEncryptorTests.this.textEncryptor;
}
});
@Parameters
public static List<Object[]> params() {
List<Object[]> list = new ArrayList<>();
list.add(new Object[] { "deadbeef", "foo" });
list.add(new Object[] { "4567890a12345678", "bar" });
return list;
}
public CipherEnvironmentEncryptorTests(String salt, String key) {
textEncryptor = new EncryptorFactory(salt).create(key);
encryptor = new CipherEnvironmentEncryptor(new TextEncryptorLocator() {
@Override
public TextEncryptor locate(Map<String, String> keys) {
return CipherEnvironmentEncryptorTests.this.textEncryptor;
}
});
}
@Test
public void shouldDecryptEnvironment() {
@@ -47,11 +67,12 @@ public class CipherEnvironmentEncryptorTests {
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), "{cipher}" + this.textEncryptor.encrypt(secret))));
environment.add(new PropertySource("a", Collections.<Object, Object>singletonMap(
environment.getName(), "{cipher}" + this.textEncryptor.encrypt(secret))));
// then
assertEquals(secret, this.encryptor.decrypt(environment).getPropertySources().get(0).getSource().get(environment.getName()));
assertEquals(secret, this.encryptor.decrypt(environment).getPropertySources()
.get(0).getSource().get(environment.getName()));
}
@Test
@@ -62,10 +83,12 @@ public class CipherEnvironmentEncryptorTests {
// when
Environment environment = new Environment("name", "profile", "label");
environment.add(new PropertySource("a",
Collections.<Object, Object>singletonMap(environment.getName(), "{cipher}{key:test}" + this.textEncryptor.encrypt(secret))));
Collections.<Object, Object>singletonMap(environment.getName(),
"{cipher}{key:test}" + this.textEncryptor.encrypt(secret))));
// then
assertEquals(secret, this.encryptor.decrypt(environment).getPropertySources().get(0).getSource().get(environment.getName()));
assertEquals(secret, this.encryptor.decrypt(environment).getPropertySources()
.get(0).getSource().get(environment.getName()));
}
}