Skip to content

Commit

Permalink
[SIEM][CASE] Server common constants (#63952)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas authored Apr 21, 2020
1 parent 3b1d0e0 commit 6987171
Show file tree
Hide file tree
Showing 39 changed files with 207 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

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

import { CASES_URL } from '../../../../../../plugins/case/common/constants';

import {
deleteCases,
getActionLicense,
Expand All @@ -22,6 +25,7 @@ import {
pushCase,
pushToService,
} from './api';

import {
actionLicenses,
allCases,
Expand All @@ -44,7 +48,7 @@ import {
caseUserActionsSnake,
casesStatusSnake,
} from './mock';
import { CASES_URL } from './constants';

import { DEFAULT_FILTER_OPTIONS, DEFAULT_QUERY_PARAMS } from './use_get_cases';
import * as i18n from './translations';

Expand Down
55 changes: 31 additions & 24 deletions x-pack/legacy/plugins/siem/public/containers/case/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ import {
ActionTypeExecutorResult,
} from '../../../../../../plugins/case/common/api';

import {
CASE_STATUS_URL,
CASES_URL,
CASE_TAGS_URL,
CASE_REPORTERS_URL,
ACTION_TYPES_URL,
ACTION_URL,
} from '../../../../../../plugins/case/common/constants';

import {
getCaseDetailsUrl,
getCaseUserActionUrl,
getCaseCommentsUrl,
} from '../../../../../../plugins/case/common/api/helpers';

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

import {
Expand All @@ -33,8 +48,6 @@ import {
CaseUserActions,
} from './types';

import { CASES_URL } from './constants';

import {
convertToCamelCase,
convertAllCasesToCamel,
Expand All @@ -54,7 +67,7 @@ export const getCase = async (
includeComments: boolean = true,
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(`${CASES_URL}/${caseId}`, {
const response = await KibanaServices.get().http.fetch<CaseResponse>(getCaseDetailsUrl(caseId), {
method: 'GET',
query: {
includeComments,
Expand All @@ -65,26 +78,23 @@ export const getCase = async (
};

export const getCasesStatus = async (signal: AbortSignal): Promise<CasesStatus> => {
const response = await KibanaServices.get().http.fetch<CasesStatusResponse>(
`${CASES_URL}/status`,
{
method: 'GET',
signal,
}
);
const response = await KibanaServices.get().http.fetch<CasesStatusResponse>(CASE_STATUS_URL, {
method: 'GET',
signal,
});
return convertToCamelCase<CasesStatusResponse, CasesStatus>(decodeCasesStatusResponse(response));
};

export const getTags = async (signal: AbortSignal): Promise<string[]> => {
const response = await KibanaServices.get().http.fetch<string[]>(`${CASES_URL}/tags`, {
const response = await KibanaServices.get().http.fetch<string[]>(CASE_TAGS_URL, {
method: 'GET',
signal,
});
return response ?? [];
};

export const getReporters = async (signal: AbortSignal): Promise<User[]> => {
const response = await KibanaServices.get().http.fetch<User[]>(`${CASES_URL}/reporters`, {
const response = await KibanaServices.get().http.fetch<User[]>(CASE_REPORTERS_URL, {
method: 'GET',
signal,
});
Expand All @@ -96,7 +106,7 @@ export const getCaseUserActions = async (
signal: AbortSignal
): Promise<CaseUserActions[]> => {
const response = await KibanaServices.get().http.fetch<CaseUserActionsResponse>(
`${CASES_URL}/${caseId}/user_actions`,
getCaseUserActionUrl(caseId),
{
method: 'GET',
signal,
Expand Down Expand Up @@ -193,14 +203,11 @@ export const patchComment = async (
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,
}
);
const response = await KibanaServices.get().http.fetch<CaseResponse>(getCaseCommentsUrl(caseId), {
method: 'PATCH',
body: JSON.stringify({ comment: commentUpdate, id: commentId, version }),
signal,
});
return convertToCamelCase<CaseResponse, Case>(decodeCaseResponse(response));
};

Expand All @@ -219,7 +226,7 @@ export const pushCase = async (
signal: AbortSignal
): Promise<Case> => {
const response = await KibanaServices.get().http.fetch<CaseResponse>(
`${CASES_URL}/${caseId}/_push`,
`${getCaseDetailsUrl(caseId)}/_push`,
{
method: 'POST',
body: JSON.stringify(push),
Expand All @@ -235,7 +242,7 @@ export const pushToService = async (
signal: AbortSignal
): Promise<ServiceConnectorCaseResponse> => {
const response = await KibanaServices.get().http.fetch<ActionTypeExecutorResult>(
`/api/action/${connectorId}/_execute`,
`${ACTION_URL}/${connectorId}/_execute`,
{
method: 'POST',
body: JSON.stringify({ params: casePushParams }),
Expand All @@ -251,7 +258,7 @@ export const pushToService = async (
};

export const getActionLicense = async (signal: AbortSignal): Promise<ActionLicense[]> => {
const response = await KibanaServices.get().http.fetch<ActionLicense[]>(`/api/action/types`, {
const response = await KibanaServices.get().http.fetch<ActionLicense[]>(ACTION_TYPES_URL, {
method: 'GET',
signal,
});
Expand Down
23 changes: 12 additions & 11 deletions x-pack/legacy/plugins/siem/public/containers/case/configure/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,27 @@ import {
} from '../../../../../../../plugins/case/common/api';
import { KibanaServices } from '../../../lib/kibana';

import { CASES_CONFIGURE_URL } from '../constants';
import {
CASE_CONFIGURE_CONNECTORS_URL,
CASE_CONFIGURE_URL,
} from '../../../../../../../plugins/case/common/constants';

import { ApiProps } from '../types';
import { convertToCamelCase, decodeCaseConfigureResponse } from '../utils';
import { CaseConfigure } from './types';

export const fetchConnectors = async ({ signal }: ApiProps): Promise<Connector[]> => {
const response = await KibanaServices.get().http.fetch(
`${CASES_CONFIGURE_URL}/connectors/_find`,
{
method: 'GET',
signal,
}
);
const response = await KibanaServices.get().http.fetch(`${CASE_CONFIGURE_CONNECTORS_URL}/_find`, {
method: 'GET',
signal,
});

return response;
};

export const getCaseConfigure = async ({ signal }: ApiProps): Promise<CaseConfigure | null> => {
const response = await KibanaServices.get().http.fetch<CasesConfigureResponse>(
CASES_CONFIGURE_URL,
CASE_CONFIGURE_URL,
{
method: 'GET',
signal,
Expand All @@ -51,7 +52,7 @@ export const postCaseConfigure = async (
signal: AbortSignal
): Promise<CaseConfigure> => {
const response = await KibanaServices.get().http.fetch<CasesConfigureResponse>(
CASES_CONFIGURE_URL,
CASE_CONFIGURE_URL,
{
method: 'POST',
body: JSON.stringify(caseConfiguration),
Expand All @@ -68,7 +69,7 @@ export const patchCaseConfigure = async (
signal: AbortSignal
): Promise<CaseConfigure> => {
const response = await KibanaServices.get().http.fetch<CasesConfigureResponse>(
CASES_CONFIGURE_URL,
CASE_CONFIGURE_URL,
{
method: 'PATCH',
body: JSON.stringify(caseConfiguration),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@
* you may not use this file except in compliance with the Elastic License.
*/

export const CASES_URL = `/api/cases`;
export const CASES_CONFIGURE_URL = `/api/cases/configure`;
export const DEFAULT_TABLE_ACTIVE_PAGE = 1;
export const DEFAULT_TABLE_LIMIT = 5;
28 changes: 28 additions & 0 deletions x-pack/plugins/case/common/api/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 {
CASE_DETAILS_URL,
CASE_COMMENTS_URL,
CASE_USER_ACTIONS_URL,
CASE_COMMENT_DETAILS_URL,
} from '../constants';

export const getCaseDetailsUrl = (id: string): string => {
return CASE_DETAILS_URL.replace('{case_id}', id);
};

export const getCaseCommentsUrl = (id: string): string => {
return CASE_COMMENTS_URL.replace('{case_id}', id);
};

export const getCaseCommentDetailsUrl = (caseId: string, commentId: string): string => {
return CASE_COMMENT_DETAILS_URL.replace('{case_id}', caseId).replace('{comment_id}', commentId);
};

export const getCaseUserActionUrl = (id: string): string => {
return CASE_USER_ACTIONS_URL.replace('{case_id}', id);
};
29 changes: 29 additions & 0 deletions x-pack/plugins/case/common/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.
*/

export const APP_ID = 'case';

/**
* Case routes
*/

export const CASES_URL = '/api/cases';
export const CASE_DETAILS_URL = `${CASES_URL}/{case_id}`;
export const CASE_CONFIGURE_URL = `${CASES_URL}/configure`;
export const CASE_CONFIGURE_CONNECTORS_URL = `${CASE_CONFIGURE_URL}/connectors`;
export const CASE_COMMENTS_URL = `${CASE_DETAILS_URL}/comments`;
export const CASE_COMMENT_DETAILS_URL = `${CASE_DETAILS_URL}/comments/{comment_id}`;
export const CASE_REPORTERS_URL = `${CASES_URL}/reporters`;
export const CASE_STATUS_URL = `${CASES_URL}/status`;
export const CASE_TAGS_URL = `${CASES_URL}/tags`;
export const CASE_USER_ACTIONS_URL = `${CASE_DETAILS_URL}/user_actions`;

/**
* Action routes
*/

export const ACTION_URL = '/api/action';
export const ACTION_TYPES_URL = '/api/action/types';
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { schema } from '@kbn/config-schema';
import { buildCommentUserActionItem } from '../../../../services/user_actions/helpers';
import { RouteDeps } from '../../types';
import { wrapError } from '../../utils';
import { CASE_COMMENTS_URL } from '../../../../../common/constants';

export function initDeleteAllCommentsApi({ caseService, router, userActionService }: RouteDeps) {
router.delete(
{
path: '/api/cases/{case_id}/comments',
path: CASE_COMMENTS_URL,
validate: {
params: schema.object({
case_id: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
mockCaseComments,
} from '../../__fixtures__';
import { initDeleteCommentApi } from './delete_comment';
import { CASE_COMMENT_DETAILS_URL } from '../../../../../common/constants';

describe('DELETE comment', () => {
let routeHandler: RequestHandler<any, any, any>;
Expand All @@ -23,7 +24,7 @@ describe('DELETE comment', () => {
});
it(`deletes the comment. responds with 204`, async () => {
const request = httpServerMock.createKibanaRequest({
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
method: 'delete',
params: {
case_id: 'mock-id-1',
Expand All @@ -43,7 +44,7 @@ describe('DELETE comment', () => {
});
it(`returns an error when thrown from deleteComment service`, async () => {
const request = httpServerMock.createKibanaRequest({
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
method: 'delete',
params: {
case_id: 'mock-id-1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import { CASE_SAVED_OBJECT } from '../../../../saved_object_types';
import { buildCommentUserActionItem } from '../../../../services/user_actions/helpers';
import { RouteDeps } from '../../types';
import { wrapError } from '../../utils';
import { CASE_COMMENT_DETAILS_URL } from '../../../../../common/constants';

export function initDeleteCommentApi({ caseService, router, userActionService }: RouteDeps) {
router.delete(
{
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
validate: {
params: schema.object({
case_id: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import {
} from '../../../../../common/api';
import { RouteDeps } from '../../types';
import { escapeHatch, transformComments, wrapError } from '../../utils';
import { CASE_COMMENTS_URL } from '../../../../../common/constants';

export function initFindCaseCommentsApi({ caseService, router }: RouteDeps) {
router.get(
{
path: '/api/cases/{case_id}/comments/_find',
path: `${CASE_COMMENTS_URL}/_find`,
validate: {
params: schema.object({
case_id: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { schema } from '@kbn/config-schema';
import { AllCommentsResponseRt } from '../../../../../common/api';
import { RouteDeps } from '../../types';
import { flattenCommentSavedObjects, wrapError } from '../../utils';
import { CASE_COMMENTS_URL } from '../../../../../common/constants';

export function initGetAllCommentsApi({ caseService, router }: RouteDeps) {
router.get(
{
path: '/api/cases/{case_id}/comments',
path: CASE_COMMENTS_URL,
validate: {
params: schema.object({
case_id: schema.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '../../__fixtures__';
import { flattenCommentSavedObject } from '../../utils';
import { initGetCommentApi } from './get_comment';
import { CASE_COMMENT_DETAILS_URL } from '../../../../../common/constants';

describe('GET comment', () => {
let routeHandler: RequestHandler<any, any, any>;
Expand All @@ -23,7 +24,7 @@ describe('GET comment', () => {
});
it(`returns the comment`, async () => {
const request = httpServerMock.createKibanaRequest({
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
method: 'get',
params: {
case_id: 'mock-id-1',
Expand All @@ -48,7 +49,7 @@ describe('GET comment', () => {
});
it(`returns an error when getComment throws`, async () => {
const request = httpServerMock.createKibanaRequest({
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
method: 'get',
params: {
case_id: 'mock-id-1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { schema } from '@kbn/config-schema';
import { CommentResponseRt } from '../../../../../common/api';
import { RouteDeps } from '../../types';
import { flattenCommentSavedObject, wrapError } from '../../utils';
import { CASE_COMMENT_DETAILS_URL } from '../../../../../common/constants';

export function initGetCommentApi({ caseService, router }: RouteDeps) {
router.get(
{
path: '/api/cases/{case_id}/comments/{comment_id}',
path: CASE_COMMENT_DETAILS_URL,
validate: {
params: schema.object({
case_id: schema.string(),
Expand Down
Loading

0 comments on commit 6987171

Please sign in to comment.