[@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 <restrry@gmail.com>
This commit is contained in:
Kibana Machine 2021-10-04 17:41:09 -04:00 committed by GitHub
parent 4d80db3ad9
commit 9f95a44fe3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 28 additions and 50 deletions

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * 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 // 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. // 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 index: string
): Promise<unknown> => { ): Promise<unknown> => {
return ( return (
await esClient.transport.request({ await esClient.indices.create({
path: `/${index}-000001`, index: `${index}-000001`,
method: 'PUT',
body: { body: {
aliases: { aliases: {
[index]: { [index]: {

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const deleteAllIndex = async ( export const deleteAllIndex = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,

View file

@ -6,16 +6,14 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const deletePolicy = async ( export const deletePolicy = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,
policy: string policy: string
): Promise<unknown> => { ): Promise<unknown> => {
return ( return (
await esClient.transport.request({ // @ts-expect-error policy_id is required by mistake. fixed in the v8.0
path: `/_ilm/policy/${policy}`, (await esClient.ilm.deleteLifecycle({ policy })).body
method: 'DELETE', );
})
).body;
}; };

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const deleteTemplate = async ( export const deleteTemplate = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,

View file

@ -10,12 +10,6 @@
// as these types aren't part of any package yet. Once they are, remove this completely // 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 { KibanaClient } from '@elastic/elasticsearch/api/kibana';
import type {
ApiResponse,
TransportRequestOptions,
TransportRequestParams,
TransportRequestPromise,
} from '@elastic/elasticsearch/lib/Transport';
/** /**
* Client used to query the elasticsearch cluster. * Client used to query the elasticsearch cluster.
@ -25,11 +19,4 @@ import type {
export type ElasticsearchClient = Omit< export type ElasticsearchClient = Omit<
KibanaClient, KibanaClient,
'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close' 'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close'
> & { >;
transport: {
request(
params: TransportRequestParams,
options?: TransportRequestOptions
): TransportRequestPromise<ApiResponse>;
};
};

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
interface AliasesResponse { interface AliasesResponse {
[indexName: string]: { [indexName: string]: {

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * 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 * Retrieves the count of documents in a given index

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const getIndexExists = async ( export const getIndexExists = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,

View file

@ -5,17 +5,15 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server * in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import type { ElasticsearchClient } from '../elasticsearch_client';
import { ElasticsearchClient } from '../elasticsearch_client';
export const getPolicyExists = async ( export const getPolicyExists = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,
policy: string policy: string
): Promise<boolean> => { ): Promise<boolean> => {
try { try {
await esClient.transport.request({ await esClient.ilm.getLifecycle({
path: `/_ilm/policy/${policy}`, policy,
method: 'GET',
}); });
// Return true that there exists a policy which is not 404 or some error // 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 // Since there is not a policy exists API, this is how we create one by calling

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const getTemplateExists = async ( export const getTemplateExists = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * 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<unknown> => { export const readIndex = async (esClient: ElasticsearchClient, index: string): Promise<unknown> => {
return esClient.indices.get({ return esClient.indices.get({

View file

@ -6,16 +6,14 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const readPrivileges = async ( export const readPrivileges = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,
index: string index: string
): Promise<unknown> => { ): Promise<unknown> => {
return ( return (
await esClient.transport.request({ await esClient.security.hasPrivileges({
path: '/_security/user/_has_privileges',
method: 'POST',
body: { body: {
cluster: [ cluster: [
'all', 'all',

View file

@ -5,8 +5,7 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server * in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import type { ElasticsearchClient } from '../elasticsearch_client';
import { ElasticsearchClient } from '../elasticsearch_client';
export const setPolicy = async ( export const setPolicy = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,
@ -14,9 +13,8 @@ export const setPolicy = async (
body: Record<string, unknown> body: Record<string, unknown>
): Promise<unknown> => { ): Promise<unknown> => {
return ( return (
await esClient.transport.request({ await esClient.ilm.putLifecycle({
path: `/_ilm/policy/${policy}`, policy,
method: 'PUT',
body, body,
}) })
).body; ).body;

View file

@ -6,7 +6,7 @@
* Side Public License, v 1. * Side Public License, v 1.
*/ */
import { ElasticsearchClient } from '../elasticsearch_client'; import type { ElasticsearchClient } from '../elasticsearch_client';
export const setTemplate = async ( export const setTemplate = async (
esClient: ElasticsearchClient, esClient: ElasticsearchClient,

View file

@ -35,8 +35,8 @@ type SecuritySolutionRequestHandlerContextMock = SecuritySolutionRequestHandlerC
asCurrentUser: { asCurrentUser: {
updateByQuery: jest.Mock; updateByQuery: jest.Mock;
search: jest.Mock; search: jest.Mock;
transport: { security: {
request: jest.Mock; hasPrivileges: jest.Mock;
}; };
}; };
}; };

View file

@ -19,7 +19,7 @@ describe('read_privileges route', () => {
server = serverMock.create(); server = serverMock.create();
({ context } = requestContextMock.createTools()); ({ context } = requestContextMock.createTools());
context.core.elasticsearch.client.asCurrentUser.transport.request.mockResolvedValue({ context.core.elasticsearch.client.asCurrentUser.security.hasPrivileges.mockResolvedValue({
body: getMockPrivilegesResult(), body: getMockPrivilegesResult(),
}); });
@ -65,7 +65,7 @@ describe('read_privileges route', () => {
}); });
test('returns 500 when bad response from cluster', async () => { 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')) elasticsearchClientMock.createErrorTransportRequestPromise(new Error('Test error'))
); );
const response = await server.inject( const response = await server.inject(