# ES 7.4.1 - Authentication \[Rest API\]

**URL:** <https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-security\
**Created:** [December 16, 2019, 9:28am UTC](https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969 "2019-12-16T09:28:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [December 16, 2019, 9:28am UTC](https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969/1 "2019-12-16T09:28:46Z")

</div>

Truly, I already asked the question in [stackoverflow](https://stackoverflow.com/questions/59307949/es-7-4-1-authentication-rest-api) but didn't receive an answer yet, so I also create a new topic here..

I’m a newbie in ES and I have a task in my new job to upgrade from 6.4.2 to 7.4.1 – From TCP client to Rest High Level API.

Previously we built the client like this (with SSL & Certificate):

```
Settings settings = Settings.builder()
      .put("xpack.security.user", String.format("%s:%s",esJavaUser,esJavaPassword))
      .put("cluster.name", esClusterName)
      .put("xpack.security.transport.ssl.enabled", xpackSecurityTransportSslEnabled)
      .put("xpack.ssl.certificate_authorities", xpackSslCertificateAuthorities)
      .build();

 client = new PreBuiltXPackTransportClient(settings);

```

Now, in rest API, it’s changed to this:

```
final CredentialsProvider credentialsProvider =
        new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
        new UsernamePasswordCredentials(esJavaUser, esJavaPassword));

RestClientBuilder restClientBuilder = RestClient.builder(hosts)
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder
                .setDefaultCredentialsProvider(credentialsProvider));
restHighLevelClient = new RestHighLevelClient(restClientBuilder);

```

With this build I set ES user and password by CredentialsProvider **but** what about `ssl.enabled` and `certificate_authorities”`? how should I provided them with rest API?

---

<div class="post-metadata">

**Author:** ![TimV](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timv/32/13162_2.png) [@TimV](https://discuss.elastic.co/u/TimV)\
**Post date:** [December 17, 2019, 5:43am UTC](https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969/2 "2019-12-17T05:43:03Z")

</div>

The documentation you are looking for is here: [Encrypted communication | Java REST Client [7.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/_encrypted_communication.html)

> [@AsfK](#):
>
> what about `ssl.enabled` and `certificate_authorities”`

SSL is automatically enabled (or not) based on the scheme (protocol) in the `HttpHost` objects you pass to the builder.

```auto
RestClient.builder(hosts)

```

If you are using SSL, you want to pass `"https"` as the scheme (3rd argument) when you construct the `HttpHost` objects (`hosts`).

Unfortunately there is no simple means to pass `certificate_authorities` to the Rest client, you need to turn those certificates into a standard Java truststore.

You can probably find some sample code on the web ("convert PEM certificates to Java truststore"), but the gist of it is:

1. Open the certificate authority files as an `InputStream`
2. Create a X.509 certificate factory: `java.security.cert.CertificateFactory.getInstance("X.509")`
3. Call `generateCertificates` on the certificate factory to read those certificate files into java `Certificate` objects
4. Construct an empty `KeyStore` object
5. Add the loaded certificates as trusted entries
6. Pass that to `SSLContextBuilder.loadTrustMaterial`

---

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [December 17, 2019, 5:56am UTC](https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969/3 "2019-12-17T05:56:34Z")

</div>

Thank you very much!!

P.S. The documentaion not mention the certificate section you wrote in your answer, maybe you might want to add it... Thanks again!

---

<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:** [January 14, 2020, 5:56am UTC](https://discuss.elastic.co/t/es-7-4-1-authentication-rest-api/211969/4 "2020-01-14T05:56:40Z")

</div>

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