diff --git a/docs/pom.xml b/docs/pom.xml index e51ce98..de9d65d 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT spring-cloud-cloudfoundry-docs pom diff --git a/pom.xml b/pom.xml index 45f0bf1..a6ce50c 100644 --- a/pom.xml +++ b/pom.xml @@ -4,21 +4,21 @@ 4.0.0 spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT pom org.springframework.cloud spring-cloud-build - 1.3.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT cloudfoundry ${basedir} - 1.3.0.BUILD-SNAPSHOT - 1.2.0.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT @@ -47,8 +47,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/spring-cloud-cloudfoundry-dependencies/pom.xml b/spring-cloud-cloudfoundry-dependencies/pom.xml index c857336..71ef7cc 100644 --- a/spring-cloud-cloudfoundry-dependencies/pom.xml +++ b/spring-cloud-cloudfoundry-dependencies/pom.xml @@ -5,11 +5,11 @@ spring-cloud-dependencies-parent org.springframework.cloud - 1.3.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT spring-cloud-cloudfoundry-dependencies - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT pom spring-cloud-cloudfoundry-dependencies Spring Cloud Cloudfoundry Dependencies diff --git a/spring-cloud-cloudfoundry-discovery/pom.xml b/spring-cloud-cloudfoundry-discovery/pom.xml index 6b710fd..f9bc95d 100644 --- a/spring-cloud-cloudfoundry-discovery/pom.xml +++ b/spring-cloud-cloudfoundry-discovery/pom.xml @@ -10,7 +10,7 @@ org.springframework.cloud spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT .. diff --git a/spring-cloud-cloudfoundry-sample/pom.xml b/spring-cloud-cloudfoundry-sample/pom.xml index f933d60..0384d65 100644 --- a/spring-cloud-cloudfoundry-sample/pom.xml +++ b/spring-cloud-cloudfoundry-sample/pom.xml @@ -9,7 +9,7 @@ org.springframework.cloud spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT .. diff --git a/spring-cloud-cloudfoundry-web/pom.xml b/spring-cloud-cloudfoundry-web/pom.xml index 9d92389..517eb02 100644 --- a/spring-cloud-cloudfoundry-web/pom.xml +++ b/spring-cloud-cloudfoundry-web/pom.xml @@ -10,7 +10,7 @@ org.springframework.cloud spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT .. diff --git a/spring-cloud-cloudfoundry-web/src/main/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessor.java b/spring-cloud-cloudfoundry-web/src/main/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessor.java index 7c59aa4..2997e90 100644 --- a/spring-cloud-cloudfoundry-web/src/main/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessor.java +++ b/spring-cloud-cloudfoundry-web/src/main/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessor.java @@ -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> 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 properties = resolver.getSubProperties("vcap.services."); - if (properties == null || properties.isEmpty()) { + Map properties = Binder.get(environment) + .bind("vcap.services", STRING_OBJECT_MAP).orElseGet(Collections::emptyMap); + if (!hasChildProperties(environment, "vcap.services")) { return; } - Map source = new HashMap(); + Map 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 properties = Binder.get(environment) + .bind(name, STRING_OBJECT_MAP).orElseGet(Collections::emptyMap); + return !properties.isEmpty(); + } + private void addProperty(Map 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), diff --git a/spring-cloud-cloudfoundry-web/src/test/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessorTests.java b/spring-cloud-cloudfoundry-web/src/test/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessorTests.java index 475e3b9..b2f8375 100644 --- a/spring-cloud-cloudfoundry-web/src/test/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessorTests.java +++ b/spring-cloud-cloudfoundry-web/src/test/java/org/springframework/cloud/cloudfoundry/environment/VcapServiceCredentialsEnvironmentPostProcessorTests.java @@ -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 properties = new RelaxedPropertyResolver(this.environment) - .getSubProperties("security.oauth2"); - assertTrue(properties == null || properties.isEmpty()); + Map 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}")); } } diff --git a/spring-cloud-starter-cloudfoundry/pom.xml b/spring-cloud-starter-cloudfoundry/pom.xml index c90ae6c..f418778 100644 --- a/spring-cloud-starter-cloudfoundry/pom.xml +++ b/spring-cloud-starter-cloudfoundry/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-cloudfoundry - 1.1.1.BUILD-SNAPSHOT + 2.0.0.BUILD-SNAPSHOT .. spring-cloud-starter-cloudfoundry