Skip to content

Commit

Permalink
redact ES client errors (#171018)
Browse files Browse the repository at this point in the history
## Summary

Define a custom `toJSON` method on errors from the ES client to have
better control of the format of the output of their JSON serialization

(cherry picked from commit c43e699)
  • Loading branch information
pgayvallet committed Nov 10, 2023
1 parent e5046ca commit 44c76a7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import { parseClientOptions } from './client_config';
import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation';
import { createTransport } from './create_transport';
import type { AgentFactoryProvider } from './agent_manager';
import { patchElasticsearchClient } from './patch_client';

const noop = () => undefined;

// Apply ES client patches on module load
patchElasticsearchClient();

export const configureClient = (
config: ElasticsearchClientConfig,
{
Expand Down
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.
*/

import { errors } from '@elastic/elasticsearch';

export const patchElasticsearchClient = () => {
const baseErrorPrototype = errors.ElasticsearchClientError.prototype;
// @ts-expect-error
baseErrorPrototype.toJSON = function () {
return {
name: this.name,
message: this.message,
};
};
};
70 changes: 70 additions & 0 deletions src/core/server/integration_tests/elasticsearch/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 {
createTestServers,
type TestElasticsearchUtils,
type TestKibanaUtils,
} from '@kbn/core-test-helpers-kbn-server';

describe('elasticsearch clients errors', () => {
let esServer: TestElasticsearchUtils;
let kibanaServer: TestKibanaUtils;

beforeAll(async () => {
const { startES, startKibana } = createTestServers({
adjustTimeout: jest.setTimeout,
});

esServer = await startES();
kibanaServer = await startKibana();
});

afterAll(async () => {
await kibanaServer.stop();
await esServer.stop();
});

it('has the proper JSON representation', async () => {
const esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser;

try {
await esClient.search({
index: '.kibana',
// @ts-expect-error yes this is invalid
query: { someInvalidQuery: { foo: 'bar' } },
});
expect('should have thrown').toEqual('but it did not');
} catch (e) {
expect(JSON.stringify(e)).toMatchInlineSnapshot(
`"{\\"name\\":\\"ResponseError\\",\\"message\\":\\"parsing_exception\\\\n\\\\tCaused by:\\\\n\\\\t\\\\tnamed_object_not_found_exception: [1:30] unknown field [someInvalidQuery]\\\\n\\\\tRoot causes:\\\\n\\\\t\\\\tparsing_exception: unknown query [someInvalidQuery]\\"}"`
);
}
});

it('has the proper string representation', async () => {
const esClient = kibanaServer.coreStart.elasticsearch.client.asInternalUser;

try {
await esClient.search({
index: '.kibana',
// @ts-expect-error yes this is invalid
query: { someInvalidQuery: { foo: 'bar' } },
});
expect('should have thrown').toEqual('but it did not');
} catch (e) {
expect(String(e)).toMatchInlineSnapshot(`
"ResponseError: parsing_exception
Caused by:
named_object_not_found_exception: [1:30] unknown field [someInvalidQuery]
Root causes:
parsing_exception: unknown query [someInvalidQuery]"
`);
}
});
});

0 comments on commit 44c76a7

Please sign in to comment.