Skip to content

Commit

Permalink
update painless_lab to use new es-js client
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jan 19, 2021
1 parent e2fc156 commit 41b5f53
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,24 @@ import { IKibanaResponse, KibanaResponseFactory } from 'kibana/server';
interface EsErrorHandlerParams {
error: ApiError;
response: KibanaResponseFactory;
handleCustomError?: () => IKibanaResponse<any>;
}

/*
* For errors returned by the new elasticsearch js client.
*/
export const handleEsError = ({ error, response }: EsErrorHandlerParams): IKibanaResponse => {
export const handleEsError = ({
error,
response,
handleCustomError,
}: EsErrorHandlerParams): IKibanaResponse => {
// error.name is slightly better in terms of performance, since all errors now have name property
if (error.name === 'ResponseError') {
// The consumer may sometimes want to provide a custom response
if (typeof handleCustomError === 'function') {
return handleCustomError();
}

const { statusCode, body } = error as ResponseError;
return response.customError({
statusCode,
Expand Down
23 changes: 12 additions & 11 deletions x-pack/plugins/painless_lab/server/routes/api/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema';

import { API_BASE_PATH } from '../../../common/constants';
import { RouteDependencies } from '../../types';
import { isEsError } from '../../shared_imports';
import { handleEsError } from '../../shared_imports';

const bodySchema = schema.string();

Expand All @@ -23,23 +23,24 @@ export function registerExecuteRoute({ router, license }: RouteDependencies) {
const body = req.body;

try {
const callAsCurrentUser = ctx.core.elasticsearch.legacy.client.callAsCurrentUser;
const response = await callAsCurrentUser('scriptsPainlessExecute', {
const client = ctx.core.elasticsearch.client.asCurrentUser;
const response = await client.scriptsPainlessExecute({
body,
});

return res.ok({
body: response,
body: response.body,
});
} catch (e) {
if (isEsError(e)) {
// Assume invalid painless script was submitted
// Return 200 with error object
} catch (error) {
// Assume invalid painless script was submitted
// Return 200 with error object
const handleCustomError = () => {
return res.ok({
body: e.body,
body: error.body,
});
}
return res.internalError({ body: e });
};

return handleEsError({ error, response: res, handleCustomError });
}
})
);
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/painless_lab/server/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
* you may not use this file except in compliance with the Elastic License.
*/

export { isEsError } from '../../../../src/plugins/es_ui_shared/server';
export { handleEsError } from '../../../../src/plugins/es_ui_shared/server';
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./transform'));
loadTestFile(require.resolve('./lists'));
loadTestFile(require.resolve('./upgrade_assistant'));
loadTestFile(require.resolve('./painless_lab'));
});
}
13 changes: 13 additions & 0 deletions x-pack/test/api_integration/apis/painless_lab/index.ts
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;
* you may not use this file except in compliance with the Elastic License.
*/

import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Painless Lab', () => {
loadTestFile(require.resolve('./painless_lab'));
});
}
49 changes: 49 additions & 0 deletions x-pack/test/api_integration/apis/painless_lab/painless_lab.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

const API_BASE_PATH = '/api/painless_lab';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('Painless Lab', function () {
describe('Execute', () => {
it('should execute a valid painless script', async () => {
const script =
'"{\\n \\"script\\": {\\n \\"source\\": \\"return true;\\",\\n \\"params\\": {\\n \\"string_parameter\\": \\"string value\\",\\n \\"number_parameter\\": 1.5,\\n \\"boolean_parameter\\": true\\n}\\n }\\n}"';

const { body } = await supertest
.post(`${API_BASE_PATH}/execute`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(script)
.expect(200);

expect(body).to.eql({
result: 'true',
});
});

it('should return error response for invalid painless script', async () => {
const invalidScript =
'"{\\n \\"script\\": {\\n \\"source\\": \\"foobar\\",\\n \\"params\\": {\\n \\"string_parameter\\": \\"string value\\",\\n \\"number_parameter\\": 1.5,\\n \\"boolean_parameter\\": true\\n}\\n }\\n}"';

const { body } = await supertest
.post(`${API_BASE_PATH}/execute`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(invalidScript)
.expect(200);

expect(body.error).to.not.be(undefined);
expect(body.error.reason).to.eql('compile error');
});
});
});
}

0 comments on commit 41b5f53

Please sign in to comment.