Allow environment overrides to be escaped for placeholders

E.g. spring.cloud.config.server.overrides.foo=$\{bar} will evaluate
to foo=${bar} in the client's Environment (allowing local replacement
of the placeholder instead of on the configserver).
This commit is contained in:
Dave Syer
2015-04-03 10:32:41 +01:00
parent 91d12e7c56
commit 280a7a2c09
2 changed files with 24 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.cloud.config.server;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -27,7 +28,6 @@ import java.util.TreeMap;
import javax.servlet.http.HttpServletResponse;
import org.yaml.snakeyaml.Yaml;
import org.springframework.boot.bind.PropertiesConfigurationFactory;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
@@ -72,7 +72,8 @@ public class EnvironmentController {
}
@RequestMapping("/{name}/{profiles:.*[^-].*}")
public Environment defaultLabel(@PathVariable String name, @PathVariable String profiles) {
public Environment defaultLabel(@PathVariable String name,
@PathVariable String profiles) {
return labelled(name, profiles, defaultLabel);
}
@@ -159,7 +160,7 @@ public class EnvironmentController {
factory.bindPropertiesToTarget();
@SuppressWarnings("unchecked")
Map<String, Object> result = (Map<String, Object>) target.get(MAP_PREFIX);
return result==null ? new LinkedHashMap<String, Object>() : result;
return result == null ? new LinkedHashMap<String, Object>() : result;
}
@ExceptionHandler(IllegalArgumentException.class)
@@ -185,9 +186,9 @@ public class EnvironmentController {
HttpStatus.OK);
}
private ResponseEntity<Map<String, Object>> getSuccess(Map<String, Object> body, MediaType mediaType) {
return new ResponseEntity<>(body, getHttpHeaders(mediaType),
HttpStatus.OK);
private ResponseEntity<Map<String, Object>> getSuccess(Map<String, Object> body,
MediaType mediaType) {
return new ResponseEntity<>(body, getHttpHeaders(mediaType), HttpStatus.OK);
}
/**
@@ -272,7 +273,12 @@ public class EnvironmentController {
* @param overrides the overrides to set
*/
public void setOverrides(Map<String, String> overrides) {
this.overrides = overrides;
this.overrides = new HashMap<String, String>(overrides);
for (String key : overrides.keySet()) {
if (overrides.get(key).contains("$\\{")) {
this.overrides.put(key, overrides.get(key).replace("$\\{", "${"));
}
}
}
}

View File

@@ -234,4 +234,15 @@ public class EnvironmentControllerTests {
.get(0).getSource().toString());
}
@Test
public void overrideWithEscapedPlaceholders() throws Exception {
controller.setOverrides(Collections.singletonMap("foo", "$\\{bar}"));
Map<String, Object> map = new HashMap<String, Object>();
map.put("bar", "foo");
environment.add(new PropertySource("one", map));
Mockito.when(repository.findOne("foo", "bar", "master")).thenReturn(environment);
assertEquals("{foo=${bar}}", controller.defaultLabel("foo", "bar").getPropertySources()
.get(0).getSource().toString());
}
}