Skip to content

Commit

Permalink
[APM] Add support for basepath (#52162)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenlouv authored Dec 4, 2019
1 parent 37d1a8e commit b9e2895
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down Expand Up @@ -112,9 +132,10 @@ async function isSecurityEnabled() {
}

async function callKibana<T>(options: AxiosRequestConfig): Promise<T> {
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
Expand Down

0 comments on commit b9e2895

Please sign in to comment.