Skip to content

Commit 5c0caa2

Browse files
author
jaserud
committed
add https support for elasticsearch
1 parent 21f07a3 commit 5c0caa2

File tree

4 files changed

+43
-20
lines changed

4 files changed

+43
-20
lines changed

Dockerfile

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
FROM adoptopenjdk/openjdk11:jdk-11.0.6_10-alpine-slim as builder
22

3-
# Build song-server jar
43
COPY . /srv
54
WORKDIR /srv
65
RUN ./mvnw clean package -DskipTests

compose/docker-compose.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ services:
3030
ports:
3131
- "9001:9001"
3232
environment:
33-
ELASTICSEARCH_HOST: http://elasticsearch
34-
ELASTICSEARCH_PORT: 9200
33+
ELASTICSEARCH_NODE: http://elasticsearch:9200
3534
ELASTICSEARCH_AUTHENABLED: "true"
3635
SPRING_PROFILES_ACTIVE: test
3736
SPRING_CLOUD_VAULT_HOST: vault

src/main/java/bio/overture/rollcall/config/ElasticsearchConfig.java

+40-15
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,37 @@
1919
package bio.overture.rollcall.config;
2020

2121
import lombok.SneakyThrows;
22+
import lombok.extern.slf4j.Slf4j;
2223
import lombok.val;
2324
import org.apache.http.HttpHost;
2425
import org.apache.http.auth.AuthScope;
2526
import org.apache.http.auth.UsernamePasswordCredentials;
27+
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
2628
import org.apache.http.impl.client.BasicCredentialsProvider;
29+
import org.apache.http.ssl.SSLContextBuilder;
2730
import org.elasticsearch.client.RestClient;
2831
import org.elasticsearch.client.RestHighLevelClient;
2932
import org.springframework.beans.factory.annotation.Value;
3033
import org.springframework.context.annotation.Bean;
3134
import org.springframework.context.annotation.Configuration;
3235

33-
import java.net.URL;
36+
import java.security.KeyManagementException;
37+
import java.security.KeyStoreException;
38+
import java.security.NoSuchAlgorithmException;
3439

3540
@Configuration
41+
@Slf4j
3642
public class ElasticsearchConfig {
3743

38-
@Value("${elasticsearch.host}")
39-
private String host;
44+
@Value("${elasticsearch.node}")
45+
private String node;
4046

41-
@Value("${elasticsearch.port}")
42-
private int port;
43-
44-
@Value("${elasticsearch.authEnabled}")
47+
@Value("${elasticsearch.authEnabled:false}")
4548
private boolean authEnabled;
4649

50+
@Value("${elasticsearch.trustSelfSignedCert:true}")
51+
private boolean trustSelfSignedCert;
52+
4753
@Value("${elasticsearch.user}")
4854
private String user;
4955

@@ -53,15 +59,34 @@ public class ElasticsearchConfig {
5359
@Bean
5460
@SneakyThrows
5561
public RestHighLevelClient restClient() {
56-
val builder = RestClient.builder(new HttpHost(new URL(host).getHost(), port));
57-
if (authEnabled) {
58-
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
59-
val credentialsProvider = new BasicCredentialsProvider();
62+
val builder = RestClient.builder(HttpHost.create(node));
63+
64+
builder.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
65+
if (trustSelfSignedCert) {
66+
log.debug("Elasticsearch Client - trustSelfSignedCert enabled so setting SSLContext");
67+
SSLContextBuilder sslCtxBuilder = new SSLContextBuilder();
68+
try {
69+
sslCtxBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
70+
httpAsyncClientBuilder.setSSLContext(sslCtxBuilder.build());
71+
httpAsyncClientBuilder.setSSLHostnameVerifier((s, sslSession) -> true); // this is for local only
72+
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
73+
throw new RuntimeException("failed to build Elastic rest client");
74+
}
75+
}
76+
77+
if (authEnabled) {
78+
log.debug("Elasticsearch Client - authEnabled enabled so setting credentials provider");
79+
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
6080
credentialsProvider.setCredentials(AuthScope.ANY,
6181
new UsernamePasswordCredentials(user, password));
62-
return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
63-
});
64-
}
65-
return new RestHighLevelClient(builder);
82+
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
83+
}
84+
85+
return httpAsyncClientBuilder;
86+
});
87+
88+
log.info("Elasticsearch Client - built");
89+
90+
return new RestHighLevelClient(builder);
6691
}
6792
}

src/main/resources/application.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ auth:
66
publicKeyUrl: "http://localhost:8081/oauth/token/public_key"
77

88
elasticsearch:
9-
host: http://127.0.0.1
10-
port: 9200
9+
node: http://127.0.0.1:9200
1110
cluster-name: elasticsearch
1211
authEnabled: false
12+
trustSelfSignedCert: true
1313
user:
1414
password:
1515

0 commit comments

Comments
 (0)