From 8378bd405d14549199c716d4feebe67e8447efa7 Mon Sep 17 00:00:00 2001 From: kvmw Date: Thu, 22 Dec 2022 16:43:42 +0100 Subject: [PATCH] Adds CfConfigClientProcessor to load config-server oauth properties --- .../boot/scs/CfConfigClientProcessor.java | 55 +++++++++++ .../main/resources/META-INF/spring.factories | 3 +- .../CfConfigClientProcessorTest.java | 98 +++++++++++++++++++ .../CfEurekaClientProcessorTest.java | 2 +- 4 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 java-cfenv-boot-pivotal-scs/src/main/java/io/pivotal/cfenv/boot/scs/CfConfigClientProcessor.java create mode 100644 java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfConfigClientProcessorTest.java diff --git a/java-cfenv-boot-pivotal-scs/src/main/java/io/pivotal/cfenv/boot/scs/CfConfigClientProcessor.java b/java-cfenv-boot-pivotal-scs/src/main/java/io/pivotal/cfenv/boot/scs/CfConfigClientProcessor.java new file mode 100644 index 00000000..77898c14 --- /dev/null +++ b/java-cfenv-boot-pivotal-scs/src/main/java/io/pivotal/cfenv/boot/scs/CfConfigClientProcessor.java @@ -0,0 +1,55 @@ +/* + * Copyright 2019 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 + * + * https://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 io.pivotal.cfenv.boot.scs; + +import java.util.Map; + +import io.pivotal.cfenv.core.CfCredentials; +import io.pivotal.cfenv.core.CfService; +import io.pivotal.cfenv.spring.boot.CfEnvProcessor; +import io.pivotal.cfenv.spring.boot.CfEnvProcessorProperties; + +/** + * Sets config-client properties with values found in service bindings of a + * spring-cloud-services config-server service instance. + */ +public class CfConfigClientProcessor implements CfEnvProcessor { + + @Override + public boolean accept(CfService service) { + return service.existsByTagIgnoreCase("configuration"); + } + + @Override + public void process(CfCredentials cfCredentials, Map properties) { + properties.put("spring.cloud.config.uri", cfCredentials.getUri()); + properties.put("spring.cloud.config.client.oauth2.client-id", cfCredentials.getString("client_id")); + properties.put("spring.cloud.config.client.oauth2.client-secret", cfCredentials.getString("client_secret")); + properties.put("spring.cloud.config.client.oauth2.access-token-uri", cfCredentials.getString("access_token_uri")); + properties.put("spring.cloud.config.client.oauth2.scope", ""); + + properties.put("spring.cloud.refresh.additional-property-sources-to-retain", this.getClass().getSimpleName()); + } + + @Override + public CfEnvProcessorProperties getProperties() { + return CfEnvProcessorProperties.builder() + .propertyPrefixes("spring.cloud.config.client") + .serviceName("Spring Cloud Config") + .build(); + } + +} diff --git a/java-cfenv-boot-pivotal-scs/src/main/resources/META-INF/spring.factories b/java-cfenv-boot-pivotal-scs/src/main/resources/META-INF/spring.factories index ac8227a0..ecca4ab1 100644 --- a/java-cfenv-boot-pivotal-scs/src/main/resources/META-INF/spring.factories +++ b/java-cfenv-boot-pivotal-scs/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ io.pivotal.cfenv.spring.boot.CfEnvProcessor=\ -io.pivotal.cfenv.boot.scs.CfEurekaClientProcessor + io.pivotal.cfenv.boot.scs.CfEurekaClientProcessor, \ + io.pivotal.cfenv.boot.scs.CfConfigClientProcessor \ No newline at end of file diff --git a/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfConfigClientProcessorTest.java b/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfConfigClientProcessorTest.java new file mode 100644 index 00000000..e32f3c0a --- /dev/null +++ b/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfConfigClientProcessorTest.java @@ -0,0 +1,98 @@ +/* + * Copyright 2019 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 + * + * https://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 io.pivotal.cfenv.boot.scs.serviceregistry; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import io.pivotal.cfenv.boot.scs.CfConfigClientProcessor; +import io.pivotal.cfenv.core.CfCredentials; +import io.pivotal.cfenv.core.CfService; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class CfConfigClientProcessorTest { + private static final String URI = "uri"; + private static final String CLIENT_ID = "clientId"; + private static final String CLIENT_SECRET = "clientSecret"; + private static final String ACCESS_TOKEN_URI = "accessTokenUri"; + + @Mock + private CfService configService; + @Mock + private CfService otherService; + @Mock + private CfCredentials cfCredentials; + + @InjectMocks + private CfConfigClientProcessor configClientProcessor; + + @Before + public void setUp() { + when(configService.existsByTagIgnoreCase("configuration")).thenReturn(true); + + when(cfCredentials.getUri()).thenReturn(URI); + when(cfCredentials.getString("client_id")).thenReturn(CLIENT_ID); + when(cfCredentials.getString("client_secret")).thenReturn(CLIENT_SECRET); + when(cfCredentials.getString("access_token_uri")).thenReturn(ACCESS_TOKEN_URI); + } + + @Test + public void shouldAcceptConfigService() { + boolean actual = configClientProcessor.accept(configService); + + assertThat(actual).isTrue(); + } + + @Test + public void shouldNotAcceptOtherService() { + boolean actual = configClientProcessor.accept(otherService); + + assertThat(actual).isFalse(); + } + + @Test + public void shouldProcessCfCredentials() { + Map properties = new HashMap<>(); + + configClientProcessor.process(cfCredentials, properties); + + assertThat(properties.get("spring.cloud.config.uri")).isEqualTo( URI); + assertThat(properties.get("spring.cloud.config.client.oauth2.client-id")).isEqualTo(CLIENT_ID); + assertThat(properties.get("spring.cloud.config.client.oauth2.client-secret")).isEqualTo(CLIENT_SECRET); + assertThat(properties.get("spring.cloud.config.client.oauth2.access-token-uri")).isEqualTo(ACCESS_TOKEN_URI); + assertThat(properties.get("spring.cloud.config.client.oauth2.scope")).isEqualTo(""); + } + + @Test + public void shouldRetainThePropertiesDuringRefresh() { + Map properties = new HashMap<>(); + + configClientProcessor.process(cfCredentials, properties); + + assertThat(properties.get("spring.cloud.refresh.additional-property-sources-to-retain")) + .isEqualTo("CfConfigClientProcessor"); + } +} diff --git a/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfEurekaClientProcessorTest.java b/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfEurekaClientProcessorTest.java index 977ec262..ff4b17b5 100644 --- a/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfEurekaClientProcessorTest.java +++ b/java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfEurekaClientProcessorTest.java @@ -53,7 +53,7 @@ public class CfEurekaClientProcessorTest { private CfEurekaClientProcessor eurekaClientProcessor; @Before - public void setUp() throws Exception { + public void setUp() { when(eurekaService.existsByTagIgnoreCase("eureka")).thenReturn(true); when(cfCredentials.getUri()).thenReturn(URI);