Allow to configure elasticsearch service with only a URI

Closes gh-789
This commit is contained in:
Stephane Nicoll
2019-01-03 13:52:35 +01:00
parent 3b88b952a0
commit b9000f322d
8 changed files with 133 additions and 74 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-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.
@@ -28,8 +28,6 @@ import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
/**
* Post-process the environment to extract the service credentials provided by
@@ -51,16 +49,7 @@ public class CloudfoundryEnvironmentPostProcessor
Map<String, Object> map = new LinkedHashMap<>();
String uri = environment.getProperty("vcap.services.stats-index.credentials.uri");
if (StringUtils.hasText(uri)) {
UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
String userInfo = uriComponents.getUserInfo();
if (StringUtils.hasText(userInfo)) {
String[] credentials = userInfo.split(":");
map.put("initializr.stats.elastic.username", credentials[0]);
map.put("initializr.stats.elastic.password", credentials[1]);
}
map.put("initializr.stats.elastic.uri", UriComponentsBuilder
.fromUriString(uri).userInfo(null).build().toString());
map.put("initializr.stats.elastic.uri", uri);
addOrReplace(environment.getPropertySources(), map);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-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.
@@ -35,21 +35,21 @@ public class CloudfoundryEnvironmentPostProcessorTests {
private final SpringApplication application = new SpringApplication();
@Test
public void parseCredentials() {
public void parseUriWithCredentials() {
this.environment.setProperty("vcap.services.stats-index.credentials.uri",
"http://user:pass@example.com/bar/biz?param=one");
"https://user:pass@example.com/bar/biz?param=one");
this.postProcessor.postProcessEnvironment(this.environment, this.application);
assertThat(this.environment.getProperty("initializr.stats.elastic.uri"))
.isEqualTo("http://example.com/bar/biz?param=one");
.isEqualTo("https://user:pass@example.com/bar/biz?param=one");
assertThat(this.environment.getProperty("initializr.stats.elastic.username"))
.isEqualTo("user");
.isNull();
assertThat(this.environment.getProperty("initializr.stats.elastic.password"))
.isEqualTo("pass");
.isNull();
}
@Test
public void parseNoCredentials() {
public void parseUri() {
this.environment.setProperty("vcap.services.stats-index.credentials.uri",
"http://example.com/bar/biz?param=one");
this.postProcessor.postProcessEnvironment(this.environment, this.application);