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 Solutions] Adds a default for indicator match custom query of *:* #81727

Merged
Merged
Changes from all commits
Commits
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 @@ -88,6 +88,17 @@ const stepDefineDefaultValue: DefineStepRule = {
},
};

/**
* This default query will be used for threat query/indicator matches
* as the default when the user swaps to using it by changing their
* rule type from any rule type to the "threatMatchRule" type. Only
* difference is that "*:*" is used instead of '' for its query.
*/
const threatQueryBarDefaultValue: DefineStepRule['queryBar'] = {
...stepDefineDefaultValue.queryBar,
query: { ...stepDefineDefaultValue.queryBar.query, query: '*:*' },
};

const MyLabelButton = styled(EuiButtonEmpty)`
height: 18px;
font-size: 12px;
Expand Down Expand Up @@ -171,6 +182,38 @@ const StepDefineRuleComponent: FC<StepDefineRuleProps> = ({
setIndexModified(!isEqual(index, indicesConfig));
}, [index, indicesConfig]);

/**
* When a rule type is changed to or from a threat match this will modify the
* default query string to either:
* * from the empty string '' to '*:*' if the rule type is "threatMatchRule"
* * from '*:*' back to the empty string '' if the rule type is not "threatMatchRule"
* This calls queryBar.reset() in both cases to not trigger validation errors as
* the user has not entered data into those areas yet.
* If the user has entered data then through reference compares we can detect reliably if
* the user has changed data.
* * queryBar.value === defaultQueryBar (Has the user changed the input of '' yet?)
* * queryBar.value === threatQueryBarDefaultValue (Has the user changed the input of '*:*' yet?)
* This is a stronger guarantee than "isPristine" off of the forms as that value can be reset
* if you go to step 2) and then back to step 1) or the form is reset in another way. Using
* the reference compare we know factually if the data is changed as the references must change
* in the form libraries form the initial defaults.
*/
useEffect(() => {
const { queryBar } = getFields();
if (queryBar != null) {
const { queryBar: defaultQueryBar } = stepDefineDefaultValue;
if (isThreatMatchRule(ruleType) && queryBar.value === defaultQueryBar) {
queryBar.reset({
defaultValue: threatQueryBarDefaultValue,
});
} else if (queryBar.value === threatQueryBarDefaultValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I was suspicious of this clause being hit if the user manually enters *:* as a query, but it looks to be good since this is a comparison of object literals (which will only be equal in the use of reset() above) and not just values 👍

queryBar.reset({
defaultValue: defaultQueryBar,
});
}
}
}, [ruleType, getFields]);

const handleSubmit = useCallback(() => {
if (onSubmit) {
onSubmit();
Expand Down