From 33e85bdf58fc216ab64adfe1fd550618fbe74809 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 22 Feb 2018 11:08:32 +0000 Subject: [PATCH] Add encrypt.salt optional configuration --- .gitignore | 2 + .../config/EncryptionAutoConfiguration.java | 15 +++--- .../CipherEnvironmentEncryptorTests.java | 51 ++++++++++++++----- 3 files changed, 47 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 9df3ab04..517cad80 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ _site/ .classpath .project .settings +.sts4-cache/ +.attach_pid* .springBeans .DS_Store *.sw* diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EncryptionAutoConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EncryptionAutoConfiguration.java index 124747b2..2ff51037 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EncryptionAutoConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EncryptionAutoConfiguration.java @@ -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(); } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/encryption/CipherEnvironmentEncryptorTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/encryption/CipherEnvironmentEncryptorTests.java index d008b5fa..f41c051f 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/encryption/CipherEnvironmentEncryptorTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/encryption/CipherEnvironmentEncryptorTests.java @@ -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 keys) { - return CipherEnvironmentEncryptorTests.this.textEncryptor; - } - }); + @Parameters + public static List params() { + List 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 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.singletonMap(environment.getName(), "{cipher}" + this.textEncryptor.encrypt(secret)))); + environment.add(new PropertySource("a", Collections.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.singletonMap(environment.getName(), "{cipher}{key:test}" + this.textEncryptor.encrypt(secret)))); + Collections.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())); } }