Skip to content

Commit c19d74c

Browse files
[Security Solutions] Adds a default for indicator match custom query of *:* (#81727) (#83352)
## Summary Allows for Indicator matches to have a default of `*:*` for the query field when it is selected. Before, indicator query is blank when first selecting the rule: <img width="1037" alt="Screen Shot 2020-11-05 at 5 44 50 PM" src="https://user-images.githubusercontent.com/1151048/98312312-afc9ff00-1f8e-11eb-822b-ad95104ca54e.png"> After, indicator query is by default `*:*` unless the user has previously edited the query field: <img width="1038" alt="Screen Shot 2020-11-05 at 5 45 38 PM" src="https://user-images.githubusercontent.com/1151048/98312363-cb350a00-1f8e-11eb-9137-8da2f770ec7e.png"> Adds a stable reference for threat matching to determine when the query field has been modified or not. This is keep the current behavior and the rules operate like this: * If you select an indicator match rule and nothing has been previously edited it will select `*:*` for the query * If you have modified your custom query and select indicator match rule, then `*:*` will be replaced with that custom query and `*:*` will not be used. * If you select EQL rule and then _back_ to this rule type the `*:*` will be re-inserted and `edit: true` will flip back to false, due to the magic that is keys within React and how the EQL rule type relies on that. ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) - [ ] Any UI touched in this PR does not create any new axe failures (run axe in browser: [FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/), [Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US)) - [ ] This renders correctly on smaller devices using a responsive layout. (You can test this [in your browser](https://www.browserstack.com/guide/responsive-testing-on-local-server)) - [ ] This was checked for [cross-browser compatibility](https://www.elastic.co/support/matrix#matrix_browsers)
1 parent d1b2e77 commit c19d74c

File tree

1 file changed

+43
-0
lines changed
  • x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule

1 file changed

+43
-0
lines changed

x-pack/plugins/security_solution/public/detections/components/rules/step_define_rule/index.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ const stepDefineDefaultValue: DefineStepRule = {
8888
},
8989
};
9090

91+
/**
92+
* This default query will be used for threat query/indicator matches
93+
* as the default when the user swaps to using it by changing their
94+
* rule type from any rule type to the "threatMatchRule" type. Only
95+
* difference is that "*:*" is used instead of '' for its query.
96+
*/
97+
const threatQueryBarDefaultValue: DefineStepRule['queryBar'] = {
98+
...stepDefineDefaultValue.queryBar,
99+
query: { ...stepDefineDefaultValue.queryBar.query, query: '*:*' },
100+
};
101+
91102
const MyLabelButton = styled(EuiButtonEmpty)`
92103
height: 18px;
93104
font-size: 12px;
@@ -171,6 +182,38 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
171182
setIndexModified(!isEqual(index, indicesConfig));
172183
}, [index, indicesConfig]);
173184

185+
/**
186+
* When a rule type is changed to or from a threat match this will modify the
187+
* default query string to either:
188+
* * from the empty string '' to '*:*' if the rule type is "threatMatchRule"
189+
* * from '*:*' back to the empty string '' if the rule type is not "threatMatchRule"
190+
* This calls queryBar.reset() in both cases to not trigger validation errors as
191+
* the user has not entered data into those areas yet.
192+
* If the user has entered data then through reference compares we can detect reliably if
193+
* the user has changed data.
194+
* * queryBar.value === defaultQueryBar (Has the user changed the input of '' yet?)
195+
* * queryBar.value === threatQueryBarDefaultValue (Has the user changed the input of '*:*' yet?)
196+
* This is a stronger guarantee than "isPristine" off of the forms as that value can be reset
197+
* if you go to step 2) and then back to step 1) or the form is reset in another way. Using
198+
* the reference compare we know factually if the data is changed as the references must change
199+
* in the form libraries form the initial defaults.
200+
*/
201+
useEffect(() => {
202+
const { queryBar } = getFields();
203+
if (queryBar != null) {
204+
const { queryBar: defaultQueryBar } = stepDefineDefaultValue;
205+
if (isThreatMatchRule(ruleType) && queryBar.value === defaultQueryBar) {
206+
queryBar.reset({
207+
defaultValue: threatQueryBarDefaultValue,
208+
});
209+
} else if (queryBar.value === threatQueryBarDefaultValue) {
210+
queryBar.reset({
211+
defaultValue: defaultQueryBar,
212+
});
213+
}
214+
}
215+
}, [ruleType, getFields]);
216+
174217
const handleSubmit = useCallback(() => {
175218
if (onSubmit) {
176219
onSubmit();

0 commit comments

Comments
 (0)