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 = { 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 ) { 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 ) { } } function btoa ( str ) { return Buffer . from ( str , 'binary' ) . toString ( 'base64' ) ; } function atob ( str ) { return Buffer . from ( str , 'base64' ) . toString ( 'binary' ) ; } export function getCommand ( command , args ? ) { const isWindows = process . platform === 'win32' ; return `${ isWindows ? `bin\\${ command }.bat` : `bin/${ command }` }${ args ? ` ${ args }` : '' }` ; }