Fail if no configserver config import.
Adds ConfigServerConfigDataMissingEnvironmentPostProcessor that checks if there is a spring.config.import=configserver: statement. If not, an exception is thrown and a FailureAnalyzer provides hints to fix the issue. Fixes gh-1813
This commit is contained in:
@@ -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<ImportException> {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user