Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 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
cea4868
Types working
Oct 7, 2021
27ae049
Add back ts-ignore to make ts work
Oct 7, 2021
f0dd9b3
Fix translation id
Oct 7, 2021
a1696ba
Data loading to edit. no saving
Oct 7, 2021
19fdf46
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 11, 2021
328866a
WIP: Edit.
Oct 11, 2021
24b7071
Edit: wip
Oct 11, 2021
307d7c0
Show correct edit/create success and error messages
Oct 11, 2021
f046266
Fix repeated translation key
Oct 11, 2021
32580f8
Fix tests for creating exceptions
Oct 11, 2021
b076edc
Add tests for edit form
Oct 11, 2021
9ff07d1
Add tests for form flyout
Oct 11, 2021
a69a678
Add tests to load an entry from host isolation exceptions
Oct 11, 2021
f18396a
Remove unused dependency
Oct 11, 2021
8878114
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 11, 2021
85f4f21
Fix TS errors
Oct 11, 2021
77b9054
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 11, 2021
501ba78
Handle error on load case
Oct 12, 2021
231988d
Fix toast copies
Oct 12, 2021
42377fd
Add comments to code
Oct 12, 2021
b0b10d8
Fix tests and improve selector performance
Oct 12, 2021
a58fa90
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 12, 2021
78d9912
Add test for load item error
Oct 12, 2021
0e851cc
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 12, 2021
b08089b
Replace ignores with expect-error and fix easy AsyncResourceState
Oct 12, 2021
3b2a41c
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 12, 2021
b492695
Merge branch 'master' into feature/host-isolation-exceptions-edit
kibanamachine Oct 12, 2021
7914970
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 13, 2021
f9c67ee
Address PR comments
Oct 13, 2021
8bc78ab
Add expect to promise await
Oct 13, 2021
9d541ce
Merge remote-tracking branch 'upstream/master' into feature/host-isol…
Oct 13, 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 @@ -231,8 +231,8 @@ const refreshListDataIfNeeded: MiddlewareActionHandler = async (store, eventFilt
dispatch({
type: 'eventFiltersListPageDataChanged',
payload: {
type: 'LoadingResourceState',
// @ts-expect-error-next-line will be fixed with when AsyncResourceState is refactored (#830)
type: 'LoadingResourceState',
previousState: getCurrentListPageDataState(state),
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
FoundExceptionListItemSchema,
UpdateExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
import { ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID } from '@kbn/securitysolution-list-constants';
import { HttpStart } from 'kibana/public';
Expand Down Expand Up @@ -88,3 +89,26 @@ export async function deleteHostIsolationExceptionItems(http: HttpStart, id: str
},
});
}

export async function getOneHostIsolationExceptionItem(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: I think the One is not necessary for the name, Item already says it's a singular thing. Unless getHostIsolationExceptionItem name collides with another 😅

http: HttpStart,
id: string
): Promise<UpdateExceptionListItemSchema> {
await ensureHostIsolationExceptionsListExists(http);
return http.get<ExceptionListItemSchema>(EXCEPTION_LIST_ITEM_URL, {
query: {
id,
namespace_type: 'agnostic',
},
});
}

export async function updateOneHostIsolationExceptionItem(
http: HttpStart,
exception: UpdateExceptionListItemSchema
): Promise<ExceptionListItemSchema> {
await ensureHostIsolationExceptionsListExists(http);
return http.put<ExceptionListItemSchema>(EXCEPTION_LIST_ITEM_URL, {
body: JSON.stringify(exception),
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import { ExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import {
ExceptionListItemSchema,
UpdateExceptionListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
import { Action } from 'redux';
import { HostIsolationExceptionsPageState } from '../types';

Expand Down Expand Up @@ -38,10 +41,24 @@ export type HostIsolationExceptionsDeleteStatusChanged =
Action<'hostIsolationExceptionsDeleteStatusChanged'> & {
payload: HostIsolationExceptionsPageState['deletion']['status'];
};

export type HostIsolationExceptionsMarkToEdit = Action<'hostIsolationExceptionsMarkToEdit'> & {
payload: {
id: string;
};
};

export type HostIsolationExceptionsSubmitEdit = Action<'hostIsolationExceptionsSubmitEdit'> & {
payload: UpdateExceptionListItemSchema;
};

export type HostIsolationExceptionsPageAction =
| HostIsolationExceptionsPageDataChanged
| HostIsolationExceptionsCreateEntry
| HostIsolationExceptionsFormStateChanged
| HostIsolationExceptionsDeleteItem
| HostIsolationExceptionsSubmitDelete
| HostIsolationExceptionsDeleteStatusChanged;
| HostIsolationExceptionsDeleteStatusChanged
| HostIsolationExceptionsFormEntryChanged
| HostIsolationExceptionsMarkToEdit
| HostIsolationExceptionsSubmitEdit;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* 2.0.
*/

import { CreateExceptionListItemSchema } from '@kbn/securitysolution-io-ts-list-types';
import {
CreateExceptionListItemSchema,
UpdateEndpointListItemSchema,
} from '@kbn/securitysolution-io-ts-list-types';
import { applyMiddleware, createStore, Store } from 'redux';
import { coreMock } from '../../../../../../../../src/core/public/mocks';
import { getFoundExceptionListItemSchemaMock } from '../../../../../../lists/common/schemas/response/found_exception_list_item_schema.mock';
Expand All @@ -24,6 +27,8 @@ import {
createHostIsolationExceptionItem,
deleteHostIsolationExceptionItems,
getHostIsolationExceptionItems,
getOneHostIsolationExceptionItem,
updateOneHostIsolationExceptionItem,
} from '../service';
import { HostIsolationExceptionsPageState } from '../types';
import { createEmptyHostIsolationException } from '../utils';
Expand All @@ -36,6 +41,8 @@ jest.mock('../service');
const getHostIsolationExceptionItemsMock = getHostIsolationExceptionItems as jest.Mock;
const deleteHostIsolationExceptionItemsMock = deleteHostIsolationExceptionItems as jest.Mock;
const createHostIsolationExceptionItemMock = createHostIsolationExceptionItem as jest.Mock;
const getOneHostIsolationExceptionItemMock = getOneHostIsolationExceptionItem as jest.Mock;
const updateOneHostIsolationExceptionItemMock = updateOneHostIsolationExceptionItem as jest.Mock;

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

Expand Down Expand Up @@ -170,6 +177,7 @@ describe('Host isolation exceptions middleware', () => {
],
};
});

it('should dispatch a form loading state when an entry is submited', async () => {
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
Expand All @@ -182,6 +190,7 @@ describe('Host isolation exceptions middleware', () => {
});
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 }) {
Expand All @@ -198,6 +207,7 @@ describe('Host isolation exceptions middleware', () => {
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' },
Expand All @@ -215,6 +225,101 @@ describe('Host isolation exceptions middleware', () => {
});
});

describe('When updating an item from host isolation exceptions', () => {
const fakeId = 'dc5d1d00-2766-11ec-981f-7f84cfc8764f';
let fakeException: UpdateEndpointListItemSchema;
beforeEach(() => {
fakeException = {
...createEmptyHostIsolationException(),
name: 'name edit me',
description: 'initial description',
id: fakeId,
item_id: fakeId,
entries: [
{
field: 'destination.ip',
operator: 'included',
type: 'match',
value: '10.0.0.5',
},
],
};
getOneHostIsolationExceptionItemMock.mockReset();
getOneHostIsolationExceptionItemMock.mockImplementation(async () => {
return fakeException;
});
});

it('should load data from an entry when an exception is marked to edit', async () => {
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormEntryChanged');
store.dispatch({
type: 'hostIsolationExceptionsMarkToEdit',
payload: {
id: fakeId,
},
});
await waiter;
expect(getOneHostIsolationExceptionItemMock).toHaveBeenCalledWith(fakeCoreStart.http, fakeId);
});

it('should call the update API when an item edit is submitted', async () => {
const waiter = Promise.all([
// loading status
spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate: ({ payload }) => {
return isLoadingResourceState(payload);
},
}),
// loaded status
spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
return isLoadedResourceState(payload);
},
}),
]);
store.dispatch({
type: 'hostIsolationExceptionsSubmitEdit',
payload: fakeException,
});
expect(updateOneHostIsolationExceptionItemMock).toHaveBeenCalledWith(fakeCoreStart.http, {
name: 'name edit me',
description: 'initial description',
id: fakeId,
item_id: fakeId,
entries: [
{
field: 'destination.ip',
operator: 'included',
type: 'match',
value: '10.0.0.5',
},
],
namespace_type: 'agnostic',
os_types: ['windows', 'linux', 'macos'],
tags: ['policy:all'],
type: 'simple',
comments: [],
});
await waiter;
Comment thread
academo marked this conversation as resolved.
});

it('should dispatch a form failure state when an entry is rejected by the API', async () => {
updateOneHostIsolationExceptionItemMock.mockRejectedValue({
body: { message: 'error message', statusCode: 500, error: 'Not today' },
});
const waiter = spyMiddleware.waitForAction('hostIsolationExceptionsFormStateChanged', {
validate({ payload }) {
return isFailedResourceState(payload);
},
});
store.dispatch({
type: 'hostIsolationExceptionsSubmitEdit',
payload: fakeException,
});
await waiter;
});
});

