Skip to content

Commit

Permalink
[Security Solution][Exceptions] - Common flyout components (elastic#1…
Browse files Browse the repository at this point in the history
…42054)

## Summary

Adds components shared between new add/edit exception flyouts. Does not yet modify the flyouts themselves. Trying to break down what would be an even larger PR into chunks.
  • Loading branch information
yctercero authored Oct 5, 2022
1 parent 63aee48 commit 0149bd0
Show file tree
Hide file tree
Showing 59 changed files with 4,206 additions and 529 deletions.
6 changes: 3 additions & 3 deletions packages/kbn-securitysolution-list-utils/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ export const getNewExceptionItem = ({
namespaceType,
ruleName,
}: {
listId: string;
namespaceType: NamespaceType;
listId: string | undefined;
namespaceType: NamespaceType | undefined;
ruleName: string;
}): CreateExceptionListItemBuilderSchema => {
return {
comments: [],
description: `${ruleName} - exception list item`,
description: 'Exception list item',
entries: addIdToEntries([
{
field: '',
Expand Down
11 changes: 10 additions & 1 deletion packages/kbn-securitysolution-list-utils/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { DataViewFieldBase } from '@kbn/es-query';
import type {
CreateExceptionListItemSchema,
CreateRuleExceptionListItemSchema,
Entry,
EntryExists,
EntryMatch,
Expand All @@ -18,6 +19,7 @@ import type {
ExceptionListItemSchema,
ListOperatorEnum as OperatorEnum,
ListOperatorTypeEnum as OperatorTypeEnum,
NamespaceType,
} from '@kbn/securitysolution-io-ts-list-types';
import {
EXCEPTION_LIST_NAMESPACE,
Expand Down Expand Up @@ -93,16 +95,23 @@ export type ExceptionListItemBuilderSchema = Omit<ExceptionListItemSchema, 'entr

export type CreateExceptionListItemBuilderSchema = Omit<
CreateExceptionListItemSchema,
'meta' | 'entries'
'meta' | 'entries' | 'list_id' | 'namespace_type'
> & {
meta: { temporaryUuid: string };
entries: BuilderEntry[];
list_id: string | undefined;
namespace_type: NamespaceType | undefined;
};

export type ExceptionsBuilderExceptionItem =
| ExceptionListItemBuilderSchema
| CreateExceptionListItemBuilderSchema;

export type ExceptionsBuilderReturnExceptionItem =
| ExceptionListItemSchema
| CreateExceptionListItemSchema
| CreateRuleExceptionListItemSchema;

export const exceptionListSavedObjectType = EXCEPTION_LIST_NAMESPACE;
export const exceptionListAgnosticSavedObjectType = EXCEPTION_LIST_NAMESPACE_AGNOSTIC;
export type SavedObjectType =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import styled from 'styled-components';
import { HttpStart } from '@kbn/core/public';
import { addIdToItem } from '@kbn/securitysolution-utils';
import {
CreateExceptionListItemSchema,
ExceptionListItemSchema,
ExceptionListType,
NamespaceType,
Expand All @@ -24,6 +23,7 @@ import {
import {
CreateExceptionListItemBuilderSchema,
ExceptionsBuilderExceptionItem,
ExceptionsBuilderReturnExceptionItem,
OperatorOption,
containsValueListEntry,
filterExceptionItems,
Expand Down Expand Up @@ -68,7 +68,7 @@ const initialState: State = {

export interface OnChangeProps {
errorExists: boolean;
exceptionItems: Array<ExceptionListItemSchema | CreateExceptionListItemSchema>;
exceptionItems: ExceptionsBuilderReturnExceptionItem[];
exceptionsToDelete: ExceptionListItemSchema[];
warningExists: boolean;
}
Expand All @@ -84,15 +84,16 @@ export interface ExceptionBuilderProps {
isNestedDisabled: boolean;
isOrDisabled: boolean;
isOrHidden?: boolean;
listId: string;
listNamespaceType: NamespaceType;
listId: string | undefined;
listNamespaceType: NamespaceType | undefined;
listType: ExceptionListType;
listTypeSpecificIndexPatternFilter?: (
pattern: DataViewBase,
type: ExceptionListType
) => DataViewBase;
onChange: (arg: OnChangeProps) => void;
ruleName: string;
exceptionItemName?: string;
ruleName?: string;
isDisabled?: boolean;
operatorsList?: OperatorOption[];
}
Expand All @@ -113,6 +114,7 @@ export const ExceptionBuilderComponent = ({
listTypeSpecificIndexPatternFilter,
onChange,
ruleName,
exceptionItemName,
isDisabled = false,
osTypes,
operatorsList,
Expand Down Expand Up @@ -289,10 +291,10 @@ export const ExceptionBuilderComponent = ({
const newException = getNewExceptionItem({
listId,
namespaceType: listNamespaceType,
ruleName,
ruleName: exceptionItemName ?? `${ruleName ?? 'Rule'} - Exception item`,
});
setUpdateExceptions([...exceptions, { ...newException }]);
}, [setUpdateExceptions, exceptions, listId, listNamespaceType, ruleName]);
}, [listId, listNamespaceType, exceptionItemName, ruleName, setUpdateExceptions, exceptions]);

// The builder can have existing exception items, or new exception items that have yet
// to be created (and thus lack an id), this was creating some React bugs with relying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('find_exception_list_references_schema', () => {
});
});

test('"ids" cannot be undefined', () => {
test('"ids" can be optional', () => {
const payload: Omit<FindExceptionReferencesOnRuleSchema, 'ids'> = {
list_ids: '123,456',
namespace_types: 'single,agnostic',
Expand All @@ -37,11 +37,14 @@ describe('find_exception_list_references_schema', () => {
const decoded = findExceptionReferencesOnRuleSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const output = foldLeftRight(checked);
expect(formatErrors(output.errors)).toEqual(['Invalid value "undefined" supplied to "ids"']);
expect(output.schema).toEqual({});
expect(formatErrors(output.errors)).toEqual([]);
expect(output.schema).toEqual({
list_ids: ['123', '456'],
namespace_types: ['single', 'agnostic'],
});
});

test('"list_ids" cannot be undefined', () => {
test('"list_ids" can be undefined', () => {
const payload: Omit<FindExceptionReferencesOnRuleSchema, 'list_ids'> = {
ids: 'abc',
namespace_types: 'single',
Expand All @@ -50,10 +53,11 @@ describe('find_exception_list_references_schema', () => {
const decoded = findExceptionReferencesOnRuleSchema.decode(payload);
const checked = exactCheck(payload, decoded);
const output = foldLeftRight(checked);
expect(formatErrors(output.errors)).toEqual([
'Invalid value "undefined" supplied to "list_ids"',
]);
expect(output.schema).toEqual({});
expect(formatErrors(output.errors)).toEqual([]);
expect(output.schema).toEqual({
ids: ['abc'],
namespace_types: ['single'],
});
});

test('defaults "namespacetypes" to ["single"] if none set', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ import * as t from 'io-ts';
import { NonEmptyStringArray } from '@kbn/securitysolution-io-ts-types';
import { DefaultNamespaceArray } from '@kbn/securitysolution-io-ts-list-types';

export const findExceptionReferencesOnRuleSchema = t.exact(
t.type({
ids: NonEmptyStringArray,
list_ids: NonEmptyStringArray,
namespace_types: DefaultNamespaceArray,
})
);
// If ids and list_ids are undefined, route will fetch all lists matching the
// specified namespace type
export const findExceptionReferencesOnRuleSchema = t.intersection([
t.exact(
t.type({
namespace_types: DefaultNamespaceArray,
})
),
t.exact(
t.partial({
ids: NonEmptyStringArray,
list_ids: NonEmptyStringArray,
})
),
]);

export type FindExceptionReferencesOnRuleSchema = t.OutputOf<
typeof findExceptionReferencesOnRuleSchema
Expand Down
Loading

0 comments on commit 0149bd0

Please sign in to comment.