kibana/packages/kbn-journeys/services/auth.ts
Dzmitry Lemechko c48cc24617
[kbn/journeys] fixes to run journeys against ESS cluster (#166923)
## Summary

I had to change `waitForRender` since `page.waitForFunction` tries to
run a script on page and it is not working due to CSP settings on Cloud.
Instead of injecting a script, we use a classical API to find
elements/attributes in the DOM.

Since `PUT /internal/core/_settings` is merged in 8.11.0, journeys run
on Cloud with on-fly labels update is supported starting deployments
8.11.0+. I added error message for 404 code just in case someone runs it
on earlier version.

`many_fields_discover` journey was update since on Cloud the data view
used by scenario is not selected by default.

How it works:

Create a deployment with QAF and re-configure it for journey run:
```
export EC_DEPLOYMENT_NAME=my-run-8.11
qaf elastic-cloud deployments create --stack-version 8.11.0-SNAPSHOT --environment staging --region gcp-us-central1
qaf elastic-cloud deployments configure-for-performance-journeys
```

Run any journey, e.g. many_fields_discover
```
TEST_CLOUD=1 TEST_ES_URL=https://username:pswd@es_url:443 TEST_KIBANA_URL=https://username:pswd@kibana-ur_url node scripts/functional_test_runner --config x-pack/performance/journeys/many_fields_discover.ts
```

You should see a log about labels being updated:

```
Updating telemetry & APM labels: {"testJobId":"local-a3272047-6724-44d1-9a61-5c79781b06a1","testBuildId":"local-d8edbace-f441-4ba9-ac83-5909be3acf2a","journeyName":"many_fields_discover","ftrConfig":"x-pack/performance/journeys/many_fields_discover.ts"}
```

And then able to find APM logs for the journey in
[Ops](https://kibana-ops-e2e-perf.kb.us-central1.gcp.cloud.es.io:9243/app/apm/services?comparisonEnabled=true&environment=ENVIRONMENT_ALL&kuery=labels.testJobId%20%3A%20%22local-d79a878c-cc7a-423b-b884-c9b6b1a8d781%22&latencyAggregationType=avg&offset=1d&rangeFrom=now-24h%2Fh&rangeTo=now&serviceGroup=&transactionType=request)
cluster
2023-09-28 12:06:00 +02:00

105 lines
3.1 KiB
TypeScript

/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import Url from 'url';
import { format } from 'util';
import axios, { AxiosResponse } from 'axios';
import { FtrService } from './ftr_context_provider';
export interface Credentials {
username: string;
password: string;
}
function extractCookieValue(authResponse: AxiosResponse) {
return authResponse.headers['set-cookie']?.[0].toString().split(';')[0].split('sid=')[1] ?? '';
}
export class AuthService extends FtrService {
private readonly config = this.ctx.getService('config');
private readonly log = this.ctx.getService('log');
private readonly kibanaServer = this.ctx.getService('kibanaServer');
public async login(credentials?: Credentials) {
const baseUrl = new URL(
Url.format({
protocol: this.config.get('servers.kibana.protocol'),
hostname: this.config.get('servers.kibana.hostname'),
port: this.config.get('servers.kibana.port'),
})
);
const loginUrl = new URL('/internal/security/login', baseUrl);
const provider = this.isCloud() ? 'cloud-basic' : 'basic';
const version = await this.kibanaServer.version.get();
this.log.info('fetching auth cookie from', loginUrl.href);
const authResponse = await axios.request({
url: loginUrl.href,
method: 'post',
data: {
providerType: 'basic',
providerName: provider,
currentURL: new URL('/login?next=%2F', baseUrl).href,
params: credentials ?? { username: this.getUsername(), password: this.getPassword() },
},
headers: {
'content-type': 'application/json',
'kbn-version': version,
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'x-elastic-internal-origin': 'Kibana',
},
validateStatus: () => true,
maxRedirects: 0,
});
if (authResponse.status !== 200) {
throw new Error(
`Kibana auth failed: code: ${authResponse.status}, message: ${authResponse.statusText}`
);
}
const cookie = extractCookieValue(authResponse);
if (cookie) {
this.log.info('captured auth cookie');
} else {
this.log.error(
format('unable to determine auth cookie from response', {
status: `${authResponse.status} ${authResponse.statusText}`,
body: authResponse.data,
headers: authResponse.headers,
})
);
throw new Error(`failed to determine auth cookie`);
}
return {
name: 'sid',
value: cookie,
url: baseUrl.href,
};
}
public getUsername() {
return this.config.get('servers.kibana.username');
}
public getPassword() {
return this.config.get('servers.kibana.password');
}
public isCloud() {
return this.config.get('servers.kibana.hostname') !== 'localhost';
}
public isServerless() {
return !!this.config.get('serverless');
}
}