Updates for Finchley and boot 2.0

This commit is contained in:
Spencer Gibb
2017-06-28 12:49:51 -06:00
parent 807d1393da
commit 72b04f00b6
9 changed files with 75 additions and 69 deletions

View File

@@ -15,16 +15,19 @@
*/
package org.springframework.cloud.cloudfoundry.environment;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.config.ConfigFileApplicationListener;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;
/**
@@ -34,6 +37,9 @@ import org.springframework.util.StringUtils;
public class VcapServiceCredentialsEnvironmentPostProcessor
implements EnvironmentPostProcessor, Ordered {
static final Bindable<Map<String, Object>> STRING_OBJECT_MAP = Bindable
.mapOf(String.class, Object.class);
// After VcapEnvironmentPostProcessor and ConfigFileEnvironmentPostProcessor so
// values here can
// use those ones
@@ -47,43 +53,42 @@ implements EnvironmentPostProcessor, Ordered {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment);
Map<String, Object> properties = resolver.getSubProperties("vcap.services.");
if (properties == null || properties.isEmpty()) {
Map<String, Object> properties = Binder.get(environment)
.bind("vcap.services", STRING_OBJECT_MAP).orElseGet(Collections::emptyMap);
if (!hasChildProperties(environment, "vcap.services")) {
return;
}
Map<String, Object> source = new HashMap<String, Object>();
Map<String, Object> source = new HashMap<>();
String serviceId;
if (!resolver.getSubProperties("security.oauth2.resource.").isEmpty()) {
serviceId = resolver.getProperty("security.oauth2.resource.serviceId",
if (hasChildProperties(environment, "security.oauth2.resource")) {
serviceId = environment.getProperty("security.oauth2.resource.service-id",
"resource");
}
else {
serviceId = resolver.getProperty("security.oauth2.sso.serviceId", "sso");
serviceId = environment.getProperty("security.oauth2.sso.service-id", "sso");
}
String authDomain = (String) properties
.get(serviceId + ".credentials.auth_domain");
String authDomain = environment.getProperty("vcap.services." +serviceId + ".credentials.auth-domain");
if (authDomain != null) {
source.put("security.oauth2.resource.userInfoUri",
source.put("security.oauth2.resource.user-info-uri",
authDomain + "/userinfo");
source.put("security.oauth2.resource.jwt.keyUri", authDomain + "/token_key");
source.put("security.oauth2.client.accessTokenUri",
source.put("security.oauth2.resource.jwt.key-uri", authDomain + "/token_key");
source.put("security.oauth2.client.access-token-uri",
authDomain + "/oauth/token");
source.put("security.oauth2.client.userAuthorizationUri",
source.put("security.oauth2.client.user-authorization-uri",
authDomain + "/oauth/authorize");
}
else {
addProperty(source, resolver, serviceId, "resource", "userInfoUri");
addProperty(source, resolver, serviceId, "resource", "tokenInfoUri");
addProperty(source, resolver, serviceId, "resource.jwt", "keyUri");
addProperty(source, resolver, serviceId, "resource", "keyValue");
addProperty(source, resolver, serviceId, "client", "accessTokenUri", "tokenUri");
addProperty(source, resolver, serviceId, "client", "userAuthorizationUri", "authorizationUri");
addProperty(source, environment, serviceId, "resource", "user-info-uri");
addProperty(source, environment, serviceId, "resource", "token-info-uri");
addProperty(source, environment, serviceId, "resource.jwt", "key-uri");
addProperty(source, environment, serviceId, "resource", "key-value");
addProperty(source, environment, serviceId, "client", "access-token-uri", "token-uri");
addProperty(source, environment, serviceId, "client", "user-authorization-uri", "authorization-uri");
}
addProperty(source, resolver, serviceId, "client", "clientId");
addProperty(source, resolver, serviceId, "client", "clientSecret");
addProperty(source, resolver, serviceId, "client", "scope");
String resourceId = resolver
addProperty(source, environment, serviceId, "client", "client-id");
addProperty(source, environment, serviceId, "client", "client-secret");
addProperty(source, environment, serviceId, "client", "scope");
String resourceId = environment
.getProperty("vcap.services." + serviceId + ".credentials.id", "");
if (StringUtils.hasText(resourceId)) {
source.put("security.oauth2.resource.id", resourceId);
@@ -92,8 +97,14 @@ implements EnvironmentPostProcessor, Ordered {
.addLast(new MapPropertySource("cloudDefaultSecurityBindings", source));
}
private boolean hasChildProperties(ConfigurableEnvironment environment, String name) {
Map<String, Object> properties = Binder.get(environment)
.bind(name, STRING_OBJECT_MAP).orElseGet(Collections::emptyMap);
return !properties.isEmpty();
}
private void addProperty(Map<String, Object> source,
RelaxedPropertyResolver resolver, String serviceId, String stem, String key, String... altKeys) {
PropertyResolver resolver, String serviceId, String stem, String key, String... altKeys) {
String value = resolve(resolver, serviceId, key);
if (StringUtils.hasText(value)) {
source.put("security.oauth2."+stem+"." + key, value);
@@ -108,7 +119,7 @@ implements EnvironmentPostProcessor, Ordered {
}
}
private String resolve(RelaxedPropertyResolver resolver, String serviceId,
private String resolve(PropertyResolver resolver, String serviceId,
String key) {
return resolver.getProperty(
String.format("vcap.services.%s.credentials.%s", serviceId, key),

View File

@@ -15,18 +15,20 @@
*/
package org.springframework.cloud.cloudfoundry.environment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.Map;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.springframework.cloud.cloudfoundry.environment.VcapServiceCredentialsEnvironmentPostProcessor.STRING_OBJECT_MAP;
/**
* @author Dave Syer
*
@@ -40,73 +42,66 @@ public class VcapServiceCredentialsEnvironmentPostProcessorTests {
@Test
public void noop() {
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
Map<String, Object> properties = new RelaxedPropertyResolver(this.environment)
.getSubProperties("security.oauth2");
assertTrue(properties == null || properties.isEmpty());
Map<String, Object> properties = Binder.get(environment)
.bind("security.oauth2", STRING_OBJECT_MAP).orElseGet(Collections::emptyMap);
assertTrue(properties.isEmpty());
}
@Test
public void addClientId() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.clientId:foo");
TestPropertyValues.of("vcap.services.sso.credentials.clientId:foo").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("foo", this.environment
.resolvePlaceholders("${security.oauth2.client.clientId}"));
.resolvePlaceholders("${security.oauth2.client.client-id}"));
}
@Test
public void addClientIdUnderscores() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.client_id:foo");
TestPropertyValues.of("vcap.services.sso.credentials.client-id:foo").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("foo", this.environment
.resolvePlaceholders("${security.oauth2.client.clientId}"));
.resolvePlaceholders("${security.oauth2.client.client-id}"));
}
@Test
public void addTokenUri() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.accessTokenUri:http://example.com");
TestPropertyValues.of( "vcap.services.sso.credentials.accessTokenUri:http://example.com").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("http://example.com", this.environment
.resolvePlaceholders("${security.oauth2.client.accessTokenUri}"));
.resolvePlaceholders("${security.oauth2.client.access-token-uri}"));
}
@Test
public void addTokenUriAuthDomain() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.auth_domain:http://example.com");
TestPropertyValues.of("vcap.services.sso.credentials.auth-domain:http://example.com").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("http://example.com/oauth/token", this.environment
.resolvePlaceholders("${security.oauth2.client.accessTokenUri}"));
.resolvePlaceholders("${security.oauth2.client.access-token-uri}"));
}
@Test
public void addUserInfoUri() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.userInfoUri:http://example.com");
TestPropertyValues.of( "vcap.services.sso.credentials.userInfoUri:http://example.com").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("http://example.com", this.environment
.resolvePlaceholders("${security.oauth2.resource.userInfoUri}"));
.resolvePlaceholders("${security.oauth2.resource.user-info-uri}"));
}
@Test
public void addServiceId() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.my.credentials.accessTokenUri:http://example.com",
"security.oauth2.sso.serviceId:my");
TestPropertyValues.of("vcap.services.my.credentials.accessTokenUri:http://example.com",
"security.oauth2.sso.serviceId:my").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("http://example.com", this.environment
.resolvePlaceholders("${security.oauth2.client.accessTokenUri}"));
.resolvePlaceholders("${security.oauth2.client.access-token-uri}"));
}
@Test
public void addJwtKeyUri() {
EnvironmentTestUtils.addEnvironment(this.environment,
"vcap.services.sso.credentials.keyUri:http://example.com");
TestPropertyValues.of("vcap.services.sso.credentials.keyUri:http://example.com").applyTo(this.environment);
this.listener.postProcessEnvironment(this.environment, new SpringApplication());
assertEquals("http://example.com", this.environment
.resolvePlaceholders("${security.oauth2.resource.jwt.keyUri}"));
.resolvePlaceholders("${security.oauth2.resource.jwt.key-uri}"));
}
}