forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
92 lines (83 loc) · 2.95 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* 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 { getConfigPath, getDataPath } from '@kbn/utils';
import inquirer from 'inquirer';
import { duration } from 'moment';
import { merge } from 'lodash';
import { kibanaPackageJson } from '@kbn/utils';
import { Logger } from '../core/server';
import { ClusterClient } from '../core/server/elasticsearch/client';
import { configSchema } from '../core/server/elasticsearch';
import { ElasticsearchService } from '../plugins/interactive_setup/server/elasticsearch_service';
import { KibanaConfigWriter } from '../plugins/interactive_setup/server/kibana_config_writer';
import type { EnrollmentToken } from '../plugins/interactive_setup/common';
const noop = () => {};
const logger: Logger = {
debug: noop,
error: noop,
warn: noop,
trace: noop,
info: noop,
fatal: noop,
log: noop,
get: () => logger,
};
export const kibanaConfigWriter = new KibanaConfigWriter(getConfigPath(), getDataPath(), logger);
export const elasticsearch = new ElasticsearchService(logger, kibanaPackageJson.version).setup({
connectionCheckInterval: duration(Infinity),
elasticsearch: {
createClient: (type, config) => {
const defaults = configSchema.validate({});
return new ClusterClient(
merge(
defaults,
{
hosts: Array.isArray(defaults.hosts) ? defaults.hosts : [defaults.hosts],
},
config
),
logger,
type
);
},
},
});
export async function promptToken() {
const answers = await inquirer.prompt({
type: 'input',
name: 'token',
message: 'Enter enrollment token:',
validate: (value = '') => (decodeEnrollmentToken(value) ? true : 'Invalid enrollment token'),
});
return answers.token;
}
export function decodeEnrollmentToken(enrollmentToken: string): EnrollmentToken | undefined {
try {
const json = JSON.parse(atob(enrollmentToken)) as EnrollmentToken;
if (
!Array.isArray(json.adr) ||
json.adr.some((adr) => typeof adr !== 'string') ||
typeof json.fgr !== 'string' ||
typeof json.key !== 'string' ||
typeof json.ver !== 'string'
) {
return;
}
return { ...json, adr: json.adr.map((adr) => `https://${adr}`), key: btoa(json.key) };
} catch (error) {} // eslint-disable-line no-empty
}
function btoa(str: string) {
return Buffer.from(str, 'binary').toString('base64');
}
function atob(str: string) {
return Buffer.from(str, 'base64').toString('binary');
}
export function getCommand(command: string, args?: string) {
const isWindows = process.platform === 'win32';
return `${isWindows ? `bin\\${command}.bat` : `bin/${command}`}${args ? ` ${args}` : ''}`;
}