Skip to content

Commit

Permalink
[Security Solution][Detection Engine] removes field_caps call for all…
Browse files Browse the repository at this point in the history
… fields in index during rule execution (elastic#193869)

## Summary

- addresses elastic#187059

### Checklist

Delete any items that are not applicable to this PR.


- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
vitaliidm and kibanamachine authored Sep 30, 2024
1 parent 49661a3 commit b005ea9
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import agent from 'elastic-apm-node';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import { TIMESTAMP } from '@kbn/rule-data-utils';
import { createPersistenceRuleTypeWrapper } from '@kbn/rule-registry-plugin/server';
import type { DataViewFieldBase } from '@kbn/es-query';
import { buildExceptionFilter } from '@kbn/lists-plugin/server/services/exception_lists';
import { technicalRuleFieldMap } from '@kbn/rule-registry-plugin/common/assets/field_maps/technical_rule_field_map';
import type { FieldMap } from '@kbn/alerts-as-data-utils';
Expand All @@ -26,8 +25,6 @@ import {
hasTimestampFields,
isMachineLearningParams,
isEsqlParams,
isQueryParams,
isEqlParams,
getDisabledActionsWarningText,
} from './utils/utils';
import { DEFAULT_MAX_SIGNALS, DEFAULT_SEARCH_AFTER_PAGE_SIZE } from '../../../../common/constants';
Expand All @@ -47,7 +44,6 @@ import { withSecuritySpan } from '../../../utils/with_security_span';
import { getInputIndex, DataViewError } from './utils/get_input_output_index';
import { TIMESTAMP_RUNTIME_FIELD } from './constants';
import { buildTimestampRuntimeMapping } from './utils/build_timestamp_runtime_mapping';
import { getFieldsForWildcard } from './utils/get_fields_for_wildcard';
import { alertsFieldMap, rulesFieldMap } from '../../../../common/field_maps';
import { sendAlertSuppressionTelemetryEvent } from './utils/telemetry/send_alert_suppression_telemetry_event';

Expand Down Expand Up @@ -139,7 +135,6 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
} = options;
let runState = state;
let inputIndex: string[] = [];
let inputIndexFields: DataViewFieldBase[] = [];
let runtimeMappings: estypes.MappingRuntimeFields | undefined;
const { from, maxSignals, timestampOverride, timestampOverrideFallbackDisabled, to } =
params;
Expand Down Expand Up @@ -337,21 +332,6 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
});
}

if (
!isMachineLearningParams(params) &&
!isEsqlParams(params) &&
!isQueryParams(params) &&
!isEqlParams(params)
) {
const dataViews = await services.getDataViews();
inputIndexFields = await getFieldsForWildcard({
index: inputIndex,
dataViews,
language: params.language,
ruleExecutionLogger,
});
}

