Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Fix credentials for es6 #2900

Merged
merged 2 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public class ElasticSearchProperties {
*/
private String documentTypeOverride = "";

/** Elasticsearch basic auth username */
private String username;

/** Elasticsearch basic auth password */
private String password;

public String getUrl() {
return url;
}
Expand Down Expand Up @@ -184,6 +190,22 @@ public void setDocumentTypeOverride(String documentTypeOverride) {
this.documentTypeOverride = documentTypeOverride;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public List<URL> toURLs() {
String clusterAddress = getUrl();
String[] hosts = clusterAddress.split(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import java.util.stream.Collectors;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
Expand Down Expand Up @@ -86,13 +90,31 @@ public RestClient restClient(ElasticSearchProperties properties) {
requestConfigBuilder.setConnectionRequestTimeout(
properties.getRestClientConnectionRequestTimeout()));
}

return restClientBuilder.build();
}

@Bean
@Conditional(IsHttpProtocol.class)
public RestClientBuilder restClientBuilder(ElasticSearchProperties properties) {
return RestClient.builder(convertToHttpHosts(properties.toURLs()));
RestClientBuilder builder = RestClient.builder(convertToHttpHosts(properties.toURLs()));

if (properties.getUsername() != null && properties.getPassword() != null) {
log.info(
"Configure ElasticSearch with BASIC authentication. User:{}",
properties.getUsername());
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(
properties.getUsername(), properties.getPassword()));
builder.setHttpClientConfigCallback(
httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
} else {
log.info("Configure ElasticSearch with no authentication.");
}
return builder;
}

@Bean
Expand Down