Browse Source

Disables endpoint creation (#208)

Fixes gh-117
pull/229/head
Fabio Matos 7 years ago committed by Spencer Gibb
parent
commit
2e81db24cf
  1. 14
      spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java
  2. 37
      spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java
  3. 4
      spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java
  4. 213
      spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java
  5. 5
      spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java
  6. 3
      spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationTests.java

14
spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/LifecycleMvcEndpointAutoConfiguration.java

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
*/
package org.springframework.cloud.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -42,19 +42,14 @@ import org.springframework.context.annotation.Configuration; @@ -42,19 +42,14 @@ import org.springframework.context.annotation.Configuration;
*
*/
@Configuration
@ConditionalOnClass(EnvironmentEndpoint.class)
@ConditionalOnWebApplication
@ConditionalOnBean(RestartEndpoint.class)
@AutoConfigureAfter({ WebMvcAutoConfiguration.class,
RefreshEndpointAutoConfiguration.class })
public class LifecycleMvcEndpointAutoConfiguration {
@Autowired
private RestartEndpoint restartEndpoint;
@Bean
@ConditionalOnBean(EnvironmentEndpoint.class)
@ConditionalOnProperty(value = "endpoints.env.post.enabled", matchIfMissing = true)
@ConditionalOnEnabledEndpoint(value = "env.post")
public EnvironmentManagerMvcEndpoint environmentManagerEndpoint(
EnvironmentEndpoint delegate, EnvironmentManager environment) {
return new EnvironmentManagerMvcEndpoint(delegate, environment);
@ -67,8 +62,9 @@ public class LifecycleMvcEndpointAutoConfiguration { @@ -67,8 +62,9 @@ public class LifecycleMvcEndpointAutoConfiguration {
}
@Bean
public RestartMvcEndpoint restartMvcEndpoint() {
return new RestartMvcEndpoint(this.restartEndpoint);
@ConditionalOnBean(RestartEndpoint.class)
public RestartMvcEndpoint restartMvcEndpoint(RestartEndpoint restartEndpoint) {
return new RestartMvcEndpoint(restartEndpoint);
}
@Bean

37
spring-cloud-context/src/main/java/org/springframework/cloud/autoconfigure/RefreshEndpointAutoConfiguration.java

@ -19,6 +19,7 @@ package org.springframework.cloud.autoconfigure; @@ -19,6 +19,7 @@ package org.springframework.cloud.autoconfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.ConditionalOnEnabledHealthIndicator;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -26,7 +27,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -26,7 +27,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder;
import org.springframework.cloud.context.refresh.ContextRefresher;
@ -57,6 +57,7 @@ public class RefreshEndpointAutoConfiguration { @@ -57,6 +57,7 @@ public class RefreshEndpointAutoConfiguration {
}
@ConditionalOnClass(IntegrationMBeanExporter.class)
@ConditionalOnEnabledEndpoint(value = "restart", enabledByDefault = false)
protected static class RestartEndpointWithIntegration {
@Autowired(required = false)
@ -75,30 +76,38 @@ public class RefreshEndpointAutoConfiguration { @@ -75,30 +76,38 @@ public class RefreshEndpointAutoConfiguration {
}
@ConditionalOnMissingClass("org.springframework.integration.monitor.IntegrationMBeanExporter")
@ConditionalOnEnabledEndpoint(value = "restart", enabledByDefault = false)
protected static class RestartEndpointWithoutIntegration {
@Bean
@ConditionalOnMissingBean
public RestartEndpoint restartEndpoint() {
public RestartEndpoint restartEndpointWithoutIntegration() {
return new RestartEndpoint();
}
}
@Bean
@ConfigurationProperties("endpoints.pause")
public RestartEndpoint.PauseEndpoint pauseEndpoint(RestartEndpoint restartEndpoint) {
return restartEndpoint.getPauseEndpoint();
}
@Bean
@ConfigurationProperties("endpoints.resume")
public RestartEndpoint.ResumeEndpoint resumeEndpoint(
RestartEndpoint restartEndpoint) {
return restartEndpoint.getResumeEndpoint();
@ConditionalOnEnabledEndpoint(value = "restart", enabledByDefault = false)
protected static class PauseResumeEndpoints {
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint("pause")
public RestartEndpoint.PauseEndpoint pauseEndpoint(RestartEndpoint restartEndpoint) {
return restartEndpoint.getPauseEndpoint();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint("resume")
public RestartEndpoint.ResumeEndpoint resumeEndpoint(RestartEndpoint restartEndpoint) {
return restartEndpoint.getResumeEndpoint();
}
}
@Configuration
@ConditionalOnProperty(value = "endpoints.refresh.enabled", matchIfMissing = true)
@ConditionalOnEnabledEndpoint("refresh")
@ConditionalOnBean(PropertySourceBootstrapConfiguration.class)
protected static class RefreshEndpointConfiguration {
@ -110,4 +119,4 @@ public class RefreshEndpointAutoConfiguration { @@ -110,4 +119,4 @@ public class RefreshEndpointAutoConfiguration {
}
}
}
}

4
spring-cloud-context/src/main/java/org/springframework/cloud/context/restart/RestartEndpoint.java

@ -114,7 +114,7 @@ public class RestartEndpoint extends AbstractEndpoint<Boolean> @@ -114,7 +114,7 @@ public class RestartEndpoint extends AbstractEndpoint<Boolean>
return new ResumeEndpoint();
}
@ConfigurationProperties("endpoints")
@ConfigurationProperties("endpoints.pause")
public class PauseEndpoint extends AbstractEndpoint<Boolean> {
public PauseEndpoint() {
@ -131,7 +131,7 @@ public class RestartEndpoint extends AbstractEndpoint<Boolean> @@ -131,7 +131,7 @@ public class RestartEndpoint extends AbstractEndpoint<Boolean>
}
}
@ConfigurationProperties("endpoints")
@ConfigurationProperties("endpoints.resume")
public class ResumeEndpoint extends AbstractEndpoint<Boolean> {
public ResumeEndpoint() {

213
spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/LifecycleMvcAutoConfigurationTests.java

@ -1,88 +1,157 @@ @@ -1,88 +1,157 @@
package org.springframework.cloud.autoconfigure;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.assertj.core.util.Lists;
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;
import org.springframework.http.ResponseEntity;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
/**
* @author Spencer Gibb
*/
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");
}
@Test
public void resumeMvcEndpointDisabled() {
endpointDisabled("endpoints.resume.enabled", "resumeMvcEndpoint");
}
@Test
public void restartMvcEndpointDisabled() {
endpointDisabled("endpoints.restart.enabled", "restartMvcEndpoint");
}
@Test
public void pauseMvcEndpointGloballyDisabled() {
endpointDisabled("endpoints.enabled", "pauseMvcEndpoint");
}
@Test
public void resumeMvcEndpointGloballyDisabled() {
endpointDisabled("endpoints.enabled", "resumeMvcEndpoint");
}
@Test
public void restartMvcEndpointGloballyDisabled() {
endpointDisabled("endpoints.enabled", "restartMvcEndpoint");
}
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);
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));
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).properties(properties).run();
}
@Configuration
@EnableAutoConfiguration
static class Config {
}
// postEnvMvcEndpoint
@Test
public void postEnvMvcEndpointDisabled() {
beanNotCreated("environmentManagerEndpoint",
"endpoints.env.post.enabled=false");
}
@Test
public void postEnvMvcEndpointGloballyDisabled() {
beanNotCreated("environmentManagerEndpoint",
"endpoints.enabled=false");
}
@Test
public void postEnvMvcEndpointEnabled() {
beanCreated("environmentManagerEndpoint",
"endpoints.env.post.enabled=true");
}
// restartMvcEndpoint
@Test
public void restartMvcEndpointDisabled() {
beanNotCreated("restartMvcEndpoint",
"endpoints.restart.enabled=false");
}
@Test
public void restartMvcEndpointGloballyDisabled() {
beanNotCreated("restartMvcEndpoint",
"endpoints.enabled=false");
}
@Test
public void restartMvcEndpointEnabled() {
beanCreatedAndEndpointEnabled("restartMvcEndpoint",
"endpoints.restart.enabled=true");
}
// pauseMvcEndpoint
@Test
public void pauseMvcEndpointDisabled() {
beanNotCreated("pauseMvcEndpoint",
"endpoints.pause.enabled=false");
}
@Test
public void pauseMvcEndpointRestartDisabled() {
beanNotCreated("pauseMvcEndpoint",
"endpoints.restart.enabled=false",
"endpoints.pause.enabled=true");
}
@Test
public void pauseMvcEndpointGloballyDisabled() {
beanNotCreated("pauseMvcEndpoint",
"endpoints.enabled=false");
}
@Test
public void pauseMvcEndpointEnabled() {
beanCreatedAndEndpointEnabled("pauseMvcEndpoint",
"endpoints.restart.enabled=true",
"endpoints.pause.enabled=true");
}
// resumeMvcEndpoint
@Test
public void resumeMvcEndpointDisabled() {
beanNotCreated("resumeMvcEndpoint",
"endpoints.restart.enabled=true",
"endpoints.resume.enabled=false");
}
@Test
public void resumeMvcEndpointRestartDisabled() {
beanNotCreated("resumeMvcEndpoint",
"endpoints.restart.enabled=false",
"endpoints.resume.enabled=true");
}
@Test
public void resumeMvcEndpointGloballyDisabled() {
beanNotCreated("resumeMvcEndpoint",
"endpoints.enabled=false");
}
@Test
public void resumeMvcEndpointEnabled() {
beanCreatedAndEndpointEnabled("resumeMvcEndpoint",
"endpoints.restart.enabled=true",
"endpoints.resume.enabled=true");
}
private void beanNotCreated(String beanName, String... contextProperties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, contextProperties)) {
assertThat("bean was created", context.containsBeanDefinition(beanName), equalTo(false));
}
}
private void beanCreated(String beanName, String... contextProperties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, contextProperties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName), equalTo(true));
}
}
private void beanCreatedAndEndpointEnabled(String beanName, String... properties) {
try (ConfigurableApplicationContext context = getApplicationContext(Config.class, properties)) {
assertThat("bean was not created", context.containsBeanDefinition(beanName), equalTo(true));
EndpointMvcAdapter endpoint = context.getBean(beanName, EndpointMvcAdapter.class);
Object result = endpoint.invoke();
assertThat("result is wrong type", result,
is(not(instanceOf(ResponseEntity.class))));
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
List<String> defaultProperties = Lists.newArrayList(properties);
defaultProperties.add("server.port=0");
defaultProperties.add("spring.jmx.default-domain=${random.uuid}");
return new SpringApplicationBuilder(configuration).properties(defaultProperties.toArray(new String[]{})).run();
}
@Configuration
@EnableAutoConfiguration
static class Config {
}
}

5
spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationClassPathTests.java

@ -24,14 +24,13 @@ public class RefreshAutoConfigurationClassPathTests { @@ -24,14 +24,13 @@ public class RefreshAutoConfigurationClassPathTests {
try (ConfigurableApplicationContext context = getApplicationContext(
Config.class)) {
assertFalse(context.getBeansOfType(RefreshEventListener.class).isEmpty());
assertFalse(context.containsBean("refeshEndpoint"));
assertFalse(context.containsBean("refreshEndpoint"));
}
}
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(false)
.properties(properties).run();
return new SpringApplicationBuilder(configuration).web(false).properties(properties).run();
}
@Configuration

3
spring-cloud-context/src/test/java/org/springframework/cloud/autoconfigure/RefreshAutoConfigurationTests.java

@ -33,8 +33,7 @@ public class RefreshAutoConfigurationTests { @@ -33,8 +33,7 @@ public class RefreshAutoConfigurationTests {
private static ConfigurableApplicationContext getApplicationContext(
Class<?> configuration, String... properties) {
return new SpringApplicationBuilder(configuration).web(false)
.properties(properties).run();
return new SpringApplicationBuilder(configuration).web(false).properties(properties).run();
}
@Configuration

Loading…
Cancel
Save