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] Initial rule upgrade/install endpoints implementation #155517

Merged
merged 1 commit into from
May 26, 2023

Conversation

xcrzx
Copy link
Contributor

@xcrzx xcrzx commented Apr 21, 2023

Resolves: #148186
Resolves (partially): #148184

Summary

This PR adds two new endpoints:

  • POST /internal/detection_engine/prebuilt_rules/installation/_perform (see this issue for more detail on the endpoint interface)
  • POST /internal/detection_engine/prebuilt_rules/upgrade/_perform (see this issue for more detail on the endpoint interface)

For both endpoints, I've implemented two modes: ALL_RULES and SPECIFIC_RULES. So from the rules management page, all rules could be installed or upgraded in bulk or one by one if needed.

Things not covered by this PR

  • Unit and integrational test for both endpoints
  • Rule upgrade: Missing the ability to pick the MERGED version for rule upgrades, but it is not needed so long as we do not allow rule modification
  • Rule upgrade: Missing the ability to specify rule field versions to pick

@xcrzx xcrzx added release_note:skip Skip the PR/issue when compiling release notes Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Feature:Rule Management Security Solution Detection Rule Management area Team:Detection Rule Management Security Detection Rule Management Team Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area labels Apr 21, 2023
@xcrzx xcrzx self-assigned this Apr 21, 2023
@xcrzx xcrzx force-pushed the upgrade-install-perform branch 6 times, most recently from ddd7761 to 8fcf210 Compare May 3, 2023 08:31
@xcrzx xcrzx force-pushed the upgrade-install-perform branch from 8fcf210 to 99f377b Compare May 8, 2023 08:52
@xcrzx xcrzx force-pushed the upgrade-install-perform branch 7 times, most recently from 7a2e0be to 48307d3 Compare May 25, 2023 10:47
@xcrzx xcrzx force-pushed the upgrade-install-perform branch from 48307d3 to 3b37309 Compare May 25, 2023 14:17
@xcrzx xcrzx marked this pull request as ready for review May 25, 2023 15:44
@xcrzx xcrzx requested a review from a team as a code owner May 25, 2023 15:44
@xcrzx xcrzx requested a review from banderror May 25, 2023 15:44
@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

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

@xcrzx xcrzx requested review from jpdjere and removed request for banderror May 25, 2023 15:45
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Unknown metric groups

ESLint disabled line counts

id before after diff
enterpriseSearch 19 21 +2
securitySolution 400 404 +4
total +6

Total ESLint disabled count

id before after diff
enterpriseSearch 20 22 +2
securitySolution 480 484 +4
total +6

History

  • 💚 Build #130136 succeeded 48307d3918bbc356aad570e43931a128585cf8b4
  • 💚 Build #129940 succeeded 7a2e0be0bd54ce9ea43718d02c740f1457da38ba
  • 💚 Build #129874 succeeded b69be26236f05f6c1340917ac3f79eb679868b2a
  • 💔 Build #129803 failed 90dc44906fa6d95b7cde116598629cecf44b0aea
  • 💔 Build #129674 failed 2ec1f8585718b4a251ecbcb16e64046774e24269
  • 💔 Build #129609 failed 9b944c602007f2273b76e6e9376bcdb1e1854003

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

cc @xcrzx

stats: PrebuiltRulesStatusStats;
};
/** Aggregated info about all prebuilt rules */
stats: PrebuiltRulesStatusStats;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the endpoint open to returning other kind of information other than stats in the future; do we have any future use cases for that?

Just wandering why we would hold all data within the single stats property instead of making the stats themselves first-class properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, I do not anticipate extending the endpoint to return information other than stats. However, it's always good practice to keep future use cases in mind and design with flexibility for potential changes. If a new use case does arise in the future, it would be pretty straightforward to adapt this endpoint.

...Array.from(baseRulesMap.keys()),
...Array.from(latestRulesMap.keys()),
...Array.from(currentRulesMap.keys()),
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's not necessary to create a Set to de-duplicate the ruleIds, since the Map will override any repeated value with the last key-pair created anyways.
I guess both ways achieve the same thing, not sure if this could make a difference time-complexity-wise, since the number of rules we'll be dealing is not sufficiently large.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea here is to gather all available rule IDs to iterate over, as these three structures only partially overlap. There could be missing base rules or rules that are no longer available in the package, meaning no latest version, and so on. Ultimately, we aim to create a structure similar to the following:

const map = {
  rule_id_1: {
    current: undefined,
    base: undefined,
    target: {},
  },
  rule_id_2: {
    current: {},
    base: {},
    target: undefined,
  },
};

But a similar result could probably be achieved by iterating over the three structures one by one.

const ruleAssetsClient = createPrebuiltRuleAssetsClient(soClient);
const ruleObjectsClient = createPrebuiltRuleObjectsClient(rulesClient);

const { mode, pick_version: globalPickVersion = PickVersionValues.TARGET } = request.body;
Copy link
Contributor

Choose a reason for hiding this comment

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

So, if I understand correctly, for the current Milestone we won't need to pass the pick_version param, since we will always want to update to the target version of the rule.
Is that correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that's right. The version to pick will always be TARGET.

Copy link
Contributor

@jpdjere jpdjere left a comment

Choose a reason for hiding this comment

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

Reviewed, great changes, thanks for this! Some of the refactoring you did here on the diff logic made it even easier to understand.

I'm approving now but I haven't tested the endpoints; maybe we can sync tomorrow on how you've been testing? I guess that's where the generate_assets_route comes in handy :)

@xcrzx
Copy link
Contributor Author

xcrzx commented May 26, 2023

I'm going to merge the PR so @jpdjere can start integrating it as soon as possible. If any issues arise, I can address them in follow-ups. However, since the functionality is under a feature flag, the risk of introducing any bugs is quite low.

@xcrzx xcrzx merged commit 74d276e into elastic:main May 26, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label May 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area Feature:Rule Management Security Solution Detection Rule Management 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.9.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Security Solution] Implement prebuilt rule installation/_perform API endpoint
5 participants