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

[Detection Engine][FTR] Add prebuilt role tests for rule execution #201509

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yctercero
Copy link
Contributor

Summary

Adds FTR for serverless prebuilt roles around rule execution. Uses the preview API to speed up the tests.

@yctercero
Copy link
Contributor Author

@approksiu @caitlinbetz could you confirm the behaviors here? All but the Endpoint Operations Analyst seem to match. The sheet says they're read on rules, but seems they can execute them.

@yctercero yctercero self-assigned this Nov 24, 2024
@yctercero yctercero added v9.0.0 Team:Detection Engine Security Solution Detection Engine Area backport:version Backport to applied version labels v8.18.0 release_note:skip Skip the PR/issue when compiling release notes labels Nov 25, 2024
@yctercero yctercero marked this pull request as ready for review November 25, 2024 11:35
@yctercero yctercero requested review from a team as code owners November 25, 2024 11:35
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-engine (Team:Detection Engine)

Copy link
Member

@jbudz jbudz left a comment

Choose a reason for hiding this comment

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

.buildkite/ftr_security_serverless_configs.yml

@yctercero
Copy link
Contributor Author

/ci

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] Jest Tests #16 / FileDownloadButton not isIcon renders download button with correct href

Metrics [docs]

✅ unchanged

History

cc @yctercero

Copy link
Contributor

@dhurley14 dhurley14 left a comment

Choose a reason for hiding this comment

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

This looks good thank you for adding test suites for this functionality on serverless! This follows the similar pattern for the value list testing with roles. After thinking about this some more I am wondering if we can speed up the test suites a little. I think by iterating over the roles with the same outcome for a given test we remove having to do all the build up and tear down that we would have to do if we used individual test suites for each individual role.

So instead of having a test suite for each role that tests the preview api, we would instead have a singular test suite for the preview api and iterate over all the roles within a singular test item in that suite. For example the following could live within a preview_rule_api test file and look something like this:

/*
 * 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 TestAgent from 'supertest/lib/agent';
import expect from '@kbn/expect';

import {
  QueryRuleCreateProps,
  RulePreviewRequestBody,
} from '@kbn/security-solution-plugin/common/api/detection_engine';
import { DETECTION_ENGINE_RULES_PREVIEW } from '@kbn/security-solution-plugin/common/constants';
import { FtrProviderContext } from '../../../../../../../ftr_provider_context';
import { EsArchivePathBuilder } from '../../../../../../../es_archive_path_builder';
import {
  deleteAllRules,
  deleteAllAlerts,
  getRuleForAlertTesting,
} from '../../../../../../../../common/utils/security_solution';

/**
 * Specific _id to use for some of the tests. If the archiver changes and you see errors
 * here, update this to a new value of a chosen auditbeat record and update the tests values.
 */
const ID = 'BhbXBmkBR346wHgn4PeZ';

export default ({ getService }: FtrProviderContext): void => {
  const log = getService('log');
  const utils = getService('securitySolutionUtils');
  const config = getService('config');
  const isServerless = config.get('serverless');
  const dataPathBuilder = new EsArchivePathBuilder(isServerless);
  const auditbeatPath = dataPathBuilder.getPath('auditbeat/hosts');
  const esArchiver = getService('esArchiver');
  const es = getService('es');
  const esDeleteAllIndices = getService('esDeleteAllIndices');

  let admin: TestAgent;
  let endpointPolicyManager: TestAgent;
  let socManager: TestAgent;
  let viewer: TestAgent;
  // ... add other roles as needed

  describe('@serverless @serverlessQA roles rule preview execution API behaviors', () => {
    before(async () => {
      await esArchiver.load(auditbeatPath);
      admin = await utils.createSuperTest('admin');
      endpointPolicyManager = await utils.createSuperTest('endpoint_policy_manager');
      socManager = await utils.createSuperTest('soc_manager');
      viewer = await utils.createSuperTest('viewer');
      // ... more roles to be tested
    });

    afterEach(async () => {
      await esDeleteAllIndices('.preview.alerts*');
    });

    after(async () => {
      await esArchiver.unload(auditbeatPath);
      await deleteAllAlerts(admin, log, es, [
        '.preview.alerts-security.alerts-*',
        '.alerts-security.alerts-*',
      ]);
      await deleteAllRules(admin, log);
    });

    // Here we want to test that these two roles can create and run a rule against the preview api
    // This will be faster than having to run a full test suite for each individual role.
    it('previews rule', async () => {
      await Promise.all(
        [endpointPolicyManager, socManager].map(async (role) => {
          const rule: QueryRuleCreateProps = {
            ...getRuleForAlertTesting(['auditbeat-*']),
            query: `_id:${ID}`,
          };
          const previewRequest: RulePreviewRequestBody = {
            ...rule,
            invocationCount: 1,
            timeframeEnd: new Date().toISOString(),
          };
          return role
            .post(DETECTION_ENGINE_RULES_PREVIEW)
            .query({ enable_logged_requests: true })
            .set('kbn-xsrf', 'true')
            .set('elastic-api-version', '2023-10-31')
            .send(previewRequest)
            .expect(200);
        })
      );
    });
  });
};

@approksiu
Copy link

@approksiu @caitlinbetz could you confirm the behaviors here? All but the Endpoint Operations Analyst seem to match. The sheet says they're read on rules, but seems they can execute them.

Looks like the configuration is indeed set to "all" here. @caitlinbetz could you check the intended definition and comment if it should be changed to read? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:version Backport to applied version labels release_note:skip Skip the PR/issue when compiling release notes Team:Detection Engine Security Solution Detection Engine Area v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants