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

[Security Solution, Lists] Replace legacy imports from 'elasticsearch' package #107226

Merged
merged 10 commits into from
Aug 5, 2021
4 changes: 2 additions & 2 deletions x-pack/plugins/lists/server/schemas/common/get_shard.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 2.0.
*/

import { ShardsResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';

export const getShardMock = (): ShardsResponse => ({
export const getShardMock = (): estypes.ShardStatistics => ({
failed: 0,
skipped: 0,
successful: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';

import {
DATE_NOW,
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getSearchEsListItemMock = (): SearchEsListItemSchema => ({
ip: VALUE,
});

export const getSearchListItemMock = (): SearchResponse<SearchEsListItemSchema> => ({
export const getSearchListItemMock = (): estypes.SearchResponse<SearchEsListItemSchema> => ({
_scroll_id: '123',
_shards: getShardMock(),
hits: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';

import {
DATE_NOW,
Expand Down Expand Up @@ -40,7 +40,7 @@ export const getSearchEsListMock = (): SearchEsListSchema => ({
version: VERSION,
});

export const getSearchListMock = (): SearchResponse<SearchEsListSchema> => ({
export const getSearchListMock = (): estypes.SearchResponse<SearchEsListSchema> => ({
_scroll_id: '123',
_shards: getShardMock(),
hits: {
Expand All @@ -60,7 +60,7 @@ export const getSearchListMock = (): SearchResponse<SearchEsListSchema> => ({
took: 10,
});

export const getEmptySearchListMock = (): SearchResponse<SearchEsListSchema> => ({
export const getEmptySearchListMock = (): estypes.SearchResponse<SearchEsListSchema> => ({
_scroll_id: '123',
_shards: getShardMock(),
hits: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { Client } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks';

Expand All @@ -14,7 +14,7 @@ import { getShardMock } from '../../schemas/common/get_shard.mock';

import { FindListItemOptions } from './find_list_item';

export const getFindCount = (): ReturnType<Client['count']> => {
export const getFindCount = (): Promise<estypes.CountResponse> => {
return Promise.resolve({
_shards: getShardMock(),
count: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ describe('write_list_items_to_stream', () => {
firstResponse.hits.hits[0].sort = ['some-sort-value'];

const secondResponse = getSearchListItemMock();
secondResponse.hits.hits[0]._source.ip = '255.255.255.255';
if (secondResponse.hits.hits[0]._source) {
secondResponse.hits.hits[0]._source.ip = '255.255.255.255';
}

const esClient = elasticsearchClientMock.createScopedClusterClient().asCurrentUser;
esClient.search.mockResolvedValueOnce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ describe('transform_elastic_to_list_item', () => {

test('it transforms an elastic keyword type to a list item type', () => {
const response = getSearchListItemMock();
response.hits.hits[0]._source.ip = undefined;
response.hits.hits[0]._source.keyword = 'host-name-example';
if (response.hits.hits[0]._source) {
response.hits.hits[0]._source.ip = undefined;
response.hits.hits[0]._source.keyword = 'host-name-example';
}
const queryFilter = transformElasticToListItem({
response,
type: 'keyword',
Expand Down Expand Up @@ -68,8 +70,10 @@ describe('transform_elastic_to_list_item', () => {
const {
hits: { hits },
} = getSearchListItemMock();
hits[0]._source.ip = undefined;
hits[0]._source.keyword = 'host-name-example';
if (hits[0]._source) {
hits[0]._source.ip = undefined;
hits[0]._source.keyword = 'host-name-example';
}
const queryFilter = transformElasticHitsToListItem({
hits,
type: 'keyword',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import React, { useCallback, useRef, useState } from 'react';
import { useDispatch } from 'react-redux';
import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { isEmpty } from 'lodash';

import {
Expand Down Expand Up @@ -75,17 +75,17 @@ const InvestigateInTimelineActionComponent = (alertIds: string[]) => {
return [];
}
const alertResponse = await KibanaServices.get().http.fetch<
SearchResponse<{ '@timestamp': string; [key: string]: unknown }>
estypes.SearchResponse<{ '@timestamp': string; [key: string]: unknown }>
>(DETECTION_ENGINE_QUERY_SIGNALS_URL, {
method: 'POST',
body: JSON.stringify(buildAlertsQuery(fetchAlertIds ?? [])),
});
return (
alertResponse?.hits.hits.reduce<Ecs[]>(
(acc, { _id, _index, _source }) => [
(acc, { _id, _index, _source = {} }) => [
...acc,
{
...formatAlertToEcsSignal(_source as {}),
...formatAlertToEcsSignal(_source),
_id,
_index,
timestamp: _source['@timestamp'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { act, renderHook, RenderHookResult } from '@testing-library/react-hooks';
import type { estypes } from '@elastic/elasticsearch';
import { coreMock } from '../../../../../../../src/core/public/mocks';
import { KibanaServices } from '../../../common/lib/kibana';

Expand All @@ -28,7 +29,6 @@ import {
ReturnUseAddOrUpdateException,
AddOrUpdateExceptionItemsFunc,
} from './use_add_exception';
import { UpdateDocumentByQueryResponse } from 'elasticsearch';

const mockKibanaHttpService = coreMock.createStart().http;
const mockKibanaServices = KibanaServices.get as jest.Mock;
Expand All @@ -39,7 +39,7 @@ const fetchMock = jest.fn();
mockKibanaServices.mockReturnValue({ http: { fetch: fetchMock } });

describe('useAddOrUpdateException', () => {
let updateAlertStatus: jest.SpyInstance<Promise<UpdateDocumentByQueryResponse>>;
let updateAlertStatus: jest.SpyInstance<Promise<estypes.UpdateByQueryResponse>>;
let addExceptionListItem: jest.SpyInstance<Promise<ExceptionListItemSchema>>;
let updateExceptionListItem: jest.SpyInstance<Promise<ExceptionListItemSchema>>;
let getQueryFilter: jest.SpyInstance<ReturnType<typeof getQueryFilterHelper.getQueryFilter>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { useEffect, useRef, useState, useCallback } from 'react';
import { UpdateDocumentByQueryResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nifty

import type {
ExceptionListItemSchema,
CreateExceptionListItemSchema,
Expand Down Expand Up @@ -120,8 +120,8 @@ export const useAddOrUpdateException = ({

try {
setIsLoading(true);
let alertIdResponse: UpdateDocumentByQueryResponse | undefined;
let bulkResponse: UpdateDocumentByQueryResponse | undefined;
let alertIdResponse: estypes.UpdateByQueryResponse | undefined;
let bulkResponse: estypes.UpdateByQueryResponse | undefined;
if (alertIdToClose != null) {
alertIdResponse = await updateAlertStatus({
query: getUpdateAlertsQuery([alertIdToClose]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { UpdateDocumentByQueryResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { getCasesFromAlertsUrl } from '../../../../../../cases/common';
import { HostIsolationResponse, HostInfo } from '../../../../../common/endpoint/types';
import {
Expand Down Expand Up @@ -62,7 +62,7 @@ export const updateAlertStatus = async ({
query,
status,
signal,
}: UpdateAlertStatusProps): Promise<UpdateDocumentByQueryResponse> =>
}: UpdateAlertStatusProps): Promise<estypes.UpdateByQueryResponse> =>
KibanaServices.get().http.fetch(DETECTION_ENGINE_SIGNALS_STATUS_URL, {
method: 'POST',
body: JSON.stringify({ conflicts: 'proceed', status, ...query }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
* 2.0.
*/

import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { HostAuthenticationsStrategyResponse } from '../../../../common/search_strategy/security_solution/hosts/authentications';

export const mockData: { Authentications: HostAuthenticationsStrategyResponse } = {
Authentications: {
rawResponse: {
took: 880,
timed_out: false,
_shards: {
total: 26,
successful: 26,
skipped: 0,
failed: 0,
},
hits: {
total: 2,
hits: [],
},
aggregations: {
group_by_users: {
buckets: [
Expand All @@ -32,7 +44,7 @@ export const mockData: { Authentications: HostAuthenticationsStrategyResponse }
sum_other_doc_count: 566,
},
},
} as SearchResponse<unknown>,
} as estypes.SearchResponse<unknown>,
totalCount: 54,
edges: [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
* 2.0.
*/

import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { HostMetadata } from '../../../../../common/endpoint/types';

export function createV2SearchResponse(hostMetadata?: HostMetadata): SearchResponse<HostMetadata> {
export function createV2SearchResponse(
hostMetadata?: HostMetadata
): estypes.SearchResponse<HostMetadata> {
return ({
took: 15,
timed_out: false,
Expand Down Expand Up @@ -38,5 +40,5 @@ export function createV2SearchResponse(hostMetadata?: HostMetadata): SearchRespo
]
: [],
},
} as unknown) as SearchResponse<HostMetadata>;
} as unknown) as estypes.SearchResponse<HostMetadata>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
loggingSystemMock,
savedObjectsClientMock,
} from '../../../../../../../src/core/server/mocks';
import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { GetHostPolicyResponse, HostPolicyResponse } from '../../../../common/endpoint/types';
import { EndpointDocGenerator } from '../../../../common/endpoint/generate_data';
import { parseExperimentalConfigValue } from '../../../../common/experimental_features';
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('test policy response handler', () => {
*/
function createSearchResponse(
hostPolicyResponse?: HostPolicyResponse
): SearchResponse<HostPolicyResponse> {
): estypes.SearchResponse<HostPolicyResponse> {
return ({
took: 15,
timed_out: false,
Expand Down Expand Up @@ -267,5 +267,5 @@ function createSearchResponse(
]
: [],
},
} as unknown) as SearchResponse<HostPolicyResponse>;
} as unknown) as estypes.SearchResponse<HostPolicyResponse>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* 2.0.
*/

import { SearchRequest } from '@elastic/elasticsearch/api/types';
import { SearchResponse } from 'elasticsearch';
import type { estypes } from '@elastic/elasticsearch';
import { HostMetadata } from '../../../../common/endpoint/types';
import { SecuritySolutionRequestHandlerContext } from '../../../types';
import { getESQueryHostMetadataByIDs } from '../../routes/metadata/query_builders';
Expand All @@ -20,7 +19,7 @@ export async function getMetadataForEndpoints(
): Promise<HostMetadata[]> {
const query = getESQueryHostMetadataByIDs(endpointIDs);
const esClient = requestHandlerContext.core.elasticsearch.client.asCurrentUser;
const { body } = await esClient.search<HostMetadata>(query as SearchRequest);
const hosts = queryResponseToHostListResult(body as SearchResponse<HostMetadata>);
const { body } = await esClient.search<HostMetadata>(query as estypes.SearchRequest);
const hosts = queryResponseToHostListResult(body as estypes.SearchResponse<HostMetadata>);
return hosts.resultList;
}