Take into account spring.config.imports in referenced imports. Fixes #1006 (#1007)

This commit is contained in:
Ryan Baxter
2021-09-09 15:38:29 -04:00
committed by Ryan Baxter
parent abd6d178d0
commit dabc2c519c
2 changed files with 144 additions and 9 deletions

View File

@@ -16,17 +16,23 @@
package org.springframework.cloud.commons;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
/**
* @author Ryan Baxter
@@ -60,25 +66,45 @@ public abstract class ConfigDataMissingEnvironmentPostProcessor implements Envir
if (!shouldProcessEnvironment(environment)) {
return;
}
List<String> property = getConfigImports(environment);
List<Object> property = getConfigImports(environment);
if (property == null || property.isEmpty()) {
throw new ImportException("No spring.config.import set", false);
}
if (!property.stream().anyMatch(impt -> impt.contains(getPrefix()))) {
if (!property.stream().anyMatch(impt -> ((String) impt).contains(getPrefix()))) {
throw new ImportException("spring.config.import missing " + getPrefix(), true);
}
}
private List<String> getConfigImports(ConfigurableEnvironment environment) {
List<String> property = environment.getProperty(CONFIG_IMPORT_PROPERTY, List.class);
if (property == null || property.isEmpty()) {
Binder binder = Binder.get(environment);
property = Arrays
.asList(binder.bind(CONFIG_IMPORT_PROPERTY, CONFIG_DATA_LOCATION_ARRAY).orElse(new String[0]));
}
private List<Object> getConfigImports(ConfigurableEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
List<Object> property = propertySources.stream().filter(this::propertySourceWithConfigImport)
.flatMap(propertySource -> {
List<Object> configImports = new ArrayList<>();
if (propertySource.getProperty(CONFIG_IMPORT_PROPERTY) != null) {
configImports.add(propertySource.getProperty(CONFIG_IMPORT_PROPERTY));
}
else {
configImports.addAll(Arrays.asList(getConfigImportArray(propertySource)));
}
return configImports.stream();
}).collect(Collectors.toList());
return property;
}
private boolean propertySourceWithConfigImport(PropertySource propertySource) {
if (CompositePropertySource.class.isInstance(propertySource)) {
return ((CompositePropertySource) propertySource).getPropertySources().stream()
.anyMatch(this::propertySourceWithConfigImport);
}
return propertySource.containsProperty(CONFIG_IMPORT_PROPERTY)
|| getConfigImportArray(propertySource).length > 0;
}
private String[] getConfigImportArray(PropertySource propertySource) {
Binder binder = new Binder(ConfigurationPropertySource.from(propertySource));
return binder.bind(CONFIG_IMPORT_PROPERTY, CONFIG_DATA_LOCATION_ARRAY).orElse(new String[0]);
}
public static class ImportException extends RuntimeException {
/**

View File

@@ -0,0 +1,109 @@
/*
* 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.commons;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.mock.env.MockPropertySource;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
/**
* @author Ryan Baxter
*/
public class ConfigDataMissingEnvironmentPostProcessorTests {
@Test
void noSpringConfigImport() {
MockEnvironment environment = new MockEnvironment();
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatThrownBy(() -> processor.postProcessEnvironment(environment, app))
.isInstanceOf(ConfigDataMissingEnvironmentPostProcessor.ImportException.class);
}
@Test
void importSinglePropertySource() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import", "configserver:http://localhost:8888");
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importMultiplePropertySource() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import", "configserver:http://localhost:8888,file:./app.properties");
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importMultiplePropertySourceAsList() {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("spring.config.import[0]", "configserver:http://localhost:8888");
environment.setProperty("spring.config.import[1]", "file:./app.properties");
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
@Test
void importCompositePropertySource() {
MockEnvironment environment = new MockEnvironment();
CompositePropertySource ps1 = new CompositePropertySource("ps1");
MockPropertySource ps2 = new MockPropertySource("ps2");
ps2.setProperty("spring.config.import", "file:./app.properties");
MockPropertySource ps3 = new MockPropertySource("ps3");
ps3.setProperty("my.property", "value");
MockPropertySource ps4 = new MockPropertySource("ps4");
ps4.setProperty("spring.config.import[0]", "file:./moreproperties.yaml");
ps4.setProperty("spring.config.import[1]", "configserver:http://localhost:8888");
CompositePropertySource compositePropertySource = new CompositePropertySource("composite");
compositePropertySource.addPropertySource(ps3);
compositePropertySource.addPropertySource(ps4);
ps1.addPropertySource(compositePropertySource);
environment.getPropertySources().addFirst(ps2);
environment.getPropertySources().addLast(ps1);
SpringApplication app = mock(SpringApplication.class);
TestConfigDataMissingEnvironmentPostProcessor processor = new TestConfigDataMissingEnvironmentPostProcessor();
assertThatCode(() -> processor.postProcessEnvironment(environment, app)).doesNotThrowAnyException();
}
public class TestConfigDataMissingEnvironmentPostProcessor extends ConfigDataMissingEnvironmentPostProcessor {
@Override
protected boolean shouldProcessEnvironment(Environment environment) {
return true;
}
@Override
protected String getPrefix() {
return "configserver:";
}
}
}