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][RAC] - Add reason field #107532

Merged
merged 20 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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 @@ -54,7 +54,7 @@ describe('Alert details with unmapped fields', () => {

it('Displays the unmapped field on the table', () => {
const expectedUnmmappedField = {
row: 88,
row: 90,
field: 'unmapped',
text: 'This is the unmapped field',
};
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"signal.original_event.module": "kibana.alert.original_event.module",
"signal.original_event.outcome": "kibana.alert.original_event.outcome",
"signal.original_event.provider": "kibana.alert.original_event.provider",
"signal.original_event.reason": "kibana.alert.original_event.reason",
"signal.original_event.risk_score": "kibana.alert.original_event.risk_score",
"signal.original_event.risk_score_norm": "kibana.alert.original_event.risk_score_norm",
"signal.original_event.sequence": "kibana.alert.original_event.sequence",
Expand All @@ -25,6 +26,7 @@
"signal.original_event.timezone": "kibana.alert.original_event.timezone",
"signal.original_event.type": "kibana.alert.original_event.type",
"signal.original_time": "kibana.alert.original_time",
"signal.reason": "kibana.alert.reason",
"signal.rule.author": "kibana.alert.rule.author",
"signal.rule.building_block_type": "kibana.alert.rule.building_block_type",
"signal.rule.created_at": "kibana.alert.rule.created_at",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
}
}
},
"reason": {
"type": "keyword"
},
"rule": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@
"provider": {
"type": "keyword"
},
"reason": {
"type": "keyword"
},
"risk_score": {
"type": "float"
},
Expand Down Expand Up @@ -421,6 +424,9 @@
},
"depth": {
"type": "integer"
},
"reason": {
"type": "keyword"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {
ALERT_OWNER,
ALERT_REASON,
ALERT_RULE_NAMESPACE,
ALERT_STATUS,
ALERT_WORKFLOW_STATUS,
Expand Down Expand Up @@ -50,8 +51,9 @@ describe('buildAlert', () => {
const doc = sampleDocNoSortIdWithTimestamp('d5e8eb51-a6a0-456d-8a15-4b79bfec3d71');
delete doc._source.event;
const rule = getRulesSchemaMock();
const reason = 'alert reasonable reason';
const alert = {
...buildAlert([doc], rule, SPACE_ID),
...buildAlert([doc], rule, SPACE_ID, reason),
...additionalAlertFields(doc),
};
const timestamp = alert['@timestamp'];
Expand All @@ -68,6 +70,7 @@ describe('buildAlert', () => {
},
],
[ALERT_ORIGINAL_TIME]: '2020-04-20T21:27:45.000Z',
[ALERT_REASON]: 'alert reasonable reason',
[ALERT_STATUS]: 'open',
[ALERT_WORKFLOW_STATUS]: 'open',
...flattenWithPrefix(ALERT_RULE_NAMESPACE, {
Expand Down Expand Up @@ -119,8 +122,9 @@ describe('buildAlert', () => {
module: 'system',
};
const rule = getRulesSchemaMock();
const reason = 'alert reasonable reason';
const alert = {
...buildAlert([doc], rule, SPACE_ID),
...buildAlert([doc], rule, SPACE_ID, reason),
...additionalAlertFields(doc),
};
const timestamp = alert['@timestamp'];
Expand All @@ -143,6 +147,7 @@ describe('buildAlert', () => {
kind: 'event',
module: 'system',
},
[ALERT_REASON]: 'alert reasonable reason',
[ALERT_STATUS]: 'open',
[ALERT_WORKFLOW_STATUS]: 'open',
...flattenWithPrefix(ALERT_RULE_NAMESPACE, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import {
ALERT_OWNER,
ALERT_REASON,
ALERT_RULE_NAMESPACE,
ALERT_STATUS,
ALERT_WORKFLOW_STATUS,
Expand Down Expand Up @@ -92,7 +93,8 @@ export const removeClashes = (doc: SimpleHit) => {
export const buildAlert = (
docs: SimpleHit[],
rule: RulesSchema,
spaceId: string | null | undefined
spaceId: string | null | undefined,
reason: string
): RACAlert => {
const removedClashes = docs.map(removeClashes);
const parents = removedClashes.map(buildParent);
Expand All @@ -110,6 +112,7 @@ export const buildAlert = (
[ALERT_STATUS]: 'open',
[ALERT_WORKFLOW_STATUS]: 'open',
[ALERT_DEPTH]: depth,
[ALERT_REASON]: reason,
...flattenWithPrefix(ALERT_RULE_NAMESPACE, rule),
} as unknown) as RACAlert;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SavedObject } from 'src/core/types';
import { BaseHit } from '../../../../../../common/detection_engine/types';
import type { ConfigType } from '../../../../../config';
import { buildRuleWithOverrides, buildRuleWithoutOverrides } from '../../../signals/build_rule';
import { BuildReasonMessage } from '../../../signals/reason_formatters';
import { getMergeStrategy } from '../../../signals/source_fields_merging/strategies';
import { AlertAttributes, SignalSource, SignalSourceHit } from '../../../signals/types';
import { RACAlert } from '../../types';
Expand All @@ -35,19 +36,35 @@ export const buildBulkBody = (
ruleSO: SavedObject<AlertAttributes>,
doc: SignalSourceHit,
mergeStrategy: ConfigType['alertMergeStrategy'],
applyOverrides: boolean
applyOverrides: boolean,
buildReasonMessage: BuildReasonMessage
): RACAlert => {
const mergedDoc = getMergeStrategy(mergeStrategy)({ doc });
const rule = applyOverrides
? buildRuleWithOverrides(ruleSO, mergedDoc._source ?? {})
: buildRuleWithoutOverrides(ruleSO);
const filteredSource = filterSource(mergedDoc);
const timestamp = new Date().toISOString();
let hostName = '';
let userName = '';
if (mergedDoc.fields) {
hostName = mergedDoc.fields['host.name'] != null ? mergedDoc.fields['host.name'] : hostName;
userName = mergedDoc.fields['user.name'] != null ? mergedDoc.fields['user.name'] : userName;
}
const reason = buildReasonMessage({
alertName: rule.name,
alertRiskScore: ruleSO.attributes.params.riskScore,
michaelolo24 marked this conversation as resolved.
Show resolved Hide resolved
alertSeverity: ruleSO.attributes.params.severity,
hostName,
timestamp,
userName,
});
if (isSourceDoc(mergedDoc)) {
return {
...filteredSource,
...buildAlert([mergedDoc], rule, spaceId),
...buildAlert([mergedDoc], rule, spaceId, reason),
...additionalAlertFields(mergedDoc),
'@timestamp': new Date().toISOString(),
'@timestamp': timestamp,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const wrapHitsFactory = ({
ruleSO: SearchAfterAndBulkCreateParams['ruleSO'];
mergeStrategy: ConfigType['alertMergeStrategy'];
spaceId: string | null | undefined;
}): WrapHits => (events) => {
}): WrapHits => (events, buildReasonMessage) => {
try {
const wrappedDocs: WrappedRACAlert[] = events.flatMap((doc) => [
{
Expand All @@ -35,7 +35,14 @@ export const wrapHitsFactory = ({
String(doc._version),
ruleSO.attributes.params.ruleId ?? ''
),
_source: buildBulkBody(spaceId, ruleSO, doc as SignalSourceHit, mergeStrategy, true),
_source: buildBulkBody(
spaceId,
ruleSO,
doc as SignalSourceHit,
mergeStrategy,
true,
buildReasonMessage
),
},
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export const alertsFieldMap: FieldMap = {
array: false,
required: true,
},
'kibana.alert.reason': {
type: 'keyword',
array: false,
required: false,
},
'kibana.alert.threat': {
type: 'object',
array: false,
Expand Down
Loading