How to redirect an application to a session of a specific index on Kibana

Hello,

I am developing a javascript application that will complete my use of Kibana.

In my application I display a table that shows all the sessions that have been requested by the user.
I would like to add a button in the table for each session that would then redirect to the corresponding Kibana session.

I first tried to build the url to which to redirect my application. So I create it like this:

const diffDays = (selectedDate: Date) => Math.ceil(Math.abs(new Date().getTime() - new Date(selectedDate).getTime()) / (1000 * 60 * 60 * 24));

const diffMonths = (selectedDate: Date) => new Date(selectedDate).getMonth() - new Date().getMonth() + 12 * (new Date(selectedDate).getFullYear() - new Date().getFullYear());

const diffYears = (selectedDate: Date) => new Date(selectedDate).getFullYear() - new Date().getFullYear();

function createKibanaUrl(timestamp: Date, idSession: string): string {
        let kibanaUrl: string = "http://HOST_NAME:5601/app/kibana#/discover?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-"
        const timeSlotInDays = diffDays(timestamp);
        const timeSlotInMonths = diffMonths(timestamp);
        const timeSlotInYears = diffYears(timestamp);
        if (timeSlotInDays <= 30) {
            kibanaUrl = kibanaUrl + timeSlotInDays.toString() + "d,to:now))";
        } else if ( timeSlotInDays > 30 && timeSlotInDays < 365) {
            kibanaUrl = kibanaUrl + timeSlotInMonths.toString() + "M,to:now))";
        } else if (timeSlotInDays >= 365) {
            kibanaUrl = kibanaUrl + timeSlotInYears.toString() + "y,to:now))";
        }
        kibanaUrl = kibanaUrl + "&_a=(columns:!(_source),index:" + "ab4ed2e0-fe..." + ",interval:auto,query:(language:kuery,query:'some_field:%22some_value%22%20and%20_id:%22" + encodeURIComponent(idSession) + "%22'),sort:!(!('@timestamp',desc))"

        return kibanaUrl;
    }

I get the following url which should work:

http://SOME_SERVER_NAME:5601/app/kibana#/discover?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-12d,to:now))&_a=(columns:!(_source),index:661de5d...e26699,interval:auto,query:(language:kuery,query:'some_field:%22some_value%22%20and%20_id:%22an_id_encoded%22'),sort:!(!('@timestamp',desc))

But when my application is redirected to this URL, Kibana automatically transforms the url and returns me to a base index with the default values displayed.

The new URL created by Kibana looks like this:

http://SOME_SERVER_NAME:5601/app/kibana#/discover?_g=(refreshInterval:(pause:!t,value:0),time:(from:now-12d,to:now))&_a=(columns:!(_source),index:'BASIC_INDEX',interval:auto,query:(language:kuery,query:''),sort:!(!('@timestamp',desc)))

What I called BASIC_INDEX in the url above corresponds to an index that exists in my organization but does not match the index I requested before the redirect. And as you can see, the query section of the url is totally empty.

What explains this behavior of Kibana and is there a way to avoid it please.

Continuing my research I found this GitHub page that seems to show a way to redirect one's application to the Discover app of Kibana.

So I try the following to redirect my application to the index I want, without specify any id (I just want to see if I can reach the index that I asked for):

function createKibanaUrl(): string {
        let testKibana = "http://HOST_NAME:5601/app/r?l=DISCOVER_APP_LOCATOR&v=7.6.0&p={%22indexPatternId%22:%22661de5...e26699%22}";
        return testKibana;
    }

But with this method, the page I am redirected to tells me that the requested application was not found:

Does anyone know how to do a redirect like this to a specific index in Kibana?

EDIT:

Looking at the web console in Kibana just after the redirection I have the following two messages that appear:
1.

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-P5polb1UreUSOe5V/Pv7tc+yeZuJXiOi/3fqhGsU7BE='), or a nonce ('nonce-...') is required to enable inline execution.

This message is marked as appearing in: kibana#/discover?_g=…mestamp',desc)):351

A single error about an inline script not firing due to content security policy is expected!

This message is marked as appearing in: bootstrap.js:9

Thanks in advance if you take the time to help me.

Hi

Which version of Kibana are you using? Are you 100% sure you're providing the right id of the index patter / data view you want to query? If yes, could you try to wrap the index param in the generated url with ' ... so like ... index:'idValue'

Thx & Best,
Matthias

Hello,

Sorry for the inconvenience, I just realized that I had forgotten a parenthesis at the end of my dynamically generated url. In the function createKibana() at the 12 line I just had to replace kibanaUrl = kibanaUrl + "&_a=(columns:!(_source),index:" + "ab4ed2e0-fe..." + ",interval:auto,query:(language:kuery,query:'some_field:%22some_value%22%20and%20_id:%22" + encodeURIComponent(idSession) + "%22'),sort:!(!('@timestamp',desc))"
by
kibanaUrl = kibanaUrl + "&_a=(columns:!(_source),index:" + "ab4ed2e0-fe..." + ",interval:auto,query:(language:kuery,query:'some_field:%22some_value%22%20and%20_id:%22" + encodeURIComponent(idSession) + "%22'),sort:!(!('@timestamp',desc)))" and everything is working normally.

1 Like