Skip to content

Commit

Permalink
Propose Elastic Authentication capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
chas galey committed Jan 3, 2024
1 parent d8e0d5c commit eb17e9a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.openmbee.mms.elastic.config;

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.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,13 +24,36 @@ public class ElasticsearchConfig {
@Value("${elasticsearch.http}")
private String elasticsearchHttp;

@Value("${elasticsearch.password}")
private String elasticsearchPassword;
@Value("${elasticsearch.username}")
private String elasticsearchUsername;

@Bean(name = "clientElastic", destroyMethod = "close")
public RestHighLevelClient restClient() {



RestClientBuilder builder = RestClient.builder(new HttpHost(elasticsearchHost, elasticsearchPort, elasticsearchHttp));
builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(10000).setSocketTimeout(1000000));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;

if (! elasticsearchPassword.isEmpty() && ! elasticsearchUsername.isEmpty()) {
final CredentialsProvider credentialsProvider =
new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(elasticsearchUsername, elasticsearchPassword));
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(
HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder
.setDefaultCredentialsProvider(credentialsProvider);
}
});
}


return new RestHighLevelClient(builder);
}

}
2 changes: 2 additions & 0 deletions elastic/src/main/resources/application.properties.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# elasticsearch.username=
# elasticsearch.password=
elasticsearch.host=localhost
elasticsearch.port=9200
elasticsearch.http=http
Expand Down
2 changes: 2 additions & 0 deletions example/src/main/resources/application-test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ spring.main.allow-circular-references=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

#Configuration for Elasticsearch
# elasticsearch.username=
# elasticsearch.password=
elasticsearch.host=elasticsearch
elasticsearch.port=9200
elasticsearch.http=http
Expand Down
2 changes: 2 additions & 0 deletions example/src/main/resources/application.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ spring.main.allow-circular-references=true
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

#Configuration for Elasticsearch
# elasticsearch.username=
# elasticsearch.password=
elasticsearch.host=localhost
elasticsearch.port=9200
elasticsearch.http=http
Expand Down

0 comments on commit eb17e9a

Please sign in to comment.