Skip to content

Commit

Permalink
clean up + review
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierM committed Mar 23, 2020
1 parent 3079c91 commit c14f1d4
Show file tree
Hide file tree
Showing 27 changed files with 218 additions and 191 deletions.
16 changes: 8 additions & 8 deletions x-pack/legacy/plugins/siem/public/containers/case/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import {
CommentResponse,
User,
CaseUserActionsResponse,
CasePushRequest,
PushCaseParams,
PushCaseResponse,
CaseExternalServiceRequest,
ServiceConnectorCaseParams,
ServiceConnectorCaseResponse,
ActionTypeExecutorResult,
} from '../../../../../../plugins/case/common/api';
import { KibanaServices } from '../../lib/kibana';
Expand All @@ -42,7 +42,7 @@ import {
decodeCasesStatusResponse,
decodeCommentResponse,
decodeCaseUserActionsResponse,
decodePushCaseResponse,
decodeServiceConnectorCaseResponse,
} from './utils';

export const getCase = async (caseId: string, includeComments: boolean = true): Promise<Case> => {
Expand Down Expand Up @@ -188,7 +188,7 @@ export const deleteCases = async (caseIds: string[]): Promise<boolean> => {

export const pushCase = async (
caseId: string,
push: CasePushRequest,
push: CaseExternalServiceRequest,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(
Expand All @@ -204,9 +204,9 @@ export const pushCase = async (

export const pushToService = async (
connectorId: string,
casePushParams: PushCaseParams,
casePushParams: ServiceConnectorCaseParams,
signal: AbortSignal
): Promise<PushCaseResponse> => {
): Promise<ServiceConnectorCaseResponse> => {
const response = await KibanaServices.get().http.fetch<ActionTypeExecutorResult>(
`/api/action/${connectorId}/_execute`,
{
Expand All @@ -215,7 +215,7 @@ export const pushToService = async (
signal,
}
);
return decodePushCaseResponse(response.data);
return decodeServiceConnectorCaseResponse(response.data);
};

export const getActionLicense = async (signal: AbortSignal): Promise<ActionLicense[]> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export interface ReturnUseCaseConfigure {
}

interface UseCaseConfigure {
setConnectorId: (newConnectorId: string, newConnectorName?: string) => void;
setConnector: (newConnectorId: string, newConnectorName?: string) => void;
setClosureType?: (newClosureType: ClosureType) => void;
}

export const useCaseConfigure = ({
setConnectorId,
setConnector,
setClosureType,
}: UseCaseConfigure): ReturnUseCaseConfigure => {
const [, dispatchToaster] = useStateToaster();
Expand All @@ -49,7 +49,7 @@ export const useCaseConfigure = ({
if (!didCancel) {
setLoading(false);
if (res != null) {
setConnectorId(res.connectorId, res.connectorName);
setConnector(res.connectorId, res.connectorName);
if (setClosureType != null) {
setClosureType(res.closureType);
}
Expand Down Expand Up @@ -99,7 +99,7 @@ export const useCaseConfigure = ({
);
if (!didCancel) {
setPersistLoading(false);
setConnectorId(res.connectorId);
setConnector(res.connectorId);
if (setClosureType) {
setClosureType(res.closureType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ export const TAG_FETCH_FAILURE = i18n.translate(
defaultMessage: 'Failed to fetch Tags',
}
);

export const SUCCESS_SEND_TO_EXTERNAL_SERVICE = i18n.translate(
'xpack.siem.containers.case.pushToExterService',
{
defaultMessage: 'Successfully sent to ServiceNow',
}
);
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/siem/public/containers/case/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export interface CaseUserActions {
oldValue: string | null;
}

export interface CasePush {
at: string;
by: string;
export interface CaseExternalService {
pushedAt: string;
pushedBy: string;
connectorId: string;
connectorName: string;
externalId: string;
Expand All @@ -46,7 +46,7 @@ export interface Case {
createdAt: string;
createdBy: ElasticUser;
description: string;
pushed: CasePush | null;
externalService: CaseExternalService | null;
status: string;
tags: string[];
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const initialData: Case = {
username: '',
},
description: '',
pushed: null,
externalService: null,
status: '',
tags: [],
title: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { isEmpty } from 'lodash/fp';
import { isEmpty, uniqBy } from 'lodash/fp';
import { useCallback, useEffect, useState } from 'react';

import { errorToToaster, useStateToaster } from '../../components/toasters';
import { getCaseUserActions } from './api';
import * as i18n from './translations';
import { CaseUserActions } from './types';
import { CaseUserActions, ElasticUser } from './types';

interface CaseUserActionsState {
caseUserActions: CaseUserActions[];
firstIndexPushToService: number;
hasDataToPush: boolean;
participants: ElasticUser[];
isLoading: boolean;
isError: boolean;
lastIndexPushToService: number;
Expand All @@ -28,6 +29,7 @@ const initialData: CaseUserActionsState = {
hasDataToPush: false,
isLoading: true,
isError: false,
participants: [],
};

interface UseGetCaseUserActions extends CaseUserActionsState {
Expand Down Expand Up @@ -75,12 +77,16 @@ export const useGetCaseUserActions = (caseId: string): UseGetCaseUserActions =>
// Attention Future developer
// We are removing the first item because it will always be the creation of the case
// and we do not want it to simplify our life
const participants = !isEmpty(response)
? uniqBy('actionBy.username', response).map(cau => cau.actionBy)
: [];
const caseUserActions = !isEmpty(response) ? response.slice(1) : [];
setCaseUserActionsState({
caseUserActions,
...getPushedInfo(caseUserActions),
isLoading: false,
isError: false,
participants,
});
}
} catch (error) {
Expand All @@ -97,6 +103,7 @@ export const useGetCaseUserActions = (caseId: string): UseGetCaseUserActions =>
hasDataToPush: false,
isLoading: false,
isError: true,
participants: [],
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

import { useReducer, useCallback } from 'react';

import { PushCaseResponse, PushCaseParams } from '../../../../../../plugins/case/common/api';
import { errorToToaster, useStateToaster } from '../../components/toasters';
import {
ServiceConnectorCaseResponse,
ServiceConnectorCaseParams,
} from '../../../../../../plugins/case/common/api';
import { errorToToaster, useStateToaster, displaySuccessToast } from '../../components/toasters';

import { pushToService, pushCase } from './api';
import { getCase, pushToService, pushCase } from './api';
import * as i18n from './translations';
import { Case } from './types';

interface PushToServiceState {
serviceData: PushCaseResponse | null;
serviceData: ServiceConnectorCaseResponse | null;
pushedCaseData: Case | null;
isLoading: boolean;
isError: boolean;
}
type Action =
| { type: 'FETCH_INIT' }
| { type: 'FETCH_SUCCESS_PUSH_SERVICE'; payload: PushCaseResponse | null }
| { type: 'FETCH_SUCCESS_PUSH_SERVICE'; payload: ServiceConnectorCaseResponse | null }
| { type: 'FETCH_SUCCESS_PUSH_CASE'; payload: Case | null }
| { type: 'FETCH_FAILURE' };

Expand Down Expand Up @@ -59,14 +62,14 @@ const dataFetchReducer = (state: PushToServiceState, action: Action): PushToServ
};

interface PushToServiceRequest {
caseId: string;
connectorId: string;
connectorName: string;
caseToPush: Case;
updateCase: (newCase: Case) => void;
}

interface UsePostPushToService extends PushToServiceState {
postPushToService: ({ caseToPush, connectorId, updateCase }: PushToServiceRequest) => void;
postPushToService: ({ caseId, connectorId, updateCase }: PushToServiceRequest) => void;
}

export const usePostPushToService = (): UsePostPushToService => {
Expand All @@ -79,18 +82,19 @@ export const usePostPushToService = (): UsePostPushToService => {
const [, dispatchToaster] = useStateToaster();

const postPushToService = useCallback(
async ({ caseToPush, connectorId, connectorName, updateCase }: PushToServiceRequest) => {
async ({ caseId, connectorId, connectorName, updateCase }: PushToServiceRequest) => {
let cancel = false;
const abortCtrl = new AbortController();
try {
dispatch({ type: 'FETCH_INIT' });
const casePushData = await getCase(caseId);
const responseService = await pushToService(
connectorId,
formatServiceRequestData(caseToPush),
formatServiceRequestData(casePushData),
abortCtrl.signal
);
const responseCase = await pushCase(
caseToPush.id,
caseId,
{
connector_id: connectorId,
connector_name: connectorName,
Expand All @@ -104,6 +108,7 @@ export const usePostPushToService = (): UsePostPushToService => {
dispatch({ type: 'FETCH_SUCCESS_PUSH_SERVICE', payload: responseService });
dispatch({ type: 'FETCH_SUCCESS_PUSH_CASE', payload: responseCase });
updateCase(responseCase);
displaySuccessToast(i18n.SUCCESS_SEND_TO_EXTERNAL_SERVICE, dispatchToaster);
}
} catch (error) {
if (!cancel) {
Expand All @@ -126,14 +131,14 @@ export const usePostPushToService = (): UsePostPushToService => {
return { ...state, postPushToService };
};

const formatServiceRequestData = (myCase: Case): PushCaseParams => {
const formatServiceRequestData = (myCase: Case): ServiceConnectorCaseParams => {
const {
id: caseId,
createdAt,
createdBy,
comments,
description,
pushed,
externalService,
title,
updatedAt,
updatedBy,
Expand Down Expand Up @@ -164,7 +169,7 @@ const formatServiceRequestData = (myCase: Case): PushCaseParams => {
: null,
})),
description,
incidentId: pushed?.externalId ?? null,
incidentId: externalService?.externalId ?? null,
title,
updatedAt,
updatedBy:
Expand Down
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/siem/public/containers/case/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import {
CaseConfigureResponseRt,
CaseUserActionsResponse,
CaseUserActionsResponseRt,
PushCaseResponseRt,
PushCaseResponse,
ServiceConnectorCaseResponseRt,
ServiceConnectorCaseResponse,
} from '../../../../../../plugins/case/common/api';
import { ToasterError } from '../../components/toasters';
import { AllCases, Case } from './types';
Expand Down Expand Up @@ -97,8 +97,8 @@ export const decodeCaseUserActionsResponse = (respUserActions?: CaseUserActionsR
fold(throwErrors(createToasterPlainError), identity)
);

export const decodePushCaseResponse = (respPushCase?: PushCaseResponse) =>
export const decodeServiceConnectorCaseResponse = (respPushCase?: ServiceConnectorCaseResponse) =>
pipe(
PushCaseResponseRt.decode(respPushCase),
ServiceConnectorCaseResponseRt.decode(respPushCase),
fold(throwErrors(createToasterPlainError), identity)
);
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const useGetCasesMockState: UseGetCasesState = {
createdBy: { username: 'elastic' },
comments: [],
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'open',
tags: ['defacement'],
title: 'Another horrible breach',
Expand All @@ -37,7 +37,7 @@ export const useGetCasesMockState: UseGetCasesState = {
createdBy: { username: 'elastic' },
comments: [],
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'open',
tags: ['phishing'],
title: 'Bad email',
Expand All @@ -54,7 +54,7 @@ export const useGetCasesMockState: UseGetCasesState = {
createdBy: { username: 'elastic' },
comments: [],
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'open',
tags: ['phishing'],
title: 'Bad email',
Expand All @@ -71,7 +71,7 @@ export const useGetCasesMockState: UseGetCasesState = {
createdBy: { username: 'elastic' },
comments: [],
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'closed',
tags: ['phishing'],
title: 'Uh oh',
Expand All @@ -88,7 +88,7 @@ export const useGetCasesMockState: UseGetCasesState = {
createdBy: { username: 'elastic' },
comments: [],
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'open',
tags: ['phishing'],
title: 'Uh oh',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const caseProps: CaseProps = {
createdAt: '2020-02-13T19:44:23.627Z',
createdBy: { fullName: null, email: '[email protected]', username: 'elastic' },
description: 'Security banana Issue',
pushed: null,
externalService: null,
status: 'open',
tags: ['defacement'],
title: 'Another horrible breach!!',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ describe('CaseView ', () => {
isLoading: false,
isError: false,
lastIndexPushToService: -1,
participants: [data.createdBy],
};

const defaultUsePushToServiceMock = {
Expand Down
Loading

0 comments on commit c14f1d4

Please sign in to comment.