Skip to content

Commit

Permalink
Authorize alerts when attached to a case
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Feb 8, 2023
1 parent 8419345 commit aafdf84
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/alerts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ export interface AlertGet {
}

export interface UpdateAlertCasesRequest {
alerts: Array<{ id: string; index: string }>;
alerts: AlertInfo[];
caseIds: string[];
}
34 changes: 15 additions & 19 deletions x-pack/plugins/cases/server/common/models/case_with_comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ import {
import type { CasesClientArgs } from '../../client';
import type { RefreshSetting } from '../../services/types';
import { createCaseError } from '../error';
import type { CaseSavedObject } from '../types';
import type { AlertInfo, CaseSavedObject } from '../types';
import {
countAlertsForID,
flattenCommentSavedObjects,
transformNewComment,
getOrUpdateLensReferences,
createAlertUpdateStatusRequest,
isCommentRequestTypeAlert,
getAlertInfoFromComments,
} from '../utils';
Expand Down Expand Up @@ -312,31 +311,28 @@ export class CaseCommentModel {
(attachment): attachment is CommentRequestAlertType => attachment.type === CommentType.alert
);

if (this.caseInfo.attributes.settings.syncAlerts) {
await this.updateAlertsStatus(alertAttachments);
}
const alerts = getAlertInfoFromComments(alertAttachments);

if (alerts.length > 0) {
await this.params.services.alertsService.ensureAlertsAuthorized({ alerts });
await this.updateAlertsSchemaWithCaseInfo(alerts);

if (alertAttachments.length > 0) {
await this.updateAlertsSchemaWithCaseInfo(alertAttachments);
if (this.caseInfo.attributes.settings.syncAlerts) {
await this.updateAlertsStatus(alerts);
}
}
}

private async updateAlertsStatus(alerts: CommentRequest[]) {
const alertsToUpdate = alerts
.map((alert) =>
createAlertUpdateStatusRequest({
comment: alert,
status: this.caseInfo.attributes.status,
})
)
.flat();
private async updateAlertsStatus(alerts: AlertInfo[]) {
const alertsToUpdate = alerts.map((alert) => ({
...alert,
status: this.caseInfo.attributes.status,
}));

await this.params.services.alertsService.updateAlertsStatus(alertsToUpdate);
}

private async updateAlertsSchemaWithCaseInfo(alertAttachments: CommentRequestAlertType[]) {
const alerts = getAlertInfoFromComments(alertAttachments);

private async updateAlertsSchemaWithCaseInfo(alerts: AlertInfo[]) {
await this.params.services.alertsService.bulkUpdateCases({
alerts,
caseIds: [this.caseInfo.id],
Expand Down
26 changes: 25 additions & 1 deletion x-pack/plugins/cases/server/services/alerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export class AlertService {
);
}

private getNonEmptyAlerts(alerts: AlertInfo[]): AlertInfo[] {
return alerts.filter((alert) => !AlertService.isEmptyAlert(alert));
}

public async getAlerts(alertsInfo: AlertInfo[]): Promise<MgetResponse<Alert> | undefined> {
try {
const docs = alertsInfo
Expand All @@ -207,7 +211,7 @@ export class AlertService {

public async bulkUpdateCases({ alerts, caseIds }: UpdateAlertCasesRequest): Promise<void> {
try {
const nonEmptyAlerts = alerts.filter((alert) => !AlertService.isEmptyAlert(alert));
const nonEmptyAlerts = this.getNonEmptyAlerts(alerts);

if (nonEmptyAlerts.length <= 0) {
return;
Expand All @@ -225,6 +229,26 @@ export class AlertService {
});
}
}

public async ensureAlertsAuthorized({ alerts }: { alerts: AlertInfo[] }): Promise<void> {
try {
const nonEmptyAlerts = this.getNonEmptyAlerts(alerts);

if (nonEmptyAlerts.length <= 0) {
return;
}

await this.alertsClient.ensureAllAlertsAuthorizedRead({
alerts: nonEmptyAlerts,
});
} catch (error) {
throw createCaseError({
message: `Failed to authorize alerts: ${error}`,
error,
logger: this.logger,
});
}
}
}

interface TranslatedUpdateAlertRequest {
Expand Down

0 comments on commit aafdf84

Please sign in to comment.