# Custom Kibana plugin - use previously set user connection

**URL:** https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357
**Category:** Kibana
**Created:** [August 25, 2017, 9:57am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357 "2017-08-25T09:57:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![peter.ridzi](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@peter.ridzi](https://discuss.elastic.co/u/peter.ridzi)
#### Post date: [August 25, 2017, 9:57am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/1 "2017-08-25T09:57:35Z")

</div>

Hi all!

I started to work on a custom Kibana plugin, which manipulates data stored in ES indices. To access the ES I build a connection manually like this:

```
...

this.client = new elasticsearch.Client({
  hosts: [
    'https://' + es_user + ':' + es_pwd + '@' + es_host + ':' + es_port + '/'
  ],
  ssl: {
    ca: fs.readFileSync('/etc/ssl/certs/ca.crt'),
    rejectUnauthorized: true
  }
});

...

```

I also use Shield in my cluster, and I am aware of that Kibana builds 2 connections toward the ES (one in the name of the technical user to access .kibana, and another for discover / console /... in the name of the authenticated user).  
I would like to use these previously built connections (just like in Console or Discover) to propagate my requests in the name of the logged in user instead of the manually built one.

Could you please help me, how can I do that?

Thanks for your help

---

<div class="post-metadata">

### Author: ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)
#### Post date: [August 30, 2017, 8:22am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/2 "2017-08-30T08:22:47Z")

</div>

Hi @peter.ridzi,

you can access preconfigured elasticsearch clients from the `elasticsearch` plugin. This is briefly described in the chapter [Communicating with Elasticsearch](https://www.elastic.co/guide/en/kibana/5.5/development-elasticsearch.html) of the Kibana documentation. Instead of the `"ping"` request shown in there you can use any method supported by [elasticsearch-js](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html).

---

<div class="post-metadata">

### Author: ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)
#### Post date: [August 30, 2017, 8:23am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/3 "2017-08-30T08:23:57Z")

</div>

You can see real-world examples of its use in the Kibana source code, e.g. at [https://github.com/elastic/kibana/blob/c3c07ef8c4cddafd25ef91899f3915817ecd59b7/src/core\_plugins/kibana/server/routes/api/suggestions/register\_value\_suggestions.js#L3-L21](https://github.com/elastic/kibana/blob/c3c07ef8c4cddafd25ef91899f3915817ecd59b7/src/core_plugins/kibana/server/routes/api/suggestions/register_value_suggestions.js#L3-L21).

---

<div class="post-metadata">

### Author: ![peter.ridzi](https://avatars.discourse-cdn.com/v4/letter/p/6de8d8/32.png) [@peter.ridzi](https://discuss.elastic.co/u/peter.ridzi)
#### Post date: [August 30, 2017, 9:50am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/4 "2017-08-30T09:50:28Z")

</div>

Hi @weltenwort,

It is exactly what I was looking for! I have already checked it based on the code snippets you sent, and it works great.

Thank you for your help.

---

<div class="post-metadata">

### Author: ![Kvin](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@Kvin](https://discuss.elastic.co/u/Kvin)
#### Post date: [August 30, 2017, 10:12am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/5 "2017-08-30T10:12:04Z")

</div>

Hi @weltenwort ,

this is something which I was also looking for, your help is much appreciated.

However the methods described in the Elasticsearch-js documentation have two input parameters: the query object and a function to handle errors and the response.

How could I achieve something similar to this

```
client.search(query, (error, res, status) => {
        if (error) {
          //something
        } else {
          //something
        }
      });

```

with this

`callWithRequest(req, 'search', { index, body });`

?

I'm currently using it like this:

```
callWithRequest(req, 'search', { index, body }).then((result) => {
//something with the result
}).catch((e) => {
//some error handling
});

```

This is fine, but I feel the other way gives more flexibility.

---

<div class="post-metadata">

### Author: ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)
#### Post date: [August 30, 2017, 10:31am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/6 "2017-08-30T10:31:11Z")

</div>

Promises are the preferred and supported way of composing asynchronous operations in Kibana. The callback style is not supported by `callWithRequest`. What particular disadvantage to you see with your use of `.then()` and `.catch()`?

You might want to try the `async`/`await` syntax which builds on Promises and is also widely used in Kibana:

```
async function myHandler(req, reply) {
  try {
    const result = await callWithRequest(req, 'search', { /* ... */ });
  } catch (error) {
    // handle error
  }
  reply(result);
}
```

---

<div class="post-metadata">

### Author: ![Kvin](https://avatars.discourse-cdn.com/v4/letter/k/e99b99/32.png) [@Kvin](https://discuss.elastic.co/u/Kvin)
#### Post date: [August 30, 2017, 11:04am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/7 "2017-08-30T11:04:32Z")

</div>

This looks like what I wanted. Thank you for the suggestion!

---

<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: [September 27, 2017, 11:04am UTC](https://discuss.elastic.co/t/custom-kibana-plugin-use-previously-set-user-connection/98357/8 "2017-09-27T11:04:38Z")

</div>

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