diff --git a/README.md b/README.md index 3acd610..1d2e966 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,17 @@ Disable Property: `org.springframework.cloud.bindings.boot.redis.enable` | `spring.redis.ssl` | `{secret/ssl}` | `spring.redis.url` | `{secret/url}` +## SCS Config Server +Kind: `Config` +Disable Property: `org.springframework.cloud.bindings.boot.config.enable` + +| Property | Value +| -------- | ------------------ +| `spring.cloud.config.uri` | `{secret/uri}` +| `spring.cloud.config.client.oauth2.clientId` | `{secret/client-id}` +| `spring.cloud.config.client.oauth2.clientSecret` | `{secret/client-secret}` +| `spring.cloud.config.client.oauth2.accessTokenUri` | `{secret/access-token-uri}` + ### SQLServer RDBMS Kind: `SQLServer` Disable Property: `org.springframework.cloud.bindings.boot.sqlserver.enable` diff --git a/src/main/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessor.java new file mode 100644 index 0000000..924bfe2 --- /dev/null +++ b/src/main/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessor.java @@ -0,0 +1,51 @@ +/* + * Copyright 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 + * + * http://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.bindings.boot; + +import org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.core.env.Environment; + +import java.util.Map; + +import static org.springframework.cloud.bindings.boot.Guards.isKindEnabled; + +/** + * An implementation of {@link BindingsPropertiesProcessor} that detects {@link Binding}s of kind: {@value KIND}. + */ +final class ConfigServerBindingsPropertiesProcessor implements BindingsPropertiesProcessor { + /** + * The {@link Binding} kind that this processor is interested in: {@value}. + **/ + public static final String KIND = "Config"; + + @Override + public void process(Environment environment, Bindings bindings, Map properties) { + if (!isKindEnabled(environment, KIND)) { + return; + } + + bindings.filterBindings(KIND).forEach(binding -> { + MapMapper map = new MapMapper(binding.getSecret(), properties); + map.from("uri").to("spring.cloud.config.uri"); + map.from("client-id").to("spring.cloud.config.client.oauth2.clientId"); + map.from("client-secret").to("spring.cloud.config.client.oauth2.clientSecret"); + map.from("access-token-uri").to("spring.cloud.config.client.oauth2.accessTokenUri"); + }); + + } +} diff --git a/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java b/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java index be4c9a2..f3cc229 100644 --- a/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java +++ b/src/main/java/org/springframework/cloud/bindings/boot/EurekaBindingsPropertiesProcessor.java @@ -44,7 +44,6 @@ final class EurekaBindingsPropertiesProcessor implements BindingsPropertiesProce map.from("client-id").to("eureka.client.oauth2.client-id"); map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri"); - map.from("access-token-uri").to("eureka.client.oauth2.access-token-uri"); map.from("uri").to("eureka.client.serviceUrl.defaultZone", (uri) -> String.format("%s/eureka/", uri) ); diff --git a/src/main/resources/META-INF/spring.factories b/src/main/resources/META-INF/spring.factories index e8e543d..3f01047 100644 --- a/src/main/resources/META-INF/spring.factories +++ b/src/main/resources/META-INF/spring.factories @@ -20,4 +20,5 @@ org.springframework.cloud.bindings.boot.BindingsPropertiesProcessor=\ org.springframework.cloud.bindings.boot.RedisBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.SqlServerBindingsPropertiesProcessor, \ org.springframework.cloud.bindings.boot.WavefrontBindingsPropertiesProcessor, \ - org.springframework.cloud.bindings.boot.EurekaBindingsPropertiesProcessor + org.springframework.cloud.bindings.boot.EurekaBindingsPropertiesProcessor, \ + org.springframework.cloud.bindings.boot.ConfigServerBindingsPropertiesProcessor diff --git a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java index 3b36200..b69fb3e 100644 --- a/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java +++ b/src/test/java/org/springframework/cloud/bindings/boot/BindingSpecificEnvironmentPostProcessorTest.java @@ -98,7 +98,7 @@ final class BindingSpecificEnvironmentPostProcessorTest { @Test @DisplayName("included implementations are registered") void includedImplementations() { - assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(15); + assertThat(new BindingSpecificEnvironmentPostProcessor().processors).hasSize(16); } } diff --git a/src/test/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessorTest.java b/src/test/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessorTest.java new file mode 100644 index 0000000..75f7be5 --- /dev/null +++ b/src/test/java/org/springframework/cloud/bindings/boot/ConfigServerBindingsPropertiesProcessorTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 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 + * + * http://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.bindings.boot; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.cloud.bindings.Binding; +import org.springframework.cloud.bindings.Bindings; +import org.springframework.cloud.bindings.FluentMap; +import org.springframework.mock.env.MockEnvironment; + +import java.nio.file.Paths; +import java.util.Collections; +import java.util.HashMap; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.cloud.bindings.boot.ConfigServerBindingsPropertiesProcessor.KIND; + +@DisplayName("Config Server BindingsPropertiesProcessor") +final class ConfigServerBindingsPropertiesProcessorTest { + + private final Bindings bindings = new Bindings( + new Binding("test-name", Paths.get("test-path"), + Collections.singletonMap("kind", KIND), + new FluentMap() + .withEntry("uri", "test-uri") + .withEntry("client-id", "test-client-id") + .withEntry("client-secret", "test-client-secret") + .withEntry("access-token-uri", "test-access-token-uri") + ) + ); + + private final MockEnvironment environment = new MockEnvironment(); + + private final HashMap properties = new HashMap<>(); + + @Test + @DisplayName("contributes properties") + void test() { + new ConfigServerBindingsPropertiesProcessor().process(environment, bindings, properties); + assertThat(properties) + .containsEntry("spring.cloud.config.uri", "test-uri") + .containsEntry("spring.cloud.config.client.oauth2.clientId", "test-client-id") + .containsEntry("spring.cloud.config.client.oauth2.clientSecret", "test-client-secret") + .containsEntry("spring.cloud.config.client.oauth2.accessTokenUri", "test-access-token-uri"); + } + + @Test + @DisplayName("can be disabled") + void disabled() { + environment.setProperty("org.springframework.cloud.bindings.boot.config.enable", "false"); + + new ConfigServerBindingsPropertiesProcessor().process(environment, bindings, properties); + + assertThat(properties).isEmpty(); + } + +}