describe('When deleting an item from host isolation exceptions', () => {
beforeEach(() => {
deleteHostIsolationExceptionItemsMock.mockReset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@ import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
FoundExceptionListItemSchema,
UpdateExceptionListItemSchema,
} 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 { transformNewItemOutput, transformOutput } from '@kbn/securitysolution-list-hooks';
import { AppLocation, Immutable, ImmutableObject } from '../../../../../common/endpoint/types';
import { ImmutableMiddleware, ImmutableMiddlewareAPI } from '../../../../common/store';
import { AppAction } from '../../../../common/store/actions';
import { MANAGEMENT_ROUTING_HOST_ISOLATION_EXCEPTIONS_PATH } from '../../../common/constants';
import { parseQueryFilterToKQL } from '../../../common/utils';
import {
createFailedResourceState,
createLoadedResourceState,
createLoadingResourceState,
} from '../../../state/async_resource_builders';
import {
deleteHostIsolationExceptionItems,
getHostIsolationExceptionItems,
createHostIsolationExceptionItem,
getOneHostIsolationExceptionItem,
updateOneHostIsolationExceptionItem,
} from '../service';
import { HostIsolationExceptionsPageState } from '../types';
import { getCurrentListPageDataState, getCurrentLocation, getItemToDelete } from './selector';
Expand Down Expand Up @@ -53,6 +57,14 @@ export const createHostIsolationExceptionsPageMiddleware = (
if (action.type === 'hostIsolationExceptionsSubmitDelete') {
deleteHostIsolationExceptionsItem(store, coreStart.http);
}

if (action.type === 'hostIsolationExceptionsMarkToEdit') {
loadHostIsolationExceptionsItem(store, coreStart.http, action.payload.id);
}

if (action.type === 'hostIsolationExceptionsSubmitEdit') {
updateHostIsolationExceptionsItem(store, coreStart.http, action.payload);
}
};
};

Expand All @@ -67,8 +79,8 @@ async function createHostIsolationException(
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: {
type: 'LoadingResourceState',
// @ts-expect-error-next-line will be fixed with when AsyncResourceState is refactored (#830)
type: 'LoadingResourceState',
previousState: entry,
},
});
Expand Down Expand Up @@ -110,8 +122,8 @@ async function loadHostIsolationExceptionsList(
dispatch({
type: 'hostIsolationExceptionsPageDataChanged',
payload: {
type: 'LoadingResourceState',
// @ts-expect-error-next-line will be fixed with when AsyncResourceState is refactored (#830)
type: 'LoadingResourceState',
previousState: getCurrentListPageDataState(store.getState()),
},
});
Expand Down Expand Up @@ -152,8 +164,8 @@ async function deleteHostIsolationExceptionsItem(
dispatch({
type: 'hostIsolationExceptionsDeleteStatusChanged',
payload: {
type: 'LoadingResourceState',
// @ts-expect-error-next-line will be fixed with when AsyncResourceState is refactored (#830)
type: 'LoadingResourceState',
previousState: store.getState().deletion.status,
},
});
Expand All @@ -172,3 +184,69 @@ async function deleteHostIsolationExceptionsItem(
});
}
}

