[8.11] [Serverless][Security Solution][Endpoint] Remove use of hooks to check access to.lists-* for endpoint exceptions access (#171412) (#171794)

# Backport

This will backport the following commits from `main` to `8.11`:
- [[Serverless][Security Solution][Endpoint] Remove use of hooks to
check access to`.lists-*` for endpoint exceptions access
(#171412)](https://github.com/elastic/kibana/pull/171412)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Ash","email":"1849116+ashokaditya@users.noreply.github.com"},"sourceCommit":{"committedDate":"2023-11-22T19:10:50Z","message":"[Serverless][Security
Solution][Endpoint] Remove use of hooks to check access to`.lists-*` for
endpoint exceptions access (#171412)\n\n## Summary\r\n\r\nInstead of
using `useListsConfig` this PR uses `useListPrivileges` to\r\nverify
access to `.lists-*` index pattern.\r\n\r\nfollow up of
elastic/kibana/pull/165613\r\nrelated elastic/kibana/pull/170671 (closed
in favour of this)\r\nfixes elastic/kibana/issues/169268\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8c3322ed44ccfbc4e91e0e9ef31f77b79c549cb8","branchLabelMapping":{"^v8.12.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","OLM
Sprint","v8.11.0","v8.12.0","v8.11.1"],"number":171412,"url":"https://github.com/elastic/kibana/pull/171412","mergeCommit":{"message":"[Serverless][Security
Solution][Endpoint] Remove use of hooks to check access to`.lists-*` for
endpoint exceptions access (#171412)\n\n## Summary\r\n\r\nInstead of
using `useListsConfig` this PR uses `useListPrivileges` to\r\nverify
access to `.lists-*` index pattern.\r\n\r\nfollow up of
elastic/kibana/pull/165613\r\nrelated elastic/kibana/pull/170671 (closed
in favour of this)\r\nfixes elastic/kibana/issues/169268\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8c3322ed44ccfbc4e91e0e9ef31f77b79c549cb8"}},"sourceBranch":"main","suggestedTargetBranches":["8.11"],"targetPullRequestStates":[{"branch":"8.11","label":"v8.11.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.12.0","labelRegex":"^v8.12.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/171412","number":171412,"mergeCommit":{"message":"[Serverless][Security
Solution][Endpoint] Remove use of hooks to check access to`.lists-*` for
endpoint exceptions access (#171412)\n\n## Summary\r\n\r\nInstead of
using `useListsConfig` this PR uses `useListPrivileges` to\r\nverify
access to `.lists-*` index pattern.\r\n\r\nfollow up of
elastic/kibana/pull/165613\r\nrelated elastic/kibana/pull/170671 (closed
in favour of this)\r\nfixes elastic/kibana/issues/169268\r\n\r\n###
Checklist\r\n\r\n- [ ] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common
scenarios\r\n\r\n---------\r\n\r\nCo-authored-by: Kibana Machine
<42973632+kibanamachine@users.noreply.github.com>","sha":"8c3322ed44ccfbc4e91e0e9ef31f77b79c549cb8"}}]}]
BACKPORT-->

Co-authored-by: Ash <1849116+ashokaditya@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2023-11-23 10:34:19 -05:00 committed by GitHub
parent eec5208619
commit ab8d894a38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 11 deletions

View file

@ -5,19 +5,10 @@
* 2.0.
*/
import { useMemo } from 'react';
import { useListsConfig } from '../../../detections/containers/detection_engine/lists/use_lists_config';
import { useHasSecurityCapability } from '../../../helper_hooks';
export const useEndpointExceptionsCapability = (
capability: 'showEndpointExceptions' | 'crudEndpointExceptions'
) => {
const { loading: listsConfigLoading, needsConfiguration: needsListsConfiguration } =
useListsConfig();
const hasEndpointExceptionCapability = useHasSecurityCapability(capability);
return useMemo(
() => !listsConfigLoading && !needsListsConfiguration && hasEndpointExceptionCapability,
[hasEndpointExceptionCapability, listsConfigLoading, needsListsConfiguration]
);
): boolean => {
return useHasSecurityCapability(capability);
};

View file

@ -0,0 +1,48 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { getNewRule } from '../../../objects/rule';
import { createRule } from '../../../tasks/api_calls/rules';
import { waitForAlertsToPopulate } from '../../../tasks/create_new_rule';
import { login } from '../../../tasks/login';
import { visit } from '../../../tasks/navigation';
import { ALERTS_URL } from '../../../urls/navigation';
/*
*
* Alert table is third party component which cannot be easily tested by jest.
* This test main checks if Alert Table does not call api/lists/index more than once.
*
* */
describe('Alert Table API calls', { tags: ['@ess', '@serverless'] }, () => {
let callCount: number = 0;
beforeEach(() => {
callCount = 0;
login();
createRule(getNewRule());
// intercept all calls to `api/lists/index`
// and count how many times it was called
cy.intercept('GET', '/api/lists/index', (req) => {
req.on('response', (res) => {
if (res.statusCode === 200) {
callCount += 1;
}
});
});
visit(ALERTS_URL);
waitForAlertsToPopulate();
});
it('should call `api/lists/index` only once', () => {
cy.get('[data-test-subj="alertsTable"]').then(() => {
expect(callCount, 'number of times lists index api is called').to.equal(1);
});
});
});