Adds property to disable JdbcEnvironmentRepository

- Add "enabled" property to JdbcEnvironmentProperties
- Make JdbcEnvironmentRepository configuration depending
  on new property
- Add integration test to verify the new property

Fixes gh-1605
Fixes gh-1623
This commit is contained in:
Thomas Vitale
2020-05-16 00:36:24 +02:00
committed by spencergibb
parent 4bbb4ca5e4
commit 841038f684
4 changed files with 105 additions and 0 deletions

View File

@@ -804,6 +804,8 @@ You can enable this feature by adding `spring-jdbc` to the classpath and using t
If you include the right dependencies on the classpath (see the user guide for more details on that), Spring Boot configures a data source.
// TODO Which user guide? When we know that, we should add a link to it.
You can disable autoconfiguration for `JdbcEnvironmentRepository` by setting the `spring.cloud.config.server.jdbc.enabled` property to `false`.
The database needs to have a table called `PROPERTIES` with columns called `APPLICATION`, `PROFILE`, and `LABEL` (with the usual `Environment` meaning), plus `KEY` and `VALUE` for the key and value pairs in `Properties` style.
All fields are of type String in Java, so you can make them `VARCHAR` of whatever length you need.
Property values behave in the same way as they would if they came from Spring Boot properties files named `{application}-{profile}.properties`, including all the encryption and decryption, which will be applied as post-processing steps (that is, not in the repository implementation directly).

View File

@@ -253,6 +253,8 @@ public class EnvironmentRepositoryConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnProperty(value = "spring.cloud.config.server.jdbc.enabled",
matchIfMissing = true)
static class JdbcFactoryConfig {
@Bean
@@ -408,6 +410,8 @@ class CredhubRepositoryConfiguration {
@Configuration(proxyBeanMethods = false)
@Profile("jdbc")
@ConditionalOnClass(JdbcTemplate.class)
@ConditionalOnProperty(value = "spring.cloud.config.server.jdbc.enabled",
matchIfMissing = true)
class JdbcRepositoryConfiguration {
@Bean

View File

@@ -22,6 +22,7 @@ import org.springframework.core.Ordered;
/**
* @author Dylan Roberts
* @author Thomas Vitale
*/
@ConfigurationProperties("spring.cloud.config.server.jdbc")
public class JdbcEnvironmentProperties implements EnvironmentRepositoryProperties {
@@ -29,11 +30,24 @@ public class JdbcEnvironmentProperties implements EnvironmentRepositoryPropertie
private static final String DEFAULT_SQL = "SELECT KEY, VALUE from PROPERTIES"
+ " where APPLICATION=? and PROFILE=? and LABEL=?";
/**
* Flag to indicate that JDBC environment repository configuration is enabled.
*/
private boolean enabled = true;
private int order = Ordered.LOWEST_PRECEDENCE - 10;
/** SQL used to query database for keys and values. */
private String sql = DEFAULT_SQL;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public int getOrder() {
return this.order;
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2016-2020 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.server.environment;
import java.io.IOException;
import org.junit.Test;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.cloud.config.server.ConfigServerApplication;
import org.springframework.cloud.config.server.test.ConfigServerTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests to verify JdbcEnvironmentRepository configuration.
*
* @author Thomas Vitale
*/
public class JdbcEnvironmentRepositoryConfigurationTests {
@Test
public void jdbcEnvironmentRepositoryBeansConfiguredWhenDefault() throws IOException {
new WebApplicationContextRunner()
.withUserConfiguration(ConfigServerApplication.class)
.withPropertyValues("spring.profiles.active=test,jdbc",
"spring.main.web-application-type=none")
.run(context -> {
assertThat(context)
.hasSingleBean(JdbcEnvironmentRepositoryFactory.class);
assertThat(context).hasSingleBean(JdbcEnvironmentRepository.class);
});
}
@Test
public void jdbcEnvironmentRepositoryBeansConfiguredWhenEnabled() throws IOException {
getApplicationContextWithJdbcEnabled(true, context -> {
assertThat(context).hasSingleBean(JdbcEnvironmentRepositoryFactory.class);
assertThat(context).hasSingleBean(JdbcEnvironmentRepository.class);
});
}
@Test
public void jdbcEnvironmentRepositoryFactoryNotConfiguredWhenDisabled()
throws IOException {
getApplicationContextWithJdbcEnabled(false, context -> assertThat(context)
.doesNotHaveBean(JdbcEnvironmentRepositoryFactory.class));
}
@Test
public void jdbcEnvironmentRepositoryNotConfiguredWhenDisabled() throws IOException {
getApplicationContextWithJdbcEnabled(false, context -> assertThat(context)
.doesNotHaveBean(JdbcEnvironmentRepository.class));
}
private void getApplicationContextWithJdbcEnabled(boolean jdbcEnabled,
ContextConsumer<? super AssertableWebApplicationContext> consumer)
throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo();
new WebApplicationContextRunner()
.withUserConfiguration(ConfigServerApplication.class)
.withPropertyValues("spring.profiles.active=test,jdbc",
"spring.main.web-application-type=none",
"spring.cloud.config.server.git.uri:" + uri,
"spring.cloud.config.server.jdbc.enabled:" + jdbcEnabled)
.run(consumer);
}
}