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

[SIEM][CASE] Add missing AbortController to API calls #61426

Merged
merged 2 commits into from
Mar 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 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 @@ -19,7 +19,9 @@ import {
ServiceConnectorCaseResponse,
ActionTypeExecutorResult,
} from '../../../../../../plugins/case/common/api';

import { KibanaServices } from '../../lib/kibana';

import {
ActionLicense,
AllCases,
Expand All @@ -30,7 +32,9 @@ import {
SortFieldCase,
CaseUserActions,
} from './types';

import { CASES_URL } from './constants';

import {
convertToCamelCase,
convertAllCasesToCamel,
Expand All @@ -43,12 +47,17 @@ import {
decodeServiceConnectorCaseResponse,
} from './utils';

export const getCase = async (caseId: string, includeComments: boolean = true): Promise<Case> => {
export const getCase = async (
caseId: string,
includeComments: boolean = true,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(`${CASES_URL}/${caseId}`, {
method: 'GET',
query: {
includeComments,
},
signal,
});
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};
Expand All @@ -64,9 +73,10 @@ export const getCasesStatus = async (signal: AbortSignal): Promise<CasesStatus>
return convertToCamelCase<CasesStatusResponse, CasesStatus>(decodeCasesStatusResponse(response));
};

export const getTags = async (): Promise<string[]> => {
export const getTags = async (signal: AbortSignal): Promise<string[]> => {
const response = await KibanaServices.get().http.fetch<string[]>(`${CASES_URL}/tags`, {
method: 'GET',
signal,
});
return response ?? [];
};
Expand Down Expand Up @@ -106,6 +116,7 @@ export const getCases = async ({
sortField: SortFieldCase.createdAt,
sortOrder: 'desc',
},
signal,
}: FetchCasesProps): Promise<AllCases> => {
const query = {
reporters: filterOptions.reporters.map(r => r.username),
Expand All @@ -117,44 +128,57 @@ export const getCases = async ({
const response = await KibanaServices.get().http.fetch<CasesFindResponse>(`${CASES_URL}/_find`, {
method: 'GET',
query,
signal,
});
return convertAllCasesToCamel(decodeCasesFindResponse(response));
};

export const postCase = async (newCase: CasePostRequest): Promise<Case> => {
export const postCase = async (newCase: CasePostRequest, signal: AbortSignal): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(CASES_URL, {
method: 'POST',
body: JSON.stringify(newCase),
signal,
});
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

export const patchCase = async (
caseId: string,
updatedCase: Pick<CasePatchRequest, 'description' | 'status' | 'tags' | 'title'>,
version: string
version: string,
signal: AbortSignal
): Promise<Case[]> => {
const response = await KibanaServices.get().http.fetch<CasesResponse>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases: [{ ...updatedCase, id: caseId, version }] }),
signal,
});
return convertToCamelCase<CasesResponse, Case[]>(decodeCasesResponse(response));
};

export const patchCasesStatus = async (cases: BulkUpdateStatus[]): Promise<Case[]> => {
export const patchCasesStatus = async (
cases: BulkUpdateStatus[],
signal: AbortSignal
): Promise<Case[]> => {
const response = await KibanaServices.get().http.fetch<CasesResponse>(CASES_URL, {
method: 'PATCH',
body: JSON.stringify({ cases }),
signal,
});
return convertToCamelCase<CasesResponse, Case[]>(decodeCasesResponse(response));
};

export const postComment = async (newComment: CommentRequest, caseId: string): Promise<Case> => {
export const postComment = async (
newComment: CommentRequest,
caseId: string,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(
`${CASES_URL}/${caseId}/comments`,
{
method: 'POST',
body: JSON.stringify(newComment),
signal,
}
);
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
Expand All @@ -164,22 +188,25 @@ export const patchComment = async (
caseId: string,
commentId: string,
commentUpdate: string,
version: string
version: string,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(
`${CASES_URL}/${caseId}/comments`,
{
method: 'PATCH',
body: JSON.stringify({ comment: commentUpdate, id: commentId, version }),
signal,
}
);
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

export const deleteCases = async (caseIds: string[]): Promise<boolean> => {
export const deleteCases = async (caseIds: string[], signal: AbortSignal): Promise<boolean> => {
const response = await KibanaServices.get().http.fetch<string>(CASES_URL, {
method: 'DELETE',
query: { ids: JSON.stringify(caseIds) },
signal,
});
return response === 'true' ? true : false;
};
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/siem/public/containers/case/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export interface ElasticUser {
readonly username: string;
}

export interface FetchCasesProps {
export interface FetchCasesProps extends ApiProps {
queryParams?: QueryParams;
filterOptions?: FilterOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ export const useUpdateCases = (): UseUpdateCase => {

const dispatchUpdateCases = useCallback((cases: BulkUpdateStatus[]) => {
let cancel = false;
const abortCtrl = new AbortController();

const patchData = async () => {
try {
dispatch({ type: 'FETCH_INIT' });
await patchCasesStatus(cases);
await patchCasesStatus(cases, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: true });
}
Expand All @@ -87,6 +89,7 @@ export const useUpdateCases = (): UseUpdateCase => {
patchData();
return () => {
cancel = true;
abortCtrl.abort();
};
}, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ export const useDeleteCases = (): UseDeleteCase => {

const dispatchDeleteCases = useCallback((caseIds: string[]) => {
let cancel = false;
const abortCtrl = new AbortController();

const deleteData = async () => {
try {
dispatch({ type: 'FETCH_INIT' });
await deleteCases(caseIds);
await deleteCases(caseIds, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: true });
}
Expand All @@ -94,6 +96,7 @@ export const useDeleteCases = (): UseDeleteCase => {
};
deleteData();
return () => {
abortCtrl.abort();
cancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ export const useGetCase = (caseId: string): UseGetCase => {

const callFetch = useCallback(async () => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT' });
try {
const response = await getCase(caseId);
const response = await getCase(caseId, true, abortCtrl.signal);
if (!didCancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: response });
}
Expand All @@ -113,6 +115,7 @@ export const useGetCase = (caseId: string): UseGetCase => {
fetchData();
return () => {
didCancel = true;
abortCtrl.abort();
};
}, [caseId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,15 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {

const fetchCases = useCallback((filterOptions: FilterOptions, queryParams: QueryParams) => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT', payload: 'cases' });
try {
const response = await getCases({
filterOptions,
queryParams,
signal: abortCtrl.signal,
});
if (!didCancel) {
dispatch({
Expand All @@ -175,6 +178,7 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
}, []);
Expand All @@ -187,13 +191,17 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
const dispatchUpdateCaseProperty = useCallback(
({ updateKey, updateValue, caseId, refetchCasesStatus, version }: UpdateCase) => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT', payload: 'caseUpdate' });
try {
await patchCase(
caseId,
{ [updateKey]: updateValue },
version ?? '' // saved object versions are typed as string | undefined, hope that's not true
// saved object versions are typed as string | undefined, hope that's not true
version ?? '',
abortCtrl.signal
);
if (!didCancel) {
dispatch({ type: 'FETCH_UPDATE_CASE_SUCCESS' });
Expand All @@ -209,6 +217,7 @@ export const useGetCases = (initialQueryParams?: QueryParams): UseGetCases => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export const useGetTags = (): TagsState => {

useEffect(() => {
let didCancel = false;
const abortCtrl = new AbortController();

const fetchData = async () => {
dispatch({ type: 'FETCH_INIT' });
try {
const response = await getTags();
const response = await getTags(abortCtrl.signal);
if (!didCancel) {
dispatch({ type: 'FETCH_SUCCESS', payload: response });
}
Expand All @@ -77,6 +79,7 @@ export const useGetTags = (): TagsState => {
};
fetchData();
return () => {
abortCtrl.abort();
didCancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ export const usePostCase = (): UsePostCase => {

const postMyCase = useCallback(async (data: CasePostRequest) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT' });
const response = await postCase(data);
const response = await postCase(data, abortCtrl.signal);
if (!cancel) {
dispatch({
type: 'FETCH_SUCCESS',
Expand All @@ -81,6 +83,7 @@ export const usePostCase = (): UsePostCase => {
}
}
return () => {
abortCtrl.abort();
cancel = true;
};
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ export const usePostComment = (caseId: string): UsePostComment => {
const postMyComment = useCallback(
async (data: CommentRequest, updateCase: (newCase: Case) => void) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT' });
const response = await postComment(data, caseId);
const response = await postComment(data, caseId, abortCtrl.signal);
if (!cancel) {
dispatch({ type: 'FETCH_SUCCESS' });
updateCase(response);
Expand All @@ -73,6 +75,7 @@ export const usePostComment = (caseId: string): UsePostComment => {
}
}
return () => {
abortCtrl.abort();
cancel = true;
};
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const usePostPushToService = (): UsePostPushToService => {
const abortCtrl = new AbortController();
try {
dispatch({ type: 'FETCH_INIT' });
const casePushData = await getCase(caseId);
const casePushData = await getCase(caseId, true, abortCtrl.signal);
const responseService = await pushToService(
connectorId,
formatServiceRequestData(casePushData),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ export const useUpdateCase = ({ caseId }: { caseId: string }): UseUpdateCase =>
const dispatchUpdateCaseProperty = useCallback(
async ({ fetchCaseUserActions, updateKey, updateValue, updateCase, version }: UpdateByKey) => {
let cancel = false;
const abortCtrl = new AbortController();

try {
dispatch({ type: 'FETCH_INIT', payload: updateKey });
const response = await patchCase(caseId, { [updateKey]: updateValue }, version);
const response = await patchCase(
caseId,
{ [updateKey]: updateValue },
version,
abortCtrl.signal
);
if (!cancel) {
if (fetchCaseUserActions != null) {
fetchCaseUserActions(caseId);
Expand All @@ -100,6 +107,7 @@ export const useUpdateCase = ({ caseId }: { caseId: string }): UseUpdateCase =>
}
return () => {
cancel = true;
abortCtrl.abort();
};
},
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ export const useUpdateComment = (): UseUpdateComment => {
version,
}: UpdateComment) => {
let cancel = false;
const abortCtrl = new AbortController();
try {
dispatch({ type: 'FETCH_INIT', payload: commentId });
const response = await patchComment(caseId, commentId, commentUpdate, version);
const response = await patchComment(
caseId,
commentId,
commentUpdate,
version,
abortCtrl.signal
);
if (!cancel) {
updateCase(response);
fetchUserActions();
Expand All @@ -101,6 +108,7 @@ export const useUpdateComment = (): UseUpdateComment => {
}
return () => {
cancel = true;
abortCtrl.abort();
};
},
[]
Expand Down