|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +import { kibanaPackageJson } from '@kbn/utils'; |
| 10 | +import chalk from 'chalk'; |
| 11 | +import ora from 'ora'; |
| 12 | +import { Command } from 'commander'; |
| 13 | +import { getConfigPath } from '@kbn/utils'; |
| 14 | + |
| 15 | +import { |
| 16 | + ElasticsearchService, |
| 17 | + EnrollResult, |
| 18 | +} from '../plugins/interactive_setup/server/elasticsearch_service'; |
| 19 | +import { getDetailedErrorMessage } from '../plugins/interactive_setup/server/errors'; |
| 20 | +import { |
| 21 | + promptToken, |
| 22 | + getCommand, |
| 23 | + decodeEnrollmentToken, |
| 24 | + kibanaConfigWriter, |
| 25 | + elasticsearch, |
| 26 | +} from './utils'; |
| 27 | +import { Logger } from '../cli_plugin/lib/logger'; |
| 28 | + |
| 29 | +const program = new Command('bin/kibana-setup'); |
| 30 | + |
| 31 | +program |
| 32 | + .version(kibanaPackageJson.version) |
| 33 | + .description( |
| 34 | + 'This command walks you through all required steps to securely connect Kibana with Elasticsearch' |
| 35 | + ) |
| 36 | + .option('-t, --token <token>', 'Elasticsearch enrollment token') |
| 37 | + .option('-s, --silent', 'Prevent all logging'); |
| 38 | + |
| 39 | +program.parse(process.argv); |
| 40 | + |
| 41 | +interface SetupOptions { |
| 42 | + token?: string; |
| 43 | + silent?: boolean; |
| 44 | +} |
| 45 | + |
| 46 | +const options = program.opts() as SetupOptions; |
| 47 | +const spinner = ora(); |
| 48 | +const logger = new Logger(options); |
| 49 | + |
| 50 | +async function initCommand() { |
| 51 | + const token = decodeEnrollmentToken( |
| 52 | + options.token ?? (options.silent ? undefined : await promptToken()) |
| 53 | + ); |
| 54 | + if (!token) { |
| 55 | + logger.error(chalk.red('Invalid enrollment token provided.')); |
| 56 | + logger.error(''); |
| 57 | + logger.error('To generate a new enrollment token run:'); |
| 58 | + logger.error(` ${getCommand('elasticsearch-create-enrollment-token', '-s kibana')}`); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | + |
| 62 | + if (!(await kibanaConfigWriter.isConfigWritable())) { |
| 63 | + logger.error(chalk.red('Kibana does not have enough permissions to write to the config file.')); |
| 64 | + logger.error(''); |
| 65 | + logger.error('To grant write access run:'); |
| 66 | + logger.error(` chmod +w ${getConfigPath()}`); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + logger.log(''); |
| 71 | + if (!options.silent) { |
| 72 | + spinner.start(chalk.dim('Configuring Kibana...')); |
| 73 | + } |
| 74 | + |
| 75 | + let configToWrite: EnrollResult; |
| 76 | + try { |
| 77 | + configToWrite = await elasticsearch.enroll({ |
| 78 | + hosts: token.adr, |
| 79 | + apiKey: token.key, |
| 80 | + caFingerprint: ElasticsearchService.formatFingerprint(token.fgr), |
| 81 | + }); |
| 82 | + } catch (error) { |
| 83 | + if (!options.silent) { |
| 84 | + spinner.fail( |
| 85 | + `${chalk.bold('Unable to enroll with Elasticsearch:')} ${chalk.red( |
| 86 | + `${getDetailedErrorMessage(error)}` |
| 87 | + )}` |
| 88 | + ); |
| 89 | + } |
| 90 | + logger.error(''); |
| 91 | + logger.error('To generate a new enrollment token run:'); |
| 92 | + logger.error(` ${getCommand('elasticsearch-create-enrollment-token', '-s kibana')}`); |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + await kibanaConfigWriter.writeConfig(configToWrite); |
| 98 | + } catch (error) { |
| 99 | + if (!options.silent) { |
| 100 | + spinner.fail( |
| 101 | + `${chalk.bold('Unable to configure Kibana:')} ${chalk.red( |
| 102 | + `${getDetailedErrorMessage(error)}` |
| 103 | + )}` |
| 104 | + ); |
| 105 | + } |
| 106 | + logger.error(chalk.red(`${getDetailedErrorMessage(error)}`)); |
| 107 | + process.exit(1); |
| 108 | + } |
| 109 | + |
| 110 | + if (!options.silent) { |
| 111 | + spinner.succeed(chalk.bold('Kibana configured successfully.')); |
| 112 | + } |
| 113 | + logger.log(''); |
| 114 | + logger.log('To start Kibana run:'); |
| 115 | + logger.log(` ${getCommand('kibana')}`); |
| 116 | +} |
| 117 | + |
| 118 | +initCommand(); |
0 commit comments