-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting property
encrypt.key
to enable symmetric encryption does no…
…t work since Dalston SR2. Fixed by creating a default `TextEncryptorLocator` when the '`encrypt.key` is set
- Loading branch information
Ollie Hughes
committed
Jul 27, 2017
1 parent
ebbe75d
commit c713f43
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...c/main/java/org/springframework/cloud/config/server/encryption/SymmetricKeyEncryptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.springframework.cloud.config.server.encryption; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.encrypt.TextEncryptor; | ||
|
||
/** | ||
* Provide a default {@link TextEncryptorLocator} when a symmetric key is configured | ||
* | ||
* @author Ollie Hughes | ||
* | ||
*/ | ||
@Configuration | ||
@ConditionalOnProperty(value = "encrypt.key", matchIfMissing = false) | ||
public class SymmetricKeyEncryptor { | ||
|
||
@Autowired | ||
private TextEncryptor encryptor; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public TextEncryptorLocator defaultTextEncryptorLocator() { | ||
return new SingleTextEncryptorLocator(this.encryptor); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...g/springframework/cloud/config/server/encryption/SymmetricEncryptionIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.springframework.cloud.config.server.encryption; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.cloud.config.server.ConfigServerApplication; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SymmetricEncryptionIntegrationTests { | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {ConfigServerApplication.class,SymmetricKeyEncryptor.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles({"test", "native"}) | ||
public static class SpringAppJsonConfigSymmetricEncryptionIntegrationTests { | ||
|
||
@BeforeClass | ||
public static void setupEnvironmentProperties() { | ||
System.setProperty("SPRING_APPLICATION_JSON", "{\"encrypt\": {\"key\": \"foobar\"}}"); | ||
} | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
|
||
@Test | ||
public void symmetricEncryptionSpringAppJson() throws Exception { | ||
ResponseEntity<String> entity = testRestTemplate.getForEntity("/encrypt/status", String.class); | ||
assertEquals(entity.getStatusCode().value(), 200); | ||
} | ||
} | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {ConfigServerApplication.class,SymmetricKeyEncryptor.class}, | ||
properties = "spring.cloud.bootstrap.name:symmetric-key-bootstrap", | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles({"test", "native"}) | ||
public static class BoostrapConfigSymmetricEncryptionIntegrationTests { | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
|
||
@Test | ||
public void symmetricEncryptionBootstrapConfig() throws Exception { | ||
ResponseEntity<String> entity = testRestTemplate.getForEntity("/encrypt/status", String.class); | ||
assertEquals(entity.getStatusCode().value(), 200); | ||
} | ||
} | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {ConfigServerApplication.class,SymmetricKeyEncryptor.class}, | ||
properties = "spring.config.name:symmetric-key-bootstrap", | ||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@ActiveProfiles({"test", "native"}) | ||
public static class ApplicationConfigSymmetricEncryptionIntegrationTests { | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
|
||
@Test | ||
public void symmetricEncryptionCannotBeConfiguredInApplicationContext() throws Exception { | ||
ResponseEntity<String> entity = testRestTemplate.getForEntity("/encrypt/status", String.class); | ||
assertEquals(entity.getStatusCode().value(), 404); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
spring-cloud-config-server/src/test/resources/symmetric-key-bootstrap.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
encrypt: | ||
key: foobar |