# 8.2 closing connection when creating an index

**URL:** <https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972>\
**Category:** Elasticsearch\
**Created:** [June 12, 2022, 9:17pm UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972 "2022-06-12T21:17:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jack\_Park](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_park/32/30787_2.png) [@Jack\_Park](https://discuss.elastic.co/u/Jack_Park)\
**Post date:** [June 12, 2022, 9:17pm UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/1 "2022-06-12T21:17:22Z")

</div>

I'm building my own REST provider for 8.2 - an upgrade from one which ran on 6, but with obvious changes to the code.  
I created a gist

> <https://gist.github.com/KnowledgeGarden/63fb01605f8ae67ffc4d9acfcb1161bd>

which shows the code with "\<\<\<\<\<" indicating where it is failing; there is the console from the ES server and the Java stack trace.

What's going on is that I am creating a named index with number of shards and replicas, plus a JSON mappings string. Right now, it's not clear to me why ES is not happy. I am using "localhost"and "9200" to address the server,as I have in the past.

I suspect I am somehow abusing the APIs provided by Elasticsearch Java, but admit that documentation for Java developers is way behind - very few examples on github for \> 7 and those which do always seem to ignore setting up indexes. I just borrow ideas from surfing the ES Java repo.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [June 12, 2022, 9:56pm UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/2 "2022-06-12T21:56:14Z")

</div>

In the error

```auto
...
received plaintext http traffic on an https channel, closing connection <!--- Here 
Netty4HttpChannel{localAddress=/127.0.0.1:9200, remoteAddress=/127.0.0.1:64392}

```

looks like you are sending the request to `http` instead of `https` when you initially created the connection, it looks like it is expecting `https` which is the default for 8.x

Think you need to look [here](https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/8.2/_encrypted_communication.html)

```auto
The following is an example of setting up the client to trust the CA that has signed the certificate that Elasticsearch is using, when that CA certificate is available as a PEM encoded file.

Path caCertificatePath = Paths.get("/path/to/ca.crt");
CertificateFactory factory =
    CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = Files.newInputStream(caCertificatePath)) {
    trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
    .loadTrustMaterial(trustStore, null);
final SSLContext sslContext = sslContextBuilder.build();
RestClient.builder(
    new HttpHost("localhost", 9200, "https"))
    .setHttpClientConfigCallback(new HttpClientConfigCallback() {
        @Override
        public HttpAsyncClientBuilder customizeHttpClient(
            HttpAsyncClientBuilder httpClientBuilder) {
            return httpClientBuilder.setSSLContext(sslContext);
        }
    });

```

---

<div class="post-metadata">

**Author:** ![Jack\_Park](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_park/32/30787_2.png) [@Jack\_Park](https://discuss.elastic.co/u/Jack_Park)\
**Post date:** [June 13, 2022, 1:13am UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/3 "2022-06-13T01:13:31Z")

</div>

Interesting. Thanks.

Added that "https" bit but now we see  
Stuck on  
co.elastic.clients.Elasticsearch.\_types.ElasticsearchException: [es/indices.create] failed: [security\_exception] missing authentication credentials for REST request [/topics]

which begs this:  
I have the password I configured for a user password.  
The documentation talks about "built-in" users and setting the password.

I created a user in kibana with editor role.  
My code addition to yours is

BasicCredentialsProvider credsProv = **new** BasicCredentialsProvider();

credsProv.setCredentials(

AuthScope. **_ANY_** , **new** UsernamePasswordCredentials(uname, pwd)

);

restClient = RestClient._builder_(

**new** HttpHost("localhost", 9200, "https"))

.setHttpClientConfigCallback(hc -\> hc.setDefaultCredentialsProvider(credsProv))

.setHttpClientConfigCallback( **new** HttpClientConfigCallback() {

@Override

**public** HttpAsyncClientBuilder customizeHttpClient(

HttpAsyncClientBuilder httpClientBuilder) {

**return** httpClientBuilder.setSSLContext(sslContext);

}

}).build();

But I get that error.

Trying to sort out users.

Thanks,

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [June 13, 2022, 1:23am UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/4 "2022-06-13T01:23:01Z")

</div>

> [@Jack\_Park](#):
>
> I created a user in kibana with editor role.  
> My code addition to yours is

First, that's not my code That's from the documentation.

Second, I don't know what you mean by editor role. There's lots of different permissions when you create a role.

What I would do is try with the `elastic` user and password That was created when you configured elasticsearch first to see if it works and connects.

And can create the indexes do the operations you want to try to see if you can get the Java client working.

**Then** I would read about users and roles and then I create a user and role and try to login through Kibana and create the same indices etc through the Dev tools to make sure you can actually do The operations you want with that user and role.

Only then would I try to use a new user and role through the Java client.

You Could spend a lot of time trying to debug the Java. Just to find out you have the user and roles permission.

Just a suggestion.

---

<div class="post-metadata">

**Author:** ![Jack\_Park](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_park/32/30787_2.png) [@Jack\_Park](https://discuss.elastic.co/u/Jack_Park)\
**Post date:** [June 14, 2022, 10:37pm UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/5 "2022-06-14T22:37:14Z")

</div>

> [@stephenb](#):
>
> ```auto
> KeyStore trustStore = KeyStore.getInstance("pkcs12");
> trustStore.load(null, null);
> 
> ```

That's the code I entered. There's an issue with it: (null, null) should be (inputstream to the keystore, keystore password).

An example I found here

> [@Missing Authentication for REST request](https://discuss.elastic.co/t/missing-authentication-for-rest-request/154807/6):
>
> For the High Level rest client see the last comment [here](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-initialization.html) that references [HttpAsyncClientBuilder](http://hc.apache.org/httpcomponents-asyncclient-dev/httpasyncclient/apidocs/org/apache/http/impl/nio/client/HttpAsyncClientBuilder.html) where you can pass the keystore parameter. For postman, I am not aware how this is done, you can probably find the answer in their docs.

uses "password".

I found that I needed to set the password, which I did.

Now that I, in theory, have jumped through those hoops, the example earlier said  
KeyStore trustStore = KeyStore.getInstance("pkcs12");  
whereas the example just found (albeit from an earlier date) says  
KeyStore keyStore = KeyStore.getInstance("jks");  
This example

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

uses pkcs12.  
That leaves me with a new error, one which comes before the authentication issue:

DerInputStream.getLength(): lengthTag=87, too big.

I am using the path to the ES keystore as the trustPath.

Google isn't very helpful on that error.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [June 15, 2022, 12:34am UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/6 "2022-06-15T00:34:43Z")

</div>

My suggestion is to open a new topic with a very descriptive subject line. Your subject line is too generic and put very specific explanation of your problem in like you have here . There should be people here that use the Java client I'm pretty sure Perhaps and you'll get an answer. It's not my area expertise.

Subject Like

_Trouble Connecting over SSL with new 8.x Java API Client._

The put your entire config in ... not just certain lines .. .and all the applicable logs.

---

<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:** [July 13, 2022, 12:34am UTC](https://discuss.elastic.co/t/8-2-closing-connection-when-creating-an-index/306972/7 "2022-07-13T00:34:59Z")

</div>

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