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

[RAC][Security Solution] Pull Gap Remediation out of search_after_bulk_create #102104

Merged
merged 15 commits into from
Jun 16, 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import dateMath from '@elastic/datemath';
import { loggingSystemMock } from 'src/core/server/mocks';
import { alertsMock, AlertServicesMock } from '../../../../../../alerting/server/mocks';
import { eqlExecutor } from './eql';
Expand All @@ -23,6 +24,7 @@ describe('eql_executor', () => {
let logger: ReturnType<typeof loggingSystemMock.createLogger>;
let alertServices: AlertServicesMock;
(getIndexVersion as jest.Mock).mockReturnValue(SIGNALS_TEMPLATE_VERSION);
const params = getEqlRuleParams();
const eqlSO = {
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd',
type: 'alert',
Expand All @@ -40,10 +42,15 @@ describe('eql_executor', () => {
interval: '5m',
},
throttle: 'no_actions',
params: getEqlRuleParams(),
params,
},
references: [],
};
const tuple = {
from: dateMath.parse(params.from)!,
to: dateMath.parse(params.to)!,
Copy link
Contributor

@FrankHassanabad FrankHassanabad Jun 21, 2021

Choose a reason for hiding this comment

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

I know this is merged but instead of turning off the checks here can we do a small follow up where we do this similar pattern here that @marshallmain did a while back?

https://github.com/elastic/kibana/blob/master/x-pack/plugins/security_solution/server/lib/detection_engine/signals/utils.ts#L468

I think that would be good to intentionally throw if for some reason these aren't parseable rather than turning off the typescript check for it.

Later if refactoring or mistakes are made and we get an SDH or issue it would be easier to track down where and what happened from a custom error message than somewhere else where the from and to have become null/undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@FrankHassanabad Is this necessary in test files? Looks like we're only doing this in the test files... the code/test will fail when the check is done here, right? https://github.com/elastic/kibana/blob/master/x-pack/plugins/security_solution/server/lib/detection_engine/signals/search_after_bulk_create.ts#L52-L58

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I didn't see it was in a test file. For test files it's optional, I typically still avoid it if I can even in test files, but that's just me probably because I really don't like that TypeScript allows type assertions to be turned off compared to other languages with strict types.

maxSignals: params.maxSignals,
};
const searchAfterSize = 7;

beforeEach(() => {
Expand All @@ -64,6 +71,7 @@ describe('eql_executor', () => {
const exceptionItems = [getExceptionListItemSchemaMock({ entries: [getEntryListMock()] })];
const response = await eqlExecutor({
rule: eqlSO,
tuple,
exceptionItems,
services: alertServices,
version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import {
AlertAttributes,
BulkCreate,
EqlSignalSearchResponse,
RuleRangeTuple,
SearchAfterAndBulkCreateReturnType,
WrappedSignalHit,
} from '../types';
import { createSearchAfterReturnType, makeFloatString, wrapSignal } from '../utils';

export const eqlExecutor = async ({
rule,
tuple,
exceptionItems,
services,
version,
Expand All @@ -43,6 +45,7 @@ export const eqlExecutor = async ({
bulkCreate,
}: {
rule: SavedObject<AlertAttributes<EqlRuleParams>>;
tuple: RuleRangeTuple;
exceptionItems: ExceptionListItemSchema[];
services: AlertServices<AlertInstanceState, AlertInstanceContext, 'default'>;
version: string;
Expand Down Expand Up @@ -81,8 +84,8 @@ export const eqlExecutor = async ({
const request = buildEqlSearchRequest(
ruleParams.query,
inputIndex,
ruleParams.from,
ruleParams.to,
tuple.from.toISOString(),
tuple.to.toISOString(),
searchAfterSize,
ruleParams.timestampOverride,
exceptionItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import dateMath from '@elastic/datemath';
import { loggingSystemMock } from 'src/core/server/mocks';
import { alertsMock, AlertServicesMock } from '../../../../../../alerting/server/mocks';
import { mlExecutor } from './ml';
Expand All @@ -26,7 +27,13 @@ describe('ml_executor', () => {
const exceptionItems = [getExceptionListItemSchemaMock()];
let logger: ReturnType<typeof loggingSystemMock.createLogger>;
let alertServices: AlertServicesMock;
const mlSO = sampleRuleSO(getMlRuleParams());
const params = getMlRuleParams();
const mlSO = sampleRuleSO(params);
const tuple = {
from: dateMath.parse(params.from)!,
to: dateMath.parse(params.to)!,
maxSignals: params.maxSignals,
};
const buildRuleMessage = buildRuleMessageFactory({
id: mlSO.id,
ruleId: mlSO.attributes.params.ruleId,
Expand Down Expand Up @@ -60,6 +67,7 @@ describe('ml_executor', () => {
await expect(
mlExecutor({
rule: mlSO,
tuple,
ml: undefined,
exceptionItems,
services: alertServices,
Expand All @@ -76,6 +84,7 @@ describe('ml_executor', () => {
jobsSummaryMock.mockResolvedValue([]);
const response = await mlExecutor({
rule: mlSO,
tuple,
ml: mlMock,
exceptionItems,
services: alertServices,
Expand All @@ -101,6 +110,7 @@ describe('ml_executor', () => {

const response = await mlExecutor({
rule: mlSO,
tuple,
ml: mlMock,
exceptionItems,
services: alertServices,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import { bulkCreateMlSignals } from '../bulk_create_ml_signals';
import { filterEventsAgainstList } from '../filters/filter_events_against_list';
import { findMlSignals } from '../find_ml_signals';
import { BuildRuleMessage } from '../rule_messages';
import { AlertAttributes, BulkCreate, WrapHits } from '../types';
import { AlertAttributes, BulkCreate, RuleRangeTuple, WrapHits } from '../types';
import { createErrorsFromShard, createSearchAfterReturnType, mergeReturns } from '../utils';

export const mlExecutor = async ({
rule,
tuple,
ml,
listClient,
exceptionItems,
Expand All @@ -36,6 +37,7 @@ export const mlExecutor = async ({
wrapHits,
}: {
rule: SavedObject<AlertAttributes<MachineLearningRuleParams>>;
tuple: RuleRangeTuple;
ml: SetupPlugins['ml'];
listClient: ListClient;
exceptionItems: ExceptionListItemSchema[];
Expand Down Expand Up @@ -88,8 +90,8 @@ export const mlExecutor = async ({
savedObjectsClient: services.savedObjectsClient,
jobIds: ruleParams.machineLearningJobId,
anomalyThreshold: ruleParams.anomalyThreshold,
from: ruleParams.from,
to: ruleParams.to,
from: tuple.from.toISOString(),
to: tuple.to.toISOString(),
exceptionItems,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { QueryRuleParams, SavedQueryRuleParams } from '../../schemas/rule_schema

export const queryExecutor = async ({
rule,
tuples,
tuple,
listClient,
exceptionItems,
services,
Expand All @@ -37,7 +37,7 @@ export const queryExecutor = async ({
wrapHits,
}: {
rule: SavedObject<AlertAttributes<QueryRuleParams | SavedQueryRuleParams>>;
tuples: RuleRangeTuple[];
tuple: RuleRangeTuple;
listClient: ListClient;
exceptionItems: ExceptionListItemSchema[];
services: AlertServices<AlertInstanceState, AlertInstanceContext, 'default'>;
Expand All @@ -63,7 +63,7 @@ export const queryExecutor = async ({
});

return searchAfterAndBulkCreate({
tuples,
tuple,
listClient,
exceptionsList: exceptionItems,
ruleSO: rule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { ThreatRuleParams } from '../../schemas/rule_schemas';

export const threatMatchExecutor = async ({
rule,
tuples,
tuple,
listClient,
exceptionItems,
services,
Expand All @@ -36,7 +36,7 @@ export const threatMatchExecutor = async ({
wrapHits,
}: {
rule: SavedObject<AlertAttributes<ThreatRuleParams>>;
tuples: RuleRangeTuple[];
tuple: RuleRangeTuple;
listClient: ListClient;
exceptionItems: ExceptionListItemSchema[];
services: AlertServices<AlertInstanceState, AlertInstanceContext, 'default'>;
Expand All @@ -51,7 +51,7 @@ export const threatMatchExecutor = async ({
const ruleParams = rule.attributes.params;
const inputIndex = await getInputIndex(services, version, ruleParams.index);
return createThreatSignals({
tuples,
tuple,
threatMapping: ruleParams.threatMapping,
query: ruleParams.query,
inputIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
* 2.0.
*/

import dateMath from '@elastic/datemath';
import { loggingSystemMock } from 'src/core/server/mocks';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks';
import { alertsMock, AlertServicesMock } from '../../../../../../alerting/server/mocks';
import { thresholdExecutor } from './threshold';
import { getExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/exception_list_item_schema.mock';
import { getEntryListMock } from '../../../../../../lists/common/schemas/types/entry_list.mock';
import { getThresholdRuleParams } from '../../schemas/rule_schemas.mock';
import { buildRuleMessageFactory } from '../rule_messages';
import { sampleEmptyDocSearchResults } from '../__mocks__/es_results';

describe('threshold_executor', () => {
const version = '8.0.0';
let logger: ReturnType<typeof loggingSystemMock.createLogger>;
let alertServices: AlertServicesMock;
const params = getThresholdRuleParams();
const thresholdSO = {
id: '04128c15-0d1b-4716-a4c5-46997ac7f3bd',
type: 'alert',
Expand All @@ -34,10 +39,15 @@ describe('threshold_executor', () => {
interval: '5m',
},
throttle: 'no_actions',
params: getThresholdRuleParams(),
params,
},
references: [],
};
const tuple = {
from: dateMath.parse(params.from)!,
to: dateMath.parse(params.to)!,
maxSignals: params.maxSignals,
};
const buildRuleMessage = buildRuleMessageFactory({
id: thresholdSO.id,
ruleId: thresholdSO.attributes.params.ruleId,
Expand All @@ -47,6 +57,9 @@ describe('threshold_executor', () => {

beforeEach(() => {
alertServices = alertsMock.createAlertServices();
alertServices.scopedClusterClient.asCurrentUser.search.mockResolvedValue(
elasticsearchClientMock.createSuccessTransportRequestPromise(sampleEmptyDocSearchResults())
);
logger = loggingSystemMock.createLogger();
});

Expand All @@ -55,14 +68,20 @@ describe('threshold_executor', () => {
const exceptionItems = [getExceptionListItemSchemaMock({ entries: [getEntryListMock()] })];
const response = await thresholdExecutor({
rule: thresholdSO,
tuples: [],
tuple,
exceptionItems,
services: alertServices,
version,
logger,
buildRuleMessage,
startedAt: new Date(),
bulkCreate: jest.fn(),
bulkCreate: jest.fn().mockImplementation((hits) => ({
errors: [],
success: true,
bulkCreateDuration: '0',
createdItemsCount: 0,
createdItems: [],
})),
wrapHits: jest.fn(),
});
expect(response.warningMessages.length).toEqual(1);
Expand Down
Loading