Adds support for SCS config server

Adds support for Spring Cloud Services Config Server. Bindings must have kind config.

Signed-off-by: Emily Casey <ecasey@pivotal.io>
This commit is contained in:
Emily Casey
2020-06-01 18:07:10 -04:00
parent a1c171e2aa
commit bbda2549f1
6 changed files with 137 additions and 3 deletions

View File

@@ -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`

View File

@@ -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<String, Object> 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");
});
}
}

View File

@@ -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)
);

View File

@@ -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

View File

@@ -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);
}
}

View File

@@ -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<String, Object> 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();
}
}