-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
marshallmain
merged 23 commits into
elastic:main
from
marshallmain:create-rules-refactor-success
Jun 5, 2023
Merged
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 3caa468
Fix unit tests, include EqlOptions in defineStepData
marshallmain 010b44b
Fix actions step test
marshallmain f90621f
Merge branch 'main' of github.com:elastic/kibana into create-rules-re…
marshallmain 0528df2
Fix cypress tests, hoist index patterns into hook
marshallmain 69a8108
Fix stepDefineRule unit tests
marshallmain e1cb0c5
Fix IM cypress tests and saved query checkbox label
marshallmain ff438b0
Fix new terms and threshold cypress tests
marshallmain ddf1e99
Fix rule actions cypress test
marshallmain c938da3
Merge branch 'main' into create-rules-refactor-success
marshallmain 672142e
Hopefully fix response actions cypress test
marshallmain 1e8db8a
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine 459fa08
Fix response actions again maybe
marshallmain 3aabd4f
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine 3820ed8
PR feedback: loading var naming, combine if statements
marshallmain 9306c98
Merge branch 'main' into create-rules-refactor-success
marshallmain 40ff7e7
Remove extra spacing
marshallmain 491f88f
PR feedback: aboutRuleSchema for threat match
marshallmain 57b4d40
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine 8f0e7cb
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine b9a3b17
Remove unused helpers and types
marshallmain 9271a79
Merge branch 'main' into create-rules-refactor-success
marshallmain 84c8f85
Merge branch 'main' into create-rules-refactor-success
marshallmain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/pages/form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issue: #159060