Omit system properties and env vars from placeholders in config
When replacing placeholders in the .yml, .properties and .json endpoints, only the properties from the config repo (not the local system and environment variables) ought to be included.
This commit is contained in:
@@ -13,13 +13,17 @@ public class EnvironmentPropertySource extends PropertySource<Environment> {
|
||||
|
||||
public static StandardEnvironment prepareEnvironment(Environment environment) {
|
||||
StandardEnvironment standardEnvironment = new StandardEnvironment();
|
||||
standardEnvironment.getPropertySources().addAfter(
|
||||
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME,
|
||||
new EnvironmentPropertySource(environment));
|
||||
standardEnvironment.getPropertySources()
|
||||
.remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
standardEnvironment.getPropertySources()
|
||||
.remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
|
||||
standardEnvironment.getPropertySources()
|
||||
.addFirst(new EnvironmentPropertySource(environment));
|
||||
return standardEnvironment;
|
||||
}
|
||||
|
||||
public static String resolvePlaceholders(StandardEnvironment preparedEnvironment, String text) {
|
||||
public static String resolvePlaceholders(StandardEnvironment preparedEnvironment,
|
||||
String text) {
|
||||
// Mask out escaped placeholders
|
||||
text = text.replace("\\${", "$_{");
|
||||
return preparedEnvironment.resolvePlaceholders(text).replace("$_{", "${");
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -63,6 +64,11 @@ public class EnvironmentControllerTests {
|
||||
this.controller = new EnvironmentController(this.repository);
|
||||
}
|
||||
|
||||
@After
|
||||
public void clean() {
|
||||
System.clearProperty("foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vanillaYaml() throws Exception {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
@@ -107,6 +113,20 @@ public class EnvironmentControllerTests {
|
||||
assertEquals("a:\n b:\n c: bar\nfoo: bar\n", yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInYaml() throws Exception {
|
||||
whenPlaceholders();
|
||||
String yaml = this.controller.yaml("foo", "bar", false).getBody();
|
||||
assertEquals("a:\n b:\n c: ${foo}\nfoo: bar\n", yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInYamlFromSystemProperties() throws Exception {
|
||||
whenPlaceholdersSystemProps();
|
||||
String yaml = this.controller.yaml("foo", "bar", true).getBody();
|
||||
assertEquals("a:\n b:\n c: ${foo}\n", yaml);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void arrayInYaml() throws Exception {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
@@ -243,6 +263,20 @@ public class EnvironmentControllerTests {
|
||||
assertEquals("a.b.c: bar\nfoo: bar", text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInProperties() throws Exception {
|
||||
whenPlaceholders();
|
||||
String text = this.controller.properties("foo", "bar", false).getBody();
|
||||
assertEquals("a.b.c: ${foo}\nfoo: bar", text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInPropertiesFromSystemProperties() throws Exception {
|
||||
whenPlaceholdersSystemProps();
|
||||
String text = this.controller.properties("foo", "bar", true).getBody();
|
||||
assertEquals("a.b.c: ${foo}", text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersResolvedInJson() throws Exception {
|
||||
whenPlaceholders();
|
||||
@@ -250,6 +284,20 @@ public class EnvironmentControllerTests {
|
||||
assertEquals("{\"a\":{\"b\":{\"c\":\"bar\"}},\"foo\":\"bar\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInJson() throws Exception {
|
||||
whenPlaceholders();
|
||||
String json = this.controller.jsonProperties("foo", "bar", false).getBody();
|
||||
assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}},\"foo\":\"bar\"}", json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void placeholdersNotResolvedInJsonFromSystemProperties() throws Exception {
|
||||
whenPlaceholdersSystemProps();
|
||||
String json = this.controller.jsonProperties("foo", "bar", true).getBody();
|
||||
assertEquals("{\"a\":{\"b\":{\"c\":\"${foo}\"}}}", json);
|
||||
}
|
||||
|
||||
private void whenPlaceholders() {
|
||||
Map<String, Object> map = new LinkedHashMap<String, Object>();
|
||||
map.put("foo", "bar");
|
||||
@@ -258,6 +306,12 @@ public class EnvironmentControllerTests {
|
||||
Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
|
||||
}
|
||||
|
||||
private void whenPlaceholdersSystemProps() {
|
||||
System.setProperty("foo", "bar");
|
||||
this.environment.addFirst(new PropertySource("two", Collections.singletonMap("a.b.c", "${foo}")));
|
||||
Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mappingForEnvironment() throws Exception {
|
||||
Mockito.when(this.repository.findOne("foo", "bar", null)).thenReturn(this.environment);
|
||||
|
||||
Reference in New Issue
Block a user