Honor order set in configuration properties for composite profile (#2686)
Fixes #2438
This commit is contained in:
@@ -29,6 +29,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
@@ -80,7 +81,11 @@ public class CompositeEnvironmentBeanFactoryPostProcessor implements BeanFactory
|
||||
Binder binder = Binder.get(environment);
|
||||
String environmentConfigurationPropertyName = String.format("spring.cloud.config.server.composite[%d]", index);
|
||||
P properties = binder.bindOrCreate(environmentConfigurationPropertyName, propertiesClass);
|
||||
properties.setOrder(index + 1);
|
||||
if (properties instanceof Ordered
|
||||
&& ((Ordered) properties).getOrder() == EnvironmentRepositoryProperties.DEFAULT_ORDER) {
|
||||
// The order is not set, it is the default so set it to the order in the list
|
||||
properties.setOrder(index + 1);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import jakarta.validation.constraints.Pattern;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
@@ -45,7 +44,7 @@ public class AwsParameterStoreEnvironmentProperties implements EnvironmentReposi
|
||||
/**
|
||||
* The order of the environment repository.
|
||||
*/
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
/**
|
||||
* The region to be used by the AWS Parameter Store client.
|
||||
|
||||
@@ -40,7 +40,7 @@ public class AwsS3EnvironmentProperties implements EnvironmentRepositoryProperti
|
||||
*/
|
||||
private String bucket;
|
||||
|
||||
private int order;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
public String getRegion() {
|
||||
return region;
|
||||
|
||||
@@ -21,7 +21,6 @@ import jakarta.validation.constraints.Pattern;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Tejas Pandilwar
|
||||
@@ -62,7 +61,7 @@ public class AwsSecretsManagerEnvironmentProperties implements EnvironmentReposi
|
||||
/**
|
||||
* The order of the environment repository.
|
||||
*/
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
/**
|
||||
* Prefix indicating first level for every property loaded from AWS Secrets Manager.
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Alberto C. Ríos
|
||||
@@ -32,7 +31,7 @@ public class CredhubEnvironmentProperties implements EnvironmentRepositoryProper
|
||||
/** The default label to be used when is not provided by client applications. */
|
||||
private String defaultLabel = "master";
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Jose Maria Alvarez
|
||||
@@ -26,7 +25,7 @@ import org.springframework.core.Ordered;
|
||||
@ConfigurationProperties("spring.cloud.config.server.gcp-secret-manager")
|
||||
public class GoogleSecretManagerEnvironmentProperties implements EnvironmentRepositoryProperties {
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
private String applicationLabel = "application";
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Dylan Roberts
|
||||
@@ -38,7 +37,7 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE - 10;
|
||||
private int order = DEFAULT_ORDER - 10;
|
||||
|
||||
/** SQL used to query database for keys and values. */
|
||||
private String sql = DEFAULT_SQL;
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Dylan Roberts
|
||||
@@ -49,7 +48,7 @@ public class NativeEnvironmentProperties implements EnvironmentRepositoryPropert
|
||||
*/
|
||||
private String version;
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
public Boolean getFailOnError() {
|
||||
return this.failOnError;
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Piotr Mińkowski
|
||||
@@ -26,7 +25,7 @@ import org.springframework.core.Ordered;
|
||||
@ConfigurationProperties("spring.cloud.config.server.redis")
|
||||
public class RedisEnvironmentProperties implements EnvironmentRepositoryProperties {
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
public int getOrder() {
|
||||
return this.order;
|
||||
|
||||
@@ -26,7 +26,6 @@ import jakarta.validation.constraints.NotEmpty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.config.server.proxy.ProxyHostProperties;
|
||||
import org.springframework.cloud.config.server.support.HttpEnvironmentRepositoryProperties;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@@ -83,7 +82,7 @@ public class VaultEnvironmentProperties implements HttpEnvironmentRepositoryProp
|
||||
*/
|
||||
private Map<ProxyHostProperties.ProxyForScheme, ProxyHostProperties> proxy = new HashMap<>();
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
/**
|
||||
* Value to indicate which version of Vault kv backend is used. Defaults to 1.
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.cloud.config.server.support;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Dylan Roberts
|
||||
*/
|
||||
@@ -63,7 +61,7 @@ public class AbstractScmAccessorProperties implements EnvironmentRepositoryPrope
|
||||
private boolean strictHostKeyChecking = true;
|
||||
|
||||
/** The order of the environment repository. */
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
private int order = DEFAULT_ORDER;
|
||||
|
||||
/** The default label to be used with the remote repository. */
|
||||
private String defaultLabel;
|
||||
|
||||
@@ -16,11 +16,18 @@
|
||||
|
||||
package org.springframework.cloud.config.server.support;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
|
||||
/**
|
||||
* @author Dylan Roberts
|
||||
*/
|
||||
public interface EnvironmentRepositoryProperties {
|
||||
|
||||
/**
|
||||
* Default order value.
|
||||
*/
|
||||
int DEFAULT_ORDER = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
void setOrder(int order);
|
||||
|
||||
}
|
||||
|
||||
@@ -92,6 +92,56 @@ public class CompositeIntegrationTests {
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = TestConfigServerApplication.class, properties = {
|
||||
"spring.config.name:compositeconfigserver", "spring.cloud.config.server.composite[0].type: svn",
|
||||
"spring.cloud.config.server.composite[0].uri:file:///./target/repos/svn-config-repo",
|
||||
"spring.cloud.config.server.composite[0].order:2", "spring.cloud.config.server.composite[1].type: git",
|
||||
"spring.cloud.config.server.composite[1].uri:file:./target/repos/config-repo",
|
||||
"spring.cloud.config.server.composite[1].order:1" }, webEnvironment = RANDOM_PORT)
|
||||
@ActiveProfiles({ "test", "composite" })
|
||||
public static class StaticCompositeTests {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@BeforeAll
|
||||
public static void init() throws Exception {
|
||||
// mock Git configuration to make tests independent of local Git configuration
|
||||
SystemReader.setInstance(new MockSystemReader());
|
||||
|
||||
ConfigServerTestUtils.prepareLocalRepo();
|
||||
ConfigServerTestUtils.prepareLocalSvnRepo("src/test/resources/svn-config-repo",
|
||||
"target/repos/svn-config-repo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
ResponseEntity<Environment> response = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/foo/development", HttpMethod.GET, getV2AcceptEntity(),
|
||||
Environment.class);
|
||||
Environment environment = response.getBody();
|
||||
assertThat(3).isEqualTo(environment.getPropertySources().size());
|
||||
assertThat("overrides").isEqualTo(environment.getPropertySources().get(0).getName());
|
||||
assertThat(environment.getPropertySources().get(1).getName().contains("config-repo")
|
||||
&& !environment.getPropertySources().get(1).getName().contains("svn-config-repo"))
|
||||
.isTrue();
|
||||
assertThat(environment.getPropertySources().get(2).getName()).contains("svn-config-repo");
|
||||
ConfigServerTestUtils.assertConfigEnabled(environment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceEndpointsWork() {
|
||||
// This request will get the file from the Git Repo because its order is first
|
||||
// The SVN repo should have the content foo: bar
|
||||
String text = new TestRestTemplate().getForObject(
|
||||
"http://localhost:" + this.port + "/foo/development/composite/bar.properties", String.class);
|
||||
|
||||
String expected = "foo: barconfig\n";
|
||||
assertThat(expected).isEqualTo(text).as("invalid content");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SpringBootTest(classes = TestConfigServerApplication.class,
|
||||
properties = { "spring.config.name:compositeconfigserver",
|
||||
"spring.cloud.config.server.svn.uri:file:///./target/repos/svn-config-repo",
|
||||
|
||||
Reference in New Issue
Block a user