async function loadHostIsolationExceptionsItem(
store: ImmutableMiddlewareAPI<HostIsolationExceptionsPageState, AppAction>,
http: HttpSetup,
id: string
) {
const { dispatch } = store;
try {
const exception: UpdateExceptionListItemSchema = await getOneHostIsolationExceptionItem(
Comment thread
paul-tavares marked this conversation as resolved.
http,
id
);
dispatch({
type: 'hostIsolationExceptionsFormEntryChanged',
payload: exception,
});
} catch (error) {
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createFailedResourceState<ExceptionListItemSchema>(error.body ?? error),
});
}
}
async function updateHostIsolationExceptionsItem(
store: ImmutableMiddlewareAPI<HostIsolationExceptionsPageState, AppAction>,
http: HttpSetup,
exception: ImmutableObject<UpdateExceptionListItemSchema>
) {
const { dispatch } = store;
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createLoadingResourceState(createLoadedResourceState(exception)),
});

try {
const entry = transformOutput(exception as UpdateExceptionListItemSchema);
// Clean unnecessary fields for update action
const fieldsToRemove: Array<keyof ExceptionListItemSchema> = [
'created_at',
'created_by',
'created_at',
'created_by',
'list_id',
'tie_breaker_id',
'updated_at',
'updated_by',
];

fieldsToRemove.forEach((field) => {
delete entry[field as keyof UpdateExceptionListItemSchema];
Comment thread
academo marked this conversation as resolved.
});
Comment thread
academo marked this conversation as resolved.
const response: ExceptionListItemSchema = await updateOneHostIsolationExceptionItem(
http,
entry
);
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createLoadedResourceState(response),
});
} catch (error) {
dispatch({
type: 'hostIsolationExceptionsFormStateChanged',
payload: createFailedResourceState<ExceptionListItemSchema>(error.body ?? error),
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ export const hostIsolationExceptionsPageReducer: StateReducer = (
},
};
}
case 'hostIsolationExceptionsFormEntryChanged': {
return {
...state,
form: {
...state.form,
entry: action.payload,
},
};
}
case 'hostIsolationExceptionsPageDataChanged': {
return {
...state,
Expand Down
Loading