diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataMissingEnvironmentPostProcessor.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataMissingEnvironmentPostProcessor.java new file mode 100644 index 00000000..f55291b2 --- /dev/null +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataMissingEnvironmentPostProcessor.java @@ -0,0 +1,98 @@ +/* + * Copyright 2015-2021 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 + * + * https://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.cloud.config.client; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.StringUtils; + +import static org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PREFIX; +import static org.springframework.cloud.util.PropertyUtils.bootstrapEnabled; +import static org.springframework.cloud.util.PropertyUtils.useLegacyProcessing; + +public class ConfigServerConfigDataMissingEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { + + /** + * Order of post processor, set to run after + * {@link ConfigDataEnvironmentPostProcessor}. + */ + public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1000; + + @Override + public int getOrder() { + return ORDER; + } + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + // don't run if using bootstrap or legacy processing + if (bootstrapEnabled(environment) || useLegacyProcessing(environment)) { + return; + } + boolean configEnabled = environment.getProperty(ConfigClientProperties.PREFIX + ".enabled", Boolean.class, + true); + boolean importCheckEnabled = environment.getProperty(ConfigClientProperties.PREFIX + ".import-check.enabled", + Boolean.class, true); + if (!configEnabled || !importCheckEnabled) { + return; + } + String property = environment.getProperty("spring.config.import"); + if (!StringUtils.hasText(property)) { + throw new ImportException("No spring.config.import set", false); + } + if (!property.contains(PREFIX)) { + throw new ImportException("spring.config.import missing " + PREFIX, true); + } + } + + static class ImportException extends RuntimeException { + + final boolean missingPrefix; + + ImportException(String message, boolean missingPrefix) { + super(message); + this.missingPrefix = missingPrefix; + } + + } + + static class ImportExceptionFailureAnalyzer extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, ImportException cause) { + String description; + if (cause.missingPrefix) { + description = "The spring.config.import property is missing a " + PREFIX + " entry"; + } + else { + description = "No spring.config.import property has been defined"; + } + String action = "Add a spring.config.import=configserver: property to your configuration.\n" + + "\tIf configuration in not required add spring.config.import=optional:configserver: instead.\n" + + "\tTo disable this check, set spring.cloud.config.enabled=false or \n" + + "\tspring.cloud.config.import-check.enabled=false."; + return new FailureAnalysis(description, action, cause); + } + + } + +} diff --git a/spring-cloud-config-client/src/main/resources/META-INF/spring.factories b/spring-cloud-config-client/src/main/resources/META-INF/spring.factories index 1e5dc9be..d64cff86 100644 --- a/spring-cloud-config-client/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-config-client/src/main/resources/META-INF/spring.factories @@ -6,6 +6,13 @@ org.springframework.cloud.bootstrap.BootstrapConfiguration=\ org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration,\ org.springframework.cloud.config.client.DiscoveryClientConfigServiceBootstrapConfiguration +# Environment PostProcessor +org.springframework.boot.env.EnvironmentPostProcessor=\ +org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor + +org.springframework.boot.diagnostics.FailureAnalyzer=\ +org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor.ImportExceptionFailureAnalyzer + # ConfigData Location Resolvers org.springframework.boot.context.config.ConfigDataLocationResolver=\ org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigClientAutoConfigurationTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigClientAutoConfigurationTests.java index 7e8e3a33..ea7c8b16 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigClientAutoConfigurationTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigClientAutoConfigurationTests.java @@ -40,7 +40,8 @@ public class ConfigClientAutoConfigurationTests { @Test public void withParent() { ConfigurableApplicationContext context = new SpringApplicationBuilder(ConfigClientAutoConfiguration.class) - .child(Object.class).web(WebApplicationType.NONE).run(); + .child(Object.class).web(WebApplicationType.NONE).properties("spring.cloud.bootstrap.enabled=true") + .run(); assertThat(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, ConfigClientProperties.class).length) .isEqualTo(1); context.close(); diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerBootstrapConfigurationTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerBootstrapConfigurationTests.java index 20a58f50..2fb8263c 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerBootstrapConfigurationTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerBootstrapConfigurationTests.java @@ -32,7 +32,8 @@ public class ConfigServerBootstrapConfigurationTests { public void withHealthIndicator() { ConfigurableApplicationContext context = new SpringApplicationBuilder( PropertySourceBootstrapConfiguration.class, ConfigServiceBootstrapConfiguration.class) - .child(ConfigClientAutoConfiguration.class).web(WebApplicationType.NONE).run(); + .child(ConfigClientAutoConfiguration.class).properties("spring.cloud.bootstrap.enabled=true") + .web(WebApplicationType.NONE).run(); assertThat(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, ConfigClientProperties.class).length) .isEqualTo(1); assertThat( diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerConfigDataNoImportIntegrationTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerConfigDataNoImportIntegrationTests.java new file mode 100644 index 00000000..5db795e6 --- /dev/null +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerConfigDataNoImportIntegrationTests.java @@ -0,0 +1,86 @@ +/* + * Copyright 2013-2019 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 + * + * https://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.cloud.config.client; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.cloud.config.client.ConfigServerConfigDataMissingEnvironmentPostProcessor.ImportException; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.config.client.ConfigServerConfigDataLocationResolver.PREFIX; + +/** + * @author Spencer Gibb + */ +@ExtendWith(OutputCaptureExtension.class) +public class ConfigServerConfigDataNoImportIntegrationTests { + + private static final String APP_NAME = "testConfigDataNoImport"; + + @Test + public void exceptionThrownIfNoImport(CapturedOutput output) { + Assertions.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE) + .run("--spring.application.name=" + APP_NAME)).isInstanceOf(ImportException.class); + + assertThat(output).contains("No spring.config.import property has been defined") + .contains("Add a spring.config.import=configserver: property to your configuration"); + } + + @Test + public void exceptionThrownIfImportMissing(CapturedOutput output) { + Assertions.assertThatThrownBy(() -> new SpringApplicationBuilder(Config.class).web(WebApplicationType.NONE).run( + "--spring.config.import=optional:file:somefile.properties", "--spring.application.name=" + APP_NAME)) + .isInstanceOf(ImportException.class); + + assertThat(output).contains("spring.config.import property is missing a " + PREFIX) + .contains("Add a spring.config.import=configserver: property to your configuration"); + } + + @Test + public void noExceptionThrownIfConfigDisabled() { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Config.class) + .web(WebApplicationType.NONE) + .run("--spring.cloud.config.enabled=false", "--spring.application.name=" + APP_NAME)) { + // nothing to do + } + } + + @Test + public void noExceptionThrownIfImportCheckDisabled() { + try (ConfigurableApplicationContext context = new SpringApplicationBuilder(Config.class) + .web(WebApplicationType.NONE) + .run("--spring.cloud.config.import-check.enabled=false", "--spring.application.name=" + APP_NAME)) { + // nothing to do + } + } + + @Configuration + @EnableAutoConfiguration + static class Config { + + } + +} diff --git a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/DiscoveryClientConfigDataConfigurationTests.java b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/DiscoveryClientConfigDataConfigurationTests.java index 156541f3..58ecf625 100644 --- a/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/DiscoveryClientConfigDataConfigurationTests.java +++ b/spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/DiscoveryClientConfigDataConfigurationTests.java @@ -66,6 +66,7 @@ public class DiscoveryClientConfigDataConfigurationTests { @Test public void offByDefault() { context = new SpringApplicationBuilder(TestConfig.class) + .properties("spring.config.import=optional:configserver:") .addBootstrapper(registry -> registry.addCloseListener(event -> { try { event.getBootstrapContext().get(ConfigServerInstanceMonitor.class); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/RefreshableConfigServerIntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/RefreshableConfigServerIntegrationTests.java index f2e88d6c..87b23027 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/RefreshableConfigServerIntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/RefreshableConfigServerIntegrationTests.java @@ -56,8 +56,8 @@ import static org.springframework.cloud.config.server.test.ConfigServerTestUtils import static org.springframework.cloud.config.server.test.ConfigServerTestUtils.getV2AcceptEntity; @RunWith(SpringRunner.class) -@SpringBootTest( - classes = TestConfiguration.class, properties = { "spring.cloud.config.enabled=true", +@SpringBootTest(classes = TestConfiguration.class, + properties = { "spring.cloud.config.enabled=true", "spring.cloud.bootstrap.enabled=true", "management.endpoint.env.post.enabled=true", "management.endpoints.web.exposure.include=env, refresh" }, webEnvironment = WebEnvironment.RANDOM_PORT) @ActiveProfiles("test")