Skip to content

Commit 3466053

Browse files
authored
Adds CfConfigClientProcessor to load config-server oauth properties (#198)
1 parent 647d3c0 commit 3466053

File tree

4 files changed

+156
-2
lines changed

4 files changed

+156
-2
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.pivotal.cfenv.boot.scs;
17+
18+
import java.util.Map;
19+
20+
import io.pivotal.cfenv.core.CfCredentials;
21+
import io.pivotal.cfenv.core.CfService;
22+
import io.pivotal.cfenv.spring.boot.CfEnvProcessor;
23+
import io.pivotal.cfenv.spring.boot.CfEnvProcessorProperties;
24+
25+
/**
26+
* Sets config-client properties with values found in service bindings of a
27+
* spring-cloud-services config-server service instance.
28+
*/
29+
public class CfConfigClientProcessor implements CfEnvProcessor {
30+
31+
@Override
32+
public boolean accept(CfService service) {
33+
return service.existsByTagIgnoreCase("configuration");
34+
}
35+
36+
@Override
37+
public void process(CfCredentials cfCredentials, Map<String, Object> properties) {
38+
properties.put("spring.cloud.config.uri", cfCredentials.getUri());
39+
properties.put("spring.cloud.config.client.oauth2.client-id", cfCredentials.getString("client_id"));
40+
properties.put("spring.cloud.config.client.oauth2.client-secret", cfCredentials.getString("client_secret"));
41+
properties.put("spring.cloud.config.client.oauth2.access-token-uri", cfCredentials.getString("access_token_uri"));
42+
properties.put("spring.cloud.config.client.oauth2.scope", "");
43+
44+
properties.put("spring.cloud.refresh.additional-property-sources-to-retain", this.getClass().getSimpleName());
45+
}
46+
47+
@Override
48+
public CfEnvProcessorProperties getProperties() {
49+
return CfEnvProcessorProperties.builder()
50+
.propertyPrefixes("spring.cloud.config.client")
51+
.serviceName("Spring Cloud Config")
52+
.build();
53+
}
54+
55+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
io.pivotal.cfenv.spring.boot.CfEnvProcessor=\
2-
io.pivotal.cfenv.boot.scs.CfEurekaClientProcessor
2+
io.pivotal.cfenv.boot.scs.CfEurekaClientProcessor, \
3+
io.pivotal.cfenv.boot.scs.CfConfigClientProcessor
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.pivotal.cfenv.boot.scs.serviceregistry;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.mockito.InjectMocks;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.MockitoJUnitRunner;
27+
28+
import io.pivotal.cfenv.boot.scs.CfConfigClientProcessor;
29+
import io.pivotal.cfenv.core.CfCredentials;
30+
import io.pivotal.cfenv.core.CfService;
31+
32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.mockito.Mockito.when;
34+
35+
@RunWith(MockitoJUnitRunner.class)
36+
public class CfConfigClientProcessorTest {
37+
private static final String URI = "uri";
38+
private static final String CLIENT_ID = "clientId";
39+
private static final String CLIENT_SECRET = "clientSecret";
40+
private static final String ACCESS_TOKEN_URI = "accessTokenUri";
41+
42+
@Mock
43+
private CfService configService;
44+
@Mock
45+
private CfService otherService;
46+
@Mock
47+
private CfCredentials cfCredentials;
48+
49+
@InjectMocks
50+
private CfConfigClientProcessor configClientProcessor;
51+
52+
@Before
53+
public void setUp() {
54+
when(configService.existsByTagIgnoreCase("configuration")).thenReturn(true);
55+
56+
when(cfCredentials.getUri()).thenReturn(URI);
57+
when(cfCredentials.getString("client_id")).thenReturn(CLIENT_ID);
58+
when(cfCredentials.getString("client_secret")).thenReturn(CLIENT_SECRET);
59+
when(cfCredentials.getString("access_token_uri")).thenReturn(ACCESS_TOKEN_URI);
60+
}
61+
62+
@Test
63+
public void shouldAcceptConfigService() {
64+
boolean actual = configClientProcessor.accept(configService);
65+
66+
assertThat(actual).isTrue();
67+
}
68+
69+
@Test
70+
public void shouldNotAcceptOtherService() {
71+
boolean actual = configClientProcessor.accept(otherService);
72+
73+
assertThat(actual).isFalse();
74+
}
75+
76+
@Test
77+
public void shouldProcessCfCredentials() {
78+
Map<String, Object> properties = new HashMap<>();
79+
80+
configClientProcessor.process(cfCredentials, properties);
81+
82+
assertThat(properties.get("spring.cloud.config.uri")).isEqualTo( URI);
83+
assertThat(properties.get("spring.cloud.config.client.oauth2.client-id")).isEqualTo(CLIENT_ID);
84+
assertThat(properties.get("spring.cloud.config.client.oauth2.client-secret")).isEqualTo(CLIENT_SECRET);
85+
assertThat(properties.get("spring.cloud.config.client.oauth2.access-token-uri")).isEqualTo(ACCESS_TOKEN_URI);
86+
assertThat(properties.get("spring.cloud.config.client.oauth2.scope")).isEqualTo("");
87+
}
88+
89+
@Test
90+
public void shouldRetainThePropertiesDuringRefresh() {
91+
Map<String, Object> properties = new HashMap<>();
92+
93+
configClientProcessor.process(cfCredentials, properties);
94+
95+
assertThat(properties.get("spring.cloud.refresh.additional-property-sources-to-retain"))
96+
.isEqualTo("CfConfigClientProcessor");
97+
}
98+
}

java-cfenv-boot-pivotal-scs/src/test/java/io/pivotal/cfenv/boot/scs/serviceregistry/CfEurekaClientProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class CfEurekaClientProcessorTest {
5353
private CfEurekaClientProcessor eurekaClientProcessor;
5454

5555
@Before
56-
public void setUp() throws Exception {
56+
public void setUp() {
5757
when(eurekaService.existsByTagIgnoreCase("eureka")).thenReturn(true);
5858

5959
when(cfCredentials.getUri()).thenReturn(URI);

0 commit comments

Comments
 (0)