From cfd58ef8f82945123fa5985e23a736fb98239e67 Mon Sep 17 00:00:00 2001 From: Ash <1849116+ashokaditya@users.noreply.github.com> Date: Wed, 22 Nov 2023 20:10:50 +0100 Subject: [PATCH] [Serverless][Security Solution][Endpoint] use `useListPrivileges` hook to check access to`.lists-*` (#171412) ## Summary Instead of using `useListsConfig` this PR uses `useListPrivileges` to verify access to `.lists-*` index pattern. follow up of elastic/kibana/pull/165613 related elastic/kibana/pull/170671 (closed in favour of this) fixes elastic/kibana/issues/169268 ### Checklist - [ ] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios --------- Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> (cherry picked from commit 8c3322ed44ccfbc4e91e0e9ef31f77b79c549cb8) --- .../index.tsx | 13 +---- .../alerts/alerts_table_api_calls.cy.ts | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 x-pack/test/security_solution_cypress/cypress/e2e/investigations/alerts/alerts_table_api_calls.cy.ts diff --git a/x-pack/plugins/security_solution/public/exceptions/hooks/use_endpoint_exceptions_capability/index.tsx b/x-pack/plugins/security_solution/public/exceptions/hooks/use_endpoint_exceptions_capability/index.tsx index a96b11225e659..4b107a5c681c1 100644 --- a/x-pack/plugins/security_solution/public/exceptions/hooks/use_endpoint_exceptions_capability/index.tsx +++ b/x-pack/plugins/security_solution/public/exceptions/hooks/use_endpoint_exceptions_capability/index.tsx @@ -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); }; diff --git a/x-pack/test/security_solution_cypress/cypress/e2e/investigations/alerts/alerts_table_api_calls.cy.ts b/x-pack/test/security_solution_cypress/cypress/e2e/investigations/alerts/alerts_table_api_calls.cy.ts new file mode 100644 index 0000000000000..3ef2c0dd21b64 --- /dev/null +++ b/x-pack/test/security_solution_cypress/cypress/e2e/investigations/alerts/alerts_table_api_calls.cy.ts @@ -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); + }); + }); +});