[bs-148] Add /env endpoint for Spring Environment

Any enumerable property source is enumerated.  Plus all
properties are available through /env/{name}.

[Fixes #51141441]
pull/2/merge
Dave Syer 12 years ago
parent 6c5f9a5961
commit 4ee6a90edd

@ -0,0 +1,49 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.autoconfigure;
import javax.servlet.Servlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.bootstrap.actuate.endpoint.env.EnvEndpoint;
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.servlet.DispatcherServlet;
/**
* {@link EnableAutoConfiguration Auto-configuration} for /metrics endpoint.
*
* @author Dave Syer
*/
@Configuration
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class })
@ConditionalOnMissingBean({ EnvEndpoint.class })
public class EnvConfiguration {
@Autowired
private ConfigurableEnvironment environment;
@Bean
public EnvEndpoint envEndpoint() {
return new EnvEndpoint(this.environment);
}
}

@ -28,7 +28,8 @@ import org.springframework.context.annotation.Import;
@Configuration
@ConditionalOnManagementContext
@Import({ MetricsConfiguration.class, HealthConfiguration.class,
ShutdownConfiguration.class, TraceConfiguration.class, BeansConfiguration.class })
ShutdownConfiguration.class, TraceConfiguration.class, BeansConfiguration.class,
EnvConfiguration.class })
public class ManagementEndpointsRegistration {
}

@ -0,0 +1,75 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.bootstrap.actuate.endpoint.env;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author Dave Syer
*/
@Controller
public class EnvEndpoint {
private final ConfigurableEnvironment environment;
public EnvEndpoint(ConfigurableEnvironment environment) {
this.environment = environment;
}
@RequestMapping("${endpoints.metrics.path:/env}")
@ResponseBody
public Map<String, Object> env() {
Map<String, Object> result = new LinkedHashMap<String, Object>();
for (PropertySource<?> source : this.environment.getPropertySources()) {
if (source instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
Map<String, Object> map = new LinkedHashMap<String, Object>();
for (String name : enumerable.getPropertyNames()) {
map.put(name, sanitize(name, enumerable.getProperty(name)));
}
result.put(source.getName(), map);
}
}
return result;
}
@RequestMapping("${endpoints.metrics.path:/env}/{name:[a-zA-Z0-9._-]+}")
@ResponseBody
public Map<String, Object> env(@PathVariable String name) {
Map<String, Object> result = new LinkedHashMap<String, Object>();
result.put(name, sanitize(name, this.environment.getProperty(name)));
return result;
}
private Object sanitize(String name, Object object) {
if (name.toLowerCase().endsWith("password")
|| name.toLowerCase().endsWith("secret")) {
return object == null ? null : "******";
}
return object;
}
}

@ -54,6 +54,9 @@ public class EndpointsProperties {
@Valid
private Endpoint beans = new Endpoint("/beans");
@Valid
private Endpoint env = new Endpoint("/env");
public Endpoint getInfo() {
return this.info;
}
@ -86,6 +89,10 @@ public class EndpointsProperties {
return this.beans;
}
public Endpoint getEnv() {
return this.env;
}
public static class Endpoint {
@NotNull
@ -111,7 +118,8 @@ public class EndpointsProperties {
public String[] getSecurePaths() {
return new String[] { getMetrics().getPath(), getBeans().getPath(),
getDump().getPath(), getShutdown().getPath(), getTrace().getPath() };
getDump().getPath(), getShutdown().getPath(), getTrace().getPath(),
getEnv().getPath() };
}
public String[] getOpenPaths() {

@ -97,6 +97,28 @@ public class ServiceBootstrapApplicationTests {
assertTrue("Wrong body: " + body, body.containsKey("counter.status.200.root"));
}
@Test
public void testEnv() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate("user", "password").getForEntity(
"http://localhost:8080/env", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertTrue("Wrong body: " + body, body.containsKey("systemProperties"));
}
@Test
public void testEnvProperty() throws Exception {
@SuppressWarnings("rawtypes")
ResponseEntity<Map> entity = getRestTemplate("user", "password").getForEntity(
"http://localhost:8080/env/logging.file", Map.class);
assertEquals(HttpStatus.OK, entity.getStatusCode());
@SuppressWarnings("unchecked")
Map<String, Object> body = entity.getBody();
assertEquals("{logging.file=/tmp/logs/app.log}", body.toString());
}
@Test
public void testHealth() throws Exception {
ResponseEntity<String> entity = getRestTemplate().getForEntity(

Loading…
Cancel
Save