elastic / kibana

Your window into the Elastic Stack
https://www.elastic.co/products/kibana
Other
19.71k stars 8.13k forks source link

[Security Solution] Extend the `POST /upgrade/_perform` API endpoint's contract and functionality #166376

Open banderror opened 1 year ago

banderror commented 1 year ago

Epics: https://github.com/elastic/security-team/issues/1974 (internal), https://github.com/elastic/kibana/issues/174168

Summary

In https://github.com/elastic/kibana/issues/148184 we implemented a basic version of the upgrade/_perform API endpoint.

We need to enhance this endpoint so it can work with prebuilt rules customized by users and resolve conflicts between user customizations and updates from Elastic in the target version.

Acceptance criteria

If the MERGED version is selected, the diffs are recalculated and the rule fields are updated to the result of the diff calculation. This is only possible if all field diffs return a conflict value of either NO. If any fields returns a value of NON_SOLVABLE or SOLVABLE, reject the request with an error specifying that there are conflicts, and that they must be resolved on a per-field basis.

Handling of special fields

Specific fields need to be handled under the hood based on https://github.com/elastic/kibana/issues/186544

Edge cases

elasticmachine commented 1 year ago

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

elasticmachine commented 1 year ago

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

jpdjere commented 1 month ago

General plan

This ticket will be worked on by following the steps:

  1. Create a shorter PR with a new PrebuiltRuleAsset-derived schema for the endpoint's upgradable fields (see details below)
  2. Create rough implementation of the new logic for the endpoint
  3. Cover with integrations tests
  4. Fix skipped/forgotten/overlooked edge cases
  5. Refactor where necessary
  6. Merge

Step 2 to 6 will be a second, single PR.

Implementation plan

banderror commented 1 month ago

@jpdjere We talked with @xcrzx yesterday and figured that it would be great, besides writing a test plan, to first create a list of edge cases that the endpoint should handle. I see that you have already added such a list to https://github.com/elastic/kibana/issues/166376#issuecomment-2273466115, could you please revisit it and add to the ticket description as an AC?

One thing we discussed is that any rule type change should yield an UNSOLVABLE_CONFLICT. We can do it via creating a specialized diff algorithm, and @xcrzx created a ticket for that. Still, the question is: how exactly should the /upgrade/_perform endpoint handle rule type changes. I believe the type field should not be part of its request schema in the upgradeable fields object, and nothing from this request schema should impact the logic used for this field. We need to think through details for this edge case.

banderror commented 5 days ago

@jpdjere As a follow-up to https://github.com/elastic/kibana/pull/190440, we'll need to handle the items_per_search and concurrent_searches fields in the upgrade/_perform endpoint. Please add it to the plan. Also, please check the previous comment.

jpdjere commented 5 days ago

@banderror

first create a list of edge cases that the endpoint should handle.

I added this to the ticket description.

Still, the question is: how exactly should the /upgrade/_perform endpoint handle rule type changes. I believe the type field should not be part of its request schema in the upgradeable fields object, and nothing from this request schema should impact the logic used for this field.

As part of the preparation PR, the type was removed from the list of upgradable fields, so it is not part of the request schema. This means that rules can only be upgraded to the target version's type with this endpoint.

I'll include two runtime checks related to this, which I described in the description above:

we'll need to handle the items_per_search and concurrent_searches fields in the upgrade/_perform endpoint. Please add it to the plan. Also, please check the previous comment.

These two fields need to be handled with the rest of fields that need special treatment as described here. In this case, the endpoint will always force their update to the CURRENT version.