Skip to content

Commit

Permalink
Adds CfConfigClientProcessor to load config-server oauth properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kvmw committed Aug 13, 2024
1 parent 647d3c0 commit 8378bd4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
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();
}

}
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
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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8378bd4

Please sign in to comment.