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

[Cases] Update cases ids in the alerts schema when attaching an alert to a case #147985

Merged
merged 26 commits into from
Feb 11, 2023

Conversation

cnasikas
Copy link
Member

@cnasikas cnasikas commented Dec 22, 2022

Summary

PR #147013 extended the alert's schema to be able to store the case id an alert is attached to. This PR adds the ability to update the case_ids field of the alert when an alert is attached to a case. It also limits the number of cases an alert can be attached to ten.

Permissions

A user to attach an alert to a case needs a) to have read access to the solution via the kibana feature privileges and b) to have write access to the case. The user that did the request should not need to have written access to the alert for Cases to add the case ID to the alert's data. For that reason, the alert data client is extended to cover this particular need: update an alert even if the user has read access.

Alerts client aside

For future reference, the alerts client uses the request to check the kibana feature authorization but uses the internal system user to perform any write and get operations on the alert indices themselves. For security solution this means that a user with only read access to the security solution kibana feature, write access to cases, and no read or write access to the alert indices could attach an alert to a case and have the case id stored in the alert.

For observability, users intentionally do not have access to the alert indices so we want to bypass the authorization check on the indices which is why the current alerts client uses an es client that is an internal system user.

Related issue: #146864

Checklist

Delete any items that are not applicable to this PR.

For maintainers

Release notes

Attaching an alert to a case requires read permissions on the alert.

@cnasikas cnasikas added release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) Feature:Cases Cases feature v8.7.0 labels Dec 22, 2022
@cnasikas cnasikas self-assigned this Dec 22, 2022
@cnasikas cnasikas mentioned this pull request Dec 22, 2022
8 tasks
@cnasikas cnasikas force-pushed the link_alerts_to_case branch 2 times, most recently from 1746c00 to 227d858 Compare December 23, 2022 13:19
@cnasikas cnasikas force-pushed the link_alerts_to_case branch from 227d858 to f9205c0 Compare January 11, 2023 16:10
indexName: string;
operation: ReadOperations.Find | ReadOperations.Get | WriteOperations.Update;
}) {
return this.mgetAlertsAuditOperate({
Copy link
Member Author

Choose a reason for hiding this comment

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

I make mgetAlertsAuditOperate generic to accept any field and I created the mgetAlertsAuditOperateStatus for the status

ids,
indexName,
operation,
fieldToUpdate: (source) => this.getAlertStatusFieldUpdate(source, status),
Copy link
Member Author

Choose a reason for hiding this comment

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

Same logic as before. I moved it out from mgetAlertsAuditOperate. The consumer of mgetAlertsAuditOperate should define how to get the field based on the source.

indexName: index,
operation: ReadOperations.Get,
fieldToUpdate: (source) => this.getAlertCaseIdsFieldUpdate(source, caseIds),
validate: (source) => this.validateTotalCasesPerAlert(source, caseIds),
Copy link
Member Author

Choose a reason for hiding this comment

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

The mgetAlertsAuditOperate is extended to do validation per alert.

@cnasikas cnasikas marked this pull request as ready for review January 12, 2023 11:27
@cnasikas cnasikas requested review from a team as code owners January 12, 2023 11:27
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops-cases (Feature:Cases)

Copy link
Contributor

@ymao1 ymao1 left a comment

Choose a reason for hiding this comment

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

Code review of the alerts client in the rule registry changes only, which LGTM. Would be nice to have unit tests for the new function but I see there are no existing unit tests for the alerts client :( and that functionality is covered by the integration test. Will let the cases team review the overall PR :)

Copy link
Contributor

@jonathan-buttner jonathan-buttner left a comment

Choose a reason for hiding this comment

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

Left a few optimization suggestions

@cnasikas cnasikas force-pushed the link_alerts_to_case branch from 065558f to bd6c48d Compare January 17, 2023 08:51
Copy link
Contributor

@CoenWarmer CoenWarmer left a comment

Choose a reason for hiding this comment

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

LGTM!

@cnasikas cnasikas added v8.8.0 and removed v8.7.0 labels Feb 7, 2023

return mgetRes;
} catch (exc) {
this.logger.error(`error in ensureAlertsAuthorized ${exc}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: error in ensureAllAlertsAuthorized ${exc}

user: superUser,
space: 'space1',
},
attachmentAuth: { user: secOnlyReadAlerts, space: 'space1' },
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 the reason that this user fails to when they don't have index privileges is because we're trying to update the status of the alert in this test:

Failed to update alert status ids: [{"id":"155bf8908ab2dee36b72f6e304fcd7de3de53104b3f8fb351f4e0463a704e63d","index":".internal.alerts-security.alerts-default-000001","status":"in-progress"}]: ResponseError: security_exception

@@ -190,6 +190,29 @@ export const securitySolutionOnlyRead: Role = {
},
};

export const securitySolutionOnlyReadAlerts: Role = {
Copy link
Contributor

Choose a reason for hiding this comment

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

How about we create another user that does not have access to the elasticsearch indices and ensure that the user cannot update the status of an alert. We may already have a test that does that I can't remember.

Either way I think it'd be good to test adding the case ID to an alert when the security solution user does not have access to the elasticsearch index.

});
});

it('should change the status of the alert when the user has read access only', async () => {
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 this test should fail if the user only has read access to the elasticsearch index. Or we can keep it but change the test name to say something like: when the user has write access to the indices and only read access to the siem solution

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.

Detections and response changes LGTM

Copy link
Contributor

@jonathan-buttner jonathan-buttner 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 the changes!

@cnasikas cnasikas enabled auto-merge (squash) February 11, 2023 08:32
@cnasikas cnasikas merged commit 1f19c9e into elastic:main Feb 11, 2023
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #24 / cases security and spaces enabled: trial push_case memoryless server alerts should change the status of all alerts attached to a case to closed when closure_type: close-by-pushing and syncAlerts: true
  • [job] [logs] FTR Configs #51 / console app console app with comments with single line comments should allow in request body, using #

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
apm 1386 1387 +1
cases 595 596 +1
infra 1250 1251 +1
observability 514 515 +1
securitySolution 3720 3721 +1
synthetics 1354 1355 +1
triggersActionsUi 510 511 +1
total +7

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/rule-data-utils 97 98 +1
ruleRegistry 224 228 +4
total +5

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
ruleRegistry 12 13 +1
Unknown metric groups

API count

id before after diff
@kbn/rule-data-utils 100 101 +1
ruleRegistry 252 257 +5
total +6

History

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

cc @cnasikas

@cnasikas cnasikas deleted the link_alerts_to_case branch February 11, 2023 10:11
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Feb 11, 2023
@cnasikas cnasikas added release_note:breaking and removed release_note:skip Skip the PR/issue when compiling release notes labels Apr 3, 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:Cases Cases feature release_note:breaking Team:ResponseOps Label for the ResponseOps team (formerly the Cases and Alerting teams) v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants