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] Add Threat Match rule specific editable fields #200308

Open
wants to merge 42 commits into
base: main
Choose a base branch
from

Conversation

maximpn
Copy link
Contributor

@maximpn maximpn commented Nov 15, 2024

Partially addresses: #171520

Summary

This PR adds is built on top of #193828 and #196948 and adds the following editable components for Threat Match rule type

  • threat_index
  • threat_query
  • threat_mapping
  • threat_indicator_path
  • threat_language threat_language was merged with threat_query

Details

This PR make a set of changes to make existing Threat Match form fields easily reusable as editable components and type safe when used in forms. In particular the following was done

  • Fixes a bug blocking Threat Match rules upgrading
  • Existing functionality was refactored to have reusable self-contained editable components for threat_index, threat_query, threat_mapping and threat_indicator_path rule fields
  • threat_language was removed since query type is included in threat_query field and can be edited with Query Bar
  • threat mapping input was split into separate component for individual fields to be reused
  • ThreatMatchComponent was refactored to be a controlled component instead of uncontrolled
    ThreatMatchComponent has a feature preventing users removing the single last entry. Instead deleting the last entry the delete button clears inputs. That functionality didn't work properly in Prebuilt Rule Customization workflow and rule creation/editing forms after creating a reusable ThreatMappingEdit component. Instead of trying to find a tricky fix ThreatMatchComponent was refactored to remove internal state. The feature preventing users removing the single last entry was reimplemented in ThreatMappingEdit component.
  • Fixes a bug reproducible in main where validation errors duplicated described in a comment
  • Fixes a bug reproducible in main allowing to save unknown source indices or indicator indices fields described in a comment

How to test

  • Ensure the prebuiltRulesCustomizationEnabled feature flag is enabled
  • Allow internal APIs via adding server.restrictInternalApis: false to kibana.dev.yaml
  • Clear Elasticsearch data
  • Run Elasticsearch and Kibana locally (do not open Kibana in a web browser)
  • Install an outdated version of the security_detection_engine Fleet package
curl -X POST --user elastic:changeme  -H 'Content-Type: application/json' -H 'kbn-xsrf: 123' -H "elastic-api-version: 2023-10-31" -d '{"force":true}' http://localhost:5601/kbn/api/fleet/epm/packages/security_detection_engine/8.14.1
  • Install prebuilt rules
curl -X POST --user elastic:changeme  -H 'Content-Type: application/json' -H 'kbn-xsrf: 123' -H "elastic-api-version: 1" -d '{"mode":"ALL_RULES"}' http://localhost:5601/kbn/internal/detection_engine/prebuilt_rules/installation/_perform
  • Open a threat_match rule for editing. For example Threat Intel Hash Indicator Match with rule_id aab184d3-72b3-4639-b242-6597c99d8bca.

  • Edit Indicator index patterns, Indicator index query and/or Indicator filters, Indicator mapping and Indicator prefix override fields

  • Open Detection Rules (SIEM) Page -> Rule Updates -> click on Threat Intel Hash Indicator Match rule -> expand each Threat Match rule type specific field -> press Edit button

Screenshots

Threat Match Query edit component
image

Threat Match Index edit component
image

Threat Match Mapping edit component
image

Threat Match Indicator Path edit component
image

Threat Match Mapping unknown field names validation warnings
Screenshot 2024-12-18 at 12 45 41

Screenshot 2024-12-18 at 12 45 53 Screenshot 2024-12-18 at 12 47 05 Screenshot 2024-12-18 at 12 47 15

@maximpn maximpn added release_note:skip Skip the PR/issue when compiling release notes v9.0.0 Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Detection Rule Management Security Detection Rule Management Team Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area backport:version Backport to applied version labels v8.17.0 labels Nov 15, 2024
@maximpn maximpn self-assigned this Nov 15, 2024
@maximpn maximpn force-pushed the add-threat-match-specific-editable-fields branch 2 times, most recently from 018a0c9 to 9b576a1 Compare November 16, 2024 11:02
@maximpn maximpn requested a review from nikitaindik November 17, 2024 22:03
@maximpn maximpn marked this pull request as ready for review November 17, 2024 22:03
@maximpn maximpn requested review from a team as code owners November 17, 2024 22:03
@maximpn maximpn requested a review from vitaliidm November 17, 2024 22:03
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detections-response (Team:Detections and Resp)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management)

Copy link
Contributor

@vitaliidm vitaliidm left a comment

Choose a reason for hiding this comment

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

I have found few issues and left some comments, mostly questions and suggestions.
Will continue testing

