Skip to content

Commit

Permalink
Move exceptions builder to lists plugin (#94002)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
marshallmain and kibanamachine committed Mar 9, 2021
1 parent ff2b3be commit 3992ed1
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 113 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import { chunk } from 'lodash/fp';

import { Filter } from '../../../../../src/plugins/data/common';
import {
ExceptionListItemSchema,
CreateExceptionListItemSchema,
EntryExists,
EntryMatch,
EntryMatchAny,
EntryNested,
ExceptionListItemSchema,
entriesExists,
entriesMatch,
entriesMatchAny,
entriesExists,
entriesNested,
EntryExists,
} from '../../../lists/common';
} from '../schemas';

import { BooleanFilter, NestedFilter } from './types';
import { hasLargeValueList } from './utils';

Expand Down Expand Up @@ -83,8 +84,8 @@ export const buildExceptionFilter = ({
const exceptionFilter: Filter = {
meta: {
alias: null,
negate: excludeExceptions,
disabled: false,
negate: excludeExceptions,
},
query: {
bool: {
Expand All @@ -108,8 +109,8 @@ export const buildExceptionFilter = ({
return {
meta: {
alias: null,
negate: false,
disabled: false,
negate: false,
},
query: {
bool: {
Expand All @@ -124,8 +125,8 @@ export const buildExceptionFilter = ({
return {
meta: {
alias: null,
negate: excludeExceptions,
disabled: false,
negate: excludeExceptions,
},
query: {
bool: {
Expand All @@ -148,14 +149,14 @@ export const buildMatchClause = (entry: EntryMatch): BooleanFilter => {
const { field, operator, value } = entry;
const matchClause = {
bool: {
minimum_should_match: 1,
should: [
{
match_phrase: {
[field]: value,
},
},
],
minimum_should_match: 1,
},
};

Expand All @@ -172,35 +173,35 @@ export const getBaseMatchAnyClause = (entry: EntryMatchAny): BooleanFilter => {
if (value.length === 1) {
return {
bool: {
minimum_should_match: 1,
should: [
{
match_phrase: {
[field]: value[0],
},
},
],
minimum_should_match: 1,
},
};
}

return {
bool: {
minimum_should_match: 1,
should: value.map((val) => {
return {
bool: {
minimum_should_match: 1,
should: [
{
match_phrase: {
[field]: val,
},
},
],
minimum_should_match: 1,
},
};
}),
minimum_should_match: 1,
},
};
};
Expand All @@ -220,14 +221,14 @@ export const buildExistsClause = (entry: EntryExists): BooleanFilter => {
const { field, operator } = entry;
const existsClause = {
bool: {
minimum_should_match: 1,
should: [
{
exists: {
field,
},
},
],
minimum_should_match: 1,
},
};

Expand Down
8 changes: 8 additions & 0 deletions x-pack/plugins/lists/common/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './build_exceptions_filter';
24 changes: 24 additions & 0 deletions x-pack/plugins/lists/common/exceptions/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*/

export interface BooleanFilter {
bool: {
must?: unknown | unknown[];
must_not?: unknown | unknown[];
should?: unknown[];
filter?: unknown | unknown[];
minimum_should_match?: number;
};
}

export interface NestedFilter {
nested: {
path: string;
query: unknown | unknown[];
score_mode: string;
};
}
24 changes: 24 additions & 0 deletions x-pack/plugins/lists/common/exceptions/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { CreateExceptionListItemSchema, EntriesArray, ExceptionListItemSchema } from '../schemas';

export const hasLargeValueItem = (
exceptionItems: Array<ExceptionListItemSchema | CreateExceptionListItemSchema>
): boolean => {
return exceptionItems.some((exceptionItem) => hasLargeValueList(exceptionItem.entries));
};

export const hasLargeValueList = (entries: EntriesArray): boolean => {
const found = entries.filter(({ type }) => type === 'list');
return found.length > 0;
};

export const hasNestedEntry = (entries: EntriesArray): boolean => {
const found = entries.filter(({ type }) => type === 'nested');
return found.length > 0;
};
2 changes: 2 additions & 0 deletions x-pack/plugins/lists/common/shared_exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ export {
OsTypeArray,
} from './schemas';

export { buildExceptionFilter } from './exceptions';

export { ENDPOINT_LIST_ID, ENDPOINT_TRUSTED_APPS_LIST_ID } from './constants';
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
CreateExceptionListItemSchema,
} from '../../../lists/common/schemas';
import { ESBoolQuery } from '../typed_json';
import { buildExceptionFilter } from './build_exceptions_filter';
import { buildExceptionFilter } from '../shared_imports';
import { Query, Language, Index, TimestampOverrideOrUndefined } from './schemas/common/schemas';

export const getQueryFilter = (
Expand Down
18 changes: 0 additions & 18 deletions x-pack/plugins/security_solution/common/detection_engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,3 @@ export interface EqlSearchResponse<T> {
events?: Array<BaseHit<T>>;
};
}

export interface BooleanFilter {
bool: {
must?: unknown | unknown[];
must_not?: unknown | unknown[];
should?: unknown[];
filter?: unknown | unknown[];
minimum_should_match?: number;
};
}

export interface NestedFilter {
nested: {
path: string;
query: unknown | unknown[];
score_mode: string;
};
}
1 change: 1 addition & 0 deletions x-pack/plugins/security_solution/common/shared_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ export {
ENDPOINT_TRUSTED_APPS_LIST_ID,
osTypeArray,
OsTypeArray,
buildExceptionFilter,
} from '../../lists/common';
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { RequestParams } from '@elastic/elasticsearch';

import { buildExceptionFilter } from '../../../common/detection_engine/build_exceptions_filter';
import { buildExceptionFilter } from '../../../common/shared_imports';
import { ExceptionListItemSchema } from '../../../../lists/common';
import { AnomalyRecordDoc as Anomaly } from '../../../../ml/server';
import { SearchResponse } from '../types';
Expand Down

0 comments on commit 3992ed1

Please sign in to comment.