|
|
|
@ -16,13 +16,14 @@
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.autoconfigure.elasticsearch;
|
|
|
|
|
|
|
|
|
|
import java.net.URI;
|
|
|
|
|
import java.net.URISyntaxException;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
|
|
|
|
|
import org.apache.http.HttpHost;
|
|
|
|
|
import org.apache.http.auth.AuthScope;
|
|
|
|
|
import org.apache.http.auth.Credentials;
|
|
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
|
|
|
|
import org.apache.http.client.CredentialsProvider;
|
|
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
|
|
|
|
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
|
|
|
|
@ -36,6 +37,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
|
|
|
|
|
import org.springframework.boot.context.properties.PropertyMapper;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Elasticsearch rest client infrastructure configurations.
|
|
|
|
@ -43,6 +45,7 @@ import org.springframework.context.annotation.Configuration;
|
|
|
|
|
* @author Brian Clozel
|
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
* @author Vedran Pavic
|
|
|
|
|
* @author Evgeniy Cheban
|
|
|
|
|
*/
|
|
|
|
|
class ElasticsearchRestClientConfigurations {
|
|
|
|
|
|
|
|
|
@ -58,7 +61,7 @@ class ElasticsearchRestClientConfigurations {
|
|
|
|
|
@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));
|
|
|
|
@ -72,6 +75,28 @@ class ElasticsearchRestClientConfigurations {
|
|
|
|
|
return builder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpHost createHttpHost(String uri) {
|
|
|
|
|
try {
|
|
|
|
|
return createHttpHost(URI.create(uri));
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
|
return HttpHost.create(uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HttpHost createHttpHost(URI uri) {
|
|
|
|
|
if (!StringUtils.hasLength(uri.getUserInfo())) {
|
|
|
|
|
return HttpHost.create(uri.toString());
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return HttpHost.create(new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(),
|
|
|
|
|
uri.getQuery(), uri.getFragment()).toString());
|
|
|
|
|
}
|
|
|
|
|
catch (URISyntaxException ex) {
|
|
|
|
|
throw new IllegalStateException(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@ -124,13 +149,7 @@ class ElasticsearchRestClientConfigurations {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void customize(HttpAsyncClientBuilder builder) {
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
builder.setDefaultCredentialsProvider(new PropertiesCredentialsProvider(this.properties));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -143,4 +162,47 @@ class ElasticsearchRestClientConfigurations {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class PropertiesCredentialsProvider extends BasicCredentialsProvider {
|
|
|
|
|
|
|
|
|
|
PropertiesCredentialsProvider(ElasticsearchRestClientProperties properties) {
|
|
|
|
|
if (StringUtils.hasText(properties.getUsername())) {
|
|
|
|
|
Credentials credentials = new UsernamePasswordCredentials(properties.getUsername(),
|
|
|
|
|
properties.getPassword());
|
|
|
|
|
setCredentials(AuthScope.ANY, credentials);
|
|
|
|
|
}
|
|
|
|
|
properties.getUris().stream().map(this::toUri).filter(this::hasUserInfo)
|
|
|
|
|
.forEach(this::addUserInfoCredentials);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private URI toUri(String uri) {
|
|
|
|
|
try {
|
|
|
|
|
return URI.create(uri);
|
|
|
|
|
}
|
|
|
|
|
catch (IllegalArgumentException ex) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean hasUserInfo(URI uri) {
|
|
|
|
|
return uri != null && StringUtils.hasLength(uri.getUserInfo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addUserInfoCredentials(URI uri) {
|
|
|
|
|
AuthScope authScope = new AuthScope(uri.getHost(), uri.getPort());
|
|
|
|
|
Credentials credentials = createUserInfoCredentials(uri.getUserInfo());
|
|
|
|
|
setCredentials(authScope, credentials);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Credentials createUserInfoCredentials(String userInfo) {
|
|
|
|
|
int delimiter = userInfo.indexOf(":");
|
|
|
|
|
if (delimiter == -1) {
|
|
|
|
|
return new UsernamePasswordCredentials(userInfo, null);
|
|
|
|
|
}
|
|
|
|
|
String username = userInfo.substring(0, delimiter);
|
|
|
|
|
String password = userInfo.substring(delimiter + 1);
|
|
|
|
|
return new UsernamePasswordCredentials(username, password);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|