-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ml-discover-embeddable-integration
- Loading branch information
Showing
109 changed files
with
2,148 additions
and
913 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
require('../src/cli_setup/dev'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
interface LoggerOptions { | ||
silent?: boolean; | ||
quiet?: boolean; | ||
} | ||
|
||
export declare class Logger { | ||
constructor(settings?: LoggerOptions); | ||
|
||
log(data: string, sameLine?: boolean): void; | ||
|
||
error(data: string): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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 { kibanaPackageJson } from '@kbn/utils'; | ||
import chalk from 'chalk'; | ||
import ora from 'ora'; | ||
import { Command } from 'commander'; | ||
import { getConfigPath } from '@kbn/utils'; | ||
|
||
import { | ||
ElasticsearchService, | ||
EnrollResult, | ||
} from '../plugins/interactive_setup/server/elasticsearch_service'; | ||
import { getDetailedErrorMessage } from '../plugins/interactive_setup/server/errors'; | ||
import { | ||
promptToken, | ||
getCommand, | ||
decodeEnrollmentToken, | ||
kibanaConfigWriter, | ||
elasticsearch, | ||
} from './utils'; | ||
import { Logger } from '../cli_plugin/lib/logger'; | ||
|
||
const program = new Command('bin/kibana-setup'); | ||
|
||
program | ||
.version(kibanaPackageJson.version) | ||
.description( | ||
'This command walks you through all required steps to securely connect Kibana with Elasticsearch' | ||
) | ||
.option('-t, --token <token>', 'Elasticsearch enrollment token') | ||
.option('-s, --silent', 'Prevent all logging'); | ||
|
||
program.parse(process.argv); | ||
|
||
interface SetupOptions { | ||
token?: string; | ||
silent?: boolean; | ||
} | ||
|
||
const options = program.opts() as SetupOptions; | ||
const spinner = ora(); | ||
const logger = new Logger(options); | ||
|
||
async function initCommand() { | ||
const token = decodeEnrollmentToken( | ||
options.token ?? (options.silent ? undefined : await promptToken()) | ||
); | ||
if (!token) { | ||
logger.error(chalk.red('Invalid enrollment token provided.')); | ||
logger.error(''); | ||
logger.error('To generate a new enrollment token run:'); | ||
logger.error(` ${getCommand('elasticsearch-create-enrollment-token', '-s kibana')}`); | ||
process.exit(1); | ||
} | ||
|
||
if (!(await kibanaConfigWriter.isConfigWritable())) { | ||
logger.error(chalk.red('Kibana does not have enough permissions to write to the config file.')); | ||
logger.error(''); | ||
logger.error('To grant write access run:'); | ||
logger.error(` chmod +w ${getConfigPath()}`); | ||
process.exit(1); | ||
} | ||
|
||
logger.log(''); | ||
if (!options.silent) { | ||
spinner.start(chalk.dim('Configuring Kibana...')); | ||
} | ||
|
||
let configToWrite: EnrollResult; | ||
try { | ||
configToWrite = await elasticsearch.enroll({ | ||
hosts: token.adr, | ||
apiKey: token.key, | ||
caFingerprint: ElasticsearchService.formatFingerprint(token.fgr), | ||
}); | ||
} catch (error) { | ||
if (!options.silent) { | ||
spinner.fail( | ||
`${chalk.bold('Unable to enroll with Elasticsearch:')} ${chalk.red( | ||
`${getDetailedErrorMessage(error)}` | ||
)}` | ||
); | ||
} | ||
logger.error(''); | ||
logger.error('To generate a new enrollment token run:'); | ||
logger.error(` ${getCommand('elasticsearch-create-enrollment-token', '-s kibana')}`); | ||
process.exit(1); | ||
} | ||
|
||
try { | ||
await kibanaConfigWriter.writeConfig(configToWrite); | ||
} catch (error) { | ||
if (!options.silent) { | ||
spinner.fail( | ||
`${chalk.bold('Unable to configure Kibana:')} ${chalk.red( | ||
`${getDetailedErrorMessage(error)}` | ||
)}` | ||
); | ||
} | ||
logger.error(chalk.red(`${getDetailedErrorMessage(error)}`)); | ||
process.exit(1); | ||
} | ||
|
||
if (!options.silent) { | ||
spinner.succeed(chalk.bold('Kibana configured successfully.')); | ||
} | ||
logger.log(''); | ||
logger.log('To start Kibana run:'); | ||
logger.log(` ${getCommand('kibana')}`); | ||
} | ||
|
||
initCommand(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
require('../setup_node_env'); | ||
require('./cli_setup'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
require('../setup_node_env/dist'); | ||
require('./cli_setup'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/src/cli_setup'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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 { decodeEnrollmentToken, getCommand } from './utils'; | ||
import type { EnrollmentToken } from '../plugins/interactive_setup/common'; | ||
|
||
describe('kibana setup cli', () => { | ||
describe('getCommand', () => { | ||
const originalPlatform = process.platform; | ||
|
||
it('should format windows correctly', () => { | ||
Object.defineProperty(process, 'platform', { | ||
value: 'win32', | ||
}); | ||
expect(getCommand('kibana')).toEqual('bin\\kibana.bat'); | ||
expect(getCommand('kibana', '--silent')).toEqual('bin\\kibana.bat --silent'); | ||
}); | ||
|
||
it('should format unix correctly', () => { | ||
Object.defineProperty(process, 'platform', { | ||
value: 'linux', | ||
}); | ||
expect(getCommand('kibana')).toEqual('bin/kibana'); | ||
expect(getCommand('kibana', '--silent')).toEqual('bin/kibana --silent'); | ||
}); | ||
|
||
afterAll(function () { | ||
Object.defineProperty(process, 'platform', { | ||
value: originalPlatform, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('decodeEnrollmentToken', () => { | ||
const token: EnrollmentToken = { | ||
ver: '8.0.0', | ||
adr: ['localhost:9200'], | ||
fgr: 'AA:C8:2C:2E:09:58:F4:FE:A1:D2:AB:7F:13:70:C2:7D:EB:FD:A2:23:88:13:E4:DA:3A:D0:59:D0:09:00:07:36', | ||
key: 'JH-36HoBo4EYIoVhHh2F:uEo4dksARMq_BSHaAHUr8Q', | ||
}; | ||
|
||
it('should decode a valid token', () => { | ||
expect(decodeEnrollmentToken(btoa(JSON.stringify(token)))).toEqual({ | ||
adr: ['https://localhost:9200'], | ||
fgr: 'AA:C8:2C:2E:09:58:F4:FE:A1:D2:AB:7F:13:70:C2:7D:EB:FD:A2:23:88:13:E4:DA:3A:D0:59:D0:09:00:07:36', | ||
key: 'SkgtMzZIb0JvNEVZSW9WaEhoMkY6dUVvNGRrc0FSTXFfQlNIYUFIVXI4UQ==', | ||
ver: '8.0.0', | ||
}); | ||
}); | ||
|
||
it('should not decode an invalid token', () => { | ||
expect(decodeEnrollmentToken(JSON.stringify(token))).toBeUndefined(); | ||
expect( | ||
decodeEnrollmentToken( | ||
btoa( | ||
JSON.stringify({ | ||
ver: [''], | ||
adr: null, | ||
fgr: false, | ||
key: undefined, | ||
}) | ||
) | ||
) | ||
).toBeUndefined(); | ||
expect(decodeEnrollmentToken(btoa(JSON.stringify({})))).toBeUndefined(); | ||
expect(decodeEnrollmentToken(btoa(JSON.stringify([])))).toBeUndefined(); | ||
expect(decodeEnrollmentToken(btoa(JSON.stringify(null)))).toBeUndefined(); | ||
expect(decodeEnrollmentToken(btoa(JSON.stringify('')))).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.