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] Remove extra data structures in create/edit rule forms #157749

Merged
merged 23 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5d9925c
Remove extra data structures in create/edit rule forms
marshallmain May 15, 2023
3caa468
Fix unit tests, include EqlOptions in defineStepData
marshallmain May 16, 2023
010b44b
Fix actions step test
marshallmain May 17, 2023
f90621f
Merge branch 'main' of github.com:elastic/kibana into create-rules-re…
marshallmain May 17, 2023
0528df2
Fix cypress tests, hoist index patterns into hook
marshallmain May 19, 2023
69a8108
Fix stepDefineRule unit tests
marshallmain May 19, 2023
e1cb0c5
Fix IM cypress tests and saved query checkbox label
marshallmain May 19, 2023
ff438b0
Fix new terms and threshold cypress tests
marshallmain May 19, 2023
ddf1e99
Fix rule actions cypress test
marshallmain May 22, 2023
c938da3
Merge branch 'main' into create-rules-refactor-success
marshallmain May 23, 2023
672142e
Hopefully fix response actions cypress test
marshallmain May 23, 2023
1e8db8a
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine May 23, 2023
459fa08
Fix response actions again maybe
marshallmain May 23, 2023
3aabd4f
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine May 23, 2023
3820ed8
PR feedback: loading var naming, combine if statements
marshallmain May 30, 2023
9306c98
Merge branch 'main' into create-rules-refactor-success
marshallmain May 30, 2023
40ff7e7
Remove extra spacing
marshallmain May 30, 2023
491f88f
PR feedback: aboutRuleSchema for threat match
marshallmain May 30, 2023
57b4d40
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine May 30, 2023
8f0e7cb
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine May 30, 2023
b9a3b17
Remove unused helpers and types
marshallmain Jun 2, 2023
9271a79
Merge branch 'main' into create-rules-refactor-success
marshallmain Jun 2, 2023
84c8f85
Merge branch 'main' into create-rules-refactor-success
marshallmain Jun 5, 2023
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 @@ -79,7 +79,6 @@ import { createTimeline } from '../../tasks/api_calls/timelines';
import { cleanKibana, deleteAlertsAndRules, deleteConnectors } from '../../tasks/common';
import { addEmailConnectorAndRuleAction } from '../../tasks/common/rule_actions';
import {
continueWithNextSection,
createAndEnableRule,
expandAdvancedSettings,
fillAboutRule,
Expand Down Expand Up @@ -131,7 +130,7 @@ describe('Custom query rules', () => {

cy.log('Filling define section');
importSavedQuery(this.timelineId);
continueWithNextSection();
cy.get(DEFINE_CONTINUE_BUTTON).click();

cy.log('Filling about section');
fillRuleName();
Expand All @@ -146,7 +145,7 @@ describe('Custom query rules', () => {
fillThreatTechnique();
fillThreatSubtechnique();
fillNote();
continueWithNextSection();
cy.get(ABOUT_CONTINUE_BTN).click();

cy.log('Filling schedule section');
fillFrom();
Expand All @@ -155,13 +154,11 @@ describe('Custom query rules', () => {
cy.get(DEFINE_EDIT_BUTTON).click();
cy.get(CUSTOM_QUERY_INPUT).should('have.value', ruleFields.ruleQuery);
cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click();
cy.get(DEFINE_CONTINUE_BUTTON).should('not.exist');

// expect about step to populate
cy.get(ABOUT_EDIT_BUTTON).click();
cy.get(RULE_NAME_INPUT).invoke('val').should('eql', ruleFields.ruleName);
cy.get(ABOUT_CONTINUE_BTN).should('exist').click();
cy.get(ABOUT_CONTINUE_BTN).should('not.exist');
cy.get(SCHEDULE_CONTINUE_BUTTON).click();

createAndEnableRule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ describe('Custom saved_query rules', () => {
cy.get(QUERY_BAR).should('contain', savedQueryFilterKey);

cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click();
cy.get(DEFINE_CONTINUE_BUTTON).should('not.exist');

fillAboutRuleAndContinue(rule);
fillScheduleRuleAndContinue(rule);
Expand Down Expand Up @@ -111,7 +110,9 @@ describe('Custom saved_query rules', () => {
cy.get(TOASTER).should('contain', FAILED_TO_LOAD_ERROR);
});

it('Shows validation error on rule edit when saved query can not be loaded', function () {
// TODO: this error depended on the schema validation running. Can we show the error
// based on the saved query failing to load instead of relying on the schema validation?
it.skip('Shows validation error on rule edit when saved query can not be loaded', function () {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create an issue to record this as being skipped if we're unable to skip it before merging?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Issue: #159060

editFirstRule();

cy.get(CUSTOM_QUERY_BAR).should('contain', FAILED_TO_LOAD_ERROR);
Expand Down Expand Up @@ -180,6 +181,7 @@ describe('Custom saved_query rules', () => {
visit(SECURITY_DETECTIONS_RULES_URL);

editFirstRule();
uncheckLoadQueryDynamically();

// type custom query, ensure Load dynamically checkbox is absent, as rule can't be saved win non valid saved query
getCustomQueryInput().type(expectedCustomTestQuery);
Expand All @@ -203,6 +205,7 @@ describe('Custom saved_query rules', () => {
visit(SECURITY_DETECTIONS_RULES_URL);

editFirstRule();
uncheckLoadQueryDynamically();

// select another saved query, edit query input, which later should be dismissed once Load query dynamically checkbox checked
selectAndLoadSavedQuery(savedQueryName, savedQueryQuery);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export const JSON_EDITOR = "[data-test-subj='actionJsonEditor']";

export const INDEX_SELECTOR = "[data-test-subj='.index-siem-ActionTypeSelectOption']";

export const INDEX_CONNECTOR_COMBO_BOX_INPUT =
'[data-test-subj="connectorIndexesComboBox"] [data-test-subj="comboBoxInput"]';

export const actionFormSelector = (position: number) =>
`[data-test-subj="alertActionAccordion-${position}"]`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export const THREAT_ITEM_ENTRY_DELETE_BUTTON = '[data-test-subj="itemEntryDelete

export const THREAT_MATCH_OR_BUTTON = '[data-test-subj="orButton"]';

export const THREAT_COMBO_BOX_INPUT = '[data-test-subj="fieldAutocompleteComboBox"]';
export const THREAT_COMBO_BOX_INPUT =
'[data-test-subj="stepDefineRule"] [data-test-subj="fieldAutocompleteComboBox"]';

export const INVALID_MATCH_CONTENT = 'All matches require both a field and threat index field.';

Expand All @@ -75,8 +76,6 @@ export const DATA_VIEW_COMBO_BOX =

export const DATA_VIEW_OPTION = '[data-test-subj="rule-index-toggle-dataView"]';

export const CONTINUE_BUTTON = '[data-test-subj$=-continue]';

export const DEFINE_CONTINUE_BUTTON = '[data-test-subj="define-continue"]';

export const DEFINE_EDIT_BUTTON = '[data-test-subj="edit-define-rule"]';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import {
ACTIONS_NOTIFY_CUSTOM_FREQUENCY_BUTTON,
actionFormSelector,
ACTIONS_NOTIFY_PER_RULE_RUN_BUTTON,
INDEX_CONNECTOR_COMBO_BOX_INPUT,
} from '../../screens/common/rule_actions';
import { COMBO_BOX_INPUT, COMBO_BOX_SELECTION } from '../../screens/common/controls';
import { COMBO_BOX_SELECTION } from '../../screens/common/controls';
import type { EmailConnector, IndexConnector } from '../../objects/connector';
import { getEmailConnector, getIndexConnector } from '../../objects/connector';

Expand Down Expand Up @@ -82,7 +83,7 @@ export const assertEmailRuleAction = (email: string, subject: string) => {

export const fillIndexConnectorForm = (connector: IndexConnector = getIndexConnector()) => {
cy.get(CONNECTOR_NAME_INPUT).type(connector.name);
cy.get(COMBO_BOX_INPUT).type(connector.index);
cy.get(INDEX_CONNECTOR_COMBO_BOX_INPUT).type(connector.index);

cy.get(COMBO_BOX_SELECTION).click({ force: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ import {
NEW_TERMS_HISTORY_SIZE,
NEW_TERMS_HISTORY_TIME_TYPE,
NEW_TERMS_INPUT_AREA,
CONTINUE_BUTTON,
CREATE_WITHOUT_ENABLING_BTN,
RULE_INDICES,
ALERTS_INDEX_BUTTON,
Expand Down Expand Up @@ -373,10 +372,6 @@ export const removeAlertsIndex = () => {
});
};

export const continueWithNextSection = () => {
cy.get(CONTINUE_BUTTON).should('exist').click();
};

export const fillDefineCustomRuleAndContinue = (rule: QueryRuleCreateProps) => {
if (rule.data_view_id !== undefined) {
cy.get(DATA_VIEW_OPTION).click();
Expand All @@ -386,7 +381,6 @@ export const fillDefineCustomRuleAndContinue = (rule: QueryRuleCreateProps) => {
.first()
.type(rule.query || '');
cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click({ force: true });
cy.get(CUSTOM_QUERY_INPUT).should('not.exist');
};

export const fillScheduleRuleAndContinue = (rule: RuleCreateProps) => {
Expand Down Expand Up @@ -458,8 +452,6 @@ export const fillDefineThresholdRuleAndContinue = (rule: ThresholdRuleCreateProp
cy.wrap(inputs[threshold]).type(`${rule.threshold.value}`);
});
cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click({ force: true });

cy.get(CUSTOM_QUERY_INPUT).should('not.exist');
};

export const fillDefineEqlRuleAndContinue = (rule: EqlRuleCreateProps) => {
Expand All @@ -480,7 +472,6 @@ export const fillDefineEqlRuleAndContinue = (rule: EqlRuleCreateProps) => {
cy.get(TOAST_ERROR).should('not.exist');

cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click({ force: true });
cy.get(`${RULES_CREATION_FORM} ${EQL_QUERY_INPUT}`).should('not.exist');
};

export const fillDefineNewTermsRuleAndContinue = (rule: NewTermsRuleCreateProps) => {
Expand All @@ -499,8 +490,6 @@ export const fillDefineNewTermsRuleAndContinue = (rule: NewTermsRuleCreateProps)
cy.get(NEW_TERMS_INPUT_AREA).find(NEW_TERMS_HISTORY_SIZE).type(historySizeNumber);
cy.get(NEW_TERMS_INPUT_AREA).find(NEW_TERMS_HISTORY_TIME_TYPE).select(historySizeType);
cy.get(DEFINE_CONTINUE_BUTTON).should('exist').click({ force: true });

cy.get(CUSTOM_QUERY_INPUT).should('not.exist');
};

/**
Expand Down Expand Up @@ -632,7 +621,6 @@ export const fillDefineIndicatorMatchRuleAndContinue = (rule: ThreatMatchRuleCre
});
getCustomIndicatorQueryInput().type('{selectall}{enter}*:*');
getDefineContinueButton().should('exist').click({ force: true });
cy.get(CUSTOM_QUERY_INPUT).should('not.exist');
};

export const fillDefineMachineLearningRuleAndContinue = (rule: MachineLearningRuleCreateProps) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* 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 { useState, useMemo, useEffect } from 'react';
import type { DataViewBase } from '@kbn/es-query';
import { isThreatMatchRule } from '../../../../common/detection_engine/utils';
import type {
AboutStepRule,
ActionsStepRule,
DefineStepRule,
ScheduleStepRule,
} from '../../../detections/pages/detection_engine/rules/types';
import { DataSourceType } from '../../../detections/pages/detection_engine/rules/types';
import { useKibana } from '../../../common/lib/kibana';
import { useForm, useFormData } from '../../../shared_imports';
import { schema as defineRuleSchema } from '../../../detections/components/rules/step_define_rule/schema';
import type { EqlOptionsSelected } from '../../../../common/search_strategy';
import {
schema as aboutRuleSchema,
threatMatchAboutSchema,
} from '../../../detections/components/rules/step_about_rule/schema';
import { schema as scheduleRuleSchema } from '../../../detections/components/rules/step_schedule_rule/schema';
import { getSchema as getActionsRuleSchema } from '../../../detections/components/rules/step_rule_actions/get_schema';
import { useFetchIndex } from '../../../common/containers/source';

export interface UseRuleFormsProps {
defineStepDefault: DefineStepRule;
aboutStepDefault: AboutStepRule;
scheduleStepDefault: ScheduleStepRule;
actionsStepDefault: ActionsStepRule;
}

export const useRuleForms = ({
defineStepDefault,
aboutStepDefault,
scheduleStepDefault,
actionsStepDefault,
}: UseRuleFormsProps) => {
const {
triggersActionsUi: { actionTypeRegistry },
} = useKibana().services;
// DEFINE STEP FORM
const { form: defineStepForm } = useForm<DefineStepRule>({
defaultValue: defineStepDefault,
options: { stripEmptyFields: false },
schema: defineRuleSchema,
});
const [eqlOptionsSelected, setEqlOptionsSelected] = useState<EqlOptionsSelected>(
defineStepDefault.eqlOptions
);
const [defineStepFormData] = useFormData<DefineStepRule | {}>({
form: defineStepForm,
});
// FormData doesn't populate on the first render, so we use the defaultValue if the formData
// doesn't have what we wanted
const defineStepData =
'index' in defineStepFormData
? { ...defineStepFormData, eqlOptions: eqlOptionsSelected }
: defineStepDefault;

// ABOUT STEP FORM
const typeDependentAboutRuleSchema = useMemo(
() => (isThreatMatchRule(defineStepData.ruleType) ? threatMatchAboutSchema : aboutRuleSchema),
[defineStepData.ruleType]
);
const { form: aboutStepForm } = useForm<AboutStepRule>({
defaultValue: aboutStepDefault,
options: { stripEmptyFields: false },
schema: typeDependentAboutRuleSchema,
});
const [aboutStepFormData] = useFormData<AboutStepRule | {}>({
form: aboutStepForm,
});
const aboutStepData = 'name' in aboutStepFormData ? aboutStepFormData : aboutStepDefault;

// SCHEDULE STEP FORM
const { form: scheduleStepForm } = useForm<ScheduleStepRule>({
defaultValue: scheduleStepDefault,
options: { stripEmptyFields: false },
schema: scheduleRuleSchema,
});
const [scheduleStepFormData] = useFormData<ScheduleStepRule | {}>({
form: scheduleStepForm,
});
const scheduleStepData =
'interval' in scheduleStepFormData ? scheduleStepFormData : scheduleStepDefault;

// ACTIONS STEP FORM
const schema = useMemo(() => getActionsRuleSchema({ actionTypeRegistry }), [actionTypeRegistry]);
const { form: actionsStepForm } = useForm<ActionsStepRule>({
defaultValue: actionsStepDefault,
options: { stripEmptyFields: false },
schema,
});
const [actionsStepFormData] = useFormData<ActionsStepRule | {}>({
form: actionsStepForm,
});
const actionsStepData =
'actions' in actionsStepFormData ? actionsStepFormData : actionsStepDefault;

return {
defineStepForm,
defineStepData,
aboutStepForm,
aboutStepData,
scheduleStepForm,
scheduleStepData,
actionsStepForm,
actionsStepData,
eqlOptionsSelected,
setEqlOptionsSelected,
};
};

interface UseRuleIndexPatternProps {
dataSourceType: DataSourceType;
index: string[];
dataViewId: string | undefined;
}

export const useRuleIndexPattern = ({
dataSourceType,
index,
dataViewId,
}: UseRuleIndexPatternProps) => {
const { data } = useKibana().services;
const [isIndexPatternLoading, { browserFields, indexPatterns: initIndexPattern }] =
useFetchIndex(index);
const [indexPattern, setIndexPattern] = useState<DataViewBase>(initIndexPattern);
// Why do we need this? to ensure the query bar auto-suggest gets the latest updates
// when the index pattern changes
// when we select new dataView
// when we choose some other dataSourceType
useEffect(() => {
if (dataSourceType === DataSourceType.IndexPatterns && !isIndexPatternLoading) {
setIndexPattern(initIndexPattern);
}

if (dataSourceType === DataSourceType.DataView) {
const fetchDataView = async () => {
if (dataViewId != null) {
const dv = await data.dataViews.get(dataViewId);
setIndexPattern(dv);
}
};

fetchDataView();
}
}, [dataSourceType, isIndexPatternLoading, data, dataViewId, initIndexPattern]);
return { indexPattern, isIndexPatternLoading, browserFields };
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import type {
ScheduleStepRuleJson,
AboutStepRuleJson,
ActionsStepRuleJson,
RuleStepsFormData,
RuleStep,
} from '../../../../detections/pages/detection_engine/rules/types';
import {
DataSourceType,
Expand Down Expand Up @@ -71,23 +69,6 @@ export const getTimeTypeValue = (time: string): { unit: Unit; value: number } =>
return timeObj;
};

export const stepIsValid = <T extends RuleStepsFormData[keyof RuleStepsFormData]>(
formData?: T
): formData is { [K in keyof T]: Exclude<T[K], undefined> } =>
!!formData?.isValid && !!formData.data;

export const isDefineStep = (input: unknown): input is RuleStepsFormData[RuleStep.defineRule] =>
has('data.ruleType', input);

export const isAboutStep = (input: unknown): input is RuleStepsFormData[RuleStep.aboutRule] =>
has('data.name', input);

export const isScheduleStep = (input: unknown): input is RuleStepsFormData[RuleStep.scheduleRule] =>
has('data.interval', input);

export const isActionsStep = (input: unknown): input is RuleStepsFormData[RuleStep.ruleActions] =>
has('data.actions', input);

export interface RuleFields {
anomalyThreshold: unknown;
machineLearningJobId: unknown;
Expand Down
Loading