Skip to content

Commit

Permalink
[Ingest pipelines] add network direction processor (#103436)
Browse files Browse the repository at this point in the history
* initial form setup

* custom solution with usemultifields

* wip: sort of working now

* fix bootstraping of initial state

* fix field validation

* add tests

* fix linter errors

* Fix i18 namespace

* Fix linter problems and remove unused whitelisting

* Fix copy for description

* lil prettier fix

* add docs and tweak copy

* small tweaks

* [Form lib] expose handler to access field defaultValue

* Refactor <NetworkDirection />

* fix up import orders

* Fix test mocks

* Move up mocks a bit

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Sébastien Loix <sabee77@gmail.com>
  • Loading branch information
3 people committed Jul 13, 2021
1 parent 538dfba commit 8c9de0b
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const UseArray = ({
const uniqueId = useRef(0);

const form = useFormContext();
const { __getFieldDefaultValue } = form;
const { getFieldDefaultValue } = form;

const getNewItemAtIndex = useCallback(
(index: number): ArrayItem => ({
Expand All @@ -75,7 +75,7 @@ export const UseArray = ({

const fieldDefaultValue = useMemo<ArrayItem[]>(() => {
const defaultValues = readDefaultValueOnForm
? (__getFieldDefaultValue(path) as any[])
? (getFieldDefaultValue(path) as any[])
: undefined;

const getInitialItemsFromValues = (values: any[]): ArrayItem[] =>
Expand All @@ -88,13 +88,7 @@ export const UseArray = ({
return defaultValues
? getInitialItemsFromValues(defaultValues)
: new Array(initialNumberOfItems).fill('').map((_, i) => getNewItemAtIndex(i));
}, [
path,
initialNumberOfItems,
readDefaultValueOnForm,
__getFieldDefaultValue,
getNewItemAtIndex,
]);
}, [path, initialNumberOfItems, readDefaultValueOnForm, getFieldDefaultValue, getNewItemAtIndex]);

// Create a new hook field with the "isIncludedInOutput" set to false so we don't use its value to build the final form data.
// Apart from that the field behaves like a normal field and is hooked into the form validation lifecycle.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ function UseFieldComp<T = unknown, FormType = FormData, I = T>(props: Props<T, F
} else {
if (readDefaultValueOnForm) {
// Read the field initial value from the "defaultValue" object passed to the form
fieldConfig.initialValue =
(form.__getFieldDefaultValue(path) as T) ?? fieldConfig.defaultValue;
fieldConfig.initialValue = (form.getFieldDefaultValue(path) as T) ?? fieldConfig.defaultValue;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,6 @@ export function useForm<T extends FormData = FormData, I extends FormData = T>(
[getFormData$, updateFormData$, fieldsToArray]
);

const getFieldDefaultValue: FormHook<T, I>['__getFieldDefaultValue'] = useCallback(
(fieldName) => get(defaultValueDeserialized.current, fieldName),
[]
);

const readFieldConfigFromSchema: FormHook<T, I>['__readFieldConfigFromSchema'] = useCallback(
(fieldName) => {
const config = (get(schema ?? {}, fieldName) as FieldConfig) || {};
Expand Down Expand Up @@ -339,6 +334,11 @@ export function useForm<T extends FormData = FormData, I extends FormData = T>(

const getFields: FormHook<T, I>['getFields'] = useCallback(() => fieldsRefs.current, []);

const getFieldDefaultValue: FormHook<T, I>['getFieldDefaultValue'] = useCallback(
(fieldName) => get(defaultValueDeserialized.current, fieldName),
[]
);

const submit: FormHook<T, I>['submit'] = useCallback(
async (e) => {
if (e) {
Expand Down Expand Up @@ -431,6 +431,7 @@ export function useForm<T extends FormData = FormData, I extends FormData = T>(
setFieldValue,
setFieldErrors,
getFields,
getFieldDefaultValue,
getFormData,
getErrors,
reset,
Expand All @@ -439,7 +440,6 @@ export function useForm<T extends FormData = FormData, I extends FormData = T>(
__updateFormDataAt: updateFormDataAt,
__updateDefaultValueAt: updateDefaultValueAt,
__readFieldConfigFromSchema: readFieldConfigFromSchema,
__getFieldDefaultValue: getFieldDefaultValue,
__addField: addField,
__removeField: removeField,
__validateFields: validateFields,
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/es_ui_shared/static/forms/hook_form_lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export interface FormHook<T extends FormData = FormData, I extends FormData = T>
setFieldErrors: (fieldName: string, errors: ValidationError[]) => void;
/** Access the fields on the form. */
getFields: () => FieldsMap;
/** Access the defaultValue for a specific field */
getFieldDefaultValue: (path: string) => unknown;
/**
* Return the form data. It accepts an optional options object with an `unflatten` parameter (defaults to `true`).
* If you are only interested in the raw form data, pass `unflatten: false` to the handler
Expand All @@ -60,7 +62,6 @@ export interface FormHook<T extends FormData = FormData, I extends FormData = T>
__updateFormDataAt: (field: string, value: unknown) => void;
__updateDefaultValueAt: (field: string, value: unknown) => void;
__readFieldConfigFromSchema: (field: string) => FieldConfig;
__getFieldDefaultValue: (path: string) => unknown;
}

export type FormSchema<T extends FormData = FormData> = {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/components/__mock__/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const mockFormHook = {
setFieldErrors: jest.fn(),
getFields: jest.fn(),
getFormData: jest.fn(),
getFieldDefaultValue: jest.fn(),
/* Returns a list of all errors in the form */
getErrors: jest.fn(),
reset: jest.fn(),
Expand All @@ -33,7 +34,6 @@ export const mockFormHook = {
__validateFields: jest.fn(),
__updateFormDataAt: jest.fn(),
__readFieldConfigFromSchema: jest.fn(),
__getFieldDefaultValue: jest.fn(),
};

export const getFormMock = (sampleData: any) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* 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 { act } from 'react-dom/test-utils';
import { setup, SetupResult, getProcessorValue } from './processor.helpers';

// Default parameter values automatically added to the network direction processor when saved
const defaultNetworkDirectionParameters = {
if: undefined,
tag: undefined,
source_ip: undefined,
description: undefined,
target_field: undefined,
ignore_missing: undefined,
ignore_failure: undefined,
destination_ip: undefined,
internal_networks: undefined,
internal_networks_field: undefined,
};

const NETWORK_DIRECTION_TYPE = 'network_direction';

describe('Processor: Network Direction', () => {
let onUpdate: jest.Mock;
let testBed: SetupResult;

beforeAll(() => {
jest.useFakeTimers();
});

afterAll(() => {
jest.useRealTimers();
});

beforeEach(async () => {
onUpdate = jest.fn();

await act(async () => {
testBed = await setup({
value: {
processors: [],
},
onFlyoutOpen: jest.fn(),
onUpdate,
});
});

testBed.component.update();

// Open flyout to add new processor
testBed.actions.addProcessor();
// Add type (the other fields are not visible until a type is selected)
await testBed.actions.addProcessorType(NETWORK_DIRECTION_TYPE);
});

test('prevents form submission if internal_network field is not provided', async () => {
const {
actions: { saveNewProcessor },
form,
} = testBed;

// Click submit button with only the type defined
await saveNewProcessor();

// Expect form error as "field" is required parameter
expect(form.getErrorsMessages()).toEqual(['A field value is required.']);
});

test('saves with default parameter values', async () => {
const {
actions: { saveNewProcessor },
find,
component,
} = testBed;

// Add "networkDirectionField" value (required)
await act(async () => {
find('networkDirectionField.input').simulate('change', [{ label: 'loopback' }]);
});
component.update();

// Save the field
await saveNewProcessor();

const processors = getProcessorValue(onUpdate, NETWORK_DIRECTION_TYPE);
expect(processors[0][NETWORK_DIRECTION_TYPE]).toEqual({
...defaultNetworkDirectionParameters,
internal_networks: ['loopback'],
});
});

test('allows to set internal_networks_field', async () => {
const {
actions: { saveNewProcessor },
form,
find,
} = testBed;

find('toggleCustomField').simulate('click');

form.setInputValue('networkDirectionField.input', 'internal_networks_field');

// Save the field with new changes
await saveNewProcessor();

const processors = getProcessorValue(onUpdate, NETWORK_DIRECTION_TYPE);
expect(processors[0][NETWORK_DIRECTION_TYPE]).toEqual({
...defaultNetworkDirectionParameters,
internal_networks_field: 'internal_networks_field',
});
});

test('allows to set just internal_networks_field or internal_networks', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;

// Set internal_networks field
await act(async () => {
find('networkDirectionField.input').simulate('change', [{ label: 'loopback' }]);
});
component.update();

// Toggle to internal_networks_field and set a random value
find('toggleCustomField').simulate('click');
form.setInputValue('networkDirectionField.input', 'internal_networks_field');

// Save the field with new changes
await saveNewProcessor();

const processors = getProcessorValue(onUpdate, NETWORK_DIRECTION_TYPE);
expect(processors[0][NETWORK_DIRECTION_TYPE]).toEqual({
...defaultNetworkDirectionParameters,
internal_networks_field: 'internal_networks_field',
});
});

test('allows optional parameters to be set', async () => {
const {
actions: { saveNewProcessor },
form,
find,
component,
} = testBed;

// Add "networkDirectionField" value (required)
await act(async () => {
find('networkDirectionField.input').simulate('change', [{ label: 'loopback' }]);
});
component.update();

// Set optional parameteres
form.toggleEuiSwitch('ignoreMissingSwitch.input');
form.toggleEuiSwitch('ignoreFailureSwitch.input');
form.setInputValue('sourceIpField.input', 'source.ip');
form.setInputValue('targetField.input', 'target_field');
form.setInputValue('destinationIpField.input', 'destination.ip');

// Save the field with new changes
await saveNewProcessor();

const processors = getProcessorValue(onUpdate, NETWORK_DIRECTION_TYPE);
expect(processors[0][NETWORK_DIRECTION_TYPE]).toEqual({
...defaultNetworkDirectionParameters,
ignore_failure: true,
ignore_missing: false,
source_ip: 'source.ip',
target_field: 'target_field',
destination_ip: 'destination.ip',
internal_networks: ['loopback'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ type TestSubject =
| 'regexFileField.input'
| 'valueFieldInput'
| 'mediaTypeSelectorField'
| 'networkDirectionField.input'
| 'sourceIpField.input'
| 'destinationIpField.input'
| 'toggleCustomField'
| 'ignoreEmptyField.input'
| 'overrideField.input'
| 'fieldsValueField.input'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export { Join } from './join';
export { Json } from './json';
export { Kv } from './kv';
export { Lowercase } from './lowercase';
export { NetworkDirection } from './network_direction';
export { Pipeline } from './pipeline';
export { RegisteredDomain } from './registered_domain';
export { Remove } from './remove';
Expand Down
Loading

0 comments on commit 8c9de0b

Please sign in to comment.