Skip to content

Commit

Permalink
Setting property encrypt.key to enable symmetric encryption does no…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
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);
}

}
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
encrypt:
key: foobar

0 comments on commit c713f43

Please sign in to comment.