From b54c8e80e8b2530fff89b7ea3430fbb628e17c7d Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 18 Apr 2016 10:04:56 +0100 Subject: [PATCH] Add support for endpoints.env.post.enabled=false Separately disable the POST /env from the GET. Fixes gh-106. --- ...LifecycleMvcEndpointAutoConfiguration.java | 2 +- ...itional-spring-configuration-metadata.json | 33 +++++++++++++++++++ .../LifecycleMvcAutoConfigurationTests.java | 28 ++++++++++++---- 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 spring-cloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java index b218e110..39af35d4 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java @@ -43,7 +43,6 @@ import org.springframework.context.annotation.Configuration; */ @Configuration @ConditionalOnClass(EnvironmentEndpoint.class) -@ConditionalOnProperty(value = "endpoints.env.enabled", matchIfMissing = true) @ConditionalOnWebApplication @ConditionalOnBean(RestartEndpoint.class) @AutoConfigureAfter({ WebMvcAutoConfiguration.class, @@ -55,6 +54,7 @@ public class LifecycleMvcEndpointAutoConfiguration { @Bean @ConditionalOnBean(EnvironmentEndpoint.class) + @ConditionalOnProperty(value = "endpoints.env.post.enabled", matchIfMissing = true) public EnvironmentManagerMvcEndpoint environmentManagerEndpoint( EnvironmentEndpoint delegate, EnvironmentManager environment) { return new EnvironmentManagerMvcEndpoint(delegate, environment); diff --git a/spring-cloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 00000000..292c16a5 --- /dev/null +++ b/spring-cloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,33 @@ +{"properties": [ + { + "name": "endpoints.env.post.enabled", + "type": "java.lang.Boolean", + "description": "Enable changing the Environment through a POST to /env.", + "defaultValue": true + }, + { + "name": "endpoints.refresh.enabled", + "type": "java.lang.Boolean", + "description": "Enable the /refresh endpoint to refresh configuration and re-initialize refresh scoped beans.", + "defaultValue": true + }, + { + "name": "endpoints.restart.enabled", + "type": "java.lang.Boolean", + "description": "Enable the /restart endpoint to restart the application context.", + "defaultValue": true + }, + { + "name": "endpoints.pause.enabled", + "type": "java.lang.Boolean", + "description": "Enable the /pause endpoint (to send Lifecycle.stop()).", + "defaultValue": true + }, + { + "name": "endpoints.resume.enabled", + "type": "java.lang.Boolean", + "description": "Enable the /resume endpoint (to send Lifecycle.start()).", + "defaultValue": true + } +]} + diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java index 10068e95..78270451 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java @@ -4,6 +4,7 @@ import org.junit.Test; import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.context.environment.EnvironmentManagerMvcEndpoint; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; @@ -19,6 +20,16 @@ import static org.junit.Assert.assertThat; */ public class LifecycleMvcAutoConfigurationTests { + @Test + public void postEnvMvcEndpointDisabled() { + try (ConfigurableApplicationContext context = getApplicationContext(Config.class, + "server.port=0", "endpoints.env.post.enabled=false")) { + assertThat(context + .getBeanNamesForType(EnvironmentManagerMvcEndpoint.class).length, + is(equalTo(0))); + } + } + @Test public void pauseMvcEndpointDisabled() { endpointDisabled("endpoints.pause.enabled", "pauseMvcEndpoint"); @@ -35,16 +46,21 @@ public class LifecycleMvcAutoConfigurationTests { } private void endpointDisabled(String enabledProp, String beanName) { - try (ConfigurableApplicationContext context = getApplicationContext(Config.class, "server.port=0", enabledProp +"=false")) { - EndpointMvcAdapter endpoint = context.getBean(beanName, EndpointMvcAdapter.class); + try (ConfigurableApplicationContext context = getApplicationContext(Config.class, + "server.port=0", enabledProp + "=false")) { + EndpointMvcAdapter endpoint = context.getBean(beanName, + EndpointMvcAdapter.class); Object result = endpoint.invoke(); - assertThat("result is wrong type", result, is(instanceOf(ResponseEntity.class))); - ResponseEntity response = (ResponseEntity) result; - assertThat("response code was wrong", response.getStatusCode(), equalTo(HttpStatus.NOT_FOUND)); + assertThat("result is wrong type", result, + is(instanceOf(ResponseEntity.class))); + ResponseEntity response = (ResponseEntity) result; + assertThat("response code was wrong", response.getStatusCode(), + equalTo(HttpStatus.NOT_FOUND)); } } - private static ConfigurableApplicationContext getApplicationContext(Class configuration, String... properties) { + private static ConfigurableApplicationContext getApplicationContext( + Class configuration, String... properties) { return new SpringApplicationBuilder(configuration).properties(properties).run(); }