# Open Source Java Client XPack Security SSL Example

**URL:** https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075
**Category:** Elasticsearch
**Tags:** elastic-stack-security, docker
**Created:** [June 1, 2022, 2:40am UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075 "2022-06-01T02:40:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nek4life](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nek4life/32/106419_2.png) [@nek4life](https://discuss.elastic.co/u/nek4life)
#### Post date: [June 1, 2022, 2:40am UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075/1 "2022-06-01T02:40:29Z")

</div>

I'm trying to use the new Java Client and running into issues when trying to connect to Elasticsearch running in a docker container now that xpack security is turned on by default.

I'm getting the following error here.

"message":"received plaintext http traffic on an https channel, closing connection Netty4HttpChannel

I'm following the instructions here and trying use the http.p12 and password

> **[Connecting to an Elasticsearch cluster](https://quarkus.io/guides/elasticsearch#programmatically-configuring-elasticsearch)**
>
> Quarkus: Supersonic Subatomic Java

and here

> **[Encrypted communication | Elasticsearch Java API Client \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_encrypted_communication.html)**

And can't seem to get a connection working.

> **[Install Elasticsearch with Docker | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html#elasticsearch-security-certificates)**

What am I missing here? I haven't been able to find a full tutorial and now that xpack security is on by default perhaps this should be part of the client documentation.

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [June 1, 2022, 3:51am UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075/2 "2022-06-01T03:51:12Z")

</div>

Hi!  
This [post](https://stackoverflow.com/questions/71492404/elasticsearch-showing-received-plaintext-http-traffic-on-an-https-channel-in-con) has a similar problem.

---

<div class="post-metadata">

### Author: ![ikakavas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ikakavas/32/34430_2.png) [@ikakavas](https://discuss.elastic.co/u/ikakavas)
#### Post date: [June 1, 2022, 3:53am UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075/3 "2022-06-01T03:53:56Z")

</div>

The second example in [Encrypted communication | Elasticsearch Java API Client [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_encrypted_communication.html) in combination with [Basic authentication | Elasticsearch Java API Client [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/_basic_authentication.html) should be _exactly_ what you need to do.

> [@nek4life](#):
>
> And can't seem to get a connection working.

In order for anyone to be able to meaningfully help you, you will need to share with us _exactly_ what you tried ( or share your code snippet ) and _exactly_ how it did not work.

---

<div class="post-metadata">

### Author: ![nek4life](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nek4life/32/106419_2.png) [@nek4life](https://discuss.elastic.co/u/nek4life)
#### Post date: [June 1, 2022, 1:24pm UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075/4 "2022-06-01T13:24:08Z")

</div>

I was able to get it working. I was missing the following property which currently defaults to `http`

`quarkus.elasticsearch.protocol=https`

The full set of necessary properties now that xpack is on by default are

`quarkus.elasticsearch.protocol=http quarkus.elasticsearch.username=elastic quarkus.elasticsearch.password=somesecret`

I also needed to use the http.p12

with the following class to configure the low level client

```auto
package com.example;

import io.quarkus.arc.Unremovable;
import io.quarkus.elasticsearch.restclient.lowlevel.ElasticsearchClientConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClientBuilder;
import org.jboss.logging.Logger;

import javax.enterprise.context.ApplicationScoped;
import javax.net.ssl.SSLContext;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyStore;

@ElasticsearchClientConfig
public class ElasticSearchSSLContextConfigurator implements RestClientBuilder.HttpClientConfigCallback {

    @Override
    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
        try {
            String keyStorePass = "secret";
            Path trustStorePath = Paths.get("/some/path/http.p12");
            KeyStore truststore = KeyStore.getInstance("pkcs12");
            try (InputStream is = Files.newInputStream(trustStorePath)) {
                truststore.load(is, keyStorePass.toCharArray());
            }
            SSLContextBuilder sslBuilder = SSLContexts.custom()
                .loadTrustMaterial(truststore, null);
            SSLContext sslContext = sslBuilder.build();
            httpAsyncClientBuilder.setSSLContext(sslContext);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return httpAsyncClientBuilder;
    }
}

```

---

<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: [June 29, 2022, 1:24pm UTC](https://discuss.elastic.co/t/open-source-java-client-xpack-security-ssl-example/306075/5 "2022-06-29T13:24:46Z")

</div>

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