From b9e2895f2258da4b01f96ac2000514ef01f47379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Wed, 4 Dec 2019 13:27:26 +0100 Subject: [PATCH] [APM] Add support for basepath (#52162) --- .../setup-custom-kibana-user-role.ts | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/x-pack/legacy/plugins/apm/scripts/kibana-security/setup-custom-kibana-user-role.ts b/x-pack/legacy/plugins/apm/scripts/kibana-security/setup-custom-kibana-user-role.ts index 57b169c8ab1d82..9591dfdecbfef0 100644 --- a/x-pack/legacy/plugins/apm/scripts/kibana-security/setup-custom-kibana-user-role.ts +++ b/x-pack/legacy/plugins/apm/scripts/kibana-security/setup-custom-kibana-user-role.ts @@ -9,7 +9,7 @@ import yaml from 'js-yaml'; import axios, { AxiosRequestConfig, AxiosError } from 'axios'; import fs from 'fs'; -import { union, difference } from 'lodash'; +import { union, difference, once } from 'lodash'; import path from 'path'; import { argv } from 'yargs'; @@ -22,6 +22,7 @@ const config = yaml.safeLoad( const GITHUB_USERNAME = argv.username as string; const KIBANA_INDEX = config['kibana.index'] as string; +const TASK_MANAGER_INDEX = config['xpack.task_manager.index'] as string; const ELASTICSEARCH_USERNAME = (argv.esUsername as string) || 'elastic'; const ELASTICSEARCH_PASSWORD = (argv.esPassword || config['elasticsearch.password']) as string; @@ -35,6 +36,18 @@ interface User { enabled?: boolean; } +const getKibanaBasePath = once(async () => { + try { + await axios.request({ url: KIBANA_BASE_URL, maxRedirects: 0 }); + } catch (e) { + const err = e as AxiosError; + const { location } = err.response?.headers; + const isBasePath = RegExp(/^\/\w{3}$/).test(location); + return isBasePath ? location : ''; + } + return ''; +}); + init().catch(e => { if (e.response) { console.log( @@ -50,14 +63,21 @@ async function init() { // kibana.index must be different from `.kibana` if (KIBANA_INDEX === '.kibana') { console.log( - 'Please use a custom `kibana.index` in kibana.dev.yml. Example: "kibana.index: .kibana-john"' + 'kibana.dev.yml: Please use a custom "kibana.index". Example: "kibana.index: .kibana-john"' ); return; } if (!KIBANA_INDEX.startsWith('.kibana')) { console.log( - 'Your `kibana.index` must be prefixed with `.kibana`. Example: "kibana.index: .kibana-john"' + 'kibana.dev.yml: "kibana.index" must be prefixed with `.kibana`. Example: "kibana.index: .kibana-john"' + ); + return; + } + + if (TASK_MANAGER_INDEX && !TASK_MANAGER_INDEX.startsWith('.kibana')) { + console.log( + 'kibana.dev.yml: "xpack.task_manager.index" must be prefixed with `.kibana`. Example: "xpack.task_manager.index: .kibana-task-manager-john"' ); return; } @@ -112,9 +132,10 @@ async function isSecurityEnabled() { } async function callKibana(options: AxiosRequestConfig): Promise { + const basePath = await getKibanaBasePath(); const { data } = await axios.request({ ...options, - baseURL: KIBANA_BASE_URL, + baseURL: KIBANA_BASE_URL + basePath, auth: { username: ELASTICSEARCH_USERNAME, password: ELASTICSEARCH_PASSWORD