diff --git a/docs/modules/ROOT/pages/server/embedding.adoc b/docs/modules/ROOT/pages/server/embedding.adoc index 805969f081..a9c4334c0e 100644 --- a/docs/modules/ROOT/pages/server/embedding.adoc +++ b/docs/modules/ROOT/pages/server/embedding.adoc @@ -86,3 +86,26 @@ The choice between embedding the Config Server and running it as a standalone se When embedding the Config Server in a multi-application environment (for example, if each microservice embeds its own Config Server), each application independently connects to the Git repository. This means each application must possess valid Git credentials and encryption keys, which can complicate secret rotation and auditing. ==== +[[disabling-the-default-environment-repository]] +== Disabling the Default Environment Repository + +When embedding Config Server in another application, you may want to disable the default environment repository so that the server does not configure a default Git repository when no `EnvironmentRepository` has been explicitly configured. + +You can disable the default environment repository by setting the following property: + +[source,yaml] +---- +spring: + cloud: + config: + server: + default-repository: + enabled: false +---- + +When this property is set to `false`, the default Git environment repository is not created. This is useful when embedding Config Server in an application that should not use the default Git repository. + +By default, this property is `true`, preserving the existing behavior. + +This property controls the creation of the default repository. The `spring.cloud.config.server.accept-empty` property separately controls whether the server returns an HTTP 404 response when the requested application is not found. + diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java index 0bd66a9254..5f0a292706 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration.java @@ -415,6 +415,8 @@ public MongoDbEnvironmentRepositoryFactory mongoDbEnvironmentRepositoryFactory(M class DefaultRepositoryConfiguration { @Bean + @ConditionalOnProperty(prefix = "spring.cloud.config.server.default-repository", name = "enabled", + havingValue = "true", matchIfMissing = true) public MultipleJGitEnvironmentRepository defaultEnvironmentRepository( MultipleJGitEnvironmentRepositoryFactory gitEnvironmentRepositoryFactory, MultipleJGitEnvironmentProperties environmentProperties) throws Exception { diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java index f4991a91c2..f3bce54a2f 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/config/EnvironmentRepositoryConfigurationTests.java @@ -29,6 +29,7 @@ import org.springframework.cloud.config.server.environment.EnvironmentConfigTokenProvider; import org.springframework.cloud.config.server.environment.EnvironmentRepository; import org.springframework.cloud.config.server.environment.GcsEnvironmentRepositoryFactory; +import org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentRepository; import org.springframework.cloud.config.server.support.AzureDevOpsWorkloadIdentitySupport; import org.springframework.cloud.config.server.support.GitCredentialsProviderFactory; import org.springframework.context.annotation.Bean; @@ -191,6 +192,18 @@ public void azureDevOpsWorkloadIdentitySupportBeanExists() { }); } + @Test + public void defaultEnvironmentRepositoryCanBeDisabled() { + new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(EnvironmentRepositoryConfiguration.class, TestBeans.class)) + .withBean(EnvironmentRepository.class, () -> mock(EnvironmentRepository.class)) + .withPropertyValues("spring.cloud.config.server.default-repository.enabled=false") + .run((context) -> { + assertThat(context).hasNotFailed(); + assertThat(context).doesNotHaveBean(MultipleJGitEnvironmentRepository.class); + }); + } + @TestConfiguration public static class TestBeans {