Custom Kibana plugin - use previously set user connection


#1

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 https://github.com/elastic/kibana/blob/c3c07ef8c4cddafd25ef91899f3915817ecd59b7/src/core_plugins/kibana/server/routes/api/suggestions/register_value_suggestions.js#L3-L21.


#4

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.


#5

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.


(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
  }
  reply(result);
}

#7

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.