@xcrzx
Copy link
Contributor

xcrzx commented Nov 20, 2024

I don’t have any changes in the filters field, but the diff shows some empty meta.alias fields that it thinks should be removed:

image

What is the meta field, and should it even be part of the diff calculation?

cc @dplumlee This seems similar to the issue we recently had with the schedule and threat fields.

@xcrzx
Copy link
Contributor

xcrzx commented Nov 20, 2024

The incorrect final field version has been selected. I have modifications to the field, so in this case, we should pre-select the current version and preserve all user modifications instead of removing them.

image

@nikitaindik nikitaindik removed their request for review November 21, 2024 10:51
@maximpn maximpn added v8.18.0 and removed v8.17.0 labels Nov 22, 2024
@maximpn maximpn force-pushed the add-threat-match-specific-editable-fields branch from e0ff1dd to f9e8e69 Compare November 27, 2024 21:50
@maximpn maximpn requested a review from vitaliidm November 27, 2024 21:52
@maximpn maximpn force-pushed the add-threat-match-specific-editable-fields branch from 1ab0f3c to 5ed85f5 Compare December 19, 2024 10:34
Comment on lines +33 to +47
// We have to make sure validation runs against the latest source events index patterns
// and threat match index patterns.
// Form lib's `fieldsToValidateOnChange` on the corresponding index patterns edit fields
// doesn't help here. It leads to running threat match mapping validation before render
// of this component happens. In the end validation runs against previous index patterns
// producing invalid validation results.
//
// Validating the field imperatively here fixes this issue.
//
// Additional pitfall here is `validate` function changing its reference every render. Including it
// in useEffect's deps leads to infinite re-render.
useEffect(() => {
validate();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [indexPatterns, threatIndexPatterns]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part looks sub-optimal but it's the best I was able to make to re-run threat match mapping validation upon index pattern changes. An alternative is using formData in validation to read index patterns/data source, transform them to data view asynchronously and then validate fields against known fields. Alternative's drawback is difficulty to reuse it in rule upgrade workflow.

Any bright ideas are welcome.

import { useFormData } from '../../../../shared_imports';

interface UsePersistentThreatMatchStateParams {
form: FormHook;
Copy link
Contributor

Choose a reason for hiding this comment

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

some of the comments still not addressed: #200308 (comment)

export function usePersistentThreatMatchState({ form }: UsePersistentThreatMatchStateParams): void {
const lastThreatMatchState = useRef<LastThreatMatchState | undefined>();
const [{ ruleType, threatIndex: threatIndexPatterns, threatQueryBar, threatMapping }] =
useFormData({ form, watch: ['ruleType', 'threatIndex', 'threatQueryBar', 'threatMapping'] });
Copy link
Contributor

Choose a reason for hiding this comment

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

some of the comments still not addressed: #200308 (comment)

Copy link
Contributor

@vitaliidm vitaliidm left a comment

Choose a reason for hiding this comment

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

There are few minor comments and mostly discussion over #200308 (comment)
I would like to be addressed before merge

About
#200308 (comment)
I think the proper solution should involve async validation.
At the same time, in alternative approach, are multiple requests to get index fields sent? (from created data view in validator and data views from parent component)
They might be very heavy, so ideally if we want to do them once.

'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownFields',
{
defaultMessage:
'Indicator mapping has unknown fields. {unknownSourceIndicesFields} field(s) not found in the source events indices and {unknownThreatMatchIndicesFields} field(s) not found in the indicator indices.',
Copy link
Contributor

Choose a reason for hiding this comment

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

we should use plurals instead of field(s)

'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownSourceIndicesFields',
{
defaultMessage:
'Indicator mapping has unknown fields. {unknownSourceIndicesFields} field(s) not found in the source events indices.',
Copy link
Contributor

Choose a reason for hiding this comment

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

we should use plurals instead of field(s)

'xpack.securitySolution.detectionEngine.ruleManagement.threatMappingField.unknownIndicatorIndicesFields',
{
defaultMessage:
'Indicator mapping has unknown fields. {unknownThreatMatchIndicesFields} field(s) not found in the indicator indices.',
Copy link
Contributor

Choose a reason for hiding this comment

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

we should use plurals instead of field(s)

@elasticmachine
Copy link
Contributor

elasticmachine commented Dec 19, 2024

💔 Build Failed

Failed CI Steps

History

cc @maximpn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:version Backport to applied version labels Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area release_note:skip Skip the PR/issue when compiling release notes Team:Detection Rule Management Security Detection Rule Management Team Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants