Skip to content

Commit

Permalink
Support userInfo in elasticsearch uri
Browse files Browse the repository at this point in the history
Fixes gh-21291
  • Loading branch information
evgeniycheban committed May 11, 2020
1 parent 7afd25f commit b04d7b3
Showing 1 changed file with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.elasticsearch;

import java.net.URI;
import java.time.Duration;

import org.apache.http.HttpHost;
Expand Down Expand Up @@ -43,6 +44,7 @@
* @author Brian Clozel
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Evgeniy Cheban
*/
class ElasticsearchRestClientConfigurations {

Expand All @@ -58,7 +60,7 @@ RestClientBuilderCustomizer defaultRestClientBuilderCustomizer(ElasticsearchRest
@Bean
RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperties properties,
ObjectProvider<RestClientBuilderCustomizer> builderCustomizers) {
HttpHost[] hosts = properties.getUris().stream().map(HttpHost::create).toArray(HttpHost[]::new);
HttpHost[] hosts = properties.getUris().stream().map(this::createHttpHost).toArray(HttpHost[]::new);
RestClientBuilder builder = RestClient.builder(hosts);
builder.setHttpClientConfigCallback((httpClientBuilder) -> {
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder));
Expand All @@ -72,6 +74,11 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchRestClientProperti
return builder;
}

private HttpHost createHttpHost(String uri) {
URI parsedUri = URI.create(uri);
String userInfo = parsedUri.getUserInfo();
return HttpHost.create((userInfo != null ? uri.replace(userInfo + "@", "") : uri));
}
}

@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -124,15 +131,34 @@ public void customize(RestClientBuilder builder) {

@Override
public void customize(HttpAsyncClientBuilder builder) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
builder.setDefaultCredentialsProvider(credentialsProvider);
this.properties.getUris().stream()
.map(URI::create)
.filter((uri) -> uri.getUserInfo() != null)
.forEach((uri) -> {
AuthScope authScope = new AuthScope(uri.getHost(), uri.getPort());
Credentials credentials = createCredentials(uri.getUserInfo());
credentialsProvider.setCredentials(authScope, credentials);
});
map.from(this.properties::getUsername).whenHasText().to((username) -> {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
Credentials credentials = new UsernamePasswordCredentials(this.properties.getUsername(),
this.properties.getPassword());
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
builder.setDefaultCredentialsProvider(credentialsProvider);
});
}

private Credentials createCredentials(String usernameAndPassword) {
int delimiter = usernameAndPassword.indexOf(":");
if (delimiter == -1) {
return new UsernamePasswordCredentials(usernameAndPassword, null);
}

String username = usernameAndPassword.substring(0, delimiter);
String password = usernameAndPassword.substring(delimiter + 1);
return new UsernamePasswordCredentials(username, password);
}

@Override
public void customize(RequestConfig.Builder builder) {
map.from(this.properties::getConnectionTimeout).whenNonNull().asInt(Duration::toMillis)
Expand Down

0 comments on commit b04d7b3

Please sign in to comment.