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] Enable case by default. Snake to camel on UI #57936

Merged
merged 22 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from 17 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
5 changes: 4 additions & 1 deletion x-pack/legacy/plugins/siem/public/components/links/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ const CaseDetailsLinkComponent: React.FC<{ children?: React.ReactNode; detailNam
children,
detailName,
}) => (
<EuiLink href={getCaseDetailsUrl(encodeURIComponent(detailName))}>
<EuiLink
href={getCaseDetailsUrl(encodeURIComponent(detailName))}
data-test-subj="case-details-link"
>
{children ? children : detailName}
</EuiLink>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('SIEM Navigation', () => {
detailName: undefined,
navTabs: {
case: {
disabled: true,
disabled: false,
href: '#/link-to/case',
id: 'case',
name: 'Case',
Expand Down Expand Up @@ -160,7 +160,7 @@ describe('SIEM Navigation', () => {
filters: [],
navTabs: {
case: {
disabled: true,
disabled: false,
href: '#/link-to/case',
id: 'case',
name: 'Case',
Expand Down
18 changes: 10 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 @@ -5,12 +5,13 @@
*/

import { KibanaServices } from '../../lib/kibana';
import { AllCases, FetchCasesProps, Case, NewCase, SortFieldCase } from './types';
import { FetchCasesProps, Case, NewCase, SortFieldCase, AllCases } from './types';
import { Direction } from '../../graphql/types';
import { throwIfNotOk } from '../../hooks/api/api';
import { CASES_URL } from './constants';
import { convertCaseToCamel, convertAllCasesToCamel, convertUpdateCaseToCamel } from './utils';

export const getCase = async (caseId: string, includeComments: boolean) => {
export const getCase = async (caseId: string, includeComments: boolean): Promise<Case> => {
const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}`, {
method: 'GET',
asResponse: true,
Expand All @@ -19,7 +20,7 @@ export const getCase = async (caseId: string, includeComments: boolean) => {
},
});
await throwIfNotOk(response.response);
return response.body!;
return convertCaseToCamel(response.body!);
};

export const getCases = async ({
Expand All @@ -46,7 +47,7 @@ export const getCases = async ({
asResponse: true,
});
await throwIfNotOk(response.response);
return response.body!;
return convertAllCasesToCamel(response.body!);
};

export const createCase = async (newCase: NewCase): Promise<Case> => {
Expand All @@ -56,18 +57,19 @@ export const createCase = async (newCase: NewCase): Promise<Case> => {
body: JSON.stringify(newCase),
});
await throwIfNotOk(response.response);
return response.body!;
return convertCaseToCamel(response.body!);
};

export const updateCaseProperty = async (
caseId: string,
updatedCase: Partial<Case>
updatedCase: Partial<Case>,
version: string
): Promise<Partial<Case>> => {
const response = await KibanaServices.get().http.fetch(`${CASES_URL}/${caseId}`, {
method: 'PATCH',
asResponse: true,
body: JSON.stringify(updatedCase),
body: JSON.stringify({ case: updatedCase, version }),
});
await throwIfNotOk(response.response);
return response.body!;
return convertUpdateCaseToCamel(response.body!);
};
38 changes: 32 additions & 6 deletions x-pack/legacy/plugins/siem/public/containers/case/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { Direction } from '../../graphql/types';
export { Direction };
interface FormData {
isNew?: boolean;
}
Expand All @@ -15,15 +16,28 @@ export interface NewCase extends FormData {
title: string;
}

export interface Case {
export interface CaseSnake {
case_id: string;
created_at: string;
created_by: ElasticUser;
created_by: ElasticUserSnake;
description: string;
state: string;
tags: string[];
title: string;
updated_at: string;
version?: string;
}

export interface Case {
caseId: string;
createdAt: string;
createdBy: ElasticUser;
description: string;
state: string;
tags: string[];
title: string;
updatedAt: string;
version?: string;
}

export interface QueryParams {
Expand All @@ -38,21 +52,33 @@ export interface FilterOptions {
tags: string[];
}

export interface AllCasesSnake {
cases: CaseSnake[];
page: number;
per_page: number;
total: number;
}

export interface AllCases {
cases: Case[];
page: number;
per_page: number;
perPage: number;
total: number;
}
export enum SortFieldCase {
createdAt = 'created_at',
createdAt = 'createdAt',
state = 'state',
updatedAt = 'updated_at',
updatedAt = 'updatedAt',
}

export interface ElasticUserSnake {
readonly username: string;
readonly full_name?: string | null;
}

export interface ElasticUser {
readonly username: string;
readonly full_name?: string;
readonly fullName?: string | null;
}

export interface FetchCasesProps {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ const dataFetchReducer = (state: CaseState, action: Action): CaseState => {
}
};
const initialData: Case = {
case_id: '',
created_at: '',
created_by: {
caseId: '',
createdAt: '',
createdBy: {
username: '',
},
description: '',
state: '',
tags: [],
title: '',
updated_at: '',
updatedAt: '',
};

export const useGetCase = (caseId: string): [CaseState] => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const dataFetchReducer = (state: UseGetCasesState, action: Action): UseGetCasesS

const initialData: AllCases = {
stephmilovic marked this conversation as resolved.
Show resolved Hide resolved
page: 0,
per_page: 0,
perPage: 0,
total: 0,
cases: [],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export const useUpdateCase = (
const updateData = async (updateKey: keyof Case) => {
dispatch({ type: FETCH_INIT });
try {
const response = await updateCaseProperty(caseId, { [updateKey]: state.data[updateKey] });
const response = await updateCaseProperty(
caseId,
{ [updateKey]: state.data[updateKey] },
state.data.version ?? '' // saved object versions are typed as string | undefined, hope that's not true
);
dispatch({ type: FETCH_SUCCESS, payload: response });
} catch (error) {
errorToToaster({ title: i18n.ERROR_TITLE, error, dispatchToaster });
Expand Down
68 changes: 68 additions & 0 deletions x-pack/legacy/plugins/siem/public/containers/case/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,72 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { AllCases, AllCasesSnake, Case, CaseSnake } from './types';

export const getTypedPayload = <T>(a: unknown): T => a as T;

export const convertCaseToCamel = (snakeCase: CaseSnake): Case => ({
stephmilovic marked this conversation as resolved.
Show resolved Hide resolved
caseId: snakeCase.case_id,
createdAt: snakeCase.created_at,
createdBy: {
username: snakeCase.created_by.username,
fullName: snakeCase.created_by.full_name,
},
description: snakeCase.description,
state: snakeCase.state,
tags: snakeCase.tags,
title: snakeCase.title,
updatedAt: snakeCase.updated_at,
version: snakeCase.version,
});

export const convertAllCasesToCamel = (snakeCases: AllCasesSnake): AllCases => ({
cases: snakeCases.cases.map(snakeCase => convertCaseToCamel(snakeCase)),
page: snakeCases.page,
perPage: snakeCases.per_page,
total: snakeCases.total,
});

export const convertUpdateCaseToCamel = (snakeCase: Partial<CaseSnake>): Partial<Case> => {
const updateCase: Partial<Case> = {};
Object.keys(snakeCase).forEach(key => {
switch (key) {
case 'case_id':
updateCase.caseId = snakeCase.case_id;
break;

case 'created_at':
updateCase.createdAt = snakeCase.created_at;
break;
case 'created_by':
if (snakeCase.created_by) {
updateCase.createdBy = {
username: snakeCase.created_by.username,
fullName: snakeCase.created_by.full_name,
};
}
break;
case 'description':
updateCase.description = snakeCase.description;
break;
case 'state':
updateCase.state = snakeCase.state;
break;
case 'tags':
updateCase.tags = snakeCase.tags;
break;
case 'title':
updateCase.title = snakeCase.title;
break;
case 'updated_at':
updateCase.updatedAt = snakeCase.updated_at;
break;
case 'version':
updateCase.version = snakeCase.version;
break;
default:
return null;
}
});
return updateCase;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { Direction, SortFieldCase } from '../../../../../containers/case/types';

export const useGetCasesMockState = {
data: {
cases: [
{
caseId: '3c4ddcc0-4e99-11ea-9290-35d05cb55c15',
createdAt: '2020-02-13T19:44:23.627Z',
createdBy: { username: 'elastic' },
description: 'Security banana Issue',
state: 'open',
tags: ['defacement'],
title: 'Another horrible breach',
updatedAt: '2020-02-13T19:44:23.627Z',
},
{
caseId: '362a5c10-4e99-11ea-9290-35d05cb55c15',
createdAt: '2020-02-13T19:44:13.328Z',
createdBy: { username: 'elastic' },
description: 'Security banana Issue',
state: 'open',
tags: ['phishing'],
title: 'Bad email',
updatedAt: '2020-02-13T19:44:13.328Z',
},
{
caseId: '34f8b9e0-4e99-11ea-9290-35d05cb55c15',
createdAt: '2020-02-13T19:44:11.328Z',
createdBy: { username: 'elastic' },
description: 'Security banana Issue',
state: 'open',
tags: ['phishing'],
title: 'Bad email',
updatedAt: '2020-02-13T19:44:11.328Z',
},
{
caseId: '31890e90-4e99-11ea-9290-35d05cb55c15',
createdAt: '2020-02-13T19:44:05.563Z',
createdBy: { username: 'elastic' },
description: 'Security banana Issue',
state: 'closed',
tags: ['phishing'],
title: 'Uh oh',
updatedAt: '2020-02-18T21:32:24.056Z',
},
{
caseId: '2f5b3210-4e99-11ea-9290-35d05cb55c15',
createdAt: '2020-02-13T19:44:01.901Z',
createdBy: { username: 'elastic' },
description: 'Security banana Issue',
state: 'open',
tags: ['phishing'],
title: 'Uh oh',
updatedAt: '2020-02-13T19:44:01.901Z',
},
],
page: 1,
perPage: 5,
total: 10,
},
isLoading: false,
isError: false,
queryParams: {
page: 1,
perPage: 5,
sortField: SortFieldCase.createdAt,
sortOrder: Direction.desc,
},
filterOptions: { search: '', tags: [] },
};
Loading