19
19
package bio .overture .rollcall .config ;
20
20
21
21
import lombok .SneakyThrows ;
22
+ import lombok .extern .slf4j .Slf4j ;
22
23
import lombok .val ;
23
24
import org .apache .http .HttpHost ;
24
25
import org .apache .http .auth .AuthScope ;
25
26
import org .apache .http .auth .UsernamePasswordCredentials ;
27
+ import org .apache .http .conn .ssl .TrustSelfSignedStrategy ;
26
28
import org .apache .http .impl .client .BasicCredentialsProvider ;
29
+ import org .apache .http .ssl .SSLContextBuilder ;
27
30
import org .elasticsearch .client .RestClient ;
28
31
import org .elasticsearch .client .RestHighLevelClient ;
29
32
import org .springframework .beans .factory .annotation .Value ;
30
33
import org .springframework .context .annotation .Bean ;
31
34
import org .springframework .context .annotation .Configuration ;
32
35
33
- import java .net .URL ;
36
+ import java .security .KeyManagementException ;
37
+ import java .security .KeyStoreException ;
38
+ import java .security .NoSuchAlgorithmException ;
34
39
35
40
@ Configuration
41
+ @ Slf4j
36
42
public class ElasticsearchConfig {
37
43
38
- @ Value ("${elasticsearch.host }" )
39
- private String host ;
44
+ @ Value ("${elasticsearch.node }" )
45
+ private String node ;
40
46
41
- @ Value ("${elasticsearch.port}" )
42
- private int port ;
43
-
44
- @ Value ("${elasticsearch.authEnabled}" )
47
+ @ Value ("${elasticsearch.authEnabled:false}" )
45
48
private boolean authEnabled ;
46
49
50
+ @ Value ("${elasticsearch.trustSelfSignedCert:true}" )
51
+ private boolean trustSelfSignedCert ;
52
+
47
53
@ Value ("${elasticsearch.user}" )
48
54
private String user ;
49
55
@@ -53,15 +59,34 @@ public class ElasticsearchConfig {
53
59
@ Bean
54
60
@ SneakyThrows
55
61
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 ();
60
80
credentialsProvider .setCredentials (AuthScope .ANY ,
61
81
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 );
66
91
}
67
92
}
0 commit comments