Custom Kibana plugin - use previously set user connection


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

(Felix Stürmer) #2

Hi @peter.ridzi,

you can access preconfigured elasticsearch clients from the elasticsearch plugin. This is briefly described in the chapter Communicating with Elasticsearch of the Kibana documentation. Instead of the "ping" request shown in there you can use any method supported by elasticsearch-js.

(Felix Stürmer) #3

You can see real-world examples of its use in the Kibana source code, e.g. at


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.


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, (error, res, status) => {
        if (error) {
        } else {

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.

(Felix Stürmer) #6

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


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

(system) #8

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