Skip to content

Commit

Permalink
Refactor case client & create internal case client
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 3, 2021
1 parent d8d3485 commit c6ccce4
Show file tree
Hide file tree
Showing 28 changed files with 508 additions and 501 deletions.
58 changes: 58 additions & 0 deletions x-pack/plugins/cases/server/client/alerts/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CaseStatuses } from '../../../common/api';
import { AlertInfo } from '../../common';
import { CasesSubClientImplementation } from '../types';
import { CasesClientGetAlertsResponse } from './types';
import { get } from './get';
import { updateStatus } from './update_status';

/**
* Defines the fields necessary to update an alert's status.
*/
export interface UpdateAlertRequest {
id: string;
index: string;
status: CaseStatuses;
}

export interface AlertUpdateStatus {
alerts: UpdateAlertRequest[];
}

export interface AlertGet {
alertsInfo: AlertInfo[];
}

export interface AlertSubClient {
get(args: AlertGet): Promise<CasesClientGetAlertsResponse>;
updateStatus(args: AlertUpdateStatus): Promise<void>;
}

export const createAlertsSubClient: CasesSubClientImplementation<AlertSubClient> = (args) => {
const { alertsService, scopedClusterClient, logger } = args;

const alertsSubClient: AlertSubClient = {
get: (params: AlertGet) =>
get({
...params,
alertsService,
scopedClusterClient,
logger,
}),
updateStatus: (params: AlertUpdateStatus) =>
updateStatus({
...params,
alertsService,
scopedClusterClient,
logger,
}),
};

return alertsSubClient;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('updateAlertsStatus', () => {
const savedObjectsClient = createMockSavedObjectsRepository();

const casesClient = await createCasesClientWithMockSavedObjectsClient({ savedObjectsClient });
await casesClient.client.updateAlertsStatus({
await casesClient.client.updateStatus({
alerts: [{ id: 'alert-id-1', index: '.siem-signals', status: CaseStatuses.closed }],
});

Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/client/alerts/update_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { ElasticsearchClient, Logger } from 'src/core/server';
import { AlertServiceContract } from '../../services';
import { UpdateAlertRequest } from '../types';
import { UpdateAlertRequest } from './client';

interface UpdateAlertsStatusArgs {
alertsService: AlertServiceContract;
Expand All @@ -16,7 +16,7 @@ interface UpdateAlertsStatusArgs {
logger: Logger;
}

export const updateAlertsStatus = async ({
export const updateStatus = async ({
alertsService,
alerts,
scopedClusterClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import {
buildCommentUserActionItem,
} from '../../services/user_actions/helpers';

import { CaseServiceSetup, CaseUserActionServiceSetup } from '../../services';
import { CaseService, CaseUserActionService } from '../../services';
import { CommentableCase, createAlertUpdateRequest } from '../../common';
import { CasesClientHandler } from '..';
import { CasesClientArgs, CasesClientInternal } from '..';
import { createCaseError } from '../../common/error';
import {
MAX_GENERATED_ALERTS_PER_SUB_CASE,
Expand All @@ -49,11 +49,11 @@ async function getSubCase({
userActionService,
user,
}: {
caseService: CaseServiceSetup;
caseService: CaseService;
savedObjectsClient: SavedObjectsClientContract;
caseId: string;
createdAt: string;
userActionService: CaseUserActionServiceSetup;
userActionService: CaseUserActionService;
user: User;
}): Promise<SavedObject<SubCaseAttributes>> {
const mostRecentSubCase = await caseService.getMostRecentSubCase(savedObjectsClient, caseId);
Expand All @@ -80,7 +80,7 @@ async function getSubCase({
caseId,
createdBy: user,
});
await userActionService.postUserActions({
await userActionService.bulkCreate({
client: savedObjectsClient,
actions: [
buildCaseUserActionItem({
Expand All @@ -98,20 +98,20 @@ async function getSubCase({
}

interface AddCommentFromRuleArgs {
casesClient: CasesClientHandler;
casesClientInternal: CasesClientInternal;
caseId: string;
comment: CommentRequestAlertType;
savedObjectsClient: SavedObjectsClientContract;
caseService: CaseServiceSetup;
userActionService: CaseUserActionServiceSetup;
caseService: CaseService;
userActionService: CaseUserActionService;
logger: Logger;
}

const addGeneratedAlerts = async ({
savedObjectsClient,
caseService,
userActionService,
casesClient,
casesClientInternal,
caseId,
comment,
logger,
Expand Down Expand Up @@ -180,12 +180,12 @@ const addGeneratedAlerts = async ({
comment: query,
status: subCase.attributes.status,
});
await casesClient.updateAlertsStatus({
await casesClientInternal.alerts.updateStatus({
alerts: alertsToUpdate,
});
}

await userActionService.postUserActions({
await userActionService.bulkCreate({
client: savedObjectsClient,
actions: [
buildCommentUserActionItem({
Expand Down Expand Up @@ -217,7 +217,7 @@ async function getCombinedCase({
id,
logger,
}: {
service: CaseServiceSetup;
service: CaseService;
client: SavedObjectsClientContract;
id: string;
logger: Logger;
Expand Down Expand Up @@ -268,31 +268,25 @@ async function getCombinedCase({
}

interface AddCommentArgs {
casesClient: CasesClientHandler;
caseId: string;
comment: CommentRequest;
savedObjectsClient: SavedObjectsClientContract;
caseService: CaseServiceSetup;
userActionService: CaseUserActionServiceSetup;
user: User;
logger: Logger;
getCasesInternalClient: () => CasesClientInternal;
}

export const addComment = async ({
savedObjectsClient,
caseService,
userActionService,
casesClient,
caseId,
comment,
user,
logger,
}: AddCommentArgs): Promise<CaseResponse> => {
getCasesInternalClient,
...rest
}: AddCommentArgs & CasesClientArgs): Promise<CaseResponse> => {
const query = pipe(
CommentRequestRt.decode(comment),
fold(throwErrors(Boom.badRequest), identity)
);

const { savedObjectsClient, caseService, userActionService, user, logger } = rest;
const casesClientInternal = getCasesInternalClient();

if (isCommentRequestTypeGenAlert(comment)) {
if (!ENABLE_CASE_CONNECTOR) {
throw Boom.badRequest(
Expand All @@ -303,7 +297,7 @@ export const addComment = async ({
return addGeneratedAlerts({
caseId,
comment,
casesClient,
casesClientInternal,
savedObjectsClient,
userActionService,
caseService,
Expand Down Expand Up @@ -342,12 +336,12 @@ export const addComment = async ({
status: updatedCase.status,
});

await casesClient.updateAlertsStatus({
await casesClientInternal.alerts.updateStatus({
alerts: alertsToUpdate,
});
}

await userActionService.postUserActions({
await userActionService.bulkCreate({
client: savedObjectsClient,
actions: [
buildCommentUserActionItem({
Expand Down
37 changes: 37 additions & 0 deletions x-pack/plugins/cases/server/client/attachments/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CaseResponse, CommentRequest as AttachmentsRequest } from '../../../common/api';
import { CasesClientArgs, CasesSubClientImplementation } from '../types';
import { addComment } from './add';

export interface AttachmentsAdd {
caseId: string;
comment: AttachmentsRequest;
}

export interface AttachmentsSubClient {
add(args: AttachmentsAdd): Promise<CaseResponse>;
}

export const createAttachmentsSubClient: CasesSubClientImplementation<AttachmentsSubClient> = (
args,
getClientsFactories
) => {
const { getCasesInternalClient } = getClientsFactories;
const attachmentSubClient: AttachmentsSubClient = {
add: ({ caseId, comment }: AttachmentsAdd) =>
addComment({
...args,
getCasesInternalClient,
caseId,
comment,
}),
};

return attachmentSubClient;
};
112 changes: 112 additions & 0 deletions x-pack/plugins/cases/server/client/cases/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { ActionsClient } from '../../../../actions/server';
import {
CasePostRequest,
CaseResponse,
CasesPatchRequest,
CasesResponse,
CasesFindRequest,
CasesFindResponse,
} from '../../../common/api';
import { CasesSubClientImplementation } from '../types';
import { create } from './create';
import { find } from './find';
import { get } from './get';
import { push } from './push';
import { update } from './update';

export interface CaseGet {
id: string;
includeComments?: boolean;
includeSubCaseComments?: boolean;
}

export interface CasePush {
actionsClient: ActionsClient;
caseId: string;
connectorId: string;
}

export interface CasesSubClient {
create(theCase: CasePostRequest): Promise<CaseResponse>;
find(args: CasesFindRequest): Promise<CasesFindResponse>;
get(args: CaseGet): Promise<CaseResponse>;
push(args: CasePush): Promise<CaseResponse>;
update(args: CasesPatchRequest): Promise<CasesResponse>;
}

export const createCasesSubClient: CasesSubClientImplementation<CasesSubClient> = (
args,
getClientsFactories
) => {
const {
caseConfigureService,
caseService,
user,
savedObjectsClient,
userActionService,
logger,
authorization,
} = args;

const { getCasesClient, getCasesInternalClient } = getClientsFactories;

const casesSubClient: CasesSubClient = {
create: (theCase: CasePostRequest) =>
create({
savedObjectsClient,
caseService,
caseConfigureService,
userActionService,
user,
theCase,
logger,
auth: authorization,
}),
find: (options: CasesFindRequest) =>
find({
savedObjectsClient,
caseService,
logger,
auth: authorization,
options,
}),
get: (params: CaseGet) =>
get({
...params,
caseService,
savedObjectsClient,
logger,
}),
push: (params: CasePush) =>
push({
...params,
savedObjectsClient,
caseService,
userActionService,
user,
getCasesClient,
getCasesInternalClient,
caseConfigureService,
logger,
}),
update: (cases: CasesPatchRequest) =>
update({
savedObjectsClient,
caseService,
userActionService,
user,
cases,
getCasesInternalClient,
logger,
}),
};

return casesSubClient;
};
Loading

0 comments on commit c6ccce4

Please sign in to comment.