Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[es/healthcheck] determine enabled scripting langs in healthcheck #12454

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/core_plugins/elasticsearch/lib/__tests__/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -20,6 +22,7 @@ describe('plugins/elasticsearch', () => {
let health;
let plugin;
let cluster;
let server;

beforeEach(() => {
const COMPATIBLE_VERSION_NUMBER = '5.0.0';
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -76,6 +82,7 @@ describe('plugins/elasticsearch', () => {

afterEach(() => {
kibanaVersion.get.restore();
determineEnabledScriptingLangs.restore();
});

it('should set the cluster green if everything is ready', function () {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
}
7 changes: 7 additions & 0 deletions src/core_plugins/elasticsearch/lib/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -92,13 +93,18 @@ module.exports = function (plugin, server, { mappings }) {
}

function check() {
const results = {};

const healthCheck =
waitForPong(callAdminAsKibanaUser, config.get('elasticsearch.url'))
.then(waitForEsVersion)
.then(() => ensureNotTribe(callAdminAsKibanaUser))
.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) {
Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 || []);
}
});
}