Skip to content

Commit

Permalink
Move default indicator path to common constant
Browse files Browse the repository at this point in the history
  • Loading branch information
rylnd committed Feb 17, 2021
1 parent 07689b4 commit c429352
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 28 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/security_solution/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export const DEFAULT_RULE_REFRESH_INTERVAL_VALUE = 60000; // ms
export const DEFAULT_RULE_REFRESH_IDLE_VALUE = 2700000; // ms
export const DEFAULT_RULE_NOTIFICATION_QUERY_SIZE = 100;

// Document path where threat indicator fields are expected. Used as
// both the source of enrichment fields and the destination for enrichment in
// the generated detection alert
export const DEFAULT_INDICATOR_PATH = 'threat.indicator';

export enum SecurityPageName {
detections = 'detections',
overview = 'overview',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { DEFAULT_INDICATOR_PATH } from '../../../constants';
import {
MachineLearningCreateSchema,
MachineLearningUpdateSchema,
Expand Down Expand Up @@ -56,7 +57,7 @@ export const getCreateThreatMatchRulesSchemaMock = (
rule_id: ruleId,
threat_query: '*:*',
threat_index: ['list-index'],
threat_indicator_path: 'threat.indicator',
threat_indicator_path: DEFAULT_INDICATOR_PATH,
threat_mapping: [
{
entries: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { DEFAULT_INDICATOR_PATH } from '../../../constants';
import { getListArrayMock } from '../types/lists.mock';

import { RulesSchema } from './rules_schema';
Expand Down Expand Up @@ -150,7 +151,7 @@ export const getThreatMatchingSchemaPartialMock = (enabled = false): Partial<Rul
language: 'kuery',
threat_query: '*:*',
threat_index: ['list-index'],
threat_indicator_path: 'threat.indicator',
threat_indicator_path: DEFAULT_INDICATOR_PATH,
threat_mapping: [
{
entries: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { RiskScoreField } from '../risk_score_mapping';
import { AutocompleteField } from '../autocomplete_field';
import { useFetchIndex } from '../../../../common/containers/source';
import { isThreatMatchRule } from '../../../../../common/detection_engine/utils';
import { DEFAULT_INDICATOR_PATH } from '../../../../../common/constants';

const CommonUseField = getUseField({ component: Field });

Expand Down Expand Up @@ -309,7 +310,7 @@ const StepAboutRuleComponent: FC<StepAboutRuleProps> = ({
euiFieldProps: {
fullWidth: true,
disabled: isLoading,
placeholder: 'threat.indicator',
placeholder: DEFAULT_INDICATOR_PATH,
},
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
* 2.0.
*/

import { DEFAULT_INDICATOR_PATH } from '../../../../../common/constants';
import { SignalSearchResponse, SignalsEnrichment } from '../types';
import { enrichSignalThreatMatches } from './enrich_signal_threat_matches';
import { getThreatList } from './get_threat_list';
import { BuildThreatEnrichmentOptions, GetMatchedThreats } from './types';

const DEFAULT_INDICATOR_PATH = 'threat.indicator';

export const buildThreatEnrichment = ({
buildRuleMessage,
exceptionItems,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { get } from 'lodash';
import { DEFAULT_INDICATOR_PATH } from '../../../../../common/constants';

import { getThreatListItemMock } from './build_threat_mapping_filter.mock';
import {
Expand Down Expand Up @@ -93,7 +94,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries: [],
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toEqual([]);
Expand All @@ -103,7 +104,7 @@ describe('buildMatchedIndicator', () => {
const [indicator] = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(get(indicator, 'matched.atomic')).toEqual('domain_1');
Expand All @@ -113,7 +114,7 @@ describe('buildMatchedIndicator', () => {
const [indicator] = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(get(indicator, 'matched.field')).toEqual('event.field');
Expand All @@ -123,7 +124,7 @@ describe('buildMatchedIndicator', () => {
const [indicator] = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(get(indicator, 'matched.type')).toEqual('type_1');
Expand Down Expand Up @@ -152,7 +153,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toHaveLength(queries.length);
Expand All @@ -162,7 +163,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toEqual([
Expand Down Expand Up @@ -227,7 +228,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toEqual([
Expand All @@ -252,7 +253,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toEqual([
Expand Down Expand Up @@ -284,7 +285,7 @@ describe('buildMatchedIndicator', () => {
const indicators = buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
});

expect(indicators).toEqual([
Expand Down Expand Up @@ -316,7 +317,7 @@ describe('buildMatchedIndicator', () => {
buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
})
).toThrowError('Expected indicator field to be an object, but found: not an object');
});
Expand All @@ -337,7 +338,7 @@ describe('buildMatchedIndicator', () => {
buildMatchedIndicator({
queries,
threats,
indicatorPath: 'threat.indicator',
indicatorPath: DEFAULT_INDICATOR_PATH,
})
).toThrowError('Expected indicator field to be an object, but found: not an object');
});
Expand Down Expand Up @@ -366,7 +367,7 @@ describe('enrichSignalThreatMatches', () => {
const enrichedSignals = await enrichSignalThreatMatches(
signals,
getMatchedThreats,
'threat.indicator'
DEFAULT_INDICATOR_PATH
);

expect(enrichedSignals.hits.hits).toEqual([]);
Expand All @@ -381,10 +382,10 @@ describe('enrichSignalThreatMatches', () => {
const enrichedSignals = await enrichSignalThreatMatches(
signals,
getMatchedThreats,
'threat.indicator'
DEFAULT_INDICATOR_PATH
);
const [enrichedHit] = enrichedSignals.hits.hits;
const indicators = get(enrichedHit._source, 'threat.indicator');
const indicators = get(enrichedHit._source, DEFAULT_INDICATOR_PATH);

expect(indicators).toEqual([
{ existing: 'indicator' },
Expand All @@ -406,10 +407,10 @@ describe('enrichSignalThreatMatches', () => {
const enrichedSignals = await enrichSignalThreatMatches(
signals,
getMatchedThreats,
'threat.indicator'
DEFAULT_INDICATOR_PATH
);
const [enrichedHit] = enrichedSignals.hits.hits;
const indicators = get(enrichedHit._source, 'threat.indicator');
const indicators = get(enrichedHit._source, DEFAULT_INDICATOR_PATH);

expect(indicators).toEqual([
{
Expand All @@ -427,10 +428,10 @@ describe('enrichSignalThreatMatches', () => {
const enrichedSignals = await enrichSignalThreatMatches(
signals,
getMatchedThreats,
'threat.indicator'
DEFAULT_INDICATOR_PATH
);
const [enrichedHit] = enrichedSignals.hits.hits;
const indicators = get(enrichedHit._source, 'threat.indicator');
const indicators = get(enrichedHit._source, DEFAULT_INDICATOR_PATH);

expect(indicators).toEqual([
{ existing: 'indicator' },
Expand All @@ -450,7 +451,7 @@ describe('enrichSignalThreatMatches', () => {
});
const signals = getSignalsResponseMock([signalHit]);
await expect(() =>
enrichSignalThreatMatches(signals, getMatchedThreats, 'threat.indicator')
enrichSignalThreatMatches(signals, getMatchedThreats, DEFAULT_INDICATOR_PATH)
).rejects.toThrowError('Expected threat field to be an object, but found: whoops');
});

Expand Down Expand Up @@ -486,7 +487,7 @@ describe('enrichSignalThreatMatches', () => {
'custom_threat.custom_indicator'
);
const [enrichedHit] = enrichedSignals.hits.hits;
const indicators = get(enrichedHit._source, 'threat.indicator');
const indicators = get(enrichedHit._source, DEFAULT_INDICATOR_PATH);

expect(indicators).toEqual([
{
Expand Down Expand Up @@ -529,13 +530,13 @@ describe('enrichSignalThreatMatches', () => {
const enrichedSignals = await enrichSignalThreatMatches(
signals,
getMatchedThreats,
'threat.indicator'
DEFAULT_INDICATOR_PATH
);
expect(enrichedSignals.hits.total).toEqual(expect.objectContaining({ value: 1 }));
expect(enrichedSignals.hits.hits).toHaveLength(1);

const [enrichedHit] = enrichedSignals.hits.hits;
const indicators = get(enrichedHit._source, 'threat.indicator');
const indicators = get(enrichedHit._source, DEFAULT_INDICATOR_PATH);

expect(indicators).toEqual([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { get, isObject } from 'lodash';
import { DEFAULT_INDICATOR_PATH } from '../../../../../common/constants';

import type { SignalSearchResponse, SignalSourceHit } from '../types';
import type {
Expand Down Expand Up @@ -91,7 +92,7 @@ export const enrichSignalThreatMatches = async (
if (!isObject(threat)) {
throw new Error(`Expected threat field to be an object, but found: ${threat}`);
}
const existingIndicatorValue = get(signalHit._source, 'threat.indicator') ?? [];
const existingIndicatorValue = get(signalHit._source, DEFAULT_INDICATOR_PATH) ?? [];
const existingIndicators = [existingIndicatorValue].flat(); // ensure indicators is an array

return {
Expand Down

0 comments on commit c429352

Please sign in to comment.