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

[Security Solution] Add host isolation exception IPs UI #113762

Merged
merged 26 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b4ecec8
Initial empty form to add an exception
academo Oct 1, 2021
da94c6e
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
academo Oct 4, 2021
0fb4d41
WIP add form construction
academo Oct 4, 2021
c73b47f
Add code to handle the add form
academo Oct 4, 2021
671faa0
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
academo Oct 5, 2021
a4775ce
WIP create entry
academo Oct 5, 2021
1754d41
Working add
academo Oct 5, 2021
6fc871f
Add toast to create and failure
academo Oct 5, 2021
0378a57
Add validation for ipv4 and CIDR format
academo Oct 5, 2021
64669d8
Reload the list of exceptions after adding
academo Oct 5, 2021
dcbb5df
Remove unused import
academo Oct 5, 2021
02b2f11
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
academo Oct 5, 2021
02cbe0b
Replace mockclear with mockreset
Oct 6, 2021
becaa1a
Add tests for reducer and add host Isolation exception
Oct 6, 2021
82a47aa
Fix TS error
Oct 6, 2021
d18264e
Firsts test for form
Oct 6, 2021
3ccdf9e
Tests for form
Oct 6, 2021
138fca7
Remove unused dependencies
Oct 6, 2021
346dbf2
Add tests for for the form flyout
Oct 6, 2021
0e2d84b
Add tests for the add button
Oct 6, 2021
de6e673
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 7, 2021
e5e8b12
Fix PR comments
Oct 7, 2021
21dd6c7
Handle onCancel directly on the form flyout
Oct 7, 2021
d79ef0e
Fix dependency issue for hook
Oct 7, 2021
07c624a
Merge branch 'master' into feature/host-isolation-exception-add
kibanamachine Oct 11, 2021
6a697ea
Fix PR comments
Oct 11, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
FoundExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
Expand Down Expand Up @@ -65,6 +66,19 @@ export async function getHostIsolationExceptionItems({
return entries;
}

export async function createHostIsolationExceptionItem({
http,
exception,
}: {
http: HttpStart;
exception: CreateExceptionListItemSchema;
}): Promise<ExceptionListItemSchema> {
await ensureHostIsolationExceptionsListExists(http);
return http.post<ExceptionListItemSchema>(EXCEPTION_LIST_ITEM_URL, {
body: JSON.stringify(exception),
});
}

export async function deleteHostIsolationExceptionItems(http: HttpStart, id: string) {
await ensureHostIsolationExceptionsListExists(http);
return http.delete<ExceptionListItemSchema>(EXCEPTION_LIST_ITEM_URL, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ export type HostIsolationExceptionsPageDataChanged =
payload: HostIsolationExceptionsPageState['entries'];
};

export type HostIsolationExceptionsFormStateChanged =
Action<'hostIsolationExceptionsFormStateChanged'> & {
payload: HostIsolationExceptionsPageState['form']['status'];
};

export type HostIsolationExceptionsFormEntryChanged =
Action<'hostIslationExceptionsFormEntryChanged'> & {
academo marked this conversation as resolved.
Show resolved Hide resolved
payload: HostIsolationExceptionsPageState['form']['entry'];
};

export type HostIsolationExceptionsCreateEntry = Action<'hostIsolationExceptionsCreateEntry'> & {
payload: HostIsolationExceptionsPageState['form']['entry'];
};

export type HostIsolationExceptionsDeleteItem = Action<'hostIsolationExceptionsMarkToDelete'> & {
payload?: ExceptionListItemSchema;
};
Expand All @@ -24,9 +38,10 @@ export type HostIsolationExceptionsDeleteStatusChanged =
Action<'hostIsolationExceptionsDeleteStatusChanged'> & {
payload: HostIsolationExceptionsPageState['deletion']['status'];
};

export type HostIsolationExceptionsPageAction =
| HostIsolationExceptionsPageDataChanged
| HostIsolationExceptionsCreateEntry
| HostIsolationExceptionsFormStateChanged
| HostIsolationExceptionsDeleteItem
| HostIsolationExceptionsSubmitDelete
| HostIsolationExceptionsDeleteStatusChanged;
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export const initialHostIsolationExceptionsPageState = (): HostIsolationExceptio
page_size: MANAGEMENT_DEFAULT_PAGE_SIZE,
filter: '',
},
form: {
entry: undefined,
status: createUninitialisedResourceState(),
},
deletion: {
item: undefined,
status: createUninitialisedResourceState(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
* 2.0.
*/

import { CreateExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import { applyMiddleware, createStore, Store } from 'redux';
import { HOST_ISOLATION_EXCEPTIONS_PATH } from '../../../../../common/constants';
import { coreMock } from '../../../../../../../../src/core/public/mocks';
import { getFoundExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/found_exception_list_item_schema.mock';
import { HOST_ISOLATION_EXCEPTIONS_PATH } from '../../../../../common/constants';
import { AppAction } from '../../../../common/store/actions';
import {
createSpyMiddleware,
Expand All @@ -19,8 +20,13 @@ import {
isLoadedResourceState,
isLoadingResourceState,
} from '../../../state';
import { getHostIsolationExceptionItems, deleteHostIsolationExceptionItems } from '../service';
import {
createHostIsolationExceptionItem,
deleteHostIsolationExceptionItems,
getHostIsolationExceptionItems,
} from '../service';
import { HostIsolationExceptionsPageState } from '../types';
import { createEmptyHostIsolationException } from '../utils';
import { initialHostIsolationExceptionsPageState } from './builders';
import { createHostIsolationExceptionsPageMiddleware } from './middleware';
import { hostIsolationExceptionsPageReducer } from './reducer';
Expand All @@ -29,6 +35,7 @@ import { getListFetchError } from './selector';
jest.mock('../service');
const getHostIsolationExceptionItemsMock = getHostIsolationExceptionItems as jest.Mock;
const deleteHostIsolationExceptionItemsMock = deleteHostIsolationExceptionItems as jest.Mock;
const createHostIsolationExceptionItemMock = createHostIsolationExceptionItem as jest.Mock;

const fakeCoreStart = coreMock.createStart({ basePath: '/mock' });

Expand Down Expand Up @@ -81,7 +88,7 @@ describe('Host isolation exceptions middleware', () => {
};

beforeEach(() => {
getHostIsolationExceptionItemsMock.mockClear();
getHostIsolationExceptionItemsMock.mockReset();
getHostIsolationExceptionItemsMock.mockImplementation(getFoundExceptionListItemSchemaMock);
});

Expand Down Expand Up @@ -145,11 +152,74 @@ describe('Host isolation exceptions middleware', () => {
});
});

describe('When adding an item to host isolation exceptions', () => {
let entry: CreateExceptionListItemSchema;
beforeEach(() => {
createHostIsolationExceptionItemMock.mockReset();
entry = {
...createEmptyHostIsolationException(),
name: 'test name',
description: 'description',
entries: [
{
field: 'destination.ip',
operator: 'included',
type: 'match',
value: '10.0.0.1',
},
],
};
});
it('should dispatch a form loading state when an entry is submited', async () => {
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
return isLoadingResourceState(payload);
},
});
store.dispatch({
type: 'hostIsolationExceptionsCreateEntry',
payload: entry,
});
await waiter;
});
it('should dispatch a form success state when an entry is confirmed by the API', async () => {
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
return isLoadedResourceState(payload);
},
});
store.dispatch({
type: 'hostIsolationExceptionsCreateEntry',
payload: entry,
});
await waiter;
expect(createHostIsolationExceptionItemMock).toHaveBeenCalledWith({
http: fakeCoreStart.http,
exception: entry,
});
});
it('should dispatch a form failure state when an entry is rejected by the API', async () => {
createHostIsolationExceptionItemMock.mockRejectedValue({
body: { message: 'error message', statusCode: 500, error: 'Not today' },
});
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
return isFailedResourceState(payload);
},
});
store.dispatch({
type: 'hostIsolationExceptionsCreateEntry',
payload: entry,
});
await waiter;
});
});

