From 9f95a44fe352f9925491087c22414183f45badbb Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 4 Oct 2021 17:41:09 -0400 Subject: [PATCH] [@kbn/securitysolution-es-utils] remove transport API in favour of typed public API (#113717) (#113831) * remove transport API in favour of typed public API * put elasticsearch_client back * fix create index call * fix setpolicy * fix unit tests in SecuritySolution Co-authored-by: Mikhail Shustov --- .../src/create_boostrap_index/index.ts | 7 +++---- .../src/delete_all_index/index.ts | 2 +- .../src/delete_policy/index.ts | 10 ++++------ .../src/delete_template/index.ts | 2 +- .../src/elasticsearch_client/index.ts | 15 +-------------- .../src/get_index_aliases/index.ts | 2 +- .../src/get_index_count/index.ts | 2 +- .../src/get_index_exists/index.ts | 2 +- .../src/get_policy_exists/index.ts | 8 +++----- .../src/get_template_exists/index.ts | 2 +- .../src/read_index/index.ts | 2 +- .../src/read_privileges/index.ts | 6 ++---- .../src/set_policy/index.ts | 8 +++----- .../src/set_template/index.ts | 2 +- .../routes/__mocks__/request_context.ts | 4 ++-- .../privileges/read_privileges_route.test.ts | 4 ++-- 16 files changed, 28 insertions(+), 50 deletions(-) diff --git a/packages/kbn-securitysolution-es-utils/src/create_boostrap_index/index.ts b/packages/kbn-securitysolution-es-utils/src/create_boostrap_index/index.ts index 9671d35dc554..6a177f5caac2 100644 --- a/packages/kbn-securitysolution-es-utils/src/create_boostrap_index/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/create_boostrap_index/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; // See the reference(s) below on explanations about why -000001 was chosen and // why the is_write_index is true as well as the bootstrapping step which is needed. @@ -16,9 +16,8 @@ export const createBootstrapIndex = async ( index: string ): Promise => { return ( - await esClient.transport.request({ - path: `/${index}-000001`, - method: 'PUT', + await esClient.indices.create({ + index: `${index}-000001`, body: { aliases: { [index]: { diff --git a/packages/kbn-securitysolution-es-utils/src/delete_all_index/index.ts b/packages/kbn-securitysolution-es-utils/src/delete_all_index/index.ts index 4df4724aaf2b..580c104752ae 100644 --- a/packages/kbn-securitysolution-es-utils/src/delete_all_index/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/delete_all_index/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const deleteAllIndex = async ( esClient: ElasticsearchClient, diff --git a/packages/kbn-securitysolution-es-utils/src/delete_policy/index.ts b/packages/kbn-securitysolution-es-utils/src/delete_policy/index.ts index 34c1d2e5da45..60a15f6d4625 100644 --- a/packages/kbn-securitysolution-es-utils/src/delete_policy/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/delete_policy/index.ts @@ -6,16 +6,14 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const deletePolicy = async ( esClient: ElasticsearchClient, policy: string ): Promise => { return ( - await esClient.transport.request({ - path: `/_ilm/policy/${policy}`, - method: 'DELETE', - }) - ).body; + // @ts-expect-error policy_id is required by mistake. fixed in the v8.0 + (await esClient.ilm.deleteLifecycle({ policy })).body + ); }; diff --git a/packages/kbn-securitysolution-es-utils/src/delete_template/index.ts b/packages/kbn-securitysolution-es-utils/src/delete_template/index.ts index 2e7a71af9f77..86565a0c43b3 100644 --- a/packages/kbn-securitysolution-es-utils/src/delete_template/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/delete_template/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const deleteTemplate = async ( esClient: ElasticsearchClient, diff --git a/packages/kbn-securitysolution-es-utils/src/elasticsearch_client/index.ts b/packages/kbn-securitysolution-es-utils/src/elasticsearch_client/index.ts index 0c2252bdc1f0..a1fb3ff3ecf3 100644 --- a/packages/kbn-securitysolution-es-utils/src/elasticsearch_client/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/elasticsearch_client/index.ts @@ -10,12 +10,6 @@ // as these types aren't part of any package yet. Once they are, remove this completely import type { KibanaClient } from '@elastic/elasticsearch/api/kibana'; -import type { - ApiResponse, - TransportRequestOptions, - TransportRequestParams, - TransportRequestPromise, -} from '@elastic/elasticsearch/lib/Transport'; /** * Client used to query the elasticsearch cluster. @@ -25,11 +19,4 @@ import type { export type ElasticsearchClient = Omit< KibanaClient, 'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close' -> & { - transport: { - request( - params: TransportRequestParams, - options?: TransportRequestOptions - ): TransportRequestPromise; - }; -}; +>; diff --git a/packages/kbn-securitysolution-es-utils/src/get_index_aliases/index.ts b/packages/kbn-securitysolution-es-utils/src/get_index_aliases/index.ts index 885103c1fb58..ba00c1144edf 100644 --- a/packages/kbn-securitysolution-es-utils/src/get_index_aliases/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/get_index_aliases/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; interface AliasesResponse { [indexName: string]: { diff --git a/packages/kbn-securitysolution-es-utils/src/get_index_count/index.ts b/packages/kbn-securitysolution-es-utils/src/get_index_count/index.ts index 523b41303a56..b1dcd4fd0ad9 100644 --- a/packages/kbn-securitysolution-es-utils/src/get_index_count/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/get_index_count/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; /** * Retrieves the count of documents in a given index diff --git a/packages/kbn-securitysolution-es-utils/src/get_index_exists/index.ts b/packages/kbn-securitysolution-es-utils/src/get_index_exists/index.ts index b7d12cab3f48..920877304847 100644 --- a/packages/kbn-securitysolution-es-utils/src/get_index_exists/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/get_index_exists/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const getIndexExists = async ( esClient: ElasticsearchClient, diff --git a/packages/kbn-securitysolution-es-utils/src/get_policy_exists/index.ts b/packages/kbn-securitysolution-es-utils/src/get_policy_exists/index.ts index cefd47dbe9d0..8172cfb2abaa 100644 --- a/packages/kbn-securitysolution-es-utils/src/get_policy_exists/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/get_policy_exists/index.ts @@ -5,17 +5,15 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const getPolicyExists = async ( esClient: ElasticsearchClient, policy: string ): Promise => { try { - await esClient.transport.request({ - path: `/_ilm/policy/${policy}`, - method: 'GET', + await esClient.ilm.getLifecycle({ + policy, }); // Return true that there exists a policy which is not 404 or some error // Since there is not a policy exists API, this is how we create one by calling diff --git a/packages/kbn-securitysolution-es-utils/src/get_template_exists/index.ts b/packages/kbn-securitysolution-es-utils/src/get_template_exists/index.ts index c56c5b968d45..72a3a9365448 100644 --- a/packages/kbn-securitysolution-es-utils/src/get_template_exists/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/get_template_exists/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const getTemplateExists = async ( esClient: ElasticsearchClient, diff --git a/packages/kbn-securitysolution-es-utils/src/read_index/index.ts b/packages/kbn-securitysolution-es-utils/src/read_index/index.ts index cc16645120b7..206a4208b2ec 100644 --- a/packages/kbn-securitysolution-es-utils/src/read_index/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/read_index/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const readIndex = async (esClient: ElasticsearchClient, index: string): Promise => { return esClient.indices.get({ diff --git a/packages/kbn-securitysolution-es-utils/src/read_privileges/index.ts b/packages/kbn-securitysolution-es-utils/src/read_privileges/index.ts index aab641367339..772a6afa18c9 100644 --- a/packages/kbn-securitysolution-es-utils/src/read_privileges/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/read_privileges/index.ts @@ -6,16 +6,14 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const readPrivileges = async ( esClient: ElasticsearchClient, index: string ): Promise => { return ( - await esClient.transport.request({ - path: '/_security/user/_has_privileges', - method: 'POST', + await esClient.security.hasPrivileges({ body: { cluster: [ 'all', diff --git a/packages/kbn-securitysolution-es-utils/src/set_policy/index.ts b/packages/kbn-securitysolution-es-utils/src/set_policy/index.ts index dc45ca3e1c08..f6c2dcf7c3c3 100644 --- a/packages/kbn-securitysolution-es-utils/src/set_policy/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/set_policy/index.ts @@ -5,8 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ - -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const setPolicy = async ( esClient: ElasticsearchClient, @@ -14,9 +13,8 @@ export const setPolicy = async ( body: Record ): Promise => { return ( - await esClient.transport.request({ - path: `/_ilm/policy/${policy}`, - method: 'PUT', + await esClient.ilm.putLifecycle({ + policy, body, }) ).body; diff --git a/packages/kbn-securitysolution-es-utils/src/set_template/index.ts b/packages/kbn-securitysolution-es-utils/src/set_template/index.ts index 89aaa44f29e0..20f6fd5719d5 100644 --- a/packages/kbn-securitysolution-es-utils/src/set_template/index.ts +++ b/packages/kbn-securitysolution-es-utils/src/set_template/index.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { ElasticsearchClient } from '../elasticsearch_client'; +import type { ElasticsearchClient } from '../elasticsearch_client'; export const setTemplate = async ( esClient: ElasticsearchClient, diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_context.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_context.ts index b47a9fc3a5d6..6039ad6ab612 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_context.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/__mocks__/request_context.ts @@ -35,8 +35,8 @@ type SecuritySolutionRequestHandlerContextMock = SecuritySolutionRequestHandlerC asCurrentUser: { updateByQuery: jest.Mock; search: jest.Mock; - transport: { - request: jest.Mock; + security: { + hasPrivileges: jest.Mock; }; }; }; diff --git a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/privileges/read_privileges_route.test.ts b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/privileges/read_privileges_route.test.ts index b79bdc857a17..7ffa45e2bf7e 100644 --- a/x-pack/plugins/security_solution/server/lib/detection_engine/routes/privileges/read_privileges_route.test.ts +++ b/x-pack/plugins/security_solution/server/lib/detection_engine/routes/privileges/read_privileges_route.test.ts @@ -19,7 +19,7 @@ describe('read_privileges route', () => { server = serverMock.create(); ({ context } = requestContextMock.createTools()); - context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue({ + context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges.mockResolvedValue({ body: getMockPrivilegesResult(), }); @@ -65,7 +65,7 @@ describe('read_privileges route', () => { }); test('returns 500 when bad response from cluster', async () => { - context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue( + context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges.mockResolvedValue( elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error')) ); const response = await server.inject(