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] fix from-to values investigate in timeline pulled from timestamp instead of @timestamp field #156884

Merged
merged 1 commit into from May 8, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -10,7 +10,7 @@ import type { EcsSecurityExtension as Ecs } from '@kbn/securitysolution-ecs';
export const getDetectionAlertMock = (overrides: Partial<Ecs> = {}): Ecs => ({
...{
_id: '1',
timestamp: '2018-11-05T19:03:25.937Z',
'@timestamp': '2018-11-05T19:03:25.937Z',
host: {
name: ['apache'],
ip: ['192.168.0.1'],
Expand Down
Expand Up @@ -1014,9 +1014,9 @@ describe('alert actions', () => {
});

test('it uses ecs.Data.timestamp if one is provided', () => {
const ecsDataMock: Ecs = {
const ecsDataMock = {
...mockEcsDataWithAlert,
timestamp: '2020-03-20T17:59:46.349Z',
'@timestamp': '2020-03-20T17:59:46.349Z',
};
const result = determineToAndFrom({ ecs: ecsDataMock });

Expand All @@ -1025,7 +1025,8 @@ describe('alert actions', () => {
});

test('it uses current time timestamp if ecsData.timestamp is not provided', () => {
const { timestamp, ...ecsDataMock } = mockEcsDataWithAlert;
// @ts-ignore // TODO remove when EcsSecurityExtension has been cleaned https://github.com/elastic/kibana/issues/156879
const { '@timestamp': timestamp, ...ecsDataMock } = mockEcsDataWithAlert;
const result = determineToAndFrom({ ecs: ecsDataMock });

expect(result.from).toEqual('2020-03-01T17:54:46.349Z');
Expand Down
Expand Up @@ -27,6 +27,7 @@ import {
ALERT_SUPPRESSION_END,
ALERT_SUPPRESSION_DOCS_COUNT,
ALERT_SUPPRESSION_TERMS,
TIMESTAMP,
} from '@kbn/rule-data-utils';

import { lastValueFrom } from 'rxjs';
Expand Down Expand Up @@ -155,10 +156,13 @@ export const determineToAndFrom = ({ ecs }: { ecs: Ecs[] | Ecs }) => {
const elapsedTimeRule = moment.duration(
moment().diff(dateMath.parse(ruleFrom != null ? ruleFrom[0] : 'now-1d'))
);
const from = moment(ecsData.timestamp ?? new Date())
.subtract(elapsedTimeRule)
.toISOString();
const to = moment(ecsData.timestamp ?? new Date()).toISOString();

const alertTimestampEcsValue = getField(ecsData, TIMESTAMP);
const alertTimestamp = Array.isArray(alertTimestampEcsValue)
? alertTimestampEcsValue[0]
: alertTimestampEcsValue;
const to = moment(alertTimestamp ?? new Date()).toISOString();
const from = moment(to).subtract(elapsedTimeRule).toISOString();

return { to, from };
};
Expand Down