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][Alerts] Use RuleDataReader to query for threshold alert history #129763

Merged
merged 3 commits into from
Apr 12, 2022
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 @@ -261,6 +261,7 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
tuple,
wrapHits,
wrapSequences,
ruleDataReader: ruleDataClient.getReader({ namespace: options.spaceId }),
},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,15 @@ export const createThresholdAlertType = (
producer: SERVER_APP_ID,
async executor(execOptions) {
const {
runOpts: { buildRuleMessage, bulkCreate, exceptionItems, completeRule, tuple, wrapHits },
runOpts: {
buildRuleMessage,
bulkCreate,
exceptionItems,
completeRule,
tuple,
wrapHits,
ruleDataReader,
},
services,
startedAt,
state,
Expand All @@ -70,6 +78,7 @@ export const createThresholdAlertType = (
tuple,
version,
wrapHits,
ruleDataReader,
});

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import {
WithoutReservedActionGroups,
} from '../../../../../alerting/common';
import { ListClient } from '../../../../../lists/server';
import { PersistenceServices, IRuleDataClient } from '../../../../../rule_registry/server';
import {
PersistenceServices,
IRuleDataClient,
IRuleDataReader,
} from '../../../../../rule_registry/server';
import { ConfigType } from '../../../config';
import { SetupPlugins } from '../../../plugin';
import { CompleteRule, RuleParams } from '../schemas/rule_schemas';
Expand Down Expand Up @@ -61,6 +65,7 @@ export interface RunOpts<TParams extends RuleParams> {
};
wrapHits: WrapHits;
wrapSequences: WrapSequences;
ruleDataReader: IRuleDataReader;
}

export type SecurityAlertType<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { buildRuleMessageFactory } from '../rule_messages';
import { sampleEmptyDocSearchResults } from '../__mocks__/es_results';
import { allowedExperimentalValues } from '../../../../../common/experimental_features';
import { ThresholdRuleParams } from '../../schemas/rule_schemas';
import { createRuleDataClientMock } from '../../../../../../rule_registry/server/rule_data_client/rule_data_client.mock';

describe('threshold_executor', () => {
const version = '8.0.0';
Expand Down Expand Up @@ -49,6 +50,7 @@ describe('threshold_executor', () => {

describe('thresholdExecutor', () => {
it('should set a warning when exception list for threshold rule contains value list exceptions', async () => {
const ruleDataClientMock = createRuleDataClientMock();
const exceptionItems = [getExceptionListItemSchemaMock({ entries: [getEntryListMock()] })];
const response = await thresholdExecutor({
completeRule: thresholdCompleteRule,
Expand All @@ -69,6 +71,7 @@ describe('threshold_executor', () => {
createdItems: [],
})),
wrapHits: jest.fn(),
ruleDataReader: ruleDataClientMock.getReader({ namespace: 'default' }),
});
expect(response.warningMessages.length).toEqual(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { BuildRuleMessage } from '../rule_messages';
import { ExperimentalFeatures } from '../../../../../common/experimental_features';
import { withSecuritySpan } from '../../../../utils/with_security_span';
import { buildThresholdSignalHistory } from '../threshold/build_signal_history';
import { IRuleDataReader } from '../../../../../../rule_registry/server';

export const thresholdExecutor = async ({
completeRule,
Expand All @@ -55,6 +56,7 @@ export const thresholdExecutor = async ({
state,
bulkCreate,
wrapHits,
ruleDataReader,
}: {
completeRule: CompleteRule<ThresholdRuleParams>;
tuple: RuleRangeTuple;
Expand All @@ -68,6 +70,7 @@ export const thresholdExecutor = async ({
state: ThresholdAlertState;
bulkCreate: BulkCreate;
wrapHits: WrapHits;
ruleDataReader: IRuleDataReader;
}): Promise<SearchAfterAndBulkCreateReturnType & { state: ThresholdAlertState }> => {
let result = createSearchAfterReturnType();
const ruleParams = completeRule.ruleParams;
Expand All @@ -77,15 +80,11 @@ export const thresholdExecutor = async ({
const { signalHistory, searchErrors: previousSearchErrors } = state.initialized
? { signalHistory: state.signalHistory, searchErrors: [] }
: await getThresholdSignalHistory({
indexPattern: ['*'], // TODO: get outputIndex?
from: tuple.from.toISOString(),
to: tuple.to.toISOString(),
services,
logger,
ruleId: ruleParams.ruleId,
bucketByFields: ruleParams.threshold.field,
timestampOverride: ruleParams.timestampOverride,
buildRuleMessage,
ruleDataReader,
});

if (!state.initialized) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 { buildPreviousThresholdAlertRequest } from './get_threshold_signal_history';

describe('buildPreviousThresholdAlertRequest', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

it('should generate a proper request when bucketByFields is empty', async () => {
const bucketByFields: string[] = [];
const to = 'now';
const from = 'now-6m';
const ruleId = 'threshold-rule';

expect(
buildPreviousThresholdAlertRequest({ from, to, ruleId, bucketByFields })
).toMatchSnapshot();
});

it('should generate a proper request when bucketByFields contains multiple fields', async () => {
const bucketByFields: string[] = ['host.name', 'user.name'];
const to = 'now';
const from = 'now-6m';
const ruleId = 'threshold-rule';

expect(
buildPreviousThresholdAlertRequest({ from, to, ruleId, bucketByFields })
).toMatchSnapshot();
});
});
Loading