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

Validate EQL search strategy requests without ignore client option #169190

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions src/plugins/data/common/search/strategies/eql_search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@
*/

import type { EqlSearchRequest } from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { TransportResult, TransportRequestOptions } from '@elastic/elasticsearch';
import type { TransportResult } from '@elastic/elasticsearch';

import { IKibanaSearchRequest, IKibanaSearchResponse } from '../../types';

export const EQL_SEARCH_STRATEGY = 'eql';

export type EqlRequestParams = EqlSearchRequest;

export interface EqlSearchStrategyRequest extends IKibanaSearchRequest<EqlRequestParams> {
/**
* @deprecated: use IAsyncSearchOptions.transport instead.
*/
options?: TransportRequestOptions;
export interface EqlRequestParams extends EqlSearchRequest {
validate?: boolean;
}

export type EqlSearchStrategyRequest = IKibanaSearchRequest<EqlRequestParams>;

export type EqlSearchStrategyResponse<T = unknown> = IKibanaSearchResponse<TransportResult<T>>;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import {
IAsyncSearchOptions,
pollSearch,
} from '../../../../common';
import { toEqlKibanaSearchResponse } from './response_utils';
import {
errorToEqlKibanaSearchResponse,
isEqlValidationResponseError,
toEqlKibanaSearchResponse,
} from './response_utils';
import { EqlSearchResponse } from './types';
import { ISearchStrategy } from '../../types';
import { getDefaultSearchParams } from '../es_search';
Expand Down Expand Up @@ -47,6 +51,7 @@ export const eqlSearchStrategyProvider = (
const { track_total_hits: _, ...defaultParams } = await getDefaultSearchParams(
uiSettingsClient
);
const { validate: isValidationRequest, ...eqlParams } = request.params ?? {};
const params = id
? getCommonDefaultAsyncGetParams(searchConfig, options, {
/* disable until full eql support */ disableSearchSessions: true,
Expand All @@ -57,27 +62,33 @@ export const eqlSearchStrategyProvider = (
...getCommonDefaultAsyncGetParams(searchConfig, options, {
/* disable until full eql support */ disableSearchSessions: true,
}),
...request.params,
...eqlParams,
};
const response = id
? await client.get(
{ ...params, id },
{
...request.options,

try {
const response = id
? await client.get(
{ ...params, id },
{
...options.transport,
signal: options.abortSignal,
meta: true,
}
)
: // @ts-expect-error optional key cannot be used since search doesn't expect undefined
await client.search(params, {
...options.transport,
signal: options.abortSignal,
meta: true,
}
)
: // @ts-expect-error optional key cannot be used since search doesn't expect undefined
await client.search(params as EqlSearchStrategyRequest['params'], {
...request.options,
...options.transport,
signal: options.abortSignal,
meta: true,
});

return toEqlKibanaSearchResponse(response as TransportResult<EqlSearchResponse>);
});
return toEqlKibanaSearchResponse(response as TransportResult<EqlSearchResponse>);
} catch (e) {
if (isValidationRequest && isEqlValidationResponseError(e)) {
return errorToEqlKibanaSearchResponse(e);
} else {
throw e;
}
}
};

const cancel = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Side Public License, v 1.
*/

import type { TransportResult } from '@elastic/elasticsearch';
import { get } from 'lodash';
import { errors, TransportResult } from '@elastic/elasticsearch';
import { EqlSearchResponse } from './types';
import { EqlSearchStrategyResponse } from '../../../../common';

Expand All @@ -24,3 +25,40 @@ export function toEqlKibanaSearchResponse(
isRunning: response.body.is_running,
};
}

export function errorToEqlKibanaSearchResponse(
response: EqlResponseError
): EqlSearchStrategyResponse {
return {
id: undefined,
rawResponse: response.meta,
isPartial: false,
isRunning: false,
};
}

const PARSING_ERROR_TYPE = 'parsing_exception';
const VERIFICATION_ERROR_TYPE = 'verification_exception';
const MAPPING_ERROR_TYPE = 'mapping_exception';

interface ErrorCause {
type: string;
reason: string;
}

interface EqlErrorBody {
error: ErrorCause & { root_cause: ErrorCause[] };
}

export interface EqlResponseError extends errors.ResponseError {
meta: TransportResult<EqlErrorBody>;
}

const isValidationErrorType = (type: unknown): boolean =>
type === PARSING_ERROR_TYPE || type === VERIFICATION_ERROR_TYPE || type === MAPPING_ERROR_TYPE;

export const isEqlResponseError = (response: unknown): response is EqlResponseError =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A few of these methods are unique to this situation, but most of the code here is identical to that in common security_solution code (which also exists in timelines 😭 ). Is this the right time for a package?

response instanceof errors.ResponseError;

export const isEqlValidationResponseError = (response: unknown): response is EqlResponseError =>
isEqlResponseError(response) && isValidationErrorType(get(response, 'meta.body.error.type'));
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const validateEql = async ({
params: {
index: dataViewTitle,
body: { query, runtime_mappings: runtimeMappings, size: 0 },
validate: true,
},
options: { ignore: [400] },
},
{
strategy: EQL_SEARCH_STRATEGY,
Expand Down