Merge remote-tracking branch 'Upstream/master' into add-vault-docs
This commit is contained in:
14
.travis.yml
14
.travis.yml
@@ -13,16 +13,4 @@ install:
|
||||
- ./mvnw install -P docs -q -U -DskipTests=true -Dmaven.test.redirectTestOutputToFile=true
|
||||
- '[ "${MVN_GOAL}" == "deploy" ] && ./docs/src/main/asciidoc/ghpages.sh || echo "Not updating docs"'
|
||||
script:
|
||||
- './mvnw -s .settings.xml $MVN_GOAL $MVN_PROFILE -nsu -Dmaven.test.redirectTestOutputToFile=true'
|
||||
env:
|
||||
global:
|
||||
- GIT_NAME="Dave Syer"
|
||||
- GIT_EMAIL=dsyer@pivotal.io
|
||||
- CI_DEPLOY_USERNAME=buildmaster
|
||||
- FEATURE_BRANCH=$(echo ${TRAVIS_BRANCH} | grep -q "^.*/.*$" && echo true || echo false)
|
||||
- SPRING_CLOUD_BUILD=$(echo ${TRAVIS_REPO_SLUG} | grep -q "^spring-cloud/.*$" && echo true || echo false)
|
||||
- MVN_GOAL=$([ "${TRAVIS_PULL_REQUEST}" == "false" -a "${TRAVIS_TAG}" == "" -a "${FEATURE_BRANCH}" == "false" -a "${SPRING_CLOUD_BUILD}" == "true" ] && echo deploy || echo install)
|
||||
- VERSION=$(mvn validate | grep Building | head -1 | sed -e 's/.* //')
|
||||
- MILESTONE=$(echo ${VERSION} | egrep 'M|RC' && echo true || echo false)
|
||||
- MVN_PROFILE=$([ "${MILESTONE}" == "true" ] && echo -P milestone)
|
||||
- secure: ebyeRdLvcMEhxeAcXjHb6s8+Gr7R9W4PDcSLEIxviZQgTQd3BM/ODNEkrKDfyE1IfIaTznit+A6qxXCjxVrsHXYn8yiEVGFMp+2ijo+e7K7VSbRXBtIfDMzELOZJ7+zIiLdDdwP/9wNGNOSe7q/wChBOuqLh2VCYngcof1ZVSxQ=
|
||||
- './mvnw -s .settings.xml install -nsu -Dmaven.test.redirectTestOutputToFile=true'
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Dave Syer
|
||||
@@ -106,7 +107,7 @@ public class EnvironmentRepositoryConfiguration {
|
||||
protected static class VaultConfiguration {
|
||||
@Bean
|
||||
public EnvironmentRepository environmentRepository(HttpServletRequest request, EnvironmentWatch watch) {
|
||||
return new VaultEnvironmentRepository(request, watch);
|
||||
return new VaultEnvironmentRepository(request, watch, new RestTemplate());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -61,16 +61,17 @@ public class VaultEnvironmentRepository implements EnvironmentRepository {
|
||||
@NotEmpty
|
||||
private String profileSeparator = ",";
|
||||
|
||||
private RestTemplate rest = new RestTemplate();
|
||||
private RestTemplate rest;
|
||||
|
||||
//TODO: move to watchState:String on findOne?
|
||||
private HttpServletRequest request;
|
||||
|
||||
private EnvironmentWatch watch;
|
||||
|
||||
public VaultEnvironmentRepository(HttpServletRequest request, EnvironmentWatch watch) {
|
||||
public VaultEnvironmentRepository(HttpServletRequest request, EnvironmentWatch watch, RestTemplate rest) {
|
||||
this.request = request;
|
||||
this.watch = watch;
|
||||
this.rest = rest;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -89,13 +90,15 @@ public class VaultEnvironmentRepository implements EnvironmentRepository {
|
||||
for (String key : keys) {
|
||||
// read raw 'data' key from vault
|
||||
String data = read(key);
|
||||
// data is in json format of which, yaml is a superset, so parse
|
||||
final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
|
||||
yaml.setResources(new ByteArrayResource(data.getBytes()));
|
||||
Properties properties = yaml.getObject();
|
||||
if (data != null) {
|
||||
// data is in json format of which, yaml is a superset, so parse
|
||||
final YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
|
||||
yaml.setResources(new ByteArrayResource(data.getBytes()));
|
||||
Properties properties = yaml.getObject();
|
||||
|
||||
if (!properties.isEmpty()) {
|
||||
environment.add(new PropertySource("vault:"+key, properties));
|
||||
if (!properties.isEmpty()) {
|
||||
environment.add(new PropertySource("vault:" + key, properties));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,74 @@
|
||||
package org.springframework.cloud.config.server.environment;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.cloud.config.environment.Environment;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
* @author Ryan Baxter
|
||||
*/
|
||||
public class VaultEnvironmentRepositoryTests {
|
||||
|
||||
|
||||
@Before
|
||||
public void init() {}
|
||||
|
||||
@Test
|
||||
public void testFindOne() {
|
||||
//TODO: implement testFindOne
|
||||
public void testFindOne() throws IOException {
|
||||
MockHttpServletRequest configRequest = new MockHttpServletRequest();
|
||||
configRequest.addHeader("X-CONFIG-TOKEN", "mytoken");
|
||||
RestTemplate rest = Mockito.mock(RestTemplate.class);
|
||||
ResponseEntity<VaultEnvironmentRepository.VaultResponse> myAppResp = Mockito.mock(ResponseEntity.class);
|
||||
Mockito.when(myAppResp.getStatusCode()).thenReturn(HttpStatus.OK);
|
||||
VaultEnvironmentRepository.VaultResponse myAppVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
|
||||
Mockito.when(myAppVaultResp.getData()).thenReturn("{\"foo\":\"bar\"}");
|
||||
Mockito.when(myAppResp.getBody()).thenReturn(myAppVaultResp);
|
||||
Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"),
|
||||
Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class),
|
||||
Mockito.eq("secret"), Mockito.eq("myapp"))).thenReturn(myAppResp);
|
||||
ResponseEntity<VaultEnvironmentRepository.VaultResponse> appResp = Mockito.mock(ResponseEntity.class);
|
||||
Mockito.when(appResp.getStatusCode()).thenReturn(HttpStatus.OK);
|
||||
VaultEnvironmentRepository.VaultResponse appVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
|
||||
Mockito.when(appVaultResp.getData()).thenReturn(null);
|
||||
Mockito.when(appResp.getBody()).thenReturn(appVaultResp);
|
||||
Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"),
|
||||
Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class),
|
||||
Mockito.eq("secret"), Mockito.eq("application"))).thenReturn(appResp);
|
||||
VaultEnvironmentRepository repo = new VaultEnvironmentRepository(configRequest, new EnvironmentWatch.Default(), rest);
|
||||
Environment e = repo.findOne("myapp", null, null);
|
||||
assertEquals("myapp", e.getName());
|
||||
Map<String,String> result = new HashMap<String,String>();
|
||||
result.put("foo", "bar");
|
||||
assertEquals(result, e.getPropertySources().get(0).getSource());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void missingConfigToken() throws IOException {
|
||||
MockHttpServletRequest configRequest = new MockHttpServletRequest();
|
||||
RestTemplate rest = Mockito.mock(RestTemplate.class);
|
||||
ResponseEntity<VaultEnvironmentRepository.VaultResponse> myAppResp = Mockito.mock(ResponseEntity.class);
|
||||
Mockito.when(myAppResp.getStatusCode()).thenReturn(HttpStatus.OK);
|
||||
VaultEnvironmentRepository.VaultResponse myAppVaultResp = Mockito.mock(VaultEnvironmentRepository.VaultResponse.class);
|
||||
Mockito.when(myAppVaultResp.getData()).thenReturn("{\"foo\":\"bar\"}");
|
||||
Mockito.when(myAppResp.getBody()).thenReturn(myAppVaultResp);
|
||||
Mockito.when(rest.exchange(Mockito.eq("http://127.0.0.1:8200/v1/{backend}/{key}"),
|
||||
Mockito.eq(HttpMethod.GET), Mockito.any(HttpEntity.class), Mockito.eq(VaultEnvironmentRepository.VaultResponse.class),
|
||||
Mockito.eq("secret"), Mockito.eq("myapp"))).thenReturn(myAppResp);
|
||||
VaultEnvironmentRepository repo = new VaultEnvironmentRepository(configRequest, new EnvironmentWatch.Default(), rest);
|
||||
repo.findOne("myapp", null, null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user