From a5334559a9a6d595e3dd3241c3546f7b38f8ab50 Mon Sep 17 00:00:00 2001 From: Spencer Date: Wed, 21 Jun 2017 14:57:29 -0400 Subject: [PATCH] [es/healthcheck] determine enabled scripting langs in healthcheck (#12454) * [es/healthcheck] determine enabled scripting langs in healthcheck * add test for "latestHealthCheckResults" exports --- .../lib/__tests__/health_check.js | 36 ++++++++++++++++++- .../lib/determine_enabled_scripting_langs.js | 14 ++++++++ .../elasticsearch/lib/health_check.js | 7 ++++ .../routes/api/scripts/register_languages.js | 21 ++--------- 4 files changed, 58 insertions(+), 20 deletions(-) create mode 100644 src/core_plugins/elasticsearch/lib/determine_enabled_scripting_langs.js diff --git a/src/core_plugins/elasticsearch/lib/__tests__/health_check.js b/src/core_plugins/elasticsearch/lib/__tests__/health_check.js index 36108487bc08c..84bf94a639ae6 100644 --- a/src/core_plugins/elasticsearch/lib/__tests__/health_check.js +++ b/src/core_plugins/elasticsearch/lib/__tests__/health_check.js @@ -9,6 +9,8 @@ import mappings from './fixtures/mappings'; import healthCheck from '../health_check'; import kibanaVersion from '../kibana_version'; import { esTestServerUrlParts } from '../../../../../test/es_test_server_url_parts'; +import * as determineEnabledScriptingLangsNS from '../determine_enabled_scripting_langs'; +import { determineEnabledScriptingLangs } from '../determine_enabled_scripting_langs'; const esPort = esTestServerUrlParts.port; const esUrl = url.format(esTestServerUrlParts); @@ -20,6 +22,7 @@ describe('plugins/elasticsearch', () => { let health; let plugin; let cluster; + let server; beforeEach(() => { const COMPATIBLE_VERSION_NUMBER = '5.0.0'; @@ -52,6 +55,8 @@ describe('plugins/elasticsearch', () => { } })); + sinon.stub(determineEnabledScriptingLangsNS, 'determineEnabledScriptingLangs').returns(Promise.resolve([])); + // setup the config().get()/.set() stubs const get = sinon.stub(); get.withArgs('elasticsearch.url').returns(esUrl); @@ -60,10 +65,11 @@ describe('plugins/elasticsearch', () => { const set = sinon.stub(); // Setup the server mock - const server = { + server = { log: sinon.stub(), info: { port: 5601 }, config: function () { return { get, set }; }, + expose: sinon.stub(), plugins: { elasticsearch: { getCluster: sinon.stub().returns(cluster) @@ -76,6 +82,7 @@ describe('plugins/elasticsearch', () => { afterEach(() => { kibanaVersion.get.restore(); + determineEnabledScriptingLangs.restore(); }); it('should set the cluster green if everything is ready', function () { @@ -170,6 +177,33 @@ describe('plugins/elasticsearch', () => { }); }); + describe('latestHealthCheckResults', () => { + it('exports an object when the health check completes', () => { + cluster.callWithInternalUser.withArgs('ping').returns(Promise.resolve()); + cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any).returns( + Promise.resolve({ timed_out: false, status: 'green' }) + ); + determineEnabledScriptingLangs.returns(Promise.resolve([ + 'foo', + 'bar' + ])); + + return health.run() + .then(function () { + sinon.assert.calledOnce(server.expose); + expect(server.expose.firstCall.args).to.eql([ + 'latestHealthCheckResults', + { + enabledScriptingLangs: [ + 'foo', + 'bar' + ] + } + ]); + }); + }); + }); + describe('#waitUntilReady', function () { it('polls health until index is ready', function () { const clusterHealth = cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any); diff --git a/src/core_plugins/elasticsearch/lib/determine_enabled_scripting_langs.js b/src/core_plugins/elasticsearch/lib/determine_enabled_scripting_langs.js new file mode 100644 index 0000000000000..4cba5514a4eb1 --- /dev/null +++ b/src/core_plugins/elasticsearch/lib/determine_enabled_scripting_langs.js @@ -0,0 +1,14 @@ +import { get, pick, omit, keys } from 'lodash'; + +export function determineEnabledScriptingLangs(callDataAsKibanaUser) { + return callDataAsKibanaUser('cluster.getSettings', { + include_defaults: true, + filter_path: '**.script.engine.*.inline' + }) + .then((esResponse) => { + const langs = get(esResponse, 'defaults.script.engine', {}); + const inlineLangs = pick(langs, lang => lang.inline === 'true'); + const supportedLangs = omit(inlineLangs, 'mustache'); + return keys(supportedLangs); + }); +} diff --git a/src/core_plugins/elasticsearch/lib/health_check.js b/src/core_plugins/elasticsearch/lib/health_check.js index bfde87e98e130..31e3d336e9847 100644 --- a/src/core_plugins/elasticsearch/lib/health_check.js +++ b/src/core_plugins/elasticsearch/lib/health_check.js @@ -7,6 +7,7 @@ import kibanaVersion from './kibana_version'; import { ensureEsVersion } from './ensure_es_version'; import { ensureNotTribe } from './ensure_not_tribe'; import { ensureAllowExplicitIndex } from './ensure_allow_explicit_index'; +import { determineEnabledScriptingLangs } from './determine_enabled_scripting_langs'; const NoConnections = elasticsearch.errors.NoConnections; import util from 'util'; @@ -92,6 +93,8 @@ module.exports = function (plugin, server, { mappings }) { } function check() { + const results = {}; + const healthCheck = waitForPong(callAdminAsKibanaUser, config.get('elasticsearch.url')) .then(waitForEsVersion) @@ -99,6 +102,9 @@ module.exports = function (plugin, server, { mappings }) { .then(() => ensureAllowExplicitIndex(callAdminAsKibanaUser, config)) .then(waitForShards) .then(_.partial(migrateConfig, server, { mappings })) + .then(async () => { + results.enabledScriptingLangs = await determineEnabledScriptingLangs(callDataAsKibanaUser); + }) .then(() => { const tribeUrl = config.get('elasticsearch.tribe.url'); if (tribeUrl) { @@ -108,6 +114,7 @@ module.exports = function (plugin, server, { mappings }) { }); return healthCheck + .then(() => server.expose('latestHealthCheckResults', results)) .then(setGreenStatus) .catch(err => plugin.status.red(err)); } diff --git a/src/core_plugins/kibana/server/routes/api/scripts/register_languages.js b/src/core_plugins/kibana/server/routes/api/scripts/register_languages.js index 125e4b7734ce8..306eb09ef23cb 100644 --- a/src/core_plugins/kibana/server/routes/api/scripts/register_languages.js +++ b/src/core_plugins/kibana/server/routes/api/scripts/register_languages.js @@ -1,27 +1,10 @@ -import _ from 'lodash'; -import handleESError from '../../../lib/handle_es_error'; - export function registerLanguages(server) { server.route({ path: '/api/kibana/scripts/languages', method: 'GET', handler: function (request, reply) { - const { callWithRequest } = server.plugins.elasticsearch.getCluster('data'); - - return callWithRequest(request, 'cluster.getSettings', { - include_defaults: true, - filter_path: '**.script.engine.*.inline' - }) - .then((esResponse) => { - const langs = _.get(esResponse, 'defaults.script.engine', {}); - const inlineLangs = _.pick(langs, (lang) => lang.inline === 'true'); - const supportedLangs = _.omit(inlineLangs, 'mustache'); - return _.keys(supportedLangs); - }) - .then(reply) - .catch((error) => { - reply(handleESError(error)); - }); + const { latestHealthCheckResults = {} } = server.plugins.elasticsearch; + reply(latestHealthCheckResults.enabledScriptingLangs || []); } }); }