From 9fadb8df6cde9bf4d2e43942a3b3ad6e2004ec6b Mon Sep 17 00:00:00 2001 From: jpdjere Date: Thu, 13 Jul 2023 14:48:47 +0200 Subject: [PATCH 1/3] Intercept individual package installation via Fleet --- ...built_rules_install_update_workflows.cy.ts | 53 ++++++++++++++----- .../cypress/tasks/api_calls/prebuilt_rules.ts | 1 + 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts index bec5b77de52c9..1520bc00d2a8f 100644 --- a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts +++ b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts @@ -45,27 +45,52 @@ describe('Detection rules, Prebuilt Rules Installation and Update workflow', () describe('Installation of prebuilt rules package via Fleet', () => { beforeEach(() => { - cy.intercept('POST', '/api/fleet/epm/packages/_bulk*').as('installPackage'); + cy.intercept('POST', '/api/fleet/epm/packages/_bulk*').as('installPackageBulk'); + cy.intercept('POST', '/api/fleet/epm/packages/security_detection_engine/*').as( + 'installPackage' + ); waitForRulesTableToBeLoaded(); }); it('should install package from Fleet in the background', () => { /* Assert that the package in installed from Fleet by checking that /* the installSource is "registry", as opposed to "bundle" */ - cy.wait('@installPackage', { + cy.wait('@installPackageBulk', { timeout: 60000, - }).then(({ response }) => { - cy.wrap(response?.statusCode).should('eql', 200); - - const packages = response?.body.items.map(({ name, result }: BulkInstallPackageInfo) => ({ - name, - installSource: result.installSource, - })); - - expect(packages.length).to.have.greaterThan(0); - expect(packages).to.deep.include.members([ - { name: 'security_detection_engine', installSource: 'registry' }, - ]); + }).then(({ response: bulkResponse }) => { + cy.wrap(bulkResponse?.statusCode).should('eql', 200); + + const packages = bulkResponse?.body.items.map( + ({ name, result }: BulkInstallPackageInfo) => ({ + name, + installSource: result.installSource, + }) + ); + + const packagesBulkInstalled = packages.map(({ name }: { name: string }) => name); + + // Under normal flow the package is installed via the Fleet bulk install API. + // However, for testing purposes the package can be installed via the Fleet individual install API, + // so we need to intercept and wait for that request as well. + if (!packagesBulkInstalled.includes('security_detection_engine')) { + // Should happen only during testing when the `xpack.securitySolution.prebuiltRulesPackageVersion` flag is set + cy.wait('@installPackage').then(({ response }) => { + cy.wrap(response?.statusCode).should('eql', 200); + cy.wrap(response?.body) + .should('have.property', 'items') + .should('have.length.greaterThan', 0); + cy.wrap(response?.body) + .should('have.property', '_meta') + .should('have.property', 'install_source') + .should('eql', 'registry'); + }); + } else { + // Normal flow, install via the Fleet bulk install API + expect(packages.length).to.have.greaterThan(0); + expect(packages).to.deep.include.members([ + { name: 'security_detection_engine', installSource: 'registry' }, + ]); + } }); }); diff --git a/x-pack/plugins/security_solution/cypress/tasks/api_calls/prebuilt_rules.ts b/x-pack/plugins/security_solution/cypress/tasks/api_calls/prebuilt_rules.ts index 1a079cb43ec20..e175fe3345f80 100644 --- a/x-pack/plugins/security_solution/cypress/tasks/api_calls/prebuilt_rules.ts +++ b/x-pack/plugins/security_solution/cypress/tasks/api_calls/prebuilt_rules.ts @@ -170,6 +170,7 @@ export const getRuleAssets = (index: string | undefined = '.kibana_security_solu /* during e2e tests, and allow for manual installation of mock rules instead. */ export const preventPrebuiltRulesPackageInstallation = () => { cy.intercept('POST', '/api/fleet/epm/packages/_bulk*', {}); + cy.intercept('POST', '/api/fleet/epm/packages/security_detection_engine/*', {}); }; /** From 1b83419e2966c0162664fdebf04b97ff9ba4e66d Mon Sep 17 00:00:00 2001 From: jpdjere Date: Thu, 13 Jul 2023 16:53:01 +0200 Subject: [PATCH 2/3] Fix test --- ...built_rules_install_update_workflows.cy.ts | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts index 1520bc00d2a8f..aa2ed479f05c8 100644 --- a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts +++ b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts @@ -95,10 +95,7 @@ describe('Detection rules, Prebuilt Rules Installation and Update workflow', () }); it('should install rules from the Fleet package when user clicks on CTA', () => { - /* Retrieve how many rules were installed from the Fleet package */ - cy.wait('@installPackage', { - timeout: 60000, - }).then(() => { + const getRulesAndAssertNumberInstalled = () => { getRuleAssets().then((response) => { const ruleIds = response.body.hits.hits.map( (hit: { _source: { ['security-rule']: Rule } }) => hit._source['security-rule'].rule_id @@ -112,6 +109,29 @@ describe('Detection rules, Prebuilt Rules Installation and Update workflow', () .should('be.visible') .should('have.text', `${numberOfRulesToInstall} rules installed successfully.`); }); + }; + /* Retrieve how many rules were installed from the Fleet package */ + cy.wait('@installPackage', { + timeout: 60000, + }).then(({ response: bulkResponse }) => { + cy.wrap(bulkResponse?.statusCode).should('eql', 200); + + const packages = bulkResponse?.body.items.map( + ({ name, result }: BulkInstallPackageInfo) => ({ + name, + installSource: result.installSource, + }) + ); + + const packagesBulkInstalled = packages.map(({ name }: { name: string }) => name); + + if (!packagesBulkInstalled.includes('security_detection_engine')) { + cy.wait('@installPackage').then(() => { + getRulesAndAssertNumberInstalled(); + }); + } else { + getRulesAndAssertNumberInstalled(); + } }); }); }); From db51815229d2daf392a204622b5dfb69de6030ea Mon Sep 17 00:00:00 2001 From: jpdjere Date: Thu, 13 Jul 2023 16:55:45 +0200 Subject: [PATCH 3/3] Fix --- .../prebuilt_rules_install_update_workflows.cy.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts index aa2ed479f05c8..355611ba42eea 100644 --- a/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts +++ b/x-pack/plugins/security_solution/cypress/e2e/detection_rules/prebuilt_rules_install_update_workflows.cy.ts @@ -111,20 +111,16 @@ describe('Detection rules, Prebuilt Rules Installation and Update workflow', () }); }; /* Retrieve how many rules were installed from the Fleet package */ - cy.wait('@installPackage', { + /* See comments in test above for more details */ + cy.wait('@installPackageBulk', { timeout: 60000, }).then(({ response: bulkResponse }) => { cy.wrap(bulkResponse?.statusCode).should('eql', 200); - const packages = bulkResponse?.body.items.map( - ({ name, result }: BulkInstallPackageInfo) => ({ - name, - installSource: result.installSource, - }) + const packagesBulkInstalled = bulkResponse?.body.items.map( + ({ name }: { name: string }) => name ); - const packagesBulkInstalled = packages.map(({ name }: { name: string }) => name); - if (!packagesBulkInstalled.includes('security_detection_engine')) { cy.wait('@installPackage').then(() => { getRulesAndAssertNumberInstalled();