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

[Custom threshold] Respect query:allowLeadingWildcards in optional query filter #189488

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ describe('The custom threshold alert type', () => {
stateResult2
);
expect(stateResult3.missingGroups).toEqual([{ key: 'b', bucketKey: { groupBy0: 'b' } }]);
expect(mockedEvaluateRule.mock.calls[2][9]).toEqual([
expect(mockedEvaluateRule.mock.calls[2][10]).toEqual([
{ bucketKey: { groupBy0: 'b' }, key: 'b' },
]);
});
Expand Down Expand Up @@ -2958,7 +2958,7 @@ describe('The custom threshold alert type', () => {
stateResult2
);
expect(stateResult3.missingGroups).toEqual([{ key: 'b', bucketKey: { groupBy0: 'b' } }]);
expect(mockedEvaluateRule.mock.calls[2][9]).toEqual([
expect(mockedEvaluateRule.mock.calls[2][10]).toEqual([
{ bucketKey: { groupBy0: 'b' }, key: 'b' },
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const createCustomThresholdExecutor = ({
executionId,
});

const { searchSourceClient, alertsClient } = services;
const { searchSourceClient, alertsClient, uiSettingsClient } = services;

if (!alertsClient) {
throw new AlertsClientError();
Expand Down Expand Up @@ -143,6 +143,7 @@ export const createCustomThresholdExecutor = ({
alertOnGroupDisappear,
logger,
{ end: dateEnd, start: dateStart },
uiSettingsClient,
state.lastRunTimestamp,
previousMissingGroups
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
* 2.0.
*/

import { ElasticsearchClient } from '@kbn/core/server';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import type { ElasticsearchClient } from '@kbn/core/server';
import type { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import type { EsQueryConfig } from '@kbn/es-query';
import type { Logger } from '@kbn/logging';
import { isString, get, identity } from 'lodash';
import {
Expand All @@ -30,6 +31,7 @@ export const checkMissingGroups = async (
searchConfiguration: SearchConfigurationType,
logger: Logger,
timeframe: { start: number; end: number },
esQueryConfig: EsQueryConfig,
missingGroups: MissingGroupsRecord[] = []
): Promise<MissingGroupsRecord[]> => {
if (missingGroups.length === 0) {
Expand All @@ -52,8 +54,10 @@ export const checkMissingGroups = async (
currentTimeFrame,
timeFieldName,
searchConfiguration,
esQueryConfig,
groupByQueries
);

return [
{ index: indexPattern },
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import moment from 'moment';
import { ElasticsearchClient } from '@kbn/core/server';
import type { ElasticsearchClient, IUiSettingsClient } from '@kbn/core/server';
import type { Logger } from '@kbn/logging';
import { getEsQueryConfig } from '../../../../utils/get_es_query_config';
import { getIntervalInSeconds } from '../../../../../common/utils/get_interval_in_seconds';
import {
Aggregators,
Expand Down Expand Up @@ -43,10 +44,12 @@ export const evaluateRule = async <Params extends EvaluatedRuleParams = Evaluate
alertOnGroupDisappear: boolean,
logger: Logger,
timeframe: { start: string; end: string },
uiSettings: IUiSettingsClient,
lastPeriodEnd?: number,
missingGroups: MissingGroupsRecord[] = []
): Promise<Array<Record<string, Evaluation>>> => {
const { criteria, groupBy, searchConfiguration } = params;
const esQueryConfig = await getEsQueryConfig(uiSettings);

return Promise.all(
criteria.map(async (criterion) => {
Expand All @@ -70,6 +73,7 @@ export const evaluateRule = async <Params extends EvaluatedRuleParams = Evaluate
timeFieldName,
groupBy,
searchConfiguration,
esQueryConfig,
compositeSize,
alertOnGroupDisappear,
calculatedTimerange,
Expand All @@ -86,6 +90,7 @@ export const evaluateRule = async <Params extends EvaluatedRuleParams = Evaluate
searchConfiguration,
logger,
calculatedTimerange,
esQueryConfig,
missingGroups
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import type { SearchResponse, AggregationsAggregate } from '@elastic/elasticsearch/lib/api/types';
import type { ElasticsearchClient } from '@kbn/core/server';
import type { EsQueryConfig } from '@kbn/es-query';
import type { Logger } from '@kbn/logging';
import type { EcsFieldsResponse } from '@kbn/rule-registry-plugin/common';
import type {
Expand Down Expand Up @@ -104,6 +105,7 @@ export const getData = async (
timeFieldName: string,
groupBy: string | undefined | string[],
searchConfiguration: SearchConfigurationType,
esQueryConfig: EsQueryConfig,
compositeSize: number,
alertOnGroupDisappear: boolean,
timeframe: { start: number; end: number },
Expand Down Expand Up @@ -163,6 +165,7 @@ export const getData = async (
timeFieldName,
groupBy,
searchConfiguration,
esQueryConfig,
compositeSize,
alertOnGroupDisappear,
timeframe,
Expand Down Expand Up @@ -205,6 +208,7 @@ export const getData = async (
compositeSize,
alertOnGroupDisappear,
searchConfiguration,
esQueryConfig,
lastPeriodEnd,
groupBy,
afterKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => {
query: '',
},
};
const esQueryConfig = {
allowLeadingWildcards: false,
queryStringOptions: {},
ignoreFilterIfFieldNotInIndex: false,
};

const groupBy = 'host.doggoname';
const timeFieldName = 'mockedTimeFieldName';
Expand All @@ -58,6 +63,7 @@ describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => {
100,
true,
searchConfiguration,
esQueryConfig,
void 0,
groupBy
);
Expand Down Expand Up @@ -114,6 +120,7 @@ describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => {
100,
true,
currentSearchConfiguration,
esQueryConfig,
void 0,
groupBy
);
Expand Down Expand Up @@ -225,6 +232,7 @@ describe("The Metric Threshold Alert's getElasticsearchMetricQuery", () => {
100,
true,
currentSearchConfiguration,
esQueryConfig,
void 0,
groupBy
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import moment from 'moment';
import { QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types';
import { Filter } from '@kbn/es-query';
import type { EsQueryConfig, Filter } from '@kbn/es-query';
import {
Aggregators,
CustomMetricExpressionParams,
Expand Down Expand Up @@ -52,6 +52,7 @@ export const createBoolQuery = (
timeframe: { start: number; end: number },
timeFieldName: string,
searchConfiguration: SearchConfigurationType,
esQueryConfig: EsQueryConfig,
additionalQueries: QueryDslQueryContainer[] = []
) => {
const rangeQuery: QueryDslQueryContainer = {
Expand All @@ -64,7 +65,7 @@ export const createBoolQuery = (
};
const filters = QueryDslQueryContainerToFilter([rangeQuery, ...additionalQueries]);

return getSearchConfigurationBoolQuery(searchConfiguration, filters);
return getSearchConfigurationBoolQuery(searchConfiguration, filters, esQueryConfig);
};

export const getElasticsearchMetricQuery = (
Expand All @@ -74,6 +75,7 @@ export const getElasticsearchMetricQuery = (
compositeSize: number,
alertOnGroupDisappear: boolean,
searchConfiguration: SearchConfigurationType,
esQueryConfig: EsQueryConfig,
lastPeriodEnd?: number,
groupBy?: string | string[],
afterKey?: Record<string, string>,
Expand Down Expand Up @@ -204,7 +206,7 @@ export const getElasticsearchMetricQuery = (
aggs.groupings.composite.after = afterKey;
}

const query = createBoolQuery(timeframe, timeFieldName, searchConfiguration);
const query = createBoolQuery(timeframe, timeFieldName, searchConfiguration, esQueryConfig);

return {
track_total_hits: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { uiSettingsServiceMock } from '@kbn/core-ui-settings-server-mocks';
import { getEsQueryConfig } from './get_es_query_config';

describe('getEsQueryConfig', () => {
const uiSettingsClient = uiSettingsServiceMock.createClient();

it('should get the es query config correctly', async () => {
const settings = await getEsQueryConfig(uiSettingsClient);

expect(settings).toEqual({
allowLeadingWildcards: false,
ignoreFilterIfFieldNotInIndex: false,
queryStringOptions: false,
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { IUiSettingsClient } from '@kbn/core/server';
import { UI_SETTINGS } from '@kbn/data-plugin/server';

export async function getEsQueryConfig(uiSettings: IUiSettingsClient) {
const allowLeadingWildcards = await uiSettings.get(UI_SETTINGS.QUERY_ALLOW_LEADING_WILDCARDS);
const queryStringOptions = await uiSettings.get(UI_SETTINGS.QUERY_STRING_OPTIONS);
const ignoreFilterIfFieldNotInIndex = await uiSettings.get(
UI_SETTINGS.COURIER_IGNORE_FILTER_IF_FIELD_NOT_IN_INDEX
);

return {
allowLeadingWildcards,
queryStringOptions,
ignoreFilterIfFieldNotInIndex,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
* 2.0.
*/

import Boom from '@hapi/boom';
import {
BoolQuery,
buildEsQuery,
EsQueryConfig,
Filter,
fromKueryExpression,
toElasticsearchQuery,
Expand All @@ -23,27 +25,21 @@ export const getParsedFilterQuery: (filter: string | undefined) => Array<Record<
const parsedQuery = toElasticsearchQuery(fromKueryExpression(filter));
return [parsedQuery];
} catch (error) {
return [];
throw Boom.badRequest(`Invalid filter query: ${error.message}`);
}
};

export const getSearchConfigurationBoolQuery: (
searchConfiguration: SearchConfigurationType,
additionalFilters: Filter[]
) => { bool: BoolQuery } = (searchConfiguration, additionalFilters) => {
additionalFilters: Filter[],
esQueryConfig?: EsQueryConfig
) => { bool: BoolQuery } = (searchConfiguration, additionalFilters, esQueryConfig) => {
try {
const searchConfigurationFilters = (searchConfiguration.filter as Filter[]) || [];
const filters = [...additionalFilters, ...searchConfigurationFilters];

return buildEsQuery(undefined, searchConfiguration.query, filters, {});
return buildEsQuery(undefined, searchConfiguration.query, filters, esQueryConfig);
} catch (error) {
return {
bool: {
must: [],
must_not: [],
filter: [],
should: [],
},
};
throw Boom.badRequest(`Invalid search query: ${error.message}`);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"@kbn/slo-schema",
"@kbn/license-management-plugin",
"@kbn/observability-alerting-rule-utils",
"@kbn/core-ui-settings-server-mocks",
],
"exclude": [
"target/**/*"
Expand Down