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] Move exceptions builder to lists plugin #94002

Merged
merged 3 commits into from
Mar 9, 2021
Merged
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

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';
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should make more things available through the plugin so for example here we'd use the lists client and do something like lists.queryBuilder. Just a thought, nothing to do in this PR.

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