# Correct use of SSL with Java REST API

**URL:** <https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [July 2, 2019, 11:41pm UTC](https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588 "2019-07-02T23:41:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jurgen\_Debedts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurgen_debedts/32/49335_2.png) [@Jurgen\_Debedts](https://discuss.elastic.co/u/Jurgen_Debedts)\
**Post date:** [July 2, 2019, 11:41pm UTC](https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588/1 "2019-07-02T23:41:30Z")

</div>

Hello,

I created a new 2 node cluster ES 7.2.0. I have created successful integrations with the Java REST API in the past, but now I am adding the security layer and I am somewhat at a loss.

I followed the procedure described in the following blog: [https://www.elastic.co/blog/configuring-ssl-tls-and-https-to-secure-elasticsearch-kibana-beats-and-logstash](https://www.elastic.co/blog/configuring-ssl-tls-and-https-to-secure-elasticsearch-kibana-beats-and-logstash)  
to enable SSL. Kibana and https connection are working in SSL.  
I found the following info on initializing the JAVA client : [https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/\_encrypted\_communication.html](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html)

However, there are a few parts missing for me, being a novice in the SSL implementation.  
Is there a more complete guide/sample on how to connect the JAVA REST client to a ES cluster that used the procedure described in the blog?

Forgive me for being a newbie in this, but for example I have no idea where the keystore should be coming from in the code shown on the page to initialize the encrypted communication.  
Do I even need a keystore on my client or do I just need to open an SSL style connection and pass on the username and password for the connection?

Regards,

Jurgen

---

<div class="post-metadata">

**Author:** ![Jurgen\_Debedts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurgen_debedts/32/49335_2.png) [@Jurgen\_Debedts](https://discuss.elastic.co/u/Jurgen_Debedts)\
**Post date:** [July 3, 2019, 2:46pm UTC](https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588/2 "2019-07-03T14:46:44Z")

</div>

So, I think I found most of the information.  
Here is what I did:

- Created a jks keystore by importing the elastic ca.crt file into a new store
- imported the node certificates in the store

Updated my java code to set the necessary config:  
private RestHighLevelClient SSLConnection(HttpHost hosts) {  
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();  
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(connectionUser, connectionPassword));

```
	KeyStore truststore;
	try {
		truststore = KeyStore.getInstance("jks");
		InputStream is = new FileInputStream(new File(jksBasePath + jksStore));
		truststore.load(is, jksPassword.toCharArray());
		SSLContextBuilder sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, null);
		final SSLContext sslContext = sslBuilder.build();
		
		RestClientBuilder builder = RestClient.builder(hosts)
				.setHttpClientConfigCallback(new HttpClientConfigCallback() {
					@Override
					public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
						httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
						httpClientBuilder.setSSLContext(sslContext);
						return httpClientBuilder;
					}
				});
		return new RestHighLevelClient(builder);
		
	} catch (KeyStoreException e) {
		Diag.error(TraceSubSection, "SSLConnection", "KeyStoreException on JKS file", e);
	} catch (FileNotFoundException e) {
		Diag.error(TraceSubSection, "SSLConnection", "JKS file not found", e);
	} catch (NoSuchAlgorithmException e) {
		Diag.error(TraceSubSection, "SSLConnection", "NoSuchAlgorithmException", e);
	} catch (CertificateException e) {
		Diag.error(TraceSubSection, "SSLConnection", "CertificateException", e);
	} catch (IOException e) {
		Diag.error(TraceSubSection, "SSLConnection", "IOException on JKS file", e);
	} catch (KeyManagementException e) {
		Diag.error(TraceSubSection, "SSLConnection", "KeyManagementException on sslBuilder.build()", e);
	} 
	return null;
}

```

I know the catch side can be more efficient but that is just for clarity sake during dev.  
It seems to work, but I wanted to check whether I missed something that might bite me later like with regards, to performance

---

<div class="post-metadata">

**Author:** ![Jurgen\_Debedts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurgen_debedts/32/49335_2.png) [@Jurgen\_Debedts](https://discuss.elastic.co/u/Jurgen_Debedts)\
**Post date:** [July 12, 2019, 9:06pm UTC](https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588/3 "2019-07-12T21:06:49Z")

</div>

Anybody has any tips/input on this?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 9, 2019, 9:06pm UTC](https://discuss.elastic.co/t/correct-use-of-ssl-with-java-rest-api/188588/4 "2019-08-09T21:06:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