try {
const { listClient, exceptionsClient } = getListClient({
esClient: services.scopedClusterClient.asCurrentUser,
Expand Down Expand Up @@ -426,7 +406,6 @@ export const createSecurityRuleTypeWrapper: CreateSecurityRuleTypeWrapper =
inputIndex,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
runtimeMappings: {
...runtimeMappings,
...timestampRuntimeMappings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export const createIndicatorMatchAlertType = (
secondaryTimestamp,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
},
services,
spaceId,
Expand Down Expand Up @@ -119,7 +118,6 @@ export const createIndicatorMatchAlertType = (
secondaryTimestamp,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
wrapSuppressedHits,
runOpts,
licensing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type {
RuleExecutorServices,
} from '@kbn/alerting-plugin/server';
import type { ListClient } from '@kbn/lists-plugin/server';
import type { Filter, DataViewFieldBase } from '@kbn/es-query';
import type { Filter } from '@kbn/es-query';
import type { RuleRangeTuple, BulkCreate, WrapHits, WrapSuppressedHits, RunOpts } from '../types';
import type { ITelemetryEventsSender } from '../../../telemetry/sender';
import { createThreatSignals } from './threat_mapping/create_threat_signals';
Expand Down Expand Up @@ -43,7 +43,6 @@ export const indicatorMatchExecutor = async ({
secondaryTimestamp,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
wrapSuppressedHits,
runOpts,
licensing,
Expand All @@ -65,7 +64,6 @@ export const indicatorMatchExecutor = async ({
secondaryTimestamp?: string;
exceptionFilter: Filter | undefined;
unprocessedExceptions: ExceptionListItemSchema[];
inputIndexFields: DataViewFieldBase[];
wrapSuppressedHits: WrapSuppressedHits;
runOpts: RunOpts<ThreatRuleParams>;
licensing: LicensingPluginSetup;
Expand Down Expand Up @@ -106,7 +104,6 @@ export const indicatorMatchExecutor = async ({
secondaryTimestamp,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
runOpts,
licensing,
experimentalFeatures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const createEventSignal = async ({
index: inputIndex,
exceptionFilter,
fields: inputIndexFields,
loadFields: true,
});

ruleExecutionLogger.debug(`${ids?.length} matched signals found`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const createThreatSignal = async ({
index: inputIndex,
exceptionFilter,
fields: inputIndexFields,
loadFields: true,
});

ruleExecutionLogger.debug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import { getEventCount, getEventList } from './get_event_count';
import { getMappingFilters } from './get_mapping_filters';
import { THREAT_PIT_KEEP_ALIVE } from '../../../../../../common/cti/constants';
import { getMaxSignalsWarning, getSafeSortIds } from '../../utils/utils';
import { getFieldsForWildcard } from '../../utils/get_fields_for_wildcard';
import { getDataTierFilter } from '../../utils/get_data_tier_filter';
import { getQueryFields } from '../../utils/get_query_fields';

export const createThreatSignals = async ({
alertId,
Expand Down Expand Up @@ -72,7 +72,6 @@ export const createThreatSignals = async ({
secondaryTimestamp,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
licensing,
experimentalFeatures,
}: CreateThreatSignalsOptions): Promise<SearchAfterAndBulkCreateReturnType> => {
Expand Down Expand Up @@ -115,6 +114,14 @@ export const createThreatSignals = async ({
const allEventFilters = [...filters, eventMappingFilter, ...dataTiersFilters];
const allThreatFilters = [...threatFilters, indicatorMappingFilter, ...dataTiersFilters];

const dataViews = await services.getDataViews();
const inputIndexFields = await getQueryFields({
dataViews,
index: inputIndex,
query,
language,
});

const eventCount = await getEventCount({
esClient: services.scopedClusterClient.asCurrentUser,
index: inputIndex,
Expand All @@ -140,12 +147,11 @@ export const createThreatSignals = async ({
if (newPitId) threatPitId = newPitId;
};

const dataViews = await services.getDataViews();
const threatIndexFields = await getFieldsForWildcard({
index: threatIndex,
language: threatLanguage ?? 'kuery',
const threatIndexFields = await getQueryFields({
dataViews,
ruleExecutionLogger,
index: threatIndex,
query: threatQuery,
language: threatLanguage,
});

const threatListCount = await getThreatListCount({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ export interface CreateThreatSignalsOptions {
secondaryTimestamp?: string;
exceptionFilter: Filter | undefined;
unprocessedExceptions: ExceptionListItemSchema[];
inputIndexFields: DataViewFieldBase[];
runOpts: RunOpts<ThreatRuleParams>;
licensing: LicensingPluginSetup;
experimentalFeatures: ExperimentalFeatures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ export const createNewTermsAlertType = (
unprocessedExceptions,
alertTimestampOverride,
publicBaseUrl,
inputIndexFields,
alertWithSuppression,
},
services,
Expand All @@ -135,7 +134,7 @@ export const createNewTermsAlertType = (
type: params.type,
query: params.query,
exceptionFilter,
fields: inputIndexFields,
loadFields: true,
};
const esFilter = await getFilter(filterArgs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const queryExecutor = async ({
services,
index: runOpts.inputIndex,
exceptionFilter: runOpts.exceptionFilter,
fields: runOpts.inputIndexFields,
loadFields: true,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export const createThresholdAlertType = (
aggregatableTimestampField,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
},
services,
startedAt,
Expand All @@ -99,7 +98,6 @@ export const createThresholdAlertType = (
aggregatableTimestampField,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
spaceId,
runOpts: execOptions.runOpts,
licensing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { ruleExecutionLogMock } from '../../rule_monitoring/mocks';
import type { RunOpts } from '../types';
import type { ExperimentalFeatures } from '../../../../../common';

jest.mock('../utils/get_filter', () => ({ getFilter: jest.fn() }));

describe('threshold_executor', () => {
let alertServices: RuleExecutorServicesMock;
let ruleExecutionLogger: ReturnType<typeof ruleExecutionLogMock.forExecutors.create>;
Expand Down Expand Up @@ -107,7 +109,6 @@ describe('threshold_executor', () => {
aggregatableTimestampField: TIMESTAMP,
exceptionFilter: undefined,
unprocessedExceptions: [],
inputIndexFields: [],
spaceId: 'default',
runOpts: {} as RunOpts<ThresholdRuleParams>,
licensing,
Expand Down Expand Up @@ -173,7 +174,6 @@ describe('threshold_executor', () => {
aggregatableTimestampField: TIMESTAMP,
exceptionFilter: undefined,
unprocessedExceptions: [getExceptionListItemSchemaMock()],
inputIndexFields: [],
spaceId: 'default',
runOpts: {} as RunOpts<ThresholdRuleParams>,
licensing,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
RuleExecutorServices,
} from '@kbn/alerting-plugin/server';
import type { IRuleDataClient } from '@kbn/rule-registry-plugin/server';
import type { Filter, DataViewFieldBase } from '@kbn/es-query';
import type { Filter } from '@kbn/es-query';
import type { CompleteRule, ThresholdRuleParams } from '../../rule_schema';
import { getFilter } from '../utils/get_filter';
import { bulkCreateThresholdSignals } from './bulk_create_threshold_signals';
Expand Down Expand Up @@ -64,7 +64,6 @@ export const thresholdExecutor = async ({
aggregatableTimestampField,
exceptionFilter,
unprocessedExceptions,
inputIndexFields,
spaceId,
runOpts,
licensing,
Expand All @@ -87,7 +86,6 @@ export const thresholdExecutor = async ({
aggregatableTimestampField: string;
exceptionFilter: Filter | undefined;
unprocessedExceptions: ExceptionListItemSchema[];
inputIndexFields: DataViewFieldBase[];
spaceId: string;
runOpts: RunOpts<ThresholdRuleParams>;
licensing: LicensingPluginSetup;
Expand Down Expand Up @@ -135,7 +133,7 @@ export const thresholdExecutor = async ({
services,
index: inputIndex,
exceptionFilter,
fields: inputIndexFields,
loadFields: true,
});

// Look for new events over threshold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import type {
} from '@kbn/rule-registry-plugin/server';
import type { EcsFieldMap } from '@kbn/rule-registry-plugin/common/assets/field_maps/ecs_field_map';
import type { TypeOfFieldMap } from '@kbn/rule-registry-plugin/common/field_map';
import type { Filter, DataViewFieldBase } from '@kbn/es-query';
import type { Filter } from '@kbn/es-query';

import type { LicensingPluginSetup } from '@kbn/licensing-plugin/server';
import type { RulePreviewLoggedRequest } from '../../../../common/api/detection_engine/rule_preview/rule_preview.gen';
Expand Down Expand Up @@ -104,7 +104,6 @@ export interface RunOpts<TParams extends RuleParams> {
alertWithSuppression: SuppressedAlertService;
refreshOnIndexingAlerts: RefreshTypes;
publicBaseUrl: string | undefined;
inputIndexFields: DataViewFieldBase[];
experimentalFeatures?: ExperimentalFeatures;
}

Expand Down

This file was deleted.

Loading

0 comments on commit b005ea9

Please sign in to comment.