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 12, 2020
1 parent 7afd25f commit 03c7ca3
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 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 All @@ -31,6 +32,7 @@
import org.elasticsearch.client.RestHighLevelClient;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.PropertyMapper;
Expand All @@ -43,6 +45,7 @@
* @author Brian Clozel
* @author Stephane Nicoll
* @author Vedran Pavic
* @author Evgeniy Cheban
*/
class ElasticsearchRestClientConfigurations {

Expand All @@ -58,7 +61,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 +75,12 @@ 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 @@ -114,25 +123,48 @@ static class DefaultRestClientBuilderCustomizer implements RestClientBuilderCust

private final ElasticsearchRestClientProperties properties;

private CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

DefaultRestClientBuilderCustomizer(ElasticsearchRestClientProperties properties) {
this.properties = properties;
}

@Autowired(required = false)
void setCredentialsProvider(CredentialsProvider credentialsProvider) {
this.credentialsProvider = credentialsProvider;
}

@Override
public void customize(RestClientBuilder builder) {
}

@Override
public void customize(HttpAsyncClientBuilder builder) {
builder.setDefaultCredentialsProvider(this.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());
this.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);
this.credentialsProvider.setCredentials(AuthScope.ANY, credentials);
});
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
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;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
Expand All @@ -47,6 +53,7 @@
*
* @author Brian Clozel
* @author Vedran Pavic
* @author Evgeniy Cheban
*/
@Testcontainers(disabledWithoutDocker = true)
class ElasticsearchRestClientAutoConfigurationTests {
Expand Down Expand Up @@ -156,6 +163,69 @@ void restClientCanQueryElasticsearchNode() {
});
}

@Test
void configureUriWithUsernameOnly() {
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)
.withPropertyValues("spring.elasticsearch.rest.uris=http://user@localhost:9200").run((context) -> {
RestClient client = context.getBean(RestClient.class);
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
.containsExactly("http://localhost:9200");

CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class);
Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200));
assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user");
assertThat(credentials.getPassword()).isNull();
});
}

@Test
void configureUriWithUsernameAndEmptyPassword() {
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)
.withPropertyValues("spring.elasticsearch.rest.uris=http://user:@localhost:9200").run((context) -> {
RestClient client = context.getBean(RestClient.class);
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
.containsExactly("http://localhost:9200");

CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class);
Credentials credentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200));
assertThat(credentials.getUserPrincipal().getName()).isEqualTo("user");
assertThat(credentials.getPassword()).isEmpty();
});
}

@Test
void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() {
this.contextRunner.withUserConfiguration(CredentialsProviderConfiguration.class)
.withPropertyValues("spring.elasticsearch.rest.uris=http://user:password@localhost:9200,localhost:9201",
"spring.elasticsearch.rest.username=admin", "spring.elasticsearch.rest.password=admin")
.run((context) -> {
RestClient client = context.getBean(RestClient.class);
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
.containsExactly("http://localhost:9200", "http://localhost:9201");

CredentialsProvider credentialsProvider = context.getBean(CredentialsProvider.class);

Credentials uriCredentials = credentialsProvider.getCredentials(new AuthScope("localhost", 9200));
assertThat(uriCredentials.getUserPrincipal().getName()).isEqualTo("user");
assertThat(uriCredentials.getPassword()).isEqualTo("password");

Credentials defaultCredentials = credentialsProvider
.getCredentials(new AuthScope("localhost", 9201));
assertThat(defaultCredentials.getUserPrincipal().getName()).isEqualTo("admin");
assertThat(defaultCredentials.getPassword()).isEqualTo("admin");
});
}

@Configuration(proxyBeanMethods = false)
static class CredentialsProviderConfiguration {

@Bean
CredentialsProvider credentialsProvider() {
return new BasicCredentialsProvider();
}

}

@Configuration(proxyBeanMethods = false)
static class CustomRestClientConfiguration {

Expand Down

0 comments on commit 03c7ca3

Please sign in to comment.