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] Create rule from timeline #143020

Merged
merged 58 commits into from Dec 8, 2022

Conversation

stephmilovic
Copy link
Contributor

@stephmilovic stephmilovic commented Oct 10, 2022

  1. Introduces ability to create rule from timeline on the timelines page as shown in the gif below
  2. Fixes an unreported bug we had with the current "Import query from saved timeline" button. We only got the query from the timeline, not the index pattern or fields.
  3. Introduces a new hook to manage importing the timeline query, index pattern, and fields: useRuleFromTimeline

rft2

Cypress

Tests that use the feature "Import query from saved timeline" now have a different index pattern. We now match exactly the index pattern to the timeline index pattern. So: getIndexPatterns().join('') becomes 'auditbeat-*'. Depending on if any alerts have been generated yet, the timeline index pattern could also include .alerts-security.alerts-default. Added a method removeAlertsIndex to check if alerts index exists and remove it if it does, or it will break tests.

How to test

  1. Create the following index via dev tools:
PUT ho_ho_ho
{
  "mappings": {
    "properties": {
      "good.job": {
        "type": "keyword"
      },
      "@timestamp": {
        "type": "date"
      }
    }
  }
}

POST ho_ho_ho/_doc
{
  "@timestamp": "2022-12-05T06:26:09.015Z",
  "good": { "job": "yessir" }
}
  1. Create a Data View, ho_ho_ho
  2. Create 2 saved timelines. One that uses the security data view and queries host.name: *, the other one uses the ho_ho_ho data view and queries good.job: *.
  3. From the Timelines page, open the actions menu for the ho_ho_ho timeline and select "Create rule from timeline"
  4. Once redirected to the rules page, ensure the index pattern and query match. Then use the KQL bar to ensure it is auto suggesting the custom field
  5. Use the "Import query from saved timeline" button and select the other timeline with the security data view
  6. Ensure the index pattern and query match. Then use the KQL bar to ensure it is auto suggesting our standard ecs fields

@stephmilovic
Copy link
Contributor Author

@elasticmachine merge upstream

(timeline: TimelineModel) => {
setLoadingTimeline(false);
const newQuery = {
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 logic gets moved to new hook, use_rule_from_timeline.tsx

@stephmilovic stephmilovic added release_note:enhancement Team:Threat Hunting Security Solution Threat Hunting Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Threat Hunting:Explore v8.7.0 labels Dec 5, 2022
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-threat-hunting (Team:Threat Hunting)

@elasticmachine
Copy link
Contributor

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

SourcererScopeName.timeline
);

const [ogDataView] = useState({ dataViewId, selectedPatterns });
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this in useState if the update function is unused?

Copy link
Contributor

Choose a reason for hiding this comment

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

also consider renaming to just originalDataView or something

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 way when dataViewId and selectedPatterns update, ogDataView will stay the original values. Do you know another way to do this?

Copy link
Contributor

Choose a reason for hiding this comment

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

good use case for useRef?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

renamed it tho

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jamster10 useRef‘s current property is mutable, but useState‘s state variable not. The React team recommends in the documentation for setState, treat state like an immutable variable.

useRef is meant to be mutable and useState is meant to be immutable. Either would work, but as I do not want these values to ever update I think useState without pulling in the update method is the correct implementation

Copy link
Contributor

@maximpn maximpn left a comment

Choose a reason for hiding this comment

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

@stephmilovic rules area related changes look good. I left some non critical comments.

@@ -133,6 +134,7 @@ describe('Custom query rules', () => {

cy.log('Filling define section');
importSavedQuery(this.timelineId);
removeAlertsIndex();
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation here. I worry that the information you shared here can be forgotten in the future so it won't be so clear why alerts index is cleared here. Is it possible to remove alerts index inside importSavedQuery()? If not I'd leave a short comment in the code.

Copy link
Contributor

@maximpn maximpn left a comment

Choose a reason for hiding this comment

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

Rules Area LGTM

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 3380 3381 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 10.1MB 10.1MB +8.1KB
Unknown metric groups

ESLint disabled in files

id before after diff
osquery 1 2 +1

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
fleet 59 65 +6
osquery 109 115 +6
securitySolution 445 451 +6
total +20

References to deprecated APIs

id before after diff
securitySolution 334 339 +5

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
fleet 68 74 +6
osquery 110 117 +7
securitySolution 521 527 +6
total +21

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @stephmilovic

Copy link
Contributor

@kqualters-elastic kqualters-elastic left a comment

Choose a reason for hiding this comment

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

thanks for fixing the small nits, love this feature! Once upon an onweek I did something similar, only for eql only, could be cool to add that on top of this eventually. lgtm though 👍

Copy link
Contributor

@YulNaumenko YulNaumenko left a comment

Choose a reason for hiding this comment

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

LGTM! This is an awesome feature!:tada:

Copy link
Contributor

@marshallmain marshallmain left a comment

Choose a reason for hiding this comment

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

Alerts area changes LGTM

@stephmilovic stephmilovic merged commit 3a5c613 into elastic:main Dec 8, 2022
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Dec 8, 2022
@gavinwye gavinwye self-requested a review December 14, 2022 15:30
@stephmilovic stephmilovic linked an issue Jan 10, 2023 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
8.7 candidate backport:skip This commit does not require backporting release_note:enhancement Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Threat Hunting:Explore Team:Threat Hunting Security Solution Threat Hunting Team UX Debt v8.7.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Security Solution] Create rule from timeline