describe('When deleting an item from host isolation exceptions', () => {
beforeEach(() => {
deleteHostIsolationExceptionItemsMock.mockClear();
deleteHostIsolationExceptionItemsMock.mockReset();
deleteHostIsolationExceptionItemsMock.mockReturnValue(undefined);
getHostIsolationExceptionItemsMock.mockClear();
getHostIsolationExceptionItemsMock.mockReset();
getHostIsolationExceptionItemsMock.mockImplementation(getFoundExceptionListItemSchemaMock);
store.dispatch({
type: 'hostIsolationExceptionsMarkToDelete',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/

import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
FoundExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
import { CoreStart, HttpSetup, HttpStart } from 'kibana/public';
import { matchPath } from 'react-router-dom';
import { transformNewItemOutput } from '@kbn/securitysolution-list-hooks';
import { AppLocation, Immutable } from '../../../../../common/endpoint/types';
import { ImmutableMiddleware, ImmutableMiddlewareAPI } from '../../../../common/store';
import { AppAction } from '../../../../common/store/actions';
Expand All @@ -20,7 +22,11 @@ import {
createFailedResourceState,
createLoadedResourceState,
} from '../../../state/async_resource_builders';
import { deleteHostIsolationExceptionItems, getHostIsolationExceptionItems } from '../service';
import {
deleteHostIsolationExceptionItems,
getHostIsolationExceptionItems,
createHostIsolationExceptionItem,
} from '../service';
import { HostIsolationExceptionsPageState } from '../types';
import { getCurrentListPageDataState, getCurrentLocation, getItemToDelete } from './selector';

Expand All @@ -39,12 +45,51 @@ export const createHostIsolationExceptionsPageMiddleware = (
if (action.type === 'userChangedUrl' && isHostIsolationExceptionsPage(action.payload)) {
loadHostIsolationExceptionsList(store, coreStart.http);
}

if (action.type === 'hostIsolationExceptionsCreateEntry') {
createHostIsolationException(store, coreStart.http);
}

if (action.type === 'hostIsolationExceptionsSubmitDelete') {
deleteHostIsolationExceptionsItem(store, coreStart.http);
}
};
};

async function createHostIsolationException(
store: ImmutableMiddlewareAPI<HostIsolationExceptionsPageState, AppAction>,
http: HttpStart
) {
const { dispatch } = store;
const entry = transformNewItemOutput(
store.getState().form.entry as CreateExceptionListItemSchema
academo marked this conversation as resolved.
Show resolved Hide resolved
);
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: {
type: 'LoadingResourceState',
// @ts-expect-error-next-line will be fixed with when AsyncResourceState is refactored (#830)
previousState: entry,
},
});
try {
const response = await createHostIsolationExceptionItem({
http,
exception: entry,
});
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createLoadedResourceState(response),
});
loadHostIsolationExceptionsList(store, http);
academo marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createFailedResourceState<ExceptionListItemSchema>(error.body ?? error),
});
}
}

async function loadHostIsolationExceptionsList(
store: ImmutableMiddlewareAPI<HostIsolationExceptionsPageState, AppAction>,
http: HttpStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { initialHostIsolationExceptionsPageState } from './builders';
import { HOST_ISOLATION_EXCEPTIONS_PATH } from '../../../../../common/constants';
import { hostIsolationExceptionsPageReducer } from './reducer';
import { getCurrentLocation } from './selector';
import { createEmptyHostIsolationException } from '../utils';

describe('Host Isolation Exceptions Reducer', () => {
let initialState: HostIsolationExceptionsPageState;
Expand Down Expand Up @@ -41,4 +42,13 @@ describe('Host Isolation Exceptions Reducer', () => {
});
});
});
it('should set an initial loading state when creating new entries', () => {
const entry = createEmptyHostIsolationException();
const result = hostIsolationExceptionsPageReducer(initialState, {
type: 'hostIsolationExceptionsCreateEntry',
payload: entry,
});
expect(result.form.status).toEqual({ type: 'UninitialisedResourceState' });
expect(result.form.entry).toBe(entry);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ export const hostIsolationExceptionsPageReducer: StateReducer = (
action
) => {
switch (action.type) {
case 'hostIsolationExceptionsCreateEntry': {
return {
...state,
form: {
entry: action.payload,
status: createUninitialisedResourceState(),
},
};
}
case 'hostIsolationExceptionsFormStateChanged': {
return {
...state,
form: {
...state.form,
status: action.payload,
},
};
}
case 'hostIsolationExceptionsPageDataChanged': {
return {
...state,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import type {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
FoundExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
Expand All @@ -27,4 +28,8 @@ export interface HostIsolationExceptionsPageState {
item?: ExceptionListItemSchema;
status: AsyncResourceState<ExceptionListItemSchema>;
};
form: {
entry: CreateExceptionListItemSchema | undefined;
academo marked this conversation as resolved.
Show resolved Hide resolved
status: AsyncResourceState<ExceptionListItemSchema>;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CreateExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import { ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID } from '@kbn/securitysolution-list-constants';
import ipaddr from 'ipaddr.js';

export function createEmptyHostIsolationException(): CreateExceptionListItemSchema {
return {
comments: [],
description: '',
entries: [
{
field: 'destination.ip',
operator: 'included',
type: 'match',
value: '',
},
],
item_id: undefined,
list_id: ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID,
name: '',
namespace_type: 'agnostic',
tags: ['policy:all'],
type: 'simple',
os_types: ['windows', 'linux', 'macos'],
academo marked this conversation as resolved.
Show resolved Hide resolved
};
}

export function isValidIPv4OrCIDR(maybeIp: string): boolean {
try {
ipaddr.IPv4.parseCIDR(maybeIp);
return true;
} catch (e) {
return ipaddr.IPv4.isValid(maybeIp);
}
}
academo marked this conversation as resolved.
Show resolved Hide resolved
Loading