Skip to content

Commit

Permalink
[Security Solution][Endpoint] Add event filters summary card to the f…
Browse files Browse the repository at this point in the history
…leet endpoint tab (#100668)

* Shows event filters card on fleet page

* Uses aggs instead of while loop to retrieve summary data

* Add request and response types in the lists package

* Fixes old import

* Removes old i18n keys

* Removes more old i18n keys

* Use consts for exception lists url and endpoint event filter list id

* Uses event filters service to retrieve summary data

* Fixes addressed pr comments such as changing the route without underscore, adding aggs type, validating response, and more

* Uses useMemo instead of useState to memoize object

* Add new e2e test for summart endpoint

* Handle api errors on event filters and trusted apps summary api calls

* Add api error message to the toast

* Fix wrong i18n key

* Change span tag by react fragment

* Uses styled components instead of modify compontent style directly and small improvements on test -> ts

* Adds curls script for summary route

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
dasansol92 and kibanamachine committed May 28, 2021
1 parent 50e6387 commit 0979a1d
Show file tree
Hide file tree
Showing 29 changed files with 857 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export * from './read_exception_list_item_schema';
export * from './read_exception_list_schema';
export * from './read_list_item_schema';
export * from './read_list_schema';
export * from './summary_exception_list_schema';
export * from './update_endpoint_list_item_schema';
export * from './update_exception_list_item_schema';
export * from './update_exception_list_item_validation';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ID, LIST_ID, NAMESPACE_TYPE } from '../../constants/index.mock';

import { SummaryExceptionListSchema } from '.';

export const getSummaryExceptionListSchemaMock = (): SummaryExceptionListSchema => ({
id: ID,
list_id: LIST_ID,
namespace_type: NAMESPACE_TYPE,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { left } from 'fp-ts/lib/Either';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';

import { getSummaryExceptionListSchemaMock } from './index.mock';
import { SummaryExceptionListSchema, summaryExceptionListSchema } from '.';

describe('summary_exception_list_schema', () => {
test('it should validate a typical exception list request', () => {
const payload = getSummaryExceptionListSchemaMock();
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should accept an undefined for "id"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should accept an undefined for "list_id"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.list_id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should accept an undefined for "namespace_type" but default to "single"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.namespace_type;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(getSummaryExceptionListSchemaMock());
});

test('it should accept an undefined for "id", "list_id", "namespace_type" but default "namespace_type" to "single"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.id;
delete payload.namespace_type;
delete payload.list_id;
const output = getSummaryExceptionListSchemaMock();
delete output.id;
delete output.list_id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(output);
});

test('it should accept an undefined for "id", "list_id"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.id;
delete payload.list_id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should accept an undefined for "id", "namespace_type" but default "namespace_type" to "single"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.id;
delete payload.namespace_type;
const output = getSummaryExceptionListSchemaMock();
delete output.id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(output);
});

test('it should accept an undefined for "list_id", "namespace_type" but default "namespace_type" to "single"', () => {
const payload = getSummaryExceptionListSchemaMock();
delete payload.namespace_type;
delete payload.list_id;
const output = getSummaryExceptionListSchemaMock();
delete output.list_id;
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(output);
});

test('it should not allow an extra key to be sent in', () => {
const payload: SummaryExceptionListSchema & {
extraKey?: string;
} = getSummaryExceptionListSchemaMock();
payload.extraKey = 'some new value';
const decoded = summaryExceptionListSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = foldLeftRight(checked);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']);
expect(message.schema).toEqual({});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import * as t from 'io-ts';

import { NamespaceType } from '../../common/default_namespace';
import { RequiredKeepUndefined } from '../../common/required_keep_undefined';
import { id } from '../../common/id';
import { list_id } from '../../common/list_id';
import { namespace_type } from '../../common/namespace_type';

export const summaryExceptionListSchema = t.exact(
t.partial({
id,
list_id,
namespace_type, // defaults to 'single' if not set during decode
})
);

export type SummaryExceptionListSchema = t.OutputOf<typeof summaryExceptionListSchema>;

// This type is used after a decode since some things are defaults after a decode.
export type SummaryExceptionListSchemaDecoded = Omit<
RequiredKeepUndefined<t.TypeOf<typeof summaryExceptionListSchema>>,
'namespace_type'
> & {
namespace_type: NamespaceType;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ExceptionListSummarySchema } from '.';

export const getListSummaryResponseMock = (): ExceptionListSummarySchema => ({
windows: 0,
linux: 1,
macos: 2,
total: 3,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { left } from 'fp-ts/lib/Either';
import { pipe } from 'fp-ts/lib/pipeable';
import { exactCheck, foldLeftRight, getPaths } from '@kbn/securitysolution-io-ts-utils';

import { getListSummaryResponseMock } from './index.mock';
import { ExceptionListSummarySchema, exceptionListSummarySchema } from '.';

describe('list_summary_schema', () => {
test('it should validate a typical list summary response', () => {
const payload = getListSummaryResponseMock();
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([]);
expect(message.schema).toEqual(payload);
});

test('it should NOT accept an undefined for "windows"', () => {
const payload = getListSummaryResponseMock();
// @ts-expect-error
delete payload.windows;
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([
'Invalid value "undefined" supplied to "windows"',
]);
expect(message.schema).toEqual({});
});

test('it should NOT accept an undefined for "linux"', () => {
const payload = getListSummaryResponseMock();
// @ts-expect-error
delete payload.linux;
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([
'Invalid value "undefined" supplied to "linux"',
]);
expect(message.schema).toEqual({});
});

test('it should NOT accept an undefined for "macos"', () => {
const payload = getListSummaryResponseMock();
// @ts-expect-error
delete payload.macos;
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([
'Invalid value "undefined" supplied to "macos"',
]);
expect(message.schema).toEqual({});
});

test('it should NOT accept an undefined for "total"', () => {
const payload = getListSummaryResponseMock();
// @ts-expect-error
delete payload.total;
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);

expect(getPaths(left(message.errors))).toEqual([
'Invalid value "undefined" supplied to "total"',
]);
expect(message.schema).toEqual({});
});

test('it should not allow an extra key to be sent in', () => {
const payload: ExceptionListSummarySchema & {
extraKey?: string;
} = getListSummaryResponseMock();
payload.extraKey = 'some new value';
const decoded = exceptionListSummarySchema.decode(payload);
const checked = exactCheck(payload, decoded);
const message = pipe(checked, foldLeftRight);
expect(getPaths(left(message.errors))).toEqual(['invalid keys "extraKey"']);
expect(message.schema).toEqual({});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { PositiveInteger } from '@kbn/securitysolution-io-ts-types';
import * as t from 'io-ts';

export const exceptionListSummarySchema = t.exact(
t.type({
windows: PositiveInteger,
linux: PositiveInteger,
macos: PositiveInteger,
total: PositiveInteger,
})
);

export type ExceptionListSummarySchema = t.TypeOf<typeof exceptionListSummarySchema>;
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ export * from './found_list_item_schema';
export * from './found_list_schema';
export * from './list_item_schema';
export * from './list_schema';
export * from './exception_list_summary_schema';
export * from './list_item_index_exist_schema';
export * from './search_list_item_schema';
1 change: 1 addition & 0 deletions x-pack/plugins/lists/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export * from './read_list_index_route';
export * from './read_list_item_route';
export * from './read_list_route';
export * from './read_privileges_route';
export * from './summary_exception_list_route';
export * from './update_endpoint_list_item_route';
export * from './update_exception_list_item_route';
export * from './update_exception_list_route';
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/lists/server/routes/init_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
readListItemRoute,
readListRoute,
readPrivilegesRoute,
summaryExceptionListRoute,
updateEndpointListItemRoute,
updateExceptionListItemRoute,
updateExceptionListRoute,
Expand Down Expand Up @@ -95,4 +96,7 @@ export const initRoutes = (router: ListsPluginRouter, config: ConfigType): void
updateEndpointListItemRoute(router);
deleteEndpointListItemRoute(router);
findEndpointListItemRoute(router);

// exception list items summary
summaryExceptionListRoute(router);
};
Loading

0 comments on commit 0979a1d

Please sign in to comment.