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][Detections] Handle conflicts on alert status update #75492

Merged
merged 24 commits into from
Sep 4, 2020
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Next Next commit
Proceed on conflict when updating alert status
madirey committed Aug 13, 2020
commit 6cc4292eb624620644b75d12d04af5f3ce5f110a
Original file line number Diff line number Diff line change
@@ -270,6 +270,9 @@ export type Status = t.TypeOf<typeof status>;
export const job_status = t.keyof({ succeeded: null, failed: null, 'going to run': null });
export type JobStatus = t.TypeOf<typeof job_status>;

export const conflicts = t.keyof({ abort: null, proceed: null });
export type Conflicts = t.TypeOf<typeof conflicts>;

// TODO: Create a regular expression type or custom date math part type here
export const to = t.string;
export type To = t.TypeOf<typeof to>;
Original file line number Diff line number Diff line change
@@ -6,13 +6,14 @@

import * as t from 'io-ts';

import { signal_ids, signal_status_query, status } from '../common/schemas';
import { conflicts, signal_ids, signal_status_query, status } from '../common/schemas';

export const setSignalsStatusSchema = t.intersection([
t.type({
status,
}),
t.partial({
conflicts,
signal_ids,
query: signal_status_query,
}),
Original file line number Diff line number Diff line change
@@ -83,7 +83,12 @@ export const updateAlertStatusAction = async ({
// TODO: Only delete those that were successfully updated from updatedRules
setEventsDeleted({ eventIds: alertIds, isDeleted: true });

onAlertStatusUpdateSuccess(response.updated, selectedStatus);
onAlertStatusUpdateSuccess(
response.updated,
response.total,
response.version_conflicts,
selectedStatus
);
} catch (error) {
onAlertStatusUpdateFailure(selectedStatus, error);
} finally {
Original file line number Diff line number Diff line change
@@ -181,17 +181,20 @@ export const AlertsTableComponent: React.FC<AlertsTableComponentProps> = ({
);

const onAlertStatusUpdateSuccess = useCallback(
(count: number, status: Status) => {
(updated: number, total: number, conflicts: number, status: Status) => {
let title: string;
switch (status) {
case 'closed':
title = i18n.CLOSED_ALERT_SUCCESS_TOAST(count);
title = i18n.CLOSED_ALERT_SUCCESS_TOAST(updated, total);
break;
case 'open':
title = i18n.OPENED_ALERT_SUCCESS_TOAST(count);
title = i18n.OPENED_ALERT_SUCCESS_TOAST(updated, total);
break;
case 'in-progress':
title = i18n.IN_PROGRESS_ALERT_SUCCESS_TOAST(count);
title = i18n.IN_PROGRESS_ALERT_SUCCESS_TOAST(updated, total);
}
if (conflicts > 0) {
// TODO: add 'try again' button
}
displaySuccessToast(title, dispatchToaster);
},
Original file line number Diff line number Diff line change
@@ -129,27 +129,27 @@ export const ACTION_ADD_ENDPOINT_EXCEPTION = i18n.translate(
}
);

export const CLOSED_ALERT_SUCCESS_TOAST = (totalAlerts: number) =>
export const CLOSED_ALERT_SUCCESS_TOAST = (totalClosed: number, totalAlerts: number) =>
i18n.translate('xpack.securitySolution.detectionEngine.alerts.closedAlertSuccessToastMessage', {
values: { totalAlerts },
values: { totalClosed, totalAlerts },
defaultMessage:
'Successfully closed {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
'Successfully closed {totalClosed}/{totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
});

export const OPENED_ALERT_SUCCESS_TOAST = (totalAlerts: number) =>
export const OPENED_ALERT_SUCCESS_TOAST = (totalOpened: number, totalAlerts: number) =>
i18n.translate('xpack.securitySolution.detectionEngine.alerts.openedAlertSuccessToastMessage', {
values: { totalAlerts },
values: { totalOpened, totalAlerts },
defaultMessage:
'Successfully opened {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
'Successfully opened {totalOpened}/{totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}}.',
});

export const IN_PROGRESS_ALERT_SUCCESS_TOAST = (totalAlerts: number) =>
export const IN_PROGRESS_ALERT_SUCCESS_TOAST = (totalInProgress: number, totalAlerts: number) =>
i18n.translate(
'xpack.securitySolution.detectionEngine.alerts.inProgressAlertSuccessToastMessage',
{
values: { totalAlerts },
values: { totalInProgress, totalAlerts },
defaultMessage:
'Successfully marked {totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}} as in progress.',
'Successfully marked {totalInProgress}/{totalAlerts} {totalAlerts, plural, =1 {alert} other {alerts}} as in progress.',
}
);

Original file line number Diff line number Diff line change
@@ -45,7 +45,12 @@ export interface UpdateAlertStatusActionProps {
selectedStatus: Status;
setEventsLoading: ({ eventIds, isLoading }: SetEventsLoadingProps) => void;
setEventsDeleted: ({ eventIds, isDeleted }: SetEventsDeletedProps) => void;
onAlertStatusUpdateSuccess: (count: number, status: Status) => void;
onAlertStatusUpdateSuccess: (
count: number,
total: number,
conflicts: number,
status: Status
) => void;
onAlertStatusUpdateFailure: (status: Status, error: Error) => void;
}

Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ export const updateAlertStatus = async ({
}: UpdateAlertStatusProps): Promise<UpdateDocumentByQueryResponse> =>
KibanaServices.get().http.fetch(DETECTION_ENGINE_SIGNALS_STATUS_URL, {
method: 'POST',
body: JSON.stringify({ status, ...query }),
body: JSON.stringify({ conflicts: 'proceed', status, ...query }),
signal,
});

Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ export const setSignalsStatusRoute = (router: IRouter) => {
},
},
async (context, request, response) => {
const { signal_ids: signalIds, query, status } = request.body;
const { conflicts, signal_ids: signalIds, query, status } = request.body;
const clusterClient = context.core.elasticsearch.legacy.client;
const siemClient = context.securitySolution?.getAppClient();
const siemResponse = buildSiemResponse(response);
@@ -52,9 +52,14 @@ export const setSignalsStatusRoute = (router: IRouter) => {
},
};
}
let onConflict = 'abort';
if (conflicts != null) {
onConflict = conflicts;
}
madirey marked this conversation as resolved.
Show resolved Hide resolved
try {
const result = await clusterClient.callAsCurrentUser('updateByQuery', {
index: siemClient.getSignalsIndex(),
conflicts: onConflict,
refresh: 'wait_for',
body: {
script: {