-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds CfConfigClientProcessor to load config-server oauth properties
- Loading branch information
Showing
4 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...env-boot-pivotal-scs/src/main/java/io/pivotal/cfenv/boot/scs/CfConfigClientProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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(); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
java-cfenv-boot-pivotal-scs/src/main/resources/META-INF/spring.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
98 changes: 98 additions & 0 deletions
98
.../src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfConfigClientProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Object> 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<String, Object> properties = new HashMap<>(); | ||
|
||
configClientProcessor.process(cfCredentials, properties); | ||
|
||
assertThat(properties.get("spring.cloud.refresh.additional-property-sources-to-retain")) | ||
.isEqualTo("CfConfigClientProcessor"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters