Kibana:getting past login page with selenium

I am trying to get some to some pages in kibana through selenium/crome dirver. I have seen some samples from some time ago where providing the user/pwd in the url worked, but now, if I do this, still gets me to the page where you get:

Log in with Elasticsearh / Log in with Elastic Cloud

Being a newbie in selenium stuff, not sure if this is easy to overcome with selenium, anyone has done it?

thanks!

Hi @jmlucjav ,

Embedding credentials into URLs isn't recommended and certain features may not work anyway.

I'm not an expert in Selenium/WebDriver, but I'd suggest you to poke around our own WebDriver based test helpers that would allow you to simulate login as a setup stage for your tests: kibana/security_page.ts at ddac0e95010756f34e655e9fe63248e89b0a8885 · elastic/kibana · GitHub

Best,
Oleg

Thanks for the pointer!

Yeah I saw you guys do something like that in those tests, but forgot to mention I am using selenium from python, no idea how mess with that typescript code...

Technically what you need is to know the proper sequence of actions to perform a login and the right CSS selectors, that should be relative easy to figure from our TypeScript code base and port to Python even if TS isn't your language of choice for day to day work :slightly_smiling_face:

So yeah, I'd encourage you to try and experiment, and if you're really stuck feel free to tag me here.

Best,
Oleg

I really appreciate the help Oleg! I tried for some hours but I see it's are rough field for me, not my area of expertise at all... Will resort to google once in a while (and checking here) see if someone solves my exact issue, and meanwhile will try to find a plan B.

By the way, I'm curious what you're trying to achieve? Maybe there are simpler ways to do what you want.

We have anonymous access and so on that may not involve login screen at all.

oh sure...I am trying to programmatically get a screenshot of an index monitoring advance page...it would be great if there was a simpler way I'm unaware of

Got it. It seems you don't need much from the Selenium itself, that's good.

If one would use Puppeteer it'd be just few lines:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  await page.authenticate({ username, password });
  await page.goto('https://kibana.com/app/monitoring#/elasticsearch/indices...');
  await page.screenshot({ path: 'example.png' });

  await browser.close();
})();

But if you don't have anything like this in Python, you most likely just need to add Authorization header to your request, and that should do the job (that's what Puppeteer basically does with page.authenticate method):

Authorization: Basic base64(username:password)

For example for elastic user with changeme password it'd be:

Authorization: Basic ZWxhc3RpYzpjaGFuZ2VtZQ==

Alternatively you can use Kibana monitoring APIs to get the data itself, but I guess you need a pretty picture :slightly_smiling_face:

Best,
Oleg

If one would use Puppeteer it'd be just few lines

ok, this sounds like the best plan. I could integrate this with my python code somehow.

Doing tests but always get the snapshot of a loading page, does it need to be some

page.waitFor...()

in there? I have tried several things:

  • documentloaded: timeout error
  • get css selector form a monitoring pane and use that, also timeout...

thanks!

Doing tests but always get the snapshot of a loading page, does it need to be some
page.waitFor...()

Yep, that's correct, if you don't have any Kibana Spaces (just default one), then you'd first need to wait for the Kibana chrome to appear (follow the link to see the selector we use and timeout may need to be large enough).

And then you may need to wait for a particular Monitoring app section. I'm not working with this app, so cannot really suggest much, but take a look at these test files. There you can find files for various parts of the Monitoring application and every file contains more or less descriptive CSS selectors for the app regions that you can waitFor.

Note that the links I've shared are for master branch, switch to the proper git tag (e.g. 7.12.0) depending on the Kibana version you're running puppeteer against.

I must be close to getting it work....does this look fine to you?

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.authenticate({ username, password });
    await page.goto(url);
    await page.waitForSelector('[data-test-subj="kibanaChrome"] .app-wrapper:not(.hidden-chrome)');
    await page.waitForSelector('#monitoringElasticsearchAdvancedIndexApp > div > main > div.euiPanel.euiPanel--paddingLarge.euiPageContent > div > div:nth-child(10)', {visible: true})
    await page.screenshot({ path: 'example.png' });

Still getting (node:4452) UnhandledPromiseRejectionWarning: TimeoutError: waiting for selector [data-test-subj="kibanaChrome"] .app-wrapper:not(.hidden-chrome) failed: timeout 30000ms exceeded

Sorry, didn't realize that Puppeteer works a bit differently, use this instead of authenticate (NodeJS code):

  await page.setExtraHTTPHeaders({
    'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
  })

I created a simple gist, but haven't checked your specific selectors yet:

$ KIBANA_USERNAME=xxx \
  KIBANA_PASSWORD=xxxx \
  KIBANA_URL=https://xxxx \
  npx https://gist.github.com/azasypkin/0681841bdd5a596fe7c6fe8ef8c7ba76
1 Like

thanks for that!

now I get still an timeout when waiting for the selector.

So I removed that wait, and just waited for 30s, that is more than enough for me when going to that page, and I get this:

which is and advance, but still something is amiss...

I'm pretty sure that you're using the wrong URL now, maybe you're missing the full hash fragment (that's super important for Kibana)?

You can console.log the URL in your puppeteer script and then try to use it in the private/anonymous browser tab to make sure it really leads to where you expect.

I checked your last selector (used in my gist as well) and it seems to work properly for the random index in my test Cloud deployment.

oh my! the url was ok but was hitting the wrong index (no longer there). Working now!! Thanks